001: /*
002: * Copyright © 2002-2005tagtraum industries
003: */
004: package com.tagtraum.jo;
005:
006: import javax.servlet.http.HttpServletRequestWrapper;
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.RequestDispatcher;
009: import java.util.Enumeration;
010: import java.util.Collection;
011: import java.util.ArrayList;
012: import java.util.Collections;
013: import java.util.Map;
014: import java.util.HashMap;
015:
016: /**
017: *
018: * @author <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
019: * @version 1.1beta1 ($Revision: 1.2 $)
020: */
021: public abstract class RequestWrapper extends HttpServletRequestWrapper {
022:
023: protected ModelMatch modelMatch;
024:
025: public RequestWrapper(HttpServletRequest httpServletRequest) {
026: super (httpServletRequest);
027: }
028:
029: public void setModelMatch(ModelMatch modelMatch) {
030: this .modelMatch = modelMatch;
031: }
032:
033: // TODO: the parameter merging code is inefficient and should be rewritten
034:
035: /**
036: * Returns the first parameter with the specified name.
037: *
038: * @param name parametername
039: * @return value of the parameter or <code>null</code>
040: */
041: public String getParameter(String name) {
042: String value;
043: if (modelMatch != null) {
044: value = modelMatch.getUri().get(name);
045: if (value != null) {
046: return value;
047: }
048: }
049: return super .getParameter(name);
050: }
051:
052: /**
053: * Returns all parametervalues for a parameter name.
054: *
055: * @param name parametername
056: * @return value array
057: */
058: public String[] getParameterValues(String name) {
059: String[] value;
060: if (modelMatch != null) {
061: value = modelMatch.getUri().getValues(name);
062: if (value != null) {
063: String[] super Values = super .getParameterValues(name);
064: if (super Values != null) {
065: String[] allValues = new String[super Values.length
066: + value.length];
067: System.arraycopy(value, 0, allValues, 0,
068: value.length);
069: System.arraycopy(super Values, 0, allValues,
070: value.length, super Values.length);
071: return allValues;
072: }
073: return value;
074: }
075: }
076: return super .getParameterValues(name);
077: }
078:
079: /**
080: * Returns all parameternames.
081: *
082: * @return Enumeration of parameter names
083: */
084: public Enumeration getParameterNames() {
085: if (modelMatch != null) {
086: Collection names = new ArrayList(modelMatch.getUri()
087: .getParameterNamesSet());
088: names.addAll(super .getParameterMap().keySet());
089: return Collections.enumeration(names);
090: }
091: return super .getParameterNames();
092: }
093:
094: public Map getParameterMap() {
095: Map map = new HashMap();
096: for (Enumeration e = getParameterNames(); e.hasMoreElements();) {
097: String name = (String) e.nextElement();
098: map.put(name, getParameterValues(name));
099: }
100: return map;
101: }
102:
103: // NOTE: we try to include resources relative to the current resource and not to the
104: // calling resource. This behaviour is inconsistent with Tomcat 4.x implementation
105: public RequestDispatcher getRequestDispatcher(String uri) {
106: if (uri.startsWith("/"))
107: return super .getRequestDispatcher(uri);
108: String servletPath = (String) getAttribute(C_Jo.C_Request_Attribute_ServletPath);
109: if (servletPath == null)
110: servletPath = getServletPath();
111: int lastSlash = servletPath.lastIndexOf('/');
112: String absoluteURI = null;
113: if (lastSlash != -1) {
114: absoluteURI = servletPath.substring(0, lastSlash + 1) + uri;
115: } else {
116: absoluteURI = servletPath + uri;
117: }
118: return super.getRequestDispatcher(absoluteURI);
119: }
120: }
|