1
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
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 }