001
014
015 package com.liferay.portal.apache.bridges.struts;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018 import com.liferay.portal.kernel.servlet.ServletInputStreamAdapter;
019 import com.liferay.portal.kernel.util.FileUtil;
020 import com.liferay.portal.kernel.util.WebKeys;
021 import com.liferay.portal.struts.StrutsUtil;
022
023 import java.io.IOException;
024 import java.io.InputStream;
025
026 import java.util.Collections;
027 import java.util.Enumeration;
028 import java.util.HashMap;
029 import java.util.List;
030 import java.util.Map;
031 import java.util.Vector;
032
033 import javax.servlet.ServletInputStream;
034 import javax.servlet.http.HttpServletRequest;
035 import javax.servlet.http.HttpServletRequestWrapper;
036
037
041 public class LiferayStrutsRequestImpl extends HttpServletRequestWrapper {
042
043 public LiferayStrutsRequestImpl(HttpServletRequest request) {
044 super(request);
045
046 Map<String, Object> strutsAttributes =
047 (Map<String, Object>)request.getAttribute(
048 WebKeys.STRUTS_BRIDGES_ATTRIBUTES);
049
050 if (strutsAttributes == null) {
051 strutsAttributes = new HashMap<>();
052
053 request.setAttribute(
054 WebKeys.STRUTS_BRIDGES_ATTRIBUTES, strutsAttributes);
055 }
056
057 _strutsAttributes = strutsAttributes;
058 }
059
060 @Override
061 public Object getAttribute(String name) {
062 Object value = null;
063
064 if (name.startsWith(StrutsUtil.STRUTS_PACKAGE) &&
065 _strutsAttributes.containsKey(name)) {
066
067 value = _strutsAttributes.get(name);
068 }
069 else {
070 value = super.getAttribute(name);
071 }
072
073 return value;
074 }
075
076 @Override
077 public Enumeration<String> getAttributeNames() {
078 List<String> attributeNames = new Vector<>();
079
080 Enumeration<String> enu = super.getAttributeNames();
081
082 while (enu.hasMoreElements()) {
083 String name = enu.nextElement();
084
085 if (!name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
086 attributeNames.add(name);
087 }
088 }
089
090 attributeNames.addAll(_strutsAttributes.keySet());
091
092 return Collections.enumeration(attributeNames);
093 }
094
095 @Override
096 public ServletInputStream getInputStream() throws IOException {
097 if (_bytes == null) {
098 InputStream is = super.getInputStream();
099
100 _bytes = FileUtil.getBytes(is);
101 }
102
103 return new ServletInputStreamAdapter(
104 new UnsyncByteArrayInputStream(_bytes));
105 }
106
107 @Override
108 public void removeAttribute(String name) {
109 if (name.startsWith(StrutsUtil.STRUTS_PACKAGE) &&
110 _strutsAttributes.containsKey(name)) {
111
112 _strutsAttributes.remove(name);
113 }
114 else {
115 super.removeAttribute(name);
116 }
117 }
118
119 @Override
120 public void setAttribute(String name, Object value) {
121 if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
122 _strutsAttributes.put(name, value);
123 }
124 else {
125 super.setAttribute(name, value);
126 }
127 }
128
129 private byte[] _bytes;
130 private final Map<String, Object> _strutsAttributes;
131
132 }