bloglookupfilter.java

### File Source: bloglookupfilter.java

             /*
        P/P   *  Method: net.sourceforge.pebble.web.filter.BlogLookupFilter__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 org.apache.commons.logging.Log;
        35  import org.apache.commons.logging.LogFactory;
        36
        37  import javax.servlet.*;
             /*
        P/P   *  Method: void net.sourceforge.pebble.web.filter.BlogLookupFilter()
              */
        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     */
               /*
        P/P     *  Method: void init(FilterConfig)
                *
                *  Postconditions:
                *    this.filterConfig == config
                *    init'ed(this.filterConfig)
                */
        71    public void init(FilterConfig config) {
        72      this.filterConfig = config;
        73    }
        74
        75    /**
        76     * Called when this filter is taken out of service.
        77     */
               /*
        P/P     *  Method: void destroy()
                */
        78    public void destroy() {
        79    }
        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      HttpServletRequest httpRequest = (HttpServletRequest)request;
        88      PebbleContext pebbleContext = PebbleContext.getInstance();
        89      AbstractBlog blog;
        90
        91      String url = pebbleContext.getConfiguration().getUrl();
        92      if (pebbleContext != null && (url == null || url.length() == 0)) {
        93        String scheme = httpRequest.getScheme();
        94        url = scheme + "://" + httpRequest.getServerName() + ":" + httpRequest.getServerPort() + httpRequest.getContextPath();
        95        log.info("Setting Pebble URL to " + url);
        96        PebbleContext.getInstance().getConfiguration().setUrl(url);
        97      }
        98
        99      // get URI and strip off the context (e.g. /blog)
       100      String uri = httpRequest.getRequestURI();
       101      uri = uri.substring(httpRequest.getContextPath().length(), uri.length());
       102
       103      // now we're left with a URI
       104      if (BlogManager.getInstance().isMultiBlog()) {
       105        if (uri.length() == 0) {
       106          uri = "/";
       107        }
       108        int index = uri.indexOf("/", 1);
       109        if (index == -1) {
       110          index = uri.length();
       111        }
       112
       113        String blogName = null;
       114        if (pebbleContext.getConfiguration().isVirtualHostingEnabled()) {
       115          String serverName = httpRequest.getServerName();
       116          blogName = serverName.substring(0, serverName.indexOf("."));
       117        } else {
       118          blogName = uri.substring(1, index);
       119        }
       120
       121        blogName = URLDecoder.decode(blogName, "UTF-8");
       122        if (BlogManager.getInstance().hasBlog(blogName)) {
       123          blog = BlogManager.getInstance().getBlog(blogName);
       124          uri = uri.substring(index, uri.length());
       125        } else {
       126          blog = BlogManager.getInstance().getMultiBlog();
       127        }
       128      } else {
       129        blog = BlogManager.getInstance().getBlog();
       130      }
       131
       132      httpRequest.setAttribute(Constants.BLOG_KEY, blog);
       133      httpRequest.setAttribute(Constants.BLOG_MANAGER, BlogManager.getInstance());
       134      httpRequest.setAttribute(Constants.PEBBLE_CONTEXT, pebbleContext);
       135
       136      if (blog instanceof Blog) {
       137        httpRequest.setAttribute(Constants.BLOG_TYPE, "singleblog");
       138      } else {
       139        httpRequest.setAttribute(Constants.BLOG_TYPE, "multiblog");
       140      }
       141
       142      chain.doFilter(request, response);
       143    }
       144  }

SofCheck Inspector Build Version : 2.22510

bloglookupfilter.java | 2010-Jun-25 19:40:32
bloglookupfilter.class | 2010-Jul-19 20:23:38