1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.apache.bridges.struts;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.struts.StrutsUtil;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.util.servlet.ByteArrayInputStreamWrapper;
26  
27  import java.io.ByteArrayInputStream;
28  import java.io.IOException;
29  import java.io.InputStream;
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.ServletInputStream;
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   * @author Deepak Gothe
48   *
49   */
50  public class LiferayStrutsRequestImpl extends HttpServletRequestWrapper {
51  
52      public LiferayStrutsRequestImpl(HttpServletRequest request) {
53          super(request);
54  
55          _strutsAttributes = (Map<String, Object>)request.getAttribute(
56              WebKeys.STRUTS_BRIDGES_ATTRIBUTES);
57  
58          if (_strutsAttributes == null) {
59              _strutsAttributes = new HashMap<String, Object>();
60  
61              request.setAttribute(
62                  WebKeys.STRUTS_BRIDGES_ATTRIBUTES, _strutsAttributes);
63          }
64      }
65  
66      public Object getAttribute(String name) {
67          Object value = null;
68  
69          if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
70              value = _strutsAttributes.get(name);
71          }
72          else {
73              value = super.getAttribute(name);
74          }
75  
76          return value;
77      }
78  
79      public void setAttribute(String name, Object value) {
80          if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
81              _strutsAttributes.put(name, value);
82          }
83          else {
84              super.setAttribute(name, value);
85          }
86      }
87  
88      public Enumeration<String> getAttributeNames() {
89          List<String> attributeNames = new Vector<String>();
90  
91          Enumeration<String> enu = super.getAttributeNames();
92  
93          while (enu.hasMoreElements()) {
94              String name = enu.nextElement();
95  
96              if (!name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
97                  attributeNames.add(name);
98              }
99          }
100 
101         Iterator<String> itr = _strutsAttributes.keySet().iterator();
102 
103         while (itr.hasNext()) {
104             String name = itr.next();
105 
106             attributeNames.add(name);
107         }
108 
109         return Collections.enumeration(attributeNames);
110     }
111 
112     public ServletInputStream getInputStream() throws IOException {
113         if (_bytes == null) {
114             InputStream is = super.getInputStream();
115 
116             _bytes = FileUtil.getBytes(is);
117 
118             is.close();
119         }
120 
121         return new ByteArrayInputStreamWrapper(
122             new ByteArrayInputStream(_bytes));
123     }
124 
125     private Map<String, Object> _strutsAttributes;
126     private byte[] _bytes;
127 
128 }