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