Hy, all;

i had a problem with RequestParamAction (cocoon-2.0.4). I enclose
the Source of a slightly modified version of RequestParamAction
that solves my problem. The class is named ExtendedRequestParamAction,
i did a little of refactoring and a bit on the documentation
of the class. In case it makes sense, i would be glad if someone
could replace the body of RequestParamAction with the body of my
modified class, which is fully compatible to the original.
(I don't know how to apply a patch myself ...)

In the following i explain, why i made the modifications:

The problem:
------------

I want to use the requestQuery of an incoming request in my sitemap
as follows:

<map:match pattern="*/canvas/search_mask">
<map:act type="request" >
<map:parameter name="default.fieldtype" value="required"/>
<map:generate src="http:/hmyserver/result{requestQuery}"/>
</map:act>
<map:serialize type = "xml"/>
</map:match>

This would forward the GET parameters of the original request to the HTTP call for the src in map:generate.
In addition i want to forward the parameter fieldtype. How would i do
this with RequestParamAction ?

The RequestParamAction can't help me there because:

1.) It won't add the fieldtype to the requestQuery automatically.
so i must add it myself, e.g. like this:

<map:generate
src="http:/hmyserver/result{requestQuery}&fieldtype={fieldtype}"/>

This fails, if {requestQuery} happens to be empty. Due to the
treatment of requestQuery in the code i would miss the
beginning "?" of my querylist in that case.

<map:generate
src="http:/hmyserver/result?fieldtype={fieldtype}&{requestQuery}"/>

wouldn't work either. In this case i would end with duplicate "?",
if requestQuery is NOT empty.

2.) I could use the parameters="true":

<map:act type="request" >
<map:parameter name="default.fieldtype" value="required"/>
<map:parameter name="parameters" value="true"/>
<map:generate ...

Then i need to know exactly, which parameters are given in the
original request and i would need to specify each parameter
in the sitemap, which i don't want (i even don't know, which
parameters are enclosed in the list).


the solution
------------

In order to get around this problem i added a new parameter named
"usedefaults". If this parameter is given in the sitemap, then
all default parameters, which are not allready set in the query are
automatically added to the requestQuery string.

By this i can now setup my sitemap as follows:

<map:match pattern="*/canvas/search_mask">
<map:act type="request" >
<map:parameter name="usedefault" value="true"/>
<map:parameter name="default.fieldtype" value="required"/>
<map:generate src="http:/hmyserver/result.jsp{requestQuery}"/>
</map:act>
<map:serialize type = "xml"/>
</map:match>

and i am shure, fieldtype is enclosed in the requestQuery, either
from the request itself, or added by the action.

limitations
-----------

default parameters are always added after the original queryString,
hence i have no control of the insertion place in the queryList. This
may be necessary in some pathological cases.


regards, Hussayn
/*

 ============================================================================
                   The Apache Software License, Version 1.1
 ============================================================================

 Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.

 Redistribution and use in source and binary forms, with or without modifica-
 tion, are permitted provided that the following conditions are met:

 1. Redistributions of  source code must  retain the above copyright  notice,
    this list of conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

 3. The end-user documentation included with the redistribution, if any, must
    include  the following  acknowledgment:  "This product includes  software
    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
    Alternately, this  acknowledgment may  appear in the software itself,  if
    and wherever such third-party acknowledgments normally appear.

 4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
    used to  endorse or promote  products derived from  this software without
    prior written permission. For written permission, please contact
    [EMAIL PROTECTED]

 5. Products  derived from this software may not  be called "Apache", nor may
    "Apache" appear  in their name,  without prior written permission  of the
    Apache Software Foundation.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
 APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
 DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
 OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
 ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
 (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 This software  consists of voluntary contributions made  by many individuals
 on  behalf of the Apache Software  Foundation and was  originally created by
 Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
 Software Foundation, please see <http://www.apache.org/>.

*/
package org.apache.cocoon.acting;

/*
 * Standard imports
 */
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.Constants;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * This action makes some request details available to the sitemap via parameter
 * substitution. Within this action context following parameters get available:
 *
 * 
 * OUPTUT PARAMETERS
 * =================
 * output parameters available within the action context:
 * 
 * {context}      - is the context path of the servlet (usually "/cocoon")
 * {requestURI}   - is the requested URI without parameters
 * {requestQuery} - is the query string like "?param1=test" if there are 
 *                  GET params available
 *
 * These parameters are always available. 
 *
 * 
 * CONTROL PARAMETERS 
 * ================== 
 * The action can be further controlled via sitemap arameters:
 * 
 * sitemap parameters:
 * 
 * parameters="true"  - all request parameters are made available as
 *                      sitemap params using their names.
 * usedefaults="true" - default values (see below) will be added to 
 *                      requestQuery if not allready exisiting in requestQuery
 * 
 * All control parameters can be used in parallel.
 *
 * 
 * CREATING DEFAULT VALUES
 * =======================
 * Default values can be set for request parameters, by including 
 * sitemap parameters following the syntax "default.<parameter-name>".
 *
 * 
 * Sitemap definition:
 *
 * <pre>
 * &lt;map:action name="request" 
src="org.apache.cocoon.acting.ExtendedRequestParamAction"/&gt;
 * </pre>
 *
 * <p>
 *
 * Example use:
 *
 * <pre>
 * &lt;map:match pattern="some-resource"&gt;
 *  &lt;map:act type="request"&gt;
 *     &lt;map:parameter name="parameters" value="true"/&gt;
 *     &lt;map:parameter name="default.dest" value="invalid-destination.html"/&gt;
 *     &lt;map:redirect-to uri="{context}/somewhereelse/{dest}"/&gt;
 *  &lt;/map:act&gt;
 * &lt;/map:match&gt;
 * </pre>
 *
 * Redirection is only one example, another use:
 *
 * <pre>
 * &lt;map:match pattern="some-resource"&gt;
 *  &lt;map:act type="request"&gt;
 *     &lt;map:parameter name="parameters" value="true"/&gt;
 *     &lt;map:generate src="users/menu-{id}.xml"/&gt;
 *  &lt;/map:act&gt;
 *  &lt;map:transform src="menus/personalisation.xsl"/&gt;
 *  &lt;map:serialize/&gt;
 * &lt;/map:match&gt;
 * </pre>
 *
 * etc, etc.
 *
 * @author <a href="mailto:[EMAIL PROTECTED]";>Marcus Crafter</a>
 * @author <a href="mailto:[EMAIL PROTECTED]";>Torsten Curdt</a>
 * @author <a href="mailto:[EMAIL PROTECTED]";>Hussayn Dabbous</a>
 */
public class ExtendedRequestParamAction extends ComposerAction implements ThreadSafe {

    public final static String MAP_URI         = "requestURI";
    public final static String MAP_QUERY       = "requestQuery";
    public final static String MAP_CONTEXTPATH = "context";

    public final static String PARAM_PARAMETERS     = "parameters";
    public final static String PARAM_USEDEFAULTS    = "usedefaults";
    public final static String PARAM_DEFAULT_PREFIX = "default.";


    public Map act( Redirector redirector, SourceResolver resolver, Map objectModel, 
String source, Parameters param )
        throws Exception
    {

        Request request = ObjectModelHelper.getRequest(objectModel);
        if (request == null) {
          getLogger().error("RequestInfoAction: no request object!");
          return(null);
        }

        Map map = new HashMap();

        map.put(MAP_URI, request.getRequestURI());
        map.put(MAP_CONTEXTPATH, request.getContextPath());

                String query = request.getQueryString();

                boolean usedefaults = 
("true".equalsIgnoreCase(param.getParameter(PARAM_USEDEFAULTS, null)));
        boolean setparams   = 
("true".equalsIgnoreCase(param.getParameter(PARAM_PARAMETERS, null)));
        if (setparams){
          Enumeration e = request.getParameterNames();
          while(e.hasMoreElements()){
            String name = (String) e.nextElement();
            String value = request.getParameter(name);

            if (value != null && !map.containsKey(name)){
              map.put(name, value);
            }
          }
        }
        
        if(setparams || usedefaults) {
          String[] paramNames = param.getNames();
          for (int i=0; i< paramNames.length; i++) {
                if (paramNames[i].startsWith(PARAM_DEFAULT_PREFIX) &&
                
(request.getParameter(paramNames[i].substring(PARAM_DEFAULT_PREFIX.length())) == 
null)) {
                        String name = 
paramNames[i].substring(PARAM_DEFAULT_PREFIX.length());
                        String value= param.getParameter(paramNames[i]);
                        if(setparams) map.put(name, value);
                        if(usedefaults){
                                if(query==null) query=""; else query +="&";
                                query += name+"="+value;
                        }
                }
          }
        }

        if (query != null && query.length() > 0){
          map.put(MAP_QUERY, "?" + query);
        }
        else{
          map.put(MAP_QUERY, "");
        }
        return(map);
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to