1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.util.FileUtil;
26  import com.liferay.portal.struts.StrutsUtil;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.util.servlet.ByteArrayInputStreamWrapper;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  
34  import java.util.Collections;
35  import java.util.Enumeration;
36  import java.util.HashMap;
37  import java.util.Iterator;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Vector;
41  
42  import javax.servlet.ServletInputStream;
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletRequestWrapper;
45  
46  /**
47   * <a href="LiferayStrutsRequestImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Michael Young
50   * @author Deepak Gothe
51   */
52  public class LiferayStrutsRequestImpl extends HttpServletRequestWrapper {
53  
54      public LiferayStrutsRequestImpl(HttpServletRequest request) {
55          super(request);
56  
57          _strutsAttributes = (Map<String, Object>)request.getAttribute(
58              WebKeys.STRUTS_BRIDGES_ATTRIBUTES);
59  
60          if (_strutsAttributes == null) {
61              _strutsAttributes = new HashMap<String, Object>();
62  
63              request.setAttribute(
64                  WebKeys.STRUTS_BRIDGES_ATTRIBUTES, _strutsAttributes);
65          }
66      }
67  
68      public Object getAttribute(String name) {
69          Object value = null;
70  
71          if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
72              value = _strutsAttributes.get(name);
73          }
74          else {
75              value = super.getAttribute(name);
76          }
77  
78          return value;
79      }
80  
81      public void setAttribute(String name, Object value) {
82          if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
83              _strutsAttributes.put(name, value);
84          }
85          else {
86              super.setAttribute(name, value);
87          }
88      }
89  
90      public Enumeration<String> getAttributeNames() {
91          List<String> attributeNames = new Vector<String>();
92  
93          Enumeration<String> enu = super.getAttributeNames();
94  
95          while (enu.hasMoreElements()) {
96              String name = enu.nextElement();
97  
98              if (!name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
99                  attributeNames.add(name);
100             }
101         }
102 
103         Iterator<String> itr = _strutsAttributes.keySet().iterator();
104 
105         while (itr.hasNext()) {
106             String name = itr.next();
107 
108             attributeNames.add(name);
109         }
110 
111         return Collections.enumeration(attributeNames);
112     }
113 
114     public ServletInputStream getInputStream() throws IOException {
115         if (_bytes == null) {
116             InputStream is = super.getInputStream();
117 
118             _bytes = FileUtil.getBytes(is);
119 
120             is.close();
121         }
122 
123         return new ByteArrayInputStreamWrapper(
124             new ByteArrayInputStream(_bytes));
125     }
126 
127     private Map<String, Object> _strutsAttributes;
128     private byte[] _bytes;
129 
130 }