Coverage Report - net.sourceforge.pebble.web.filter.PreProcessingFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
PreProcessingFilter
0%
0/51
0%
0/32
6.333
 
 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.Configuration;
 35  
 import net.sourceforge.pebble.Constants;
 36  
 import net.sourceforge.pebble.PebbleContext;
 37  
 import net.sourceforge.pebble.security.PebbleUserDetails;
 38  
 import net.sourceforge.pebble.util.HttpsURLRewriter;
 39  
 import net.sourceforge.pebble.util.SecurityUtils;
 40  
 import net.sourceforge.pebble.util.UrlRewriter;
 41  
 import net.sourceforge.pebble.util.Utilities;
 42  
 import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
 43  
 import net.sourceforge.pebble.comparator.BlogEntryComparator;
 44  
 import net.sourceforge.pebble.comparator.BlogByLastModifiedDateComparator;
 45  
 import net.sourceforge.pebble.decorator.ContentDecoratorChain;
 46  
 import net.sourceforge.pebble.domain.*;
 47  
 import org.apache.commons.logging.Log;
 48  
 import org.apache.commons.logging.LogFactory;
 49  
 
 50  
 import javax.servlet.*;
 51  
 import javax.servlet.http.HttpServletRequest;
 52  
 import javax.servlet.jsp.jstl.core.Config;
 53  
 import java.io.IOException;
 54  
 import java.util.Collections;
 55  
 import java.util.List;
 56  
 import java.util.Locale;
 57  
 
 58  
 /**
 59  
  * A filter respsonsible for setting up common objects.
 60  
  *
 61  
  * @author    Simon Brown
 62  
  */
 63  0
 public class PreProcessingFilter implements Filter {
 64  
 
 65  
   /** the log used by this class */
 66  0
   private static Log log = LogFactory.getLog(PreProcessingFilter.class);
 67  
 
 68  
   /**
 69  
    * Initialises this instance.
 70  
    *
 71  
    * @param config    a FilterConfig instance
 72  
    */
 73  
   public void init(FilterConfig config) {
 74  0
   }
 75  
 
 76  
   /**
 77  
    * Called when this filter is taken out of service.
 78  
    */
 79  
   public void destroy() {
 80  0
   }
 81  
 
 82  
   /**
 83  
    * Contains the processing associated with this filter.
 84  
    */
 85  
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 86  
       throws ServletException, IOException {
 87  
 
 88  0
     HttpServletRequest httpRequest = (HttpServletRequest)request;
 89  
 
 90  
 //    log.info("Session is " + httpRequest.getSession().getId());
 91  
 //    HttpServletResponse httpResponse = (HttpServletResponse)response;
 92  
 //    Cookie cookie = CookieUtils.getCookie(httpRequest.getCookies(), "JSESSIONID");
 93  
 //    if (cookie != null) {
 94  
 //      log.info("Domain is " + cookie.getDomain());
 95  
 //      log.info("Path is " + cookie.getPath());
 96  
 //      cookie.setDomain(".example.com");
 97  
 //      cookie.setPath("/");
 98  
 //      httpResponse.addCookie(cookie);
 99  
 //    }
 100  
 
 101  0
     String externalUri = (String)request.getAttribute(Constants.EXTERNAL_URI);
 102  0
     if (externalUri.startsWith("/common/") ||
 103  
         externalUri.startsWith("/dwr/") ||
 104  
         externalUri.startsWith("/FCKeditor/") ||
 105  
         externalUri.startsWith("/scripts/") ||
 106  
         externalUri.startsWith("/themes/") ||
 107  
         externalUri.equals("/pebble.css") ||
 108  
         externalUri.equals("/jckconfig_pebble.js") ||
 109  
         externalUri.equals("/favicon.ico")
 110  
         ) {
 111  
         // do nothing
 112  
     } else {
 113  0
       AbstractBlog blog = (AbstractBlog)request.getAttribute(Constants.BLOG_KEY);
 114  0
       if (blog instanceof Blog) {
 115  0
         Blog b = (Blog)blog;
 116  0
         ContentDecoratorContext context = new ContentDecoratorContext();
 117  0
         context.setView(ContentDecoratorContext.SUMMARY_VIEW);
 118  0
         context.setMedia(ContentDecoratorContext.HTML_PAGE);
 119  
 
 120  0
         List<BlogEntry> blogEntries = b.getRecentPublishedBlogEntries();
 121  0
         ContentDecoratorChain.decorate(context, blogEntries);
 122  0
         Collections.sort(blogEntries, new BlogEntryComparator());
 123  0
         httpRequest.setAttribute(Constants.RECENT_BLOG_ENTRIES, blogEntries);
 124  
 
 125  0
         List<Response> recentApprovedResponses = b.getRecentApprovedResponses();
 126  0
         for (Response r : recentApprovedResponses) {
 127  0
           if (r instanceof Comment) {
 128  0
             b.getContentDecoratorChain().decorate(context, (Comment)r);
 129  0
           } else if (r instanceof TrackBack){
 130  0
             b.getContentDecoratorChain().decorate(context, (TrackBack)r);
 131  
           }
 132  
         }
 133  0
         httpRequest.setAttribute(Constants.RECENT_RESPONSES, recentApprovedResponses);
 134  
 
 135  0
         httpRequest.setAttribute(Constants.CATEGORIES, b.getCategories());
 136  0
         httpRequest.setAttribute(Constants.TAGS, b.getTags());
 137  0
         httpRequest.setAttribute(Constants.PLUGIN_PROPERTIES, b.getPluginProperties());
 138  0
         httpRequest.setAttribute(Constants.ARCHIVES, b.getArchives());
 139  0
         httpRequest.setAttribute(Constants.BLOG_TYPE, "singleblog");
 140  0
       } else {
 141  0
         httpRequest.setAttribute(Constants.BLOG_TYPE, "multiblog");
 142  
       }
 143  
 
 144  0
       if (PebbleContext.getInstance().getConfiguration().isMultiBlog()) {
 145  0
         httpRequest.setAttribute(Constants.MULTI_BLOG_KEY, BlogManager.getInstance().getMultiBlog());
 146  0
         httpRequest.setAttribute(Constants.MULTI_BLOG_URL, Utilities.calcBaseUrl(request.getScheme(), BlogManager.getInstance().getMultiBlog().getUrl()));
 147  
 
 148  0
         List<Blog> blogs = BlogManager.getInstance().getPublicBlogs();
 149  0
         Collections.sort(blogs, new BlogByLastModifiedDateComparator());
 150  0
         httpRequest.setAttribute(Constants.BLOGS, blogs);
 151  
       }
 152  
 
 153  
       // change the character encoding so that we can successfully get
 154  
       // international characters from the request when HTML forms are submitted
 155  
       // ... but only if the browser doesn't send the character encoding back
 156  0
       if (request.getCharacterEncoding() == null) {
 157  0
         request.setCharacterEncoding(blog.getCharacterEncoding());
 158  
       }
 159  
 
 160  0
       PebbleUserDetails user = SecurityUtils.getUserDetails();
 161  0
       if (user != null) {
 162  0
         httpRequest.setAttribute(Constants.AUTHENTICATED_USER, user);
 163  
       }
 164  
 
 165  0
       Config.set(request, Config.FMT_LOCALE, blog.getLocale());
 166  0
       Config.set(request, Config.FMT_FALLBACK_LOCALE, Locale.ENGLISH);
 167  
     }
 168  
 
 169  
     try {
 170  0
                   Configuration configuration = PebbleContext.getInstance().getConfiguration();
 171  0
                   if(configuration.getSecureUrl().startsWith("https")) {
 172  0
                           UrlRewriter.useThisRewriter(new HttpsURLRewriter(request.getScheme()));
 173  
                   }
 174  
             
 175  0
             chain.doFilter(request, response);
 176  
     } finally {
 177  0
             UrlRewriter.clear();
 178  0
     }
 179  0
   }
 180  
 }