Coverage Report - net.sourceforge.pebble.util.UrlRewriter
 
Classes in this File Line Coverage Branch Coverage Complexity
UrlRewriter
100%
8/8
N/A
1
UrlRewriter$1
N/A
N/A
1
UrlRewriter$TheRewriter
100%
2/2
N/A
1
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 
 33  
 package net.sourceforge.pebble.util;
 34  
 
 35  
 /**
 36  
  * This class is used to keep track of a thread local instance 
 37  
  * for rewriting URLs - ultimately for deciding if a link should
 38  
  * be decorated 'secure' (as in config value secureUrl) or plain.
 39  
  * 
 40  
  * The initialization (and clean up) is meant to be done in a 
 41  
  * servlet filter, so that the thread local rewriter is available 
 42  
  * to each request. This is done because a blog itself serves its
 43  
  * Url to anybody and doesn't know how to determine if it should be
 44  
  * talked to 'secure' or not.
 45  
  * 
 46  
  * In the current scenario, secureUrl may be used for logging in 
 47  
  * and performing administrative work. Be aware though that mixing
 48  
  * http and https access to an application may itself impose severe
 49  
  * risk while the user feels comfortably safe. Best and most secure 
 50  
  * mode for operation would be to go https exclusively. If you 
 51  
  * can't, the current solution (this class is participating in)
 52  
  * will help you the best it can.
 53  
  * 
 54  
  * @author Olaf Kock
 55  
  *
 56  
  */
 57  
 
 58  20
 public abstract class UrlRewriter {
 59  4
         private static TheRewriter x = new TheRewriter(); 
 60  
         
 61  
         /**
 62  
          * Initialize the threadlocal urlRewriter. The given rewriter 
 63  
          * will be used until this method is called again (or clear()
 64  
          * is called) 
 65  
          * @param urlRewriter
 66  
          */
 67  
         public static void useThisRewriter(UrlRewriter urlRewriter) {
 68  16
                 x.set(urlRewriter);
 69  16
         }
 70  
         
 71  
         /**
 72  
          * Urls will not be rewritten after this call - this clears 
 73  
          * the threadlocal UrlRewriter.
 74  
          */
 75  
         public static void clear() {
 76  4
                 x.remove();
 77  4
         }
 78  
 
 79  
         /**
 80  
          * Delegate url rewriting to the current threadlocal rewriter.
 81  
          * @param url
 82  
          * @return
 83  
          */
 84  
         public static String doRewrite(String url) {
 85  24
                 return x.get().rewrite(url);
 86  
         }
 87  
 
 88  
         /**
 89  
          * This class serves as its own interface for the implementing 
 90  
          * UrlRewriters. Might be seen as a bit unclean, but keeps the 
 91  
          * implementation tightly together without introducing another
 92  
          * one-method-interface.
 93  
          * @param url
 94  
          * @return
 95  
          */
 96  
         abstract public String rewrite(String url);
 97  
         
 98  16
         private static class TheRewriter extends ThreadLocal<UrlRewriter> {
 99  
                 @Override
 100  
                 protected UrlRewriter initialValue() {
 101  8
                         return NullUrlRewriter.instance;
 102  
                 }
 103  
         }
 104  
 }