001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/utils/mvc/impl/HttpServletHelper.java $
003: * $Id: HttpServletHelper.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 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.metaobj.utils.mvc.impl;
021:
022: import java.util.Enumeration;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import javax.servlet.http.HttpServletRequest;
028:
029: public class HttpServletHelper {
030: private static HttpServletHelper ourInstance = null;
031:
032: public static HttpServletHelper getInstance() {
033: if (ourInstance == null) {
034: ourInstance = new HttpServletHelper();
035: }
036:
037: return ourInstance;
038: }
039:
040: private HttpServletHelper() {
041: }
042:
043: public void reloadApplicationMap(HttpServletRequest request, Map map) {
044:
045: /*
046: for (Iterator keys = map.keySet().iterator(); keys.hasNext();) {
047: String key = (String) keys.next();
048: if (map.get(key) == null) {
049: request.getSession().getServletContext().removeAttribute(key);
050: }
051: else if (!map.get(key).equals(request.getSession().getServletContext().getAttribute(key))) {
052: request.getSession().getServletContext().setAttribute(key,
053: map.get(key));
054: }
055: }
056:
057: Enumeration enumer = request.getSession().getServletContext().getAttributeNames();
058:
059: while (enumer.hasMoreElements()) {
060: String key = (String)enumer.nextElement();
061:
062: if (map.get(key) == null) {
063: request.getSession().getServletContext().removeAttribute(key);
064: }
065: }
066: */
067: }
068:
069: public Map createApplicationMap(HttpServletRequest request) {
070: Map parameters = new HashMap();
071: //Enumeration enumer = request.getSession().getServletContext().getAttributeNames();
072:
073: //while (enumer.hasMoreElements()) {
074: // String key = (String) enumer.nextElement();
075: // parameters.put(key, request.getSession().getServletContext().getAttribute(key));
076: //}
077:
078: return parameters;
079: }
080:
081: public void reloadSessionMap(HttpServletRequest request, Map map) {
082:
083: for (Iterator keys = map.keySet().iterator(); keys.hasNext();) {
084: String key = (String) keys.next();
085:
086: if (map.get(key) == null) {
087: request.getSession().removeAttribute(key);
088: } else if (!map.get(key).equals(
089: request.getSession().getAttribute(key))) {
090: request.getSession().setAttribute(key, map.get(key));
091: }
092: }
093:
094: Enumeration enumer = request.getSession().getAttributeNames();
095:
096: while (enumer.hasMoreElements()) {
097: String key = (String) enumer.nextElement();
098:
099: if (map.get(key) == null) {
100: request.getSession().removeAttribute(key);
101: }
102: }
103: }
104:
105: public Map createSessionMap(HttpServletRequest request) {
106: Map parameters = new HashMap();
107: Enumeration enumer = request.getSession().getAttributeNames();
108:
109: while (enumer.hasMoreElements()) {
110: String key = (String) enumer.nextElement();
111: parameters.put(key, request.getSession().getAttribute(key));
112: }
113:
114: return parameters;
115: }
116:
117: public void reloadRequestMap(HttpServletRequest request, Map map) {
118:
119: for (Iterator keys = map.keySet().iterator(); keys.hasNext();) {
120: String key = (String) keys.next();
121: if (map.get(key) == null) {
122: request.removeAttribute(key);
123: } else if (!map.get(key).equals(request.getAttribute(key))) {
124: request.setAttribute(key, map.get(key));
125: }
126: }
127:
128: Enumeration enumer = request.getAttributeNames();
129:
130: while (enumer.hasMoreElements()) {
131: String key = (String) enumer.nextElement();
132:
133: if (map.get(key) == null) {
134: request.removeAttribute(key);
135: }
136: }
137: }
138:
139: public Map createRequestMap(HttpServletRequest request) {
140: Map parameters = new HashMap();
141: Enumeration enumer = request.getAttributeNames();
142:
143: while (enumer.hasMoreElements()) {
144: String key = (String) enumer.nextElement();
145: parameters.put(key, request.getAttribute(key));
146: }
147:
148: enumer = request.getParameterNames();
149:
150: while (enumer.hasMoreElements()) {
151: String key = (String) enumer.nextElement();
152: if (request.getParameterValues(key).length > 1) {
153: parameters.put(key, request.getParameterValues(key));
154: } else {
155: parameters.put(key, request.getParameter(key));
156: }
157: }
158:
159: return parameters;
160: }
161:
162: }
|