File Source: RequestMappingFilter.java

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.ui.rendering.filters;
    20  
    21  import java.io.IOException;
    22  import java.util.ArrayList;
    23  import java.util.Iterator;
    24  import java.util.List;
    25  import javax.servlet.Filter;
    26  import javax.servlet.FilterChain;
    27  import javax.servlet.FilterConfig;
    28  import javax.servlet.ServletException;
    29  import javax.servlet.ServletRequest;
    30  import javax.servlet.ServletResponse;
    31  import javax.servlet.http.HttpServletRequest;
    32  import javax.servlet.http.HttpServletResponse;
    33  import org.apache.commons.logging.Log;
    34  import org.apache.commons.logging.LogFactory;
    35  import org.apache.roller.weblogger.config.WebloggerConfig;
    36  import org.apache.roller.weblogger.ui.rendering.RequestMapper;
    37  
    38  /**
    39   * Provides generalized request mapping capablilites.
    40   *
    41   * Incoming requests can be inspected by a series of RequestMappers and can
    42   * potentially be re-routed to different places within the application.
    43   *
    44   * @web.filter name="RequestMappingFilter"
    45   */
         /* 
    P/P   *  Method: void org.apache.roller.weblogger.ui.rendering.filters.RequestMappingFilter()
          * 
          *  Postconditions:
          *    this.requestMappers == &new ArrayList(RequestMappingFilter#1)
          *    new ArrayList(RequestMappingFilter#1) num objects == 1
          */
    46  public class RequestMappingFilter implements Filter {
    47      
             /* 
    P/P       *  Method: org.apache.roller.weblogger.ui.rendering.filters.RequestMappingFilter__static_init
              * 
              *  Postconditions:
              *    init'ed(log)
              */
    48      private static Log log = LogFactory.getLog(RequestMappingFilter.class);
    49      
    50      // list of RequestMappers that want to inspect the request
    51      private final List requestMappers = new ArrayList();
    52      
    53      
    54      public void init(FilterConfig filterConfig) {
    55          
    56          // lookup set of request mappers we are going to use
                 /* 
    P/P           *  Method: void init(FilterConfig)
                  * 
                  *  Preconditions:
                  *    log != null
                  *    this.requestMappers != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:length(...)@61: {0}, {1..232-1}
                  *    java.lang.String:length(...)@80: {0}, {1..232-1}
                  *    java.util.List:size(...)@98: {1..232-1}, {-231..0}
                  *    org.apache.roller.weblogger.config.WebloggerConfig:getProperty(...)@57: Addr_Set{null}, Inverse{null}
                  *    org.apache.roller.weblogger.config.WebloggerConfig:getProperty(...)@58: Addr_Set{null}, Inverse{null}
                  */
    57          String rollerMappers = WebloggerConfig.getProperty("rendering.rollerRequestMappers");
    58          String userMappers = WebloggerConfig.getProperty("rendering.userRequestMappers");
    59          
    60          // instantiate user defined request mapper classes
    61          if(userMappers != null && userMappers.trim().length() > 0) {
    62              
    63              RequestMapper requestMapper = null;
    64              String[] uMappers = userMappers.split(",");
+   65              for(int i=0; i < uMappers.length; i++) {
    66                  try {
+   67                      Class mapperClass = Class.forName(uMappers[i]);
    68                      requestMapper = (RequestMapper) mapperClass.newInstance();
    69                      requestMappers.add(requestMapper);
    70                  } catch(ClassCastException cce) {
    71                      log.error("It appears that your mapper does not implement "+
    72                              "the RequestMapper interface", cce);
    73                  } catch(Exception e) {
    74                      log.error("Unable to instantiate request mapper ["+uMappers[i]+"]", e);
    75                  }
    76              }
    77          }
    78          
    79          // instantiate roller standard request mapper classes
    80          if(rollerMappers != null && rollerMappers.trim().length() > 0) {
    81              
    82              RequestMapper requestMapper = null;
    83              String[] rMappers = rollerMappers.split(",");
+   84              for(int i=0; i < rMappers.length; i++) {
    85                  try {
+   86                      Class mapperClass = Class.forName(rMappers[i]);
    87                      requestMapper = (RequestMapper) mapperClass.newInstance();
    88                      requestMappers.add(requestMapper);
    89                  } catch(ClassCastException cce) {
    90                      log.error("It appears that your mapper does not implement "+
    91                              "the RequestMapper interface", cce);
    92                  } catch(Exception e) {
    93                      log.error("Unable to instantiate request mapper ["+rMappers[i]+"]", e);
    94                  }
    95              }
    96          }
    97          
    98          if(requestMappers.size() < 1) {
    99              // hmm ... failed to load any request mappers?
   100              log.warn("Failed to load any request mappers.  "+
   101                      "Weblog urls probably won't function as you expect.");
   102          }
   103          
   104          log.info("Request mapping filter initialized, "+requestMappers.size()+
   105                  " mappers configured.");
   106      }
   107      
   108      
   109      /**
   110       * Inspect incoming urls and see if they should be routed.
   111       */
   112      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
   113              throws IOException, ServletException {
   114          
                 /* 
    P/P           *  Method: void doFilter(ServletRequest, ServletResponse, FilterChain)
                  * 
                  *  Preconditions:
                  *    log != null
                  *    this.requestMappers != null
                  *    (soft) chain != null
                  *    (soft) org/apache/roller/weblogger/ui/rendering/WeblogRequestMapper.log != null
                  *    (soft) req != null
                  *    (soft) res != null
                  * 
                  *  Presumptions:
                  *    java.lang.Object:getClass(...)@126 != null
                  *    java.lang.Object:getClass(...)@131 != null
                  *    java.util.Iterator:next(...)@124 != null
                  *    mapper.restricted@126 != null
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@123: {0}, {1}
                  */
   115          HttpServletRequest request = (HttpServletRequest) req;
   116          HttpServletResponse response = (HttpServletResponse) res;
   117          
   118          log.debug("entering");
   119          
   120          // give each mapper a chance to handle the request
   121          RequestMapper mapper = null;
   122          Iterator mappersIT = this.requestMappers.iterator();
   123          while(mappersIT.hasNext()) {
   124              mapper = (RequestMapper) mappersIT.next();
   125              
   126              log.debug("trying mapper "+mapper.getClass().getName());
   127              
   128              boolean wasHandled = mapper.handleRequest(request, response);
   129              if(wasHandled) {
   130                  // if mapper has handled the request then we are done
   131                  log.debug("request handled by "+mapper.getClass().getName());
   132                  log.debug("exiting");
   133                  return;
   134              }
   135          }
   136          
   137          log.debug("request not mapped");
   138          
   139          // nobody handled the request, so let it continue as usual
   140          chain.doFilter(request, response);
   141          
   142          log.debug("exiting");
   143      }
   144      
   145      
             /* 
    P/P       *  Method: void destroy()
              */
   146      public void destroy() {}
   147      
   148  }








SofCheck Inspector Build Version : 2.18479
RequestMappingFilter.java 2009-Jan-02 14:24:52
RequestMappingFilter.class 2009-Sep-04 03:12:44