001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.language.markup.xsp;
018:
019: import org.apache.avalon.framework.CascadingRuntimeException;
020: import org.apache.cocoon.Constants;
021: import org.apache.cocoon.environment.ObjectModelHelper;
022: import org.apache.cocoon.environment.Request;
023: import org.apache.cocoon.environment.Session;
024:
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.SAXException;
027: import org.xml.sax.helpers.AttributesImpl;
028:
029: import java.util.ArrayList;
030: import java.util.Date;
031: import java.util.Enumeration;
032: import java.util.Locale;
033: import java.util.Map;
034:
035: /**
036: * The <code>Request</code> object helper
037: *
038: * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
039: * @version CVS $Id: XSPRequestHelper.java 433543 2006-08-22 06:22:54Z crossley $
040: */
041: public class XSPRequestHelper {
042: /**
043: * Assign values to the object's namespace uri and prefix
044: */
045: private static final String URI = Constants.XSP_REQUEST_URI;
046: private static final String PREFIX = Constants.XSP_REQUEST_PREFIX;
047:
048: private static void getLocale(Locale locale, ContentHandler handler)
049: throws SAXException {
050: XSPObjectHelper.start(URI, PREFIX, handler, "locale");
051:
052: XSPObjectHelper.elementData(URI, PREFIX, handler, "language",
053: locale.getLanguage());
054: XSPObjectHelper.elementData(URI, PREFIX, handler, "country",
055: locale.getCountry());
056: XSPObjectHelper.elementData(URI, PREFIX, handler, "variant",
057: locale.getVariant());
058:
059: XSPObjectHelper.end(URI, PREFIX, handler, "locale");
060: }
061:
062: public static void getLocale(Map objectModel, ContentHandler handler)
063: throws SAXException {
064: Request request = ObjectModelHelper.getRequest(objectModel);
065: getLocale(request.getLocale(), handler);
066: }
067:
068: /**
069: * Return the request locales as array
070: * @return Array containing request locales.
071: */
072: public static Locale[] getLocales(Map objectModel) {
073: Request request = ObjectModelHelper.getRequest(objectModel);
074:
075: ArrayList a = new ArrayList(10);
076: Enumeration e = request.getLocales();
077: while (e.hasMoreElements()) {
078: a.add(e.nextElement());
079: }
080:
081: return (Locale[]) a.toArray(new Locale[a.size()]);
082: }
083:
084: /**
085: * Output request locales
086: * @param objectModel The Map objectModel
087: * @param contentHandler The SAX content handler
088: * @exception SAXException If a SAX error occurs
089: */
090: public static void getLocales(Map objectModel,
091: ContentHandler contentHandler) throws SAXException {
092: Request request = ObjectModelHelper.getRequest(objectModel);
093: XSPObjectHelper.start(URI, PREFIX, contentHandler, "locales");
094: Enumeration e = request.getLocales();
095: while (e.hasMoreElements()) {
096: getLocale((Locale) e.nextElement(), contentHandler);
097: }
098: XSPObjectHelper.end(URI, PREFIX, contentHandler, "locales");
099: }
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 getParameter(Map objectModel, String name,
110: String 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 getParameter(Map objectModel, String name,
125: String defaultValue, String form_encoding,
126: String 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 value = request.getParameter(name);
132: if (form_encoding != null && value != null
133: && value.length() > 0) {
134: try {
135: value = new String(value.getBytes(container_encoding),
136: form_encoding);
137: } catch (java.io.UnsupportedEncodingException uee) {
138: throw new CascadingRuntimeException(
139: "Unsupported Encoding Exception", uee);
140: }
141: }
142:
143: if (value == null) {
144: value = defaultValue;
145: }
146:
147: return value;
148: }
149:
150: /**
151: * Output the given request parameter value or a user-provided default if
152: * none was specified.
153: *
154: * @param objectModel The Map objectModel
155: * @param contentHandler The SAX content handler
156: * @param name The parameter name
157: * @param defaultValue Value to substitute in absence of a parameter value
158: * @exception SAXException If a SAX error occurs
159: */
160: public static void getParameter(Map objectModel,
161: ContentHandler contentHandler, String name,
162: String defaultValue) throws SAXException {
163: AttributesImpl attr = new AttributesImpl();
164: XSPObjectHelper.addAttribute(attr, "name", name);
165:
166: XSPObjectHelper.elementData(URI, PREFIX, contentHandler,
167: "parameter", getParameter(objectModel, name,
168: defaultValue, null, null), attr);
169: }
170:
171: /**
172: * Output the given request parameter value or a user-provided default if
173: * none was specified.
174: *
175: * @param objectModel The Map objectModel
176: * @param contentHandler The SAX content handler
177: * @param name The parameter name
178: * @param defaultValue Value to substitute in absence of a parameter value
179: * @param form_encoding The supposed encoding of the request parameter.
180: * @param container_encoding The encoding used by container.
181: * @exception SAXException If a SAX error occurs
182: */
183: public static void getParameter(Map objectModel,
184: ContentHandler contentHandler, String name,
185: String defaultValue, String form_encoding,
186: String container_encoding) throws SAXException {
187: AttributesImpl attr = new AttributesImpl();
188: XSPObjectHelper.addAttribute(attr, "name", name);
189: XSPObjectHelper.elementData(URI, PREFIX, contentHandler,
190: "parameter", String.valueOf(getParameter(objectModel,
191: name, defaultValue, form_encoding,
192: container_encoding)), attr);
193: }
194:
195: /**
196: * Output the request parameter values for a given name
197: *
198: * @param objectModel The Map objectModel
199: * @param contentHandler The SAX content handler
200: * @exception SAXException If a SAX error occurs
201: */
202: public static void getParameterValues(Map objectModel,
203: ContentHandler contentHandler, String name)
204: throws SAXException {
205: Request request = ObjectModelHelper.getRequest(objectModel);
206: AttributesImpl attr = new AttributesImpl();
207: XSPObjectHelper.addAttribute(attr, "name", name);
208:
209: XSPObjectHelper.start(URI, PREFIX, contentHandler,
210: "parameter-values", attr);
211:
212: String[] values = request.getParameterValues(name);
213:
214: if (values != null) {
215: for (int i = 0; i < values.length; i++) {
216: XSPObjectHelper.elementData(URI, PREFIX,
217: contentHandler, "value", values[i]);
218: }
219: }
220:
221: XSPObjectHelper.end(URI, PREFIX, contentHandler,
222: "parameter-values");
223: }
224:
225: /**
226: * Output the request parameter values for a given name
227: *
228: * @param objectModel The Map objectModel
229: * @param contentHandler The SAX content handler
230: * @param form_encoding The supposed encoding of the request parameter.
231: * @param container_encoding The encoding used by container.
232: * @exception SAXException If a SAX error occurs
233: */
234: public static void getParameterValues(Map objectModel,
235: ContentHandler contentHandler, String name,
236: String form_encoding, String container_encoding)
237: throws SAXException {
238: Request request = ObjectModelHelper.getRequest(objectModel);
239:
240: AttributesImpl attr = new AttributesImpl();
241: XSPObjectHelper.addAttribute(attr, "name", name);
242: XSPObjectHelper.start(URI, PREFIX, contentHandler,
243: "parameter-values", attr);
244:
245: String[] values = request.getParameterValues(name);
246: if (values != null) {
247: for (int i = 0; i < values.length; i++) {
248: if (form_encoding != null && values[i] != null
249: && values[i].length() > 0) {
250: try {
251: values[i] = new String(values[i]
252: .getBytes(container_encoding),
253: form_encoding);
254: } catch (java.io.UnsupportedEncodingException uee) {
255: throw new CascadingRuntimeException(
256: "Unsupported Encoding Exception", uee);
257: }
258: }
259: XSPObjectHelper.elementData(URI, PREFIX,
260: contentHandler, "value", values[i]);
261: }
262: }
263:
264: XSPObjectHelper.end(URI, PREFIX, contentHandler,
265: "parameter-values");
266: }
267:
268: /**
269: * return the request parameter values for a given name as array
270: *
271: * @param objectModel The Map objectModel
272: * @param form_encoding The supposed encoding of the request parameter.
273: * @param container_encoding The encoding used by container.
274: * @return Array containing requested values.
275: */
276: public static String[] getParameterValues(Map objectModel,
277: String name, String form_encoding, String container_encoding) {
278: Request request = ObjectModelHelper.getRequest(objectModel);
279:
280: String[] values = request.getParameterValues(name);
281:
282: if (values != null) {
283: for (int i = 0; i < values.length; i++) {
284: if (form_encoding != null && values[i] != null
285: && values[i].length() > 0) {
286: try {
287: values[i] = new String(values[i]
288: .getBytes(container_encoding),
289: form_encoding);
290: } catch (java.io.UnsupportedEncodingException uee) {
291: throw new CascadingRuntimeException(
292: "Unsupported Encoding Exception", uee);
293: }
294: }
295: }
296: }
297: return values;
298: }
299:
300: /**
301: * return the request parameter names as array
302: *
303: * @return Array containing parameter names.
304: */
305: public static String[] getParameterNames(Map objectModel) {
306: Request request = ObjectModelHelper.getRequest(objectModel);
307:
308: ArrayList a = new ArrayList(10);
309: Enumeration e = request.getParameterNames();
310: while (e.hasMoreElements()) {
311: a.add(e.nextElement());
312: }
313:
314: return (String[]) a.toArray(new String[a.size()]);
315: }
316:
317: /**
318: * Output parameter names for a given request
319: *
320: * @param objectModel The Map objectModel
321: * @param contentHandler The SAX content handler
322: * @exception SAXException If a SAX error occurs
323: */
324: public static void getParameterNames(Map objectModel,
325: ContentHandler contentHandler) throws SAXException {
326: Request request = ObjectModelHelper.getRequest(objectModel);
327: XSPObjectHelper.start(URI, PREFIX, contentHandler,
328: "parameter-names");
329:
330: Enumeration e = request.getParameterNames();
331: while (e.hasMoreElements()) {
332: String name = (String) e.nextElement();
333: XSPObjectHelper.elementData(URI, PREFIX, contentHandler,
334: "name", name);
335: }
336:
337: XSPObjectHelper.end(URI, PREFIX, contentHandler,
338: "parameter-names");
339: }
340:
341: /**
342: * Output the header names for a given request
343: *
344: * @param objectModel The Map objectModel
345: * @param contentHandler The SAX content handler
346: * @exception SAXException If a SAX error occurs
347: */
348: public static void getHeaderNames(Map objectModel,
349: ContentHandler contentHandler) throws SAXException {
350: Request request = ObjectModelHelper.getRequest(objectModel);
351: XSPObjectHelper.start(URI, PREFIX, contentHandler,
352: "header-names");
353:
354: Enumeration e = request.getHeaderNames();
355: while (e.hasMoreElements()) {
356: String name = (String) e.nextElement();
357: XSPObjectHelper.elementData(URI, PREFIX, contentHandler,
358: "name", name);
359: }
360:
361: XSPObjectHelper
362: .end(URI, PREFIX, contentHandler, "header-names");
363: }
364:
365: /**
366: * Returns the header names for a given request
367: *
368: * @param objectModel The Map objectModel
369: */
370: public static String[] getHeaderNames(Map objectModel) {
371: Request request = ObjectModelHelper.getRequest(objectModel);
372:
373: ArrayList a = new ArrayList(10);
374: Enumeration e = request.getHeaderNames();
375: while (e.hasMoreElements()) {
376: a.add(e.nextElement());
377: }
378:
379: return (String[]) a.toArray(new String[a.size()]);
380: }
381:
382: public static String[] getHeaders(Map objectModel, String name) {
383: Request request = ObjectModelHelper.getRequest(objectModel);
384:
385: ArrayList a = new ArrayList(10);
386: Enumeration e = request.getHeaders(name);
387: while (e.hasMoreElements()) {
388: a.add(e.nextElement());
389: }
390:
391: return (String[]) a.toArray(new String[a.size()]);
392: }
393:
394: public static void getHeaders(Map objectModel, String name,
395: ContentHandler contentHandler) throws SAXException {
396: Request request = ObjectModelHelper.getRequest(objectModel);
397:
398: AttributesImpl attr = new AttributesImpl();
399: XSPObjectHelper.addAttribute(attr, "name", name);
400: XSPObjectHelper.start(URI, PREFIX, contentHandler,
401: "header-values");
402:
403: Enumeration e = request.getHeaders(name);
404: while (e.hasMoreElements()) {
405: String value = (String) e.nextElement();
406: XSPObjectHelper.elementData(URI, PREFIX, contentHandler,
407: "value", value);
408: }
409:
410: XSPObjectHelper.end(URI, PREFIX, contentHandler,
411: "header-values");
412: }
413:
414: public static Date getDateHeader(Map objectModel, String name) {
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: return new Date(dateHeader);
425: }
426:
427: public static String getDateHeader(Map objectModel, String name,
428: String format) {
429: Request request = ObjectModelHelper.getRequest(objectModel);
430:
431: if (name == null || name.length() == 0) {
432: return null;
433: }
434: long dateHeader = request.getDateHeader(name);
435: if (dateHeader == -1) {
436: return null;
437: }
438: if (format != null)
439: format = format.trim();
440: return XSPUtil.formatDate(new Date(dateHeader), format);
441: }
442:
443: /**
444: * Output attribute names for a given request
445: *
446: * @param objectModel The Map objectModel
447: * @param contentHandler The SAX content handler
448: * @exception SAXException If a SAX error occurs
449: */
450: public static void getAttributeNames(Map objectModel,
451: ContentHandler contentHandler) throws SAXException {
452: Request request = ObjectModelHelper.getRequest(objectModel);
453: XSPObjectHelper.start(URI, PREFIX, contentHandler,
454: "attribute-names");
455:
456: Enumeration e = request.getAttributeNames();
457: while (e.hasMoreElements()) {
458: String name = (String) e.nextElement();
459: XSPObjectHelper.elementData(URI, PREFIX, contentHandler,
460: "name", name);
461: }
462:
463: XSPObjectHelper.end(URI, PREFIX, contentHandler,
464: "attribute-names");
465: }
466:
467: /**
468: * Returns the attribute names
469: *
470: * @param objectModel The Map objectModel
471: */
472: public static String[] getAttributeNames(Map objectModel) {
473: Request request = ObjectModelHelper.getRequest(objectModel);
474:
475: ArrayList a = new ArrayList(10);
476: Enumeration e = request.getAttributeNames();
477: while (e.hasMoreElements()) {
478: a.add(e.nextElement());
479: }
480:
481: return (String[]) a.toArray(new String[a.size()]);
482: }
483:
484: public static String getRequestedURL(Map objectModel) {
485: Request request = ObjectModelHelper.getRequest(objectModel);
486: StringBuffer uribuf = null;
487: boolean isSecure = request.isSecure();
488: int port = request.getServerPort();
489:
490: if (isSecure) {
491: uribuf = new StringBuffer("https://");
492: } else {
493: uribuf = new StringBuffer("http://");
494: }
495:
496: uribuf.append(request.getServerName());
497: if (isSecure) {
498: if (port != 443) {
499: uribuf.append(":").append(port);
500: }
501: } else {
502: if (port != 80) {
503: uribuf.append(":").append(port);
504: }
505: }
506:
507: uribuf.append(request.getRequestURI());
508: return uribuf.toString();
509: }
510:
511: /**
512: * Return the given session attribute value or a user-provided default if
513: * none was specified.
514: *
515: * @param objectModel The Map objectModel
516: * @param name The attribute name
517: * @param defaultValue Value to substitute in absence of a attribute value
518: */
519: public static Object getSessionAttribute(Map objectModel,
520: String name, Object defaultValue) {
521:
522: Session session = ObjectModelHelper.getRequest(objectModel)
523: .getSession();
524: Object obj = session.getAttribute(name);
525: return (obj != null ? obj : defaultValue);
526: }
527:
528: /**
529: * Output the given session attribute value or a user-provided default if
530: * none was specified.
531: *
532: * @param objectModel The Map objectModel
533: * @param contentHandler The SAX content handler
534: * @param name The attribute name
535: * @param defaultValue Value to substitute in absence of an attribute value
536: * @exception SAXException If a SAX error occurs
537: */
538: public static void getSessionAttribute(Map objectModel,
539: ContentHandler contentHandler, String name,
540: Object defaultValue) throws SAXException {
541: AttributesImpl attr = new AttributesImpl();
542: XSPObjectHelper.addAttribute(attr, "name", name);
543:
544: XSPObjectHelper.elementData(URI, PREFIX, contentHandler,
545: "parameter", getSessionAttribute(objectModel, name,
546: defaultValue).toString(), attr);
547: }
548: }
|