001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.http.core;
016:
017: import java.io.UnsupportedEncodingException;
018: import java.util.Collections;
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.Locale;
024: import java.util.Map;
025: import javax.servlet.http.HttpServletRequest;
026: import org.apache.commons.collections.iterators.EnumerationIterator;
027: import org.araneaframework.OutputData;
028: import org.araneaframework.Path;
029: import org.araneaframework.core.Assert;
030: import org.araneaframework.core.NoCurrentOutputDataSetException;
031: import org.araneaframework.core.NoSuchNarrowableException;
032: import org.araneaframework.core.util.ExceptionUtil;
033: import org.araneaframework.http.HttpInputData;
034:
035: /**
036: * A ServletInputdata implementation which uses a StandardPath for determining
037: * the scope.
038: *
039: * @author "Toomas Römer" <toomas@webmedia.ee>
040: */
041: public class StandardServletInputData implements HttpInputData {
042: private HttpServletRequest req;
043:
044: private Map extensions = new HashMap();
045:
046: private Map globalData = new HashMap();
047: private Map scopedData = new HashMap();
048: private boolean dataInited;
049:
050: private StringBuffer path;
051: private LinkedList pathPrefixes = new LinkedList();
052:
053: private String servletPath;
054:
055: /**
056: * Constructs a StandardServletInputData from the request.
057: * @param request
058: */
059: public StandardServletInputData(HttpServletRequest request) {
060: Assert.notNullParam(request, "request");
061:
062: servletPath = request.getServletPath();
063:
064: setRequest(request);
065: extend(HttpServletRequest.class, req);
066: }
067:
068: private void setRequest(HttpServletRequest request) {
069: req = request;
070: dataInited = false;
071:
072: path = new StringBuffer(req.getPathInfo() == null ? "" : req
073: .getPathInfo());
074: pathPrefixes = new LinkedList();
075: }
076:
077: private void initData() {
078: globalData.clear();
079: scopedData.clear();
080:
081: Enumeration params = req.getParameterNames();
082:
083: while (params.hasMoreElements()) {
084: String key = (String) params.nextElement();
085:
086: if (key.lastIndexOf(".") == -1) {
087: //global data - no prefix data
088: globalData.put(key, req.getParameter(key));
089: } else {
090: //scoped data
091: String prefix = key.substring(0, key.lastIndexOf("."));
092: String subKey = key.substring(key.lastIndexOf(".") + 1);
093:
094: Map map = (Map) scopedData.get(prefix);
095:
096: if (map == null) {
097: map = new HashMap();
098: scopedData.put(prefix, map);
099: }
100:
101: map.put(subKey, req.getParameter(key));
102: }
103: }
104:
105: dataInited = true;
106: }
107:
108: public Map getGlobalData() {
109: if (!dataInited)
110: initData();
111: return Collections.unmodifiableMap(globalData);
112: }
113:
114: public Map getScopedData(Path scope) {
115: if (!dataInited)
116: initData();
117: Map result = (Map) scopedData.get(scope.toString());
118: if (result != null) {
119: return Collections.unmodifiableMap(result);
120: } else {
121: return Collections.EMPTY_MAP;
122: }
123: }
124:
125: public void extend(Class interfaceClass, Object implementation) {
126: if (HttpServletRequest.class.equals(interfaceClass)
127: && implementation != null)
128: setRequest((HttpServletRequest) implementation);
129:
130: extensions.put(interfaceClass, implementation);
131: }
132:
133: public Object narrow(Class interfaceClass) {
134: Object extension = extensions.get(interfaceClass);
135: if (extension == null)
136: throw new NoSuchNarrowableException(interfaceClass);
137: return extension;
138: }
139:
140: public OutputData getOutputData() {
141: OutputData output = (OutputData) req
142: .getAttribute(OutputData.OUTPUT_DATA_KEY);
143: if (output == null)
144: throw new NoCurrentOutputDataSetException(
145: "No OutputData set in the request.");
146: else
147: return output;
148: }
149:
150: public String getCharacterEncoding() {
151: return req.getCharacterEncoding();
152: }
153:
154: public String getContainerURL() {
155: StringBuffer url = new StringBuffer();
156: url.append(req.getScheme());
157: url.append("://");
158: url.append(req.getServerName());
159: url.append(":");
160: url.append(req.getServerPort());
161: url.append(req.getContextPath());
162: url.append(servletPath);
163: return url.toString();
164: }
165:
166: public String getContainerPath() {
167: return servletPath;
168: }
169:
170: public String getContextURL() {
171: StringBuffer url = new StringBuffer();
172: url.append(req.getScheme());
173: url.append("://");
174: url.append(req.getServerName());
175: url.append(":");
176: url.append(req.getServerPort());
177: url.append(req.getContextPath());
178: return url.toString();
179: }
180:
181: public String getContextPath() {
182: return req.getContextPath();
183: }
184:
185: public String getRequestURL() {
186: return req.getRequestURL().toString();
187: }
188:
189: public String getContentType() {
190: return req.getContentType();
191: }
192:
193: public Locale getLocale() {
194: return req.getLocale();
195: }
196:
197: public Iterator getParameterNames() {
198: return new EnumerationIterator(req.getParameterNames());
199: }
200:
201: public String[] getParameterValues(String name) {
202: return req.getParameterValues(name);
203: }
204:
205: public String getPath() {
206: return path.toString();
207: }
208:
209: public void popPathPrefix() {
210: path.insert(0, (String) pathPrefixes.removeLast());
211: }
212:
213: public void pushPathPrefix(String pathPrefix) {
214: Assert.notEmptyParam(pathPrefix, "pathPrefix");
215:
216: pathPrefixes.addLast(pathPrefix);
217: path.delete(0, pathPrefix.length() - 1);
218: }
219:
220: public void setCharacterEncoding(String encoding) {
221: Assert.notEmptyParam(encoding, "encoding");
222:
223: try {
224: req.setCharacterEncoding(encoding);
225: } catch (UnsupportedEncodingException e) {
226: ExceptionUtil.uncheckException(e);
227: }
228: }
229: }
|