1   /**
2    * Copyright (c) 2000-2009 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.upload.LiferayFileItem;
27  import com.liferay.portal.upload.UploadServletRequestImpl;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portal.util.WebKeys;
30  
31  import java.util.Collections;
32  import java.util.Enumeration;
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Vector;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletRequestWrapper;
41  
42  /**
43   * <a href="LiferayStrutsRequestImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Michael Young
46   * @author Deepak Gothe
47   *
48   */
49  public class LiferayStrutsRequestImpl extends HttpServletRequestWrapper {
50  
51      public LiferayStrutsRequestImpl(HttpServletRequest request) {
52          super(request);
53  
54          _strutsAttributes = (Map<String, Object>)request.getAttribute(
55              WebKeys.STRUTS_BRIDGES_ATTRIBUTES);
56  
57          if (_strutsAttributes == null) {
58              _strutsAttributes = new HashMap<String, Object>();
59  
60              request.setAttribute(
61                  WebKeys.STRUTS_BRIDGES_ATTRIBUTES, _strutsAttributes);
62          }
63  
64          UploadServletRequestImpl uploadRequest =
65              (UploadServletRequestImpl)PortalUtil.getUploadServletRequest(
66                  request);
67  
68          if (uploadRequest != null) {
69              _multipartParams = uploadRequest.getMultipartParameterMap();
70          }
71      }
72  
73      public Object getAttribute(String name) {
74          Object value = null;
75  
76          if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
77              value = _strutsAttributes.get(name);
78          }
79          else {
80              value = super.getAttribute(name);
81          }
82  
83          return value;
84      }
85  
86      public void setAttribute(String name, Object value) {
87          if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
88              _strutsAttributes.put(name, value);
89          }
90          else {
91              super.setAttribute(name, value);
92          }
93      }
94  
95      public Enumeration<String> getAttributeNames() {
96          List<String> attributeNames = new Vector<String>();
97  
98          Enumeration<String> enu = super.getAttributeNames();
99  
100         while (enu.hasMoreElements()) {
101             String name = enu.nextElement();
102 
103             if (!name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
104                 attributeNames.add(name);
105             }
106         }
107 
108         Iterator<String> itr = _strutsAttributes.keySet().iterator();
109 
110         while (itr.hasNext()) {
111             String name = itr.next();
112 
113             attributeNames.add(name);
114         }
115 
116         return Collections.enumeration(attributeNames);
117     }
118 
119     public String getParameter(String name) {
120         if (_multipartParams.get(name) != null) {
121             return null;
122         }
123         else {
124             return super.getParameter(name);
125         }
126     }
127 
128     public Map<String, String[]> getParameterMap() {
129         Map<String, String[]> params = new HashMap<String, String[]>();
130 
131         Enumeration<String> enu = getParameterNames();
132 
133         while (enu.hasMoreElements()) {
134             String name = enu.nextElement();
135 
136             String[] values = super.getParameterValues(name);
137 
138             params.put(name, values);
139         }
140 
141         return params;
142     }
143 
144     public Enumeration<String> getParameterNames() {
145         List<String> parameterNames = new Vector<String>();
146 
147         Enumeration<String> enu = super.getParameterNames();
148 
149         while (enu.hasMoreElements()) {
150             String name = enu.nextElement();
151 
152             if (!_multipartParams.containsKey(name)) {
153                 parameterNames.add(name);
154             }
155         }
156 
157         return Collections.enumeration(parameterNames);
158     }
159 
160     public String[] getParameterValues(String name) {
161         if (_multipartParams.get(name) != null) {
162             return null;
163         }
164         else {
165             return super.getParameterValues(name);
166         }
167     }
168 
169     private Map<String, Object> _strutsAttributes;
170     private Map<String, LiferayFileItem[]> _multipartParams =
171         new HashMap<String, LiferayFileItem[]>();
172 
173 }