001: package org.apache.struts.wrapper;
002:
003: import java.util.Enumeration;
004: import java.util.Map;
005: import java.util.HashMap;
006: import java.util.StringTokenizer;
007:
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletRequestWrapper;
010:
011: import javax.portlet.PortletRequest;
012:
013: import org.apache.struts.StrutsConstants;
014:
015: public class StrutsServletRequest extends HttpServletRequestWrapper {
016:
017: private static final String INCLUDE_SERVLET_PATH = "javax.servlet.include.servlet_path";
018: private static final String INCLUDE_PATH_INFO = "javax.servlet.include.path_inf";
019:
020: private PortletRequest _pReq;
021: private String _servletPath;
022: private String _pathInfo;
023: private String _queryString = "";
024: private Map _queryParamMap = new HashMap();
025:
026: public StrutsServletRequest(HttpServletRequest sReq,
027: PortletRequest pReq) {
028: super (sReq);
029: pReq.setAttribute("javax.portlet.request", pReq);
030: _pReq = pReq;
031: parsePath();
032: }
033:
034: public String getServletPath() {
035: return _servletPath;
036: }
037:
038: public String getPathInfo() {
039: return _pathInfo;
040: }
041:
042: public String getParameter(String name) {
043: String value = getParamFromQueryString(name);
044: if (value == null) {
045: value = _pReq.getParameter(name);
046: }
047: return value;
048: }
049:
050: public Enumeration getParameterNames() {
051: return _pReq.getParameterNames();
052: }
053:
054: public String[] getParameterValues(String name) {
055: return _pReq.getParameterValues(name);
056: }
057:
058: public Map getParameterMap() {
059: return _pReq.getParameterMap();
060: }
061:
062: public Object getAttribute(String name) {
063: /*
064: Object retValue = null;
065: retValue = super.getAttribute(name);
066: if (retValue == null) {
067: retValue = _pReq.getAttribute(name);
068: }
069: return retValue;
070: */
071: Object retValue = null;
072:
073: if (name.startsWith("javax")
074: && !name.equals("javax.portlet.request")
075: && !name.equals("javax.portlet.response")) {
076: retValue = super .getAttribute(name);
077: if (retValue == null) {
078: retValue = _pReq.getAttribute(name);
079: }
080: } else {
081: retValue = _pReq.getAttribute(name);
082: if (retValue == null) {
083: retValue = super .getAttribute(name);
084: }
085: }
086: return retValue;
087: }
088:
089: public Enumeration getAttributeNames() {
090: return _pReq.getAttributeNames();
091: }
092:
093: public void setAttribute(String name, Object o) {
094: _pReq.setAttribute(name, o);
095: super .setAttribute(name, o);
096: }
097:
098: public void removeAttribute(String name) {
099: _pReq.removeAttribute(name);
100: super .removeAttribute(name);
101: }
102:
103: public String getContextPath() {
104: return _pReq.getContextPath();
105: }
106:
107: // TODO: needs improvement
108: protected void parsePath() {
109: String servletMapping = (String) _pReq
110: .getAttribute(StrutsConstants.SERVLET_MAPPING);
111: String strutsPath = _pReq
112: .getParameter(StrutsConstants.STRUTS_PATH);
113:
114: if (servletMapping.indexOf("*.") >= 0) {
115: // filter out the context path
116: int secondSlash = strutsPath.substring(1).indexOf('/');
117: _servletPath = strutsPath.substring(secondSlash + 1);
118: _pathInfo = null;
119: } else {
120: String matchPath = servletMapping;
121: int starIndex = matchPath.indexOf("/*");
122: if (starIndex >= 0) {
123: matchPath = matchPath.substring(0, starIndex - 1);
124: }
125: int matchPathIndex = strutsPath.indexOf(matchPath);
126: _servletPath = matchPath;
127: _pathInfo = matchPath.substring(matchPathIndex
128: + (matchPath.length()));
129: }
130: super .setAttribute(INCLUDE_SERVLET_PATH, _servletPath);
131: super .setAttribute(INCLUDE_PATH_INFO, _pathInfo);
132: }
133:
134: // TODO: incomplete, need to redo
135: private String getParamFromQueryString(String name) {
136: String queryString = (String) getAttribute("javax.servlet.include.query_string");
137: if (queryString != null) {
138: if (!queryString.equals(_queryString)) {
139: _queryString = queryString;
140: _queryParamMap = new HashMap();
141: StringTokenizer st = new StringTokenizer(queryString,
142: "&", false);
143: while (st.hasMoreTokens()) {
144: String next = st.nextToken();
145: String key = next.substring(0, next.indexOf("="));
146: String value = next.substring(
147: next.indexOf("=") + 1, next.length());
148: _queryParamMap.put(key, value);
149: }
150: }
151: return (String) _queryParamMap.get(name);
152: }
153: return null;
154: }
155:
156: }
|