Save This Page
Home » cocoon-2.1.11-src » org.apache » cocoon » acting » [javadoc | source]
    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.cocoon.acting;
   18   
   19   import java.util.Enumeration;
   20   import java.util.HashMap;
   21   import java.util.Map;
   22   
   23   import org.apache.avalon.framework.parameters.Parameters;
   24   import org.apache.avalon.framework.thread.ThreadSafe;
   25   
   26   import org.apache.cocoon.environment.ObjectModelHelper;
   27   import org.apache.cocoon.environment.Redirector;
   28   import org.apache.cocoon.environment.Request;
   29   import org.apache.cocoon.environment.SourceResolver;
   30   
   31   /**
   32    * This action makes some request details available to the sitemap via parameter
   33    * substitution.
   34    *
   35    * {context}      - is the context path of the servlet (usually "/cocoon")
   36    * {requestURI}   - is the requested URI without parameters
   37    * {requestQuery} - is the query string like "?param1=test" if there is one
   38    *
   39    * Additionlly all request parameters can be made available for use in the sitemap.
   40    * if the parameter "parameters" is set to true.
   41    * (A variable is created for each request parameter (only if it doesn't exist)
   42    * with the same name as the parameter itself)
   43    *
   44    * Default values can be set for request parameters, by including sitemap parameters
   45    * named "default.<parameter-name>".
   46    *
   47    * Sitemap definition:
   48    *
   49    * <pre>
   50    * &lt;map:action name="request" src="org.apache.cocoon.acting.RequestParamAction"/&gt;
   51    * </pre>
   52    *
   53    * <p>
   54    *
   55    * Example use:
   56    *
   57    * <pre>
   58    * &lt;map:match pattern="some-resource"&gt;
   59    *  &lt;map:act type="request"&gt;
   60    *     &lt;map:parameter name="parameters" value="true"/&gt;
   61    *     &lt;map:parameter name="default.dest" value="invalid-destination.html"/&gt;
   62    *     &lt;map:redirect-to uri="{context}/somewhereelse/{dest}"/&gt;
   63    *  &lt;/map:act&gt;
   64    * &lt;/map:match&gt;
   65    * </pre>
   66    *
   67    * Redirection is only one example, another use:
   68    *
   69    * <pre>
   70    * &lt;map:match pattern="some-resource"&gt;
   71    *  &lt;map:act type="request"&gt;
   72    *     &lt;map:parameter name="parameters" value="true"/&gt;
   73    *     &lt;map:generate src="users/menu-{id}.xml"/&gt;
   74    *  &lt;/map:act&gt;
   75    *  &lt;map:transform src="menus/personalisation.xsl"/&gt;
   76    *  &lt;map:serialize/&gt;
   77    * &lt;/map:match&gt;
   78    * </pre>
   79    *
   80    * etc, etc.
   81    *
   82    * @author <a href="mailto:Marcus.Crafter@osa.de">Marcus Crafter</a>
   83    * @author <a href="mailto:tcurdt@dff.st">Torsten Curdt</a>
   84    * @version CVS $Id: RequestParamAction.java 433543 2006-08-22 06:22:54Z crossley $
   85    */
   86   public class RequestParamAction extends ServiceableAction implements ThreadSafe {
   87   
   88       public final static String MAP_URI         = "requestURI";
   89       public final static String MAP_QUERY       = "requestQuery";
   90       public final static String MAP_CONTEXTPATH = "context";
   91   
   92       public final static String PARAM_PARAMETERS     = "parameters";
   93       public final static String PARAM_DEFAULT_PREFIX = "default.";
   94   
   95       public Map act(Redirector redirector, SourceResolver resolver, Map objectModel,
   96                      String source, Parameters param) throws Exception {
   97   
   98           Request request = ObjectModelHelper.getRequest(objectModel);
   99   
  100           Map map = new HashMap();
  101   
  102           map.put(MAP_URI, request.getRequestURI());
  103   
  104           String query = request.getQueryString();
  105           if (query != null && query.length() > 0) {
  106               map.put(MAP_QUERY, "?" + query);
  107           } else {
  108               map.put(MAP_QUERY, "");
  109           }
  110   
  111           map.put(MAP_CONTEXTPATH, request.getContextPath());
  112   
  113           if ("true".equalsIgnoreCase(param.getParameter(PARAM_PARAMETERS, null))) {
  114               Enumeration e = request.getParameterNames();
  115               while (e.hasMoreElements()) {
  116                   String name = (String)e.nextElement();
  117                   String value = request.getParameter(name);
  118   
  119                   if (value != null && !map.containsKey(name)) {
  120                       map.put(name, value);
  121                   }
  122               }
  123   
  124               String[] paramNames = param.getNames();
  125               for (int i = 0; i < paramNames.length; i++) {
  126                   if (paramNames[i].startsWith(PARAM_DEFAULT_PREFIX)
  127                           && (request.getParameter(paramNames[i].substring(PARAM_DEFAULT_PREFIX.length())) == null)) {
  128                       map.put(paramNames[i].substring(PARAM_DEFAULT_PREFIX.length()),
  129                               param.getParameter(paramNames[i]));
  130                   }
  131               }
  132           }
  133           return (map);
  134       }
  135   
  136   }

Save This Page
Home » cocoon-2.1.11-src » org.apache » cocoon » acting » [javadoc | source]