Coverage Report - net.sourceforge.pebble.web.filter.TransformingFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
TransformingFilter
94%
34/36
87%
14/16
3.667
 
 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  
 package net.sourceforge.pebble.web.filter;
 33  
 
 34  
 import net.sourceforge.pebble.Constants;
 35  
 import net.sourceforge.pebble.domain.AbstractBlog;
 36  
 import net.sourceforge.pebble.domain.Blog;
 37  
 import net.sourceforge.pebble.domain.BlogManager;
 38  
 import net.sourceforge.pebble.domain.MultiBlog;
 39  
 import org.apache.commons.logging.Log;
 40  
 import org.apache.commons.logging.LogFactory;
 41  
 
 42  
 import javax.servlet.*;
 43  
 import javax.servlet.http.HttpServletRequest;
 44  
 import java.io.IOException;
 45  
 import java.net.URLDecoder;
 46  
 
 47  
 /**
 48  
  * A filter that transforms an incoming URI into an action URI.
 49  
  *
 50  
  * @author    Simon Brown
 51  
  */
 52  12
 public class TransformingFilter implements Filter {
 53  
 
 54  
   /** the config of this filter */
 55  
   private FilterConfig filterConfig;
 56  
 
 57  
   /** the log used by this class */
 58  4
   private static Log log = LogFactory.getLog(TransformingFilter.class);
 59  
 
 60  
   /**
 61  
    * Initialises this instance.
 62  
    *
 63  
    * @param config    a FilterConfig instance
 64  
    */
 65  
   public void init(FilterConfig config) {
 66  12
     this.filterConfig = config;
 67  12
   }
 68  
 
 69  
   /**
 70  
    * Called when this filter is taken out of service.
 71  
    */
 72  
   public void destroy() {
 73  12
   }
 74  
 
 75  
   /**
 76  
    * Contains the processing associated with this filter.
 77  
    */
 78  
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 79  
       throws ServletException, IOException {
 80  
 
 81  32
     HttpServletRequest httpRequest = (HttpServletRequest)request;
 82  32
     AbstractBlog blog = (AbstractBlog)request.getAttribute(Constants.BLOG_KEY);
 83  
 
 84  32
     String originalUri = httpRequest.getRequestURI();
 85  
     // get URI and strip off the context (e.g. /blog)
 86  32
     String uri = originalUri.substring(httpRequest.getContextPath().length(), originalUri.length());
 87  
 
 88  
     // now we're left with a URI
 89  32
     if (BlogManager.getInstance().isMultiBlog()) {
 90  16
       if (uri.length() == 0) {
 91  4
         uri = "/";
 92  
       }
 93  16
       int index = uri.indexOf("/", 1);
 94  16
       if (index == -1) {
 95  8
         index = uri.length();
 96  
       }
 97  16
       String blogName = uri.substring(1, index);
 98  16
       blogName = URLDecoder.decode(blogName, "UTF-8");
 99  16
       if (BlogManager.getInstance().hasBlog(blogName)) {
 100  8
         uri = uri.substring(index, uri.length());
 101  
       }
 102  
     }
 103  
 
 104  32
     if (uri == null || uri.trim().equals("")) {
 105  4
       uri = "/";
 106  
     }
 107  32
     log.trace("uri : " + uri);
 108  
 
 109  
     // Only add the query string to external URI, otherwise each parameter in the query string will end up being put in
 110  
     // twice to the parameter map.
 111  32
     String externalUri = uri;
 112  32
     if (httpRequest.getQueryString() != null) {
 113  0
       externalUri += "?" + httpRequest.getQueryString();
 114  0
       originalUri += "?" + httpRequest.getQueryString();
 115  
     }
 116  32
     httpRequest.setAttribute(Constants.ORIGINAL_URI, originalUri);
 117  32
     httpRequest.setAttribute(Constants.EXTERNAL_URI, externalUri);
 118  
 
 119  32
     UriTransformer transformer = new UriTransformer();
 120  
     String internalUri;
 121  
 
 122  32
     if (blog instanceof Blog) {
 123  16
       internalUri = transformer.getUri(uri, (Blog)blog);
 124  
     } else {
 125  16
       internalUri = transformer.getUri(uri, (MultiBlog)blog);
 126  
     }
 127  
 
 128  32
     request.setAttribute(Constants.INTERNAL_URI, internalUri);
 129  
 
 130  32
     log.trace(uri + " -> " + internalUri);
 131  
 
 132  32
     chain.doFilter(request, response);
 133  32
   }
 134  
 
 135  
 }