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.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   */
53  public class LiferayStrutsRequestImpl extends HttpServletRequestWrapper {
54  
55      public LiferayStrutsRequestImpl(HttpServletRequest request) {
56          super(request);
57  
58          _strutsAttributes = (Map<String, Object>)request.getAttribute(
59              WebKeys.STRUTS_BRIDGES_ATTRIBUTES);
60  
61          if (_strutsAttributes == null) {
62              _strutsAttributes = new HashMap<String, Object>();
63  
64              request.setAttribute(
65                  WebKeys.STRUTS_BRIDGES_ATTRIBUTES, _strutsAttributes);
66          }
67      }
68  
69      public Object getAttribute(String name) {
70          Object value = null;
71  
72          if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
73              value = _strutsAttributes.get(name);
74          }
75          else {
76              value = super.getAttribute(name);
77          }
78  
79          return value;
80      }
81  
82      public void setAttribute(String name, Object value) {
83          if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
84              _strutsAttributes.put(name, value);
85          }
86          else {
87              super.setAttribute(name, value);
88          }
89      }
90  
91      public Enumeration<String> getAttributeNames() {
92          List<String> attributeNames = new Vector<String>();
93  
94          Enumeration<String> enu = super.getAttributeNames();
95  
96          while (enu.hasMoreElements()) {
97              String name = enu.nextElement();
98  
99              if (!name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
100                 attributeNames.add(name);
101             }
102         }
103 
104         Iterator<String> itr = _strutsAttributes.keySet().iterator();
105 
106         while (itr.hasNext()) {
107             String name = itr.next();
108 
109             attributeNames.add(name);
110         }
111 
112         return Collections.enumeration(attributeNames);
113     }
114 
115     public ServletInputStream getInputStream() throws IOException {
116         if (_bytes == null) {
117             InputStream is = super.getInputStream();
118 
119             _bytes = FileUtil.getBytes(is);
120 
121             is.close();
122         }
123 
124         return new ByteArrayInputStreamWrapper(
125             new ByteArrayInputStream(_bytes));
126     }
127 
128     private Map<String, Object> _strutsAttributes;
129     private byte[] _bytes;
130 
131 }