KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > portlet > handler > ParameterMappingInterceptor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16
17 package org.springframework.web.portlet.handler;
18
19 import javax.portlet.ActionRequest;
20 import javax.portlet.ActionResponse;
21
22 /**
23  * <p>Interceptor to forward a request parameter from the <code>ActionRequest</code> to the
24  * <code>RenderRequest</code>.</p>
25  *
26  * <p>This can be useful when using {@link ParameterHandlerMapping ParameterHandlerMapping}
27  * or {@link PortletModeParameterHandlerMapping PortletModeParameterHandlerMapping}.
28  * It will ensure that the parameter that was used to map the <code>ActionRequest</code>
29  * to a handler will be forwarded to the <code>RenderRequest</code> so that it will also be
30  * mapped the same way.</p>
31  *
32  * <p>When using this Interceptor, you can still change the value of the mapping parameter
33  * in your handler in order to change where the render request will get mapped.</p>
34  *
35  * <p>Be aware that this Interceptor does call <code>ActionResponse.setRenderParameter</code>,
36  * which means that you will not be able to call <code>ActionResponse.sendRedirect</code> in
37  * your handler. If you may need to issue a redirect, then you should avoid this Interceptor
38  * and either write a different one that does this in a different way, or manually forward
39  * the parameter from within your handler(s).</p>
40  *
41  * @author Rainer Schmitz
42  * @author John A. Lewis
43  * @since 2.0
44  * @see ParameterHandlerMapping
45  * @see PortletModeParameterHandlerMapping
46  */

47 public class ParameterMappingInterceptor extends HandlerInterceptorAdapter {
48
49     /** Request parameter name to use for mapping to handlers */
50     public final static String JavaDoc DEFAULT_PARAMETER_NAME = "action";
51
52     private String JavaDoc parameterName = DEFAULT_PARAMETER_NAME;
53
54
55     /**
56      * Set the name of the parameter used for mapping.
57      */

58     public void setParameterName(String JavaDoc parameterName) {
59         this.parameterName = (parameterName != null ? parameterName : DEFAULT_PARAMETER_NAME);
60     }
61
62
63     /**
64      * If request is an {@link javax.portlet.ActionRequest ActionRequest},
65      * get handler mapping parameter and add it to the ActionResponse.
66      */

67     public boolean preHandleAction(ActionRequest request, ActionResponse response, Object JavaDoc handler) {
68         String JavaDoc mappingParameter = request.getParameter(this.parameterName);
69         if (mappingParameter != null) {
70             response.setRenderParameter(parameterName, mappingParameter);
71         }
72         return true;
73     }
74
75 }
76
Popular Tags