Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../../img/srcFileCovDistChart9.png 23% of files have more coverage
36   144   12   12
14   75   0,33   3
3     4  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  BlogLookupFilter       Line # 58 36 0% 12 8 84,9% 0.8490566
 
  (4)
 
1    /*
2    * Copyright (c) 2003-2006, 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    public class BlogLookupFilter implements Filter {
59   
60    /** the log used by this class */
61    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  4 toggle public void init(FilterConfig config) {
72  4 this.filterConfig = config;
73    }
74   
75    /**
76    * Called when this filter is taken out of service.
77    */
 
78  4 toggle public void destroy() {
79    }
80   
81    /**
82    * Contains the processing associated with this filter.
83    */
 
84  14 toggle public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
85    throws ServletException, IOException {
86   
87  14 HttpServletRequest httpRequest = (HttpServletRequest)request;
88  14 PebbleContext pebbleContext = PebbleContext.getInstance();
89  14 AbstractBlog blog;
90   
91  14 String url = pebbleContext.getConfiguration().getUrl();
92  14 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  14 String uri = httpRequest.getRequestURI();
101  14 uri = uri.substring(httpRequest.getContextPath().length(), uri.length());
102   
103    // now we're left with a URI
104  14 if (BlogManager.getInstance().isMultiBlog()) {
105  8 if (uri.length() == 0) {
106  2 uri = "/";
107    }
108  8 int index = uri.indexOf("/", 1);
109  8 if (index == -1) {
110  6 index = uri.length();
111    }
112   
113  8 String blogName = null;
114  8 if (pebbleContext.getConfiguration().isVirtualHostingEnabled()) {
115  0 String serverName = httpRequest.getServerName();
116  0 blogName = serverName.substring(0, serverName.indexOf("."));
117    } else {
118  8 blogName = uri.substring(1, index);
119    }
120   
121  8 blogName = URLDecoder.decode(blogName, "UTF-8");
122  8 if (BlogManager.getInstance().hasBlog(blogName)) {
123  4 blog = BlogManager.getInstance().getBlog(blogName);
124  4 uri = uri.substring(index, uri.length());
125    } else {
126  4 blog = BlogManager.getInstance().getMultiBlog();
127    }
128    } else {
129  6 blog = BlogManager.getInstance().getBlog();
130    }
131   
132  14 httpRequest.setAttribute(Constants.BLOG_KEY, blog);
133  14 httpRequest.setAttribute(Constants.BLOG_MANAGER, BlogManager.getInstance());
134  14 httpRequest.setAttribute(Constants.PEBBLE_CONTEXT, pebbleContext);
135   
136  14 if (blog instanceof Blog) {
137  10 httpRequest.setAttribute(Constants.BLOG_TYPE, "singleblog");
138    } else {
139  4 httpRequest.setAttribute(Constants.BLOG_TYPE, "multiblog");
140    }
141   
142  14 chain.doFilter(request, response);
143    }
144    }