PathInfoMapper.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.chain.web.javax.servlet;

  18. import javax.servlet.http.HttpServletRequest;

  19. import org.apache.commons.chain.Catalog;
  20. import org.apache.commons.chain.Command;
  21. import org.apache.commons.chain.Context;
  22. import org.apache.commons.chain.generic.LookupCommand;

  23. /**
  24.  * {@link Command} that uses the "path info" component of the request URI
  25.  * to select a {@link Command} from the appropriate {@link Catalog}, and
  26.  * execute it. To use this command, you would typically map an instance
  27.  * of {@link ChainProcessor} to a wildcard pattern like "/execute/*" and
  28.  * then arrange that this is the default command to be executed. In such
  29.  * an environment, a request for the context-relative URI "/execute/foo"
  30.  * would cause the "/foo" command to be loaded and executed.
  31.  *
  32.  * @author Craig R. McClanahan
  33.  */
  34. public class PathInfoMapper extends LookupCommand<ServletWebContext> {

  35.     // ------------------------------------------------------ Instance Variables

  36.     private String catalogKey = ChainProcessor.CATALOG_DEFAULT;

  37.     // ------------------------------------------------------------ Constructors

  38.     /**
  39.      * The Default-Constructor for this class.
  40.      */
  41.     public PathInfoMapper() {
  42.     }

  43.     // -------------------------------------------------------------- Properties

  44.     /**
  45.      * Return the context key under which our {@link Catalog} has been
  46.      * stored.
  47.      *
  48.      * @return The context key for the Catalog.
  49.      *
  50.      * @deprecated Use catalogName to specify the name of the catalog in the
  51.      *             catalog factory.
  52.      */
  53.     @Deprecated
  54.     public String getCatalogKey() {
  55.         return this.catalogKey;
  56.     }

  57.     /**
  58.      * Set the context key under which our {@link Catalog} has been
  59.      * stored.
  60.      *
  61.      * @param catalogKey The new catalog key
  62.      *
  63.      * @deprecated Use catalogName to specify the name of the catalog in the
  64.      *             catalog factory.
  65.      */
  66.     @Deprecated
  67.     public void setCatalogKey(String catalogKey) {
  68.         this.catalogKey = catalogKey;
  69.     }

  70.     // --------------------------------------------------------- Command Methods

  71.     /**
  72.      * Look up the extra path information for this request, and use it to
  73.      * select an appropriate {@link Command} to be executed.
  74.      *
  75.      * @param context Context for the current request
  76.      *
  77.      * @return The name of the {@link Command} instance
  78.      *
  79.      * @since Chain 1.2
  80.      */
  81.     @Override
  82.     protected String getCommandName(ServletWebContext context) {
  83.         // Look up the extra path information for this request
  84.         final HttpServletRequest request = context.getRequest();
  85.         final Object attrPathInfo = request.getAttribute("javax.servlet.include.path_info");
  86.         final String pathInfo = attrPathInfo == null
  87.                 ? request.getPathInfo()
  88.                 : attrPathInfo.toString();

  89.         return pathInfo;
  90.     }

  91.     /**
  92.      * Return the {@link Catalog} to look up the {@link Command} in.
  93.      *
  94.      * @param context {@link Context} for this request
  95.      *
  96.      * @return The catalog.
  97.      *
  98.      * @throws IllegalArgumentException if no {@link Catalog}
  99.      *         can be found
  100.      *
  101.      * @since Chain 1.2
  102.      */
  103.     @Override
  104.     protected Catalog<ServletWebContext> getCatalog(ServletWebContext context) {
  105.         /* If the object returned from the passed context is not a valid catalog
  106.          * then we use the super class's catalog extraction logic to pull it
  107.          * or to error gracefully.
  108.          */
  109.         Object testCatalog = context.get(getCatalogKey());

  110.         /* Assume that the underlying implementation is following convention and
  111.          * returning a catalog with the current context.
  112.          */
  113.         @SuppressWarnings("unchecked")
  114.         Catalog<ServletWebContext> catalog = testCatalog instanceof Catalog
  115.                     ? (Catalog<ServletWebContext>) testCatalog
  116.                     : super.getCatalog(context);

  117.         return catalog;
  118.     }
  119. }