preprocessingfilter.java

### File Source: preprocessingfilter.java

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

SofCheck Inspector Build Version : 2.22510

preprocessingfilter.java | 2010-Jul-19 18:33:16
preprocessingfilter.class | 2010-Jul-19 20:23:38