transformingfilter.java

### File Source: transformingfilter.java

             /*
        P/P   *  Method: void net.sourceforge.pebble.web.filter.TransformingFilter()
              */
         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.Constants;
        35  import net.sourceforge.pebble.domain.AbstractBlog;
        36  import net.sourceforge.pebble.domain.Blog;
        37  import net.sourceforge.pebble.domain.BlogManager;
        38  import net.sourceforge.pebble.domain.MultiBlog;
        39  import org.apache.commons.logging.Log;
        40  import org.apache.commons.logging.LogFactory;
        41
        42  import javax.servlet.*;
        43  import javax.servlet.http.HttpServletRequest;
        44  import java.io.IOException;
        45  import java.net.URLDecoder;
        46
        47  /**
        48   * A filter that transforms an incoming URI into an action URI.
        49   *
        50   * @author    Simon Brown
        51   */
        52  public class TransformingFilter implements Filter {
        53
        54    /** the config of this filter */
        55    private FilterConfig filterConfig;
        56
        57    /** the log used by this class */
        58    private static Log log = LogFactory.getLog(TransformingFilter.class);
        59
        60    /**
        61     * Initialises this instance.
        62     *
        63     * @param config    a FilterConfig instance
        64     */
               /*
        P/P     *  Method: void init(FilterConfig)
                *
                *  Postconditions:
                *    this.filterConfig == config
                *    init'ed(this.filterConfig)
                */
        65    public void init(FilterConfig config) {
        66      this.filterConfig = config;
        67    }
        68
        69    /**
        70     * Called when this filter is taken out of service.
        71     */
               /*
        P/P     *  Method: void destroy()
                */
        72    public void destroy() {
        73    }
        74
        75    /**
        76     * Contains the processing associated with this filter.
        77     */
        78    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        79        throws ServletException, IOException {
        80
        81      HttpServletRequest httpRequest = (HttpServletRequest)request;
        82      AbstractBlog blog = (AbstractBlog)request.getAttribute(Constants.BLOG_KEY);
        83
        84      // get URI and strip off the context (e.g. /blog)
        85      String uri = httpRequest.getRequestURI();
        86      uri = uri.substring(httpRequest.getContextPath().length(), uri.length());
        87
        88      // now we're left with a URI
        89      if (BlogManager.getInstance().isMultiBlog()) {
        90        if (uri.length() == 0) {
        91          uri = "/";
        92        }
        93        int index = uri.indexOf("/", 1);
        94        if (index == -1) {
        95          index = uri.length();
        96        }
        97        String blogName = uri.substring(1, index);
        98        blogName = URLDecoder.decode(blogName, "UTF-8");
        99        if (BlogManager.getInstance().hasBlog(blogName)) {
       100          uri = uri.substring(index, uri.length());
       101        }
       102      }
       103
       104      if (uri == null || uri.trim().equals("")) {
       105        uri = "/";
       106      }
       107      log.trace("uri : " + uri);
       108
       109      if (httpRequest.getQueryString() != null) {
       110        uri += "?" + httpRequest.getQueryString();
       111      }
       112      httpRequest.setAttribute(Constants.EXTERNAL_URI, uri);
       113
       114      UriTransformer transformer = new UriTransformer();
       115      String internalUri;
       116
       117      if (blog instanceof Blog) {
       118        internalUri = transformer.getUri(uri, (Blog)blog);
       119      } else {
       120        internalUri = transformer.getUri(uri, (MultiBlog)blog);
       121      }
       122
       123      request.setAttribute(Constants.INTERNAL_URI, internalUri);
       124
       125      log.trace(uri + " -> " + internalUri);
       126
       127      chain.doFilter(request, response);
       128    }
       129
       130  }

SofCheck Inspector Build Version : 2.22510

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