KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > util > DefaultRequestHelper


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.util;
13
14 import java.io.UnsupportedEncodingException JavaDoc;
15 import java.net.URLEncoder JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 import org.apache.commons.lang.ObjectUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.lang.UnhandledException;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.displaytag.Messages;
29
30
31 /**
32  * Default RequestHelper implementation.
33  * @author Fabrizio Giustina
34  * @version $Revision: 956 $ ($Author: fgiust $)
35  * @see org.displaytag.util.Href
36  * @see org.displaytag.util.RequestHelper
37  */

38 public class DefaultRequestHelper implements RequestHelper
39 {
40
41     /**
42      * logger.
43      */

44     private static Log log = LogFactory.getLog(DefaultRequestHelper.class);
45
46     /**
47      * original HttpServletRequest.
48      */

49     private HttpServletRequest JavaDoc request;
50
51     /**
52      * original HttpServletResponse.
53      */

54     private HttpServletResponse JavaDoc response;
55
56     /**
57      * Construct a new RequestHelper for the given request.
58      * @param servletRequest HttpServletRequest needed to generate the base href
59      * @param servletResponse HttpServletResponse needed to encode generated urls
60      */

61     public DefaultRequestHelper(HttpServletRequest JavaDoc servletRequest, HttpServletResponse JavaDoc servletResponse)
62     {
63         this.request = servletRequest;
64         this.response = servletResponse;
65     }
66
67     /**
68      * @see org.displaytag.util.RequestHelper#getHref()
69      */

70     public Href getHref()
71     {
72         String JavaDoc requestURI = this.request.getRequestURI();
73         // call encodeURL to preserve session id when cookies are disabled
74
Href href = new DefaultHref(this.response.encodeURL(requestURI));
75         href.setParameterMap(getParameterMap());
76         return href;
77     }
78
79     /**
80      * @see org.displaytag.util.RequestHelper#getParameter(java.lang.String)
81      */

82     public String JavaDoc getParameter(String JavaDoc key)
83     {
84         // actually simply return the parameter, this behaviour could be changed
85
return this.request.getParameter(key);
86     }
87
88     /**
89      * @see org.displaytag.util.RequestHelper#getIntParameter(java.lang.String)
90      */

91     public Integer JavaDoc getIntParameter(String JavaDoc key)
92     {
93         String JavaDoc value = this.request.getParameter(key);
94
95         if (value != null)
96         {
97             try
98             {
99                 return new Integer JavaDoc(value);
100             }
101             catch (NumberFormatException JavaDoc e)
102             {
103                 // It's ok to ignore, simply return null
104
log.debug(Messages.getString("RequestHelper.invalidparameter", //$NON-NLS-1$
105
new Object JavaDoc[]{key, value}));
106             }
107         }
108
109         return null;
110     }
111
112     /**
113      * @see org.displaytag.util.RequestHelper#getParameterMap()
114      */

115     public Map JavaDoc getParameterMap()
116     {
117
118         Map JavaDoc map = new HashMap JavaDoc();
119
120         // get the parameters names
121
Enumeration JavaDoc parametersName = this.request.getParameterNames();
122
123         while (parametersName.hasMoreElements())
124         {
125             // ... get the value
126
String JavaDoc paramName = (String JavaDoc) parametersName.nextElement();
127
128             // put key/value in the map
129
String JavaDoc[] originalValues = (String JavaDoc[]) ObjectUtils.defaultIfNull(
130                 this.request.getParameterValues(paramName),
131                 new String JavaDoc[0]);
132             String JavaDoc[] values = new String JavaDoc[originalValues.length];
133
134             for (int i = 0; i < values.length; i++)
135             {
136                 try
137                 {
138                     values[i] = URLEncoder.encode(StringUtils.defaultString(originalValues[i]), StringUtils
139                         .defaultString(response.getCharacterEncoding(), "UTF8")); //$NON-NLS-1$
140
}
141                 catch (UnsupportedEncodingException JavaDoc e)
142                 {
143                     throw new UnhandledException(e);
144                 }
145             }
146             map.put(paramName, values);
147
148         }
149
150         // return the Map
151
return map;
152     }
153
154 }
Popular Tags