1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.apache.bridges.struts;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStreamWrapper;
19  import com.liferay.portal.kernel.util.FileUtil;
20  import com.liferay.portal.struts.StrutsUtil;
21  import com.liferay.portal.util.WebKeys;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  import java.util.Collections;
27  import java.util.Enumeration;
28  import java.util.HashMap;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Vector;
33  
34  import javax.servlet.ServletInputStream;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletRequestWrapper;
37  
38  /**
39   * <a href="LiferayStrutsRequestImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Michael Young
42   * @author Deepak Gothe
43   */
44  public class LiferayStrutsRequestImpl extends HttpServletRequestWrapper {
45  
46      public LiferayStrutsRequestImpl(HttpServletRequest request) {
47          super(request);
48  
49          _strutsAttributes = (Map<String, Object>)request.getAttribute(
50              WebKeys.STRUTS_BRIDGES_ATTRIBUTES);
51  
52          if (_strutsAttributes == null) {
53              _strutsAttributes = new HashMap<String, Object>();
54  
55              request.setAttribute(
56                  WebKeys.STRUTS_BRIDGES_ATTRIBUTES, _strutsAttributes);
57          }
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 ServletInputStream getInputStream() throws IOException {
107         if (_bytes == null) {
108             InputStream is = super.getInputStream();
109 
110             _bytes = FileUtil.getBytes(is);
111 
112             is.close();
113         }
114 
115         return new UnsyncByteArrayInputStreamWrapper(
116             new UnsyncByteArrayInputStream(_bytes));
117     }
118 
119     private Map<String, Object> _strutsAttributes;
120     private byte[] _bytes;
121 
122 }