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