1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.apache.bridges.struts;
24  
25  import com.liferay.portal.struts.StrutsUtil;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.portlet.ActionRequestImpl;
29  import com.liferay.portlet.RenderRequestImpl;
30  import com.liferay.util.CollectionFactory;
31  import com.liferay.util.servlet.UploadServletRequest;
32  
33  import java.util.Collections;
34  import java.util.Enumeration;
35  import java.util.Iterator;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.Vector;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletRequestWrapper;
42  
43  /**
44   * <a href="LiferayStrutsRequestImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Michael Young
47   *
48   */
49  public class LiferayStrutsRequestImpl extends HttpServletRequestWrapper {
50  
51      public LiferayStrutsRequestImpl(ActionRequestImpl req) {
52          this(req.getHttpServletRequest());
53      }
54  
55      public LiferayStrutsRequestImpl(RenderRequestImpl req) {
56          this(req.getHttpServletRequest());
57      }
58  
59      public Object getAttribute(String name) {
60          Object value = null;
61  
62          if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
63              value = _strutsAttributes.get(name);
64          }
65          else {
66              value = super.getAttribute(name);
67          }
68  
69          return value;
70      }
71  
72      public void setAttribute(String name, Object value) {
73          if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
74              _strutsAttributes.put(name, value);
75          }
76          else {
77              super.setAttribute(name, value);
78          }
79      }
80  
81      public Enumeration getAttributeNames() {
82          List attributeNames = new Vector();
83  
84          Enumeration enu = super.getAttributeNames();
85  
86          while (enu.hasMoreElements()) {
87              String name = (String)enu.nextElement();
88  
89              if (!name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
90                  attributeNames.add(name);
91              }
92          }
93  
94          Iterator itr = _strutsAttributes.keySet().iterator();
95  
96          while (itr.hasNext()) {
97              String name = (String)itr.next();
98  
99              attributeNames.add(name);
100         }
101 
102         return Collections.enumeration(attributeNames);
103     }
104 
105     public String getParameter(String name) {
106         if (_multipartParams.get(name) != null) {
107             return null;
108         }
109         else {
110             return super.getParameter(name);
111         }
112     }
113 
114     public Map getParameterMap() {
115         Map params = CollectionFactory.getHashMap();
116 
117         Enumeration enu = getParameterNames();
118 
119         while (enu.hasMoreElements()) {
120             String name = (String)enu.nextElement();
121 
122             String[] values = super.getParameterValues(name);
123 
124             params.put(name, values);
125         }
126 
127         return params;
128     }
129 
130     public Enumeration getParameterNames() {
131         List parameterNames = new Vector();
132 
133         Enumeration enu = super.getParameterNames();
134 
135         while (enu.hasMoreElements()) {
136             String name = (String)enu.nextElement();
137 
138             if (!_multipartParams.containsKey(name)) {
139                 parameterNames.add(name);
140             }
141         }
142 
143         return Collections.enumeration(parameterNames);
144     }
145 
146     public String[] getParameterValues(String name) {
147         if (_multipartParams.get(name) != null) {
148             return null;
149         }
150         else {
151             return super.getParameterValues(name);
152         }
153     }
154 
155     protected LiferayStrutsRequestImpl(HttpServletRequest req) {
156         super(req);
157 
158         _strutsAttributes =
159             (Map)req.getAttribute(WebKeys.STRUTS_BRIDGES_ATTRIBUTES);
160 
161         if (_strutsAttributes == null) {
162             _strutsAttributes = CollectionFactory.getHashMap();
163 
164             req.setAttribute(
165                 WebKeys.STRUTS_BRIDGES_ATTRIBUTES, _strutsAttributes);
166         }
167 
168         UploadServletRequest uploadReq =
169             PortalUtil.getUploadServletRequest(req);
170 
171         if (uploadReq != null) {
172             _multipartParams = uploadReq.getMultipartParameterMap();
173         }
174     }
175 
176     private Map _strutsAttributes;
177     private Map _multipartParams = CollectionFactory.getHashMap();
178 
179 }