001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/portal/tags/sakai_2-4-1/portal-service-impl/impl/src/java/org/sakaiproject/portal/service/SessionRequestHolder.java $
003: * $Id: SessionRequestHolder.java 29143 2007-04-19 01:10:38Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.portal.service;
021:
022: import java.text.SimpleDateFormat;
023: import java.util.ArrayList;
024: import java.util.Date;
025: import java.util.Enumeration;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030:
031: import javax.servlet.http.HttpServletRequest;
032:
033: /**
034: * @author ieb
035: * @since Sakai 2.4
036: * @version $Rev: 29143 $
037: */
038:
039: public class SessionRequestHolder {
040: private Map<String, List> headers;
041:
042: private String contextPath;
043:
044: private String method;
045:
046: private String queryString;
047:
048: private Map parameterMap;
049:
050: public SessionRequestHolder(HttpServletRequest request,
051: String marker, String replacement) {
052: headers = new HashMap<String, List>();
053: Enumeration e = request.getHeaderNames();
054: while (e.hasMoreElements()) {
055: String s = (String) e.nextElement();
056: List v = new ArrayList();
057: Enumeration e1 = request.getHeaders(s);
058: while (e1.hasMoreElements()) {
059: v.add(e1.nextElement());
060: }
061: headers.put(s, v);
062: }
063: Map m = request.getParameterMap();
064: parameterMap = new HashMap();
065: for (Iterator i = m.keySet().iterator(); i.hasNext();) {
066: Object o = i.next();
067: parameterMap.put(o, m.get(o));
068: }
069: contextPath = PortalStringUtil.replaceFirst(request
070: .getContextPath(), marker, replacement);
071: method = request.getMethod();
072: queryString = request.getQueryString();
073: }
074:
075: public String getContextPath() {
076: return contextPath;
077: }
078:
079: public long getDateHeader(String arg0) {
080: try {
081: SimpleDateFormat f = new SimpleDateFormat();
082: Date d = f.parse(getHeader(arg0));
083: return d.getTime();
084: } catch (Throwable t) {
085: return 0;
086: }
087: }
088:
089: public String getHeader(String arg0) {
090: try {
091: List v = (List) headers.get(arg0);
092: return (String) v.get(0);
093: } catch (Throwable t) {
094: return null;
095: }
096: }
097:
098: public Enumeration getHeaderNames() {
099: final Iterator<String> i = headers.keySet().iterator();
100: return new Enumeration() {
101:
102: public boolean hasMoreElements() {
103: return i.hasNext();
104: }
105:
106: public Object nextElement() {
107: return i.next();
108: }
109:
110: };
111: }
112:
113: public Enumeration getHeaders(String arg0) {
114: try {
115: final Iterator i = headers.get(arg0).iterator();
116: return new Enumeration() {
117: public boolean hasMoreElements() {
118: return i.hasNext();
119: }
120:
121: public Object nextElement() {
122: return i.next();
123: }
124:
125: };
126: } catch (Throwable t) {
127: return null;
128: }
129: }
130:
131: public int getIntHeader(String arg0) {
132: try {
133: return Integer.parseInt(getHeader(arg0));
134: } catch (Throwable t) {
135: return 0;
136: }
137: }
138:
139: public String getMethod() {
140: return method;
141: }
142:
143: public String getQueryString() {
144: return queryString;
145: }
146:
147: public String getParameter(String arg0) {
148: Object o = parameterMap.get(arg0);
149: if (o instanceof String[]) {
150: String[] s = (String[]) o;
151: return s[0];
152: } else if (o instanceof String) {
153: return (String) o;
154: } else if (o != null) {
155: return o.toString();
156: } else {
157: return null;
158: }
159: }
160:
161: public Map getParameterMap() {
162: return parameterMap;
163: }
164:
165: public Enumeration getParameterNames() {
166: final Iterator i = parameterMap.keySet().iterator();
167: return new Enumeration() {
168:
169: public boolean hasMoreElements() {
170: return i.hasNext();
171: }
172:
173: public Object nextElement() {
174: return i.next();
175: }
176:
177: };
178: }
179:
180: public String[] getParameterValues(String arg0) {
181: Object o = parameterMap.get(arg0);
182: if (o instanceof String[]) {
183: String[] s = (String[]) o;
184: return s;
185: } else if (o instanceof String) {
186: return new String[] { (String) o };
187: } else if (o != null) {
188: return new String[] { o.toString() };
189: } else {
190: return null;
191: }
192: }
193: }
|