Coverage Report - net.sourceforge.pebble.web.filter.BlogLookupFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
BlogLookupFilter
83%
35/42
77%
14/18
4
 
 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 org.apache.commons.logging.Log;
 35  
 import org.apache.commons.logging.LogFactory;
 36  
 
 37  
 import javax.servlet.*;
 38  
 import javax.servlet.jsp.jstl.core.Config;
 39  
 import javax.servlet.http.HttpServletRequest;
 40  
 import java.io.IOException;
 41  
 import java.net.URLDecoder;
 42  
 import java.util.List;
 43  
 import java.util.Collections;
 44  
 import java.util.Locale;
 45  
 
 46  
 import net.sourceforge.pebble.PebbleContext;
 47  
 import net.sourceforge.pebble.Constants;
 48  
 import net.sourceforge.pebble.comparator.BlogEntryComparator;
 49  
 import net.sourceforge.pebble.decorator.ContentDecoratorChain;
 50  
 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 51  
 import net.sourceforge.pebble.domain.*;
 52  
 
 53  
 /**
 54  
  * A filter respsonsible for setting up the blog object.
 55  
  *
 56  
  * @author    Simon Brown
 57  
  */
 58  8
 public class BlogLookupFilter implements Filter {
 59  
 
 60  
   /** the log used by this class */
 61  4
   private static Log log = LogFactory.getLog(BlogLookupFilter.class);
 62  
 
 63  
   /** the config of this filter */
 64  
   private FilterConfig filterConfig;
 65  
 
 66  
   /**
 67  
    * Initialises this instance.
 68  
    *
 69  
    * @param config    a FilterConfig instance
 70  
    */
 71  
   public void init(FilterConfig config) {
 72  8
     this.filterConfig = config;
 73  8
   }
 74  
 
 75  
   /**
 76  
    * Called when this filter is taken out of service.
 77  
    */
 78  
   public void destroy() {
 79  8
   }
 80  
 
 81  
   /**
 82  
    * Contains the processing associated with this filter.
 83  
    */
 84  
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 85  
       throws ServletException, IOException {
 86  
 
 87  28
     HttpServletRequest httpRequest = (HttpServletRequest)request;
 88  28
     PebbleContext pebbleContext = PebbleContext.getInstance();
 89  
     AbstractBlog blog;
 90  
 
 91  28
     String url = pebbleContext.getConfiguration().getUrl();
 92  28
     if (pebbleContext != null && (url == null || url.length() == 0)) {
 93  0
       String scheme = httpRequest.getScheme();
 94  0
       url = scheme + "://" + httpRequest.getServerName() + ":" + httpRequest.getServerPort() + httpRequest.getContextPath();
 95  0
       log.info("Setting Pebble URL to " + url);
 96  0
       PebbleContext.getInstance().getConfiguration().setUrl(url);
 97  
     }
 98  
 
 99  
     // get URI and strip off the context (e.g. /blog)
 100  28
     String uri = httpRequest.getRequestURI();
 101  28
     uri = uri.substring(httpRequest.getContextPath().length(), uri.length());
 102  
 
 103  
     // now we're left with a URI
 104  28
     if (BlogManager.getInstance().isMultiBlog()) {
 105  16
       if (uri.length() == 0) {
 106  4
         uri = "/";
 107  
       }
 108  16
       int index = uri.indexOf("/", 1);
 109  16
       if (index == -1) {
 110  12
         index = uri.length();
 111  
       }
 112  
 
 113  16
       String blogName = null;
 114  16
       if (pebbleContext.getConfiguration().isVirtualHostingEnabled()) {
 115  0
         String serverName = httpRequest.getServerName();
 116  0
         blogName = serverName.substring(0, serverName.indexOf("."));
 117  0
       } else {
 118  16
         blogName = uri.substring(1, index);
 119  
       }
 120  
 
 121  16
       blogName = URLDecoder.decode(blogName, "UTF-8");
 122  16
       if (BlogManager.getInstance().hasBlog(blogName)) {
 123  8
         blog = BlogManager.getInstance().getBlog(blogName);
 124  8
         uri = uri.substring(index, uri.length());
 125  
       } else {
 126  8
         blog = BlogManager.getInstance().getMultiBlog();
 127  
       }
 128  16
     } else {
 129  12
       blog = BlogManager.getInstance().getBlog();
 130  
     }
 131  
 
 132  28
     httpRequest.setAttribute(Constants.BLOG_KEY, blog);
 133  28
     httpRequest.setAttribute(Constants.BLOG_MANAGER, BlogManager.getInstance());
 134  28
     httpRequest.setAttribute(Constants.PEBBLE_CONTEXT, pebbleContext);
 135  
 
 136  28
     if (blog instanceof Blog) {
 137  20
       httpRequest.setAttribute(Constants.BLOG_TYPE, "singleblog");
 138  
     } else {
 139  8
       httpRequest.setAttribute(Constants.BLOG_TYPE, "multiblog");
 140  
     }
 141  
 
 142  28
     chain.doFilter(request, response);
 143  28
   }
 144  
 }