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