KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > xsp > XSPRequestHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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 package org.apache.cocoon.components.language.markup.xsp;
17
18 import org.apache.avalon.framework.CascadingRuntimeException;
19 import org.apache.cocoon.Constants;
20 import org.apache.cocoon.environment.ObjectModelHelper;
21 import org.apache.cocoon.environment.Request;
22 import org.apache.cocoon.environment.Session;
23
24 import org.xml.sax.ContentHandler JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.helpers.AttributesImpl JavaDoc;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * The <code>Request</code> object helper
36  *
37  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
38  * @version CVS $Id: XSPRequestHelper.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

40 public class XSPRequestHelper {
41     /**
42      * Assign values to the object's namespace uri and prefix
43      */

44     private static final String JavaDoc URI = Constants.XSP_REQUEST_URI;
45     private static final String JavaDoc PREFIX = Constants.XSP_REQUEST_PREFIX;
46
47     private static void getLocale(Locale JavaDoc locale, ContentHandler JavaDoc handler)
48         throws SAXException JavaDoc
49     {
50         XSPObjectHelper.start(URI, PREFIX, handler, "locale");
51
52         XSPObjectHelper.elementData(URI, PREFIX, handler, "language",
53             locale.getLanguage());
54         XSPObjectHelper.elementData(URI, PREFIX, handler, "country",
55             locale.getCountry());
56         XSPObjectHelper.elementData(URI, PREFIX, handler, "variant",
57             locale.getVariant());
58
59         XSPObjectHelper.end(URI, PREFIX, handler, "locale");
60     }
61
62     public static void getLocale(Map JavaDoc objectModel, ContentHandler JavaDoc handler)
63         throws SAXException JavaDoc {
64         Request request = ObjectModelHelper.getRequest(objectModel);
65         getLocale(request.getLocale(), handler);
66     }
67
68     /**
69      * Return the request locales as array
70      * @return Array containing request locales.
71      */

72     public static Locale JavaDoc[] getLocales(Map JavaDoc objectModel) {
73         Request request = ObjectModelHelper.getRequest(objectModel);
74
75         ArrayList JavaDoc a = new ArrayList JavaDoc(10);
76         Enumeration JavaDoc e = request.getLocales();
77         while (e.hasMoreElements()) {
78             a.add(e.nextElement());
79         }
80
81         return (Locale JavaDoc[])a.toArray(new Locale JavaDoc[a.size()]);
82     }
83
84     /**
85      * Output request locales
86      * @param objectModel The Map objectModel
87      * @param contentHandler The SAX content handler
88      * @exception SAXException If a SAX error occurs
89      */

90     public static void getLocales(Map JavaDoc objectModel, ContentHandler JavaDoc contentHandler)
91         throws SAXException JavaDoc {
92         Request request = ObjectModelHelper.getRequest(objectModel);
93         XSPObjectHelper.start(URI, PREFIX, contentHandler, "locales");
94         Enumeration JavaDoc e = request.getLocales();
95         while (e.hasMoreElements()) {
96             getLocale((Locale JavaDoc)e.nextElement(), contentHandler);
97         }
98         XSPObjectHelper.end(URI, PREFIX, contentHandler, "locales");
99     }
100
101     /**
102      * Return the given request parameter value or a user-provided default if
103      * none was specified.
104      *
105      * @param objectModel The Map objectModel
106      * @param name The parameter name
107      * @param defaultValue Value to substitute in absence of a parameter value
108      */

109     public static String JavaDoc getParameter(Map JavaDoc objectModel, String JavaDoc name,
110                                       String JavaDoc defaultValue) {
111         return getParameter(objectModel, name, defaultValue, null, null);
112     }
113
114     /**
115      * Return the given request parameter value or a user-provided default if
116      * none was specified.
117      *
118      * @param objectModel The Map objectModel
119      * @param name The parameter name
120      * @param defaultValue Value to substitute in absence of a parameter value
121      * @param form_encoding The supposed encoding of the request parameter.
122      * @param container_encoding The encoding used by container.
123      */

124     public static String JavaDoc getParameter(Map JavaDoc objectModel, String JavaDoc name,
125                                       String JavaDoc defaultValue, String JavaDoc form_encoding,
126                                       String JavaDoc container_encoding) {
127         if(container_encoding == null)
128             container_encoding = "ISO-8859-1"; // default per Servlet spec
129

130         Request request = ObjectModelHelper.getRequest(objectModel);
131         String JavaDoc value = request.getParameter(name);
132         if(form_encoding != null && value != null && value.length() > 0) {
133             try {
134                 value = new String JavaDoc(value.getBytes(container_encoding), form_encoding);
135             } catch(java.io.UnsupportedEncodingException JavaDoc uee) {
136                 throw new CascadingRuntimeException("Unsupported Encoding Exception", uee);
137            }
138         }
139
140         if (value == null) {
141             value = defaultValue;
142         }
143
144         return value;
145     }
146
147     /**
148      * Output the given request parameter value or a user-provided default if
149      * none was specified.
150      *
151      * @param objectModel The Map objectModel
152      * @param contentHandler The SAX content handler
153      * @param name The parameter name
154      * @param defaultValue Value to substitute in absence of a parameter value
155      * @exception SAXException If a SAX error occurs
156      */

157     public static void getParameter(Map JavaDoc objectModel, ContentHandler JavaDoc contentHandler,
158                                     String JavaDoc name, String JavaDoc defaultValue)
159         throws SAXException JavaDoc {
160         AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
161         XSPObjectHelper.addAttribute(attr, "name", name);
162
163         XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "parameter",
164             getParameter(objectModel, name, defaultValue, null, null), attr);
165     }
166
167     /**
168      * Output the given request parameter value or a user-provided default if
169      * none was specified.
170      *
171      * @param objectModel The Map objectModel
172      * @param contentHandler The SAX content handler
173      * @param name The parameter name
174      * @param defaultValue Value to substitute in absence of a parameter value
175      * @param form_encoding The supposed encoding of the request parameter.
176      * @param container_encoding The encoding used by container.
177      * @exception SAXException If a SAX error occurs
178      */

179     public static void getParameter(Map JavaDoc objectModel, ContentHandler JavaDoc contentHandler,
180                                     String JavaDoc name, String JavaDoc defaultValue,
181                                     String JavaDoc form_encoding,
182                                     String JavaDoc container_encoding)
183         throws SAXException JavaDoc {
184         AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
185         XSPObjectHelper.addAttribute(attr, "name", name);
186         XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "parameter",
187             String.valueOf(getParameter(objectModel, name, defaultValue,
188                 form_encoding, container_encoding)), attr);
189     }
190
191     /**
192      * Output the request parameter values for a given name
193      *
194      * @param objectModel The Map objectModel
195      * @param contentHandler The SAX content handler
196      * @exception SAXException If a SAX error occurs
197      */

198     public static void getParameterValues(Map JavaDoc objectModel,
199                                           ContentHandler JavaDoc contentHandler,
200                                           String JavaDoc name)
201         throws SAXException JavaDoc {
202         Request request = ObjectModelHelper.getRequest(objectModel);
203         AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
204         XSPObjectHelper.addAttribute(attr, "name", name);
205
206         XSPObjectHelper.start(URI, PREFIX, contentHandler,
207             "parameter-values", attr);
208
209         String JavaDoc[] values = request.getParameterValues(name);
210
211         if (values != null) {
212             for (int i = 0; i < values.length; i++) {
213                 XSPObjectHelper.elementData(URI, PREFIX, contentHandler,
214                     "value", values[i]);
215             }
216         }
217
218         XSPObjectHelper.end(URI, PREFIX, contentHandler, "parameter-values");
219     }
220
221     /**
222      * Output the request parameter values for a given name
223      *
224      * @param objectModel The Map objectModel
225      * @param contentHandler The SAX content handler
226      * @param form_encoding The supposed encoding of the request parameter.
227      * @param container_encoding The encoding used by container.
228      * @exception SAXException If a SAX error occurs
229      */

230     public static void getParameterValues(Map JavaDoc objectModel,
231                                           ContentHandler JavaDoc contentHandler,
232                                           String JavaDoc name, String JavaDoc form_encoding,
233                                           String JavaDoc container_encoding)
234         throws SAXException JavaDoc {
235         Request request = ObjectModelHelper.getRequest(objectModel);
236
237         AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
238         XSPObjectHelper.addAttribute(attr, "name", name);
239         XSPObjectHelper.start(URI, PREFIX, contentHandler,
240             "parameter-values", attr);
241
242         String JavaDoc[] values = request.getParameterValues(name);
243         if (values != null) {
244             for (int i = 0; i < values.length; i++) {
245                 if(form_encoding != null && values[i] != null &&
246                     values[i].length() > 0) {
247                     try {
248                         values[i] = new String JavaDoc(values[i].getBytes(container_encoding),
249                             form_encoding);
250                     } catch(java.io.UnsupportedEncodingException JavaDoc uee) {
251                         throw new CascadingRuntimeException("Unsupported Encoding Exception", uee);
252                     }
253                 }
254                 XSPObjectHelper.elementData(URI, PREFIX, contentHandler,
255                     "value", values[i]);
256             }
257         }
258
259         XSPObjectHelper.end(URI, PREFIX, contentHandler, "parameter-values");
260     }
261
262     /**
263      * return the request parameter values for a given name as array
264      *
265      * @param objectModel The Map objectModel
266      * @param form_encoding The supposed encoding of the request parameter.
267      * @param container_encoding The encoding used by container.
268      * @return Array containing requested values.
269      */

270     public static String JavaDoc[] getParameterValues(Map JavaDoc objectModel, String JavaDoc name,
271                                               String JavaDoc form_encoding,
272                                               String JavaDoc container_encoding) {
273         Request request = ObjectModelHelper.getRequest(objectModel);
274
275         String JavaDoc[] values = request.getParameterValues(name);
276
277         if (values != null) {
278             for (int i = 0; i < values.length; i++) {
279                 if(form_encoding != null && values[i] != null &&
280                     values[i].length() > 0) {
281                     try {
282                         values[i] = new String JavaDoc(values[i].getBytes(container_encoding),
283                             form_encoding);
284                     } catch(java.io.UnsupportedEncodingException JavaDoc uee) {
285                         throw new CascadingRuntimeException("Unsupported Encoding Exception", uee);
286                     }
287                 }
288             }
289         }
290         return values;
291     }
292
293     /**
294      * return the request parameter names as array
295      *
296      * @return Array containing parameter names.
297      */

298     public static String JavaDoc[] getParameterNames(Map JavaDoc objectModel) {
299         Request request = ObjectModelHelper.getRequest(objectModel);
300
301         ArrayList JavaDoc a = new ArrayList JavaDoc(10);
302         Enumeration JavaDoc e = request.getParameterNames();
303         while (e.hasMoreElements()) {
304             a.add(e.nextElement());
305         }
306
307         return (String JavaDoc[])a.toArray(new String JavaDoc[a.size()]);
308     }
309
310     /**
311      * Output parameter names for a given request
312      *
313      * @param objectModel The Map objectModel
314      * @param contentHandler The SAX content handler
315      * @exception SAXException If a SAX error occurs
316      */

317     public static void getParameterNames(Map JavaDoc objectModel,
318                                          ContentHandler JavaDoc contentHandler)
319         throws SAXException JavaDoc {
320         Request request = ObjectModelHelper.getRequest(objectModel);
321         XSPObjectHelper.start(URI, PREFIX, contentHandler, "parameter-names");
322
323         Enumeration JavaDoc e = request.getParameterNames();
324         while (e.hasMoreElements()) {
325             String JavaDoc name = (String JavaDoc) e.nextElement();
326             XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "name", name);
327         }
328
329         XSPObjectHelper.end(URI, PREFIX, contentHandler, "parameter-names");
330     }
331
332     /**
333      * Output the header names for a given request
334      *
335      * @param objectModel The Map objectModel
336      * @param contentHandler The SAX content handler
337      * @exception SAXException If a SAX error occurs
338      */

339     public static void getHeaderNames(Map JavaDoc objectModel,
340                                       ContentHandler JavaDoc contentHandler)
341         throws SAXException JavaDoc {
342         Request request = ObjectModelHelper.getRequest(objectModel);
343         XSPObjectHelper.start(URI, PREFIX, contentHandler, "header-names");
344
345         Enumeration JavaDoc e = request.getHeaderNames();
346         while (e.hasMoreElements()) {
347             String JavaDoc name = (String JavaDoc) e.nextElement();
348             XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "name", name);
349         }
350
351         XSPObjectHelper.end(URI, PREFIX, contentHandler, "header-names");
352     }
353
354     /**
355      * Returns the header names for a given request
356      *
357      * @param objectModel The Map objectModel
358      */

359     public static String JavaDoc[] getHeaderNames(Map JavaDoc objectModel) {
360         Request request = ObjectModelHelper.getRequest(objectModel);
361
362         ArrayList JavaDoc a = new ArrayList JavaDoc(10);
363         Enumeration JavaDoc e = request.getHeaderNames();
364         while (e.hasMoreElements()) {
365             a.add(e.nextElement());
366         }
367
368         return (String JavaDoc[])a.toArray(new String JavaDoc[a.size()]);
369     }
370
371     public static String JavaDoc[] getHeaders(Map JavaDoc objectModel, String JavaDoc name) {
372         Request request = ObjectModelHelper.getRequest(objectModel);
373
374         ArrayList JavaDoc a = new ArrayList JavaDoc(10);
375         Enumeration JavaDoc e = request.getHeaders(name);
376         while (e.hasMoreElements()) {
377             a.add(e.nextElement());
378         }
379
380         return (String JavaDoc[])a.toArray(new String JavaDoc[a.size()]);
381     }
382
383     public static void getHeaders(Map JavaDoc objectModel, String JavaDoc name,
384                                   ContentHandler JavaDoc contentHandler)
385         throws SAXException JavaDoc {
386         Request request = ObjectModelHelper.getRequest(objectModel);
387
388         AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
389         XSPObjectHelper.addAttribute(attr, "name", name);
390         XSPObjectHelper.start(URI, PREFIX, contentHandler, "header-values");
391
392         Enumeration JavaDoc e = request.getHeaders(name);
393         while (e.hasMoreElements()) {
394             String JavaDoc value = (String JavaDoc) e.nextElement();
395             XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "value", value);
396         }
397
398         XSPObjectHelper.end(URI, PREFIX, contentHandler, "header-values");
399     }
400
401     public static Date JavaDoc getDateHeader(Map JavaDoc objectModel, String JavaDoc name) {
402         Request request = ObjectModelHelper.getRequest(objectModel);
403
404         if (name == null || name.length() == 0) {
405             return null;
406         }
407         long dateHeader = request.getDateHeader(name);
408         if (dateHeader == -1) {
409             return null;
410         }
411         return new Date JavaDoc(dateHeader);
412     }
413
414     public static String JavaDoc getDateHeader(Map JavaDoc objectModel, String JavaDoc name, String JavaDoc format) {
415         Request request = ObjectModelHelper.getRequest(objectModel);
416
417         if (name == null || name.length() == 0) {
418             return null;
419         }
420         long dateHeader = request.getDateHeader(name);
421         if (dateHeader == -1) {
422             return null;
423         }
424         if (format != null) format = format.trim();
425         return XSPUtil.formatDate(new Date JavaDoc(dateHeader), format);
426     }
427
428     /**
429      * Output attribute names for a given request
430      *
431      * @param objectModel The Map objectModel
432      * @param contentHandler The SAX content handler
433      * @exception SAXException If a SAX error occurs
434      */

435     public static void getAttributeNames(Map JavaDoc objectModel,
436                                          ContentHandler JavaDoc contentHandler)
437         throws SAXException JavaDoc {
438         Request request = ObjectModelHelper.getRequest(objectModel);
439         XSPObjectHelper.start(URI, PREFIX, contentHandler, "attribute-names");
440
441         Enumeration JavaDoc e = request.getAttributeNames();
442         while (e.hasMoreElements()) {
443             String JavaDoc name = (String JavaDoc) e.nextElement();
444             XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "name", name);
445         }
446
447         XSPObjectHelper.end(URI, PREFIX, contentHandler, "attribute-names");
448     }
449
450     /**
451      * Returns the attribute names
452      *
453      * @param objectModel The Map objectModel
454      */

455     public static String JavaDoc[] getAttributeNames(Map JavaDoc objectModel)
456     {
457         Request request = ObjectModelHelper.getRequest(objectModel);
458
459         ArrayList JavaDoc a = new ArrayList JavaDoc(10);
460         Enumeration JavaDoc e = request.getAttributeNames();
461         while (e.hasMoreElements()) {
462             a.add(e.nextElement());
463         }
464
465         return (String JavaDoc[])a.toArray(new String JavaDoc[a.size()]);
466     }
467
468     public static String JavaDoc getRequestedURL(Map JavaDoc objectModel) {
469         Request request = ObjectModelHelper.getRequest(objectModel);
470         StringBuffer JavaDoc uribuf = null;
471         boolean isSecure = request.isSecure();
472         int port = request.getServerPort();
473
474         if (isSecure) {
475             uribuf = new StringBuffer JavaDoc("https://");
476         } else {
477             uribuf = new StringBuffer JavaDoc("http://");
478         }
479
480         uribuf.append(request.getServerName());
481         if (isSecure) {
482             if (port != 443) {
483                 uribuf.append(":").append(port);
484             }
485         } else {
486             if (port != 80) {
487                 uribuf.append(":").append(port);
488             }
489         }
490
491         uribuf.append(request.getRequestURI());
492         return uribuf.toString();
493     }
494
495     /**
496      * Return the given session attribute value or a user-provided default if
497      * none was specified.
498      *
499      * @param objectModel The Map objectModel
500      * @param name The attribute name
501      * @param defaultValue Value to substitute in absence of a attribute value
502      */

503     public static Object JavaDoc getSessionAttribute(Map JavaDoc objectModel, String JavaDoc name,
504                                              Object JavaDoc defaultValue ) {
505
506         Session session = ObjectModelHelper.getRequest(objectModel).getSession();
507         Object JavaDoc obj = session.getAttribute(name);
508         return (obj != null ? obj : defaultValue );
509     }
510
511     /**
512      * Output the given session attribute value or a user-provided default if
513      * none was specified.
514      *
515      * @param objectModel The Map objectModel
516      * @param contentHandler The SAX content handler
517      * @param name The attribute name
518      * @param defaultValue Value to substitute in absence of an attribute value
519      * @exception SAXException If a SAX error occurs
520      */

521     public static void getSessionAttribute(Map JavaDoc objectModel, ContentHandler JavaDoc contentHandler,
522                                            String JavaDoc name, Object JavaDoc defaultValue)
523         throws SAXException JavaDoc {
524         AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
525         XSPObjectHelper.addAttribute(attr, "name", name);
526
527         XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "parameter",
528             getSessionAttribute(objectModel, name, defaultValue).toString(), attr);
529     }
530 }
531
Popular Tags