001
014
015 package com.liferay.portal.sharepoint;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
019 import com.liferay.portal.kernel.util.ArrayUtil;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.HttpUtil;
022 import com.liferay.portal.kernel.util.StreamUtil;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.model.User;
025
026 import java.io.ByteArrayInputStream;
027 import java.io.InputStream;
028 import java.io.InputStreamReader;
029
030 import java.util.HashMap;
031 import java.util.Map;
032
033 import javax.servlet.http.HttpServletRequest;
034 import javax.servlet.http.HttpServletResponse;
035
036
039 public class SharepointRequest {
040
041 public SharepointRequest(
042 HttpServletRequest request, HttpServletResponse response, User user)
043 throws SharepointException {
044
045 this(request, response, user, StringPool.BLANK);
046 }
047
048 public SharepointRequest(String rootPath) throws SharepointException {
049 this(null, null, null, rootPath);
050 }
051
052 public void addParam(String key, String value) {
053 _params.put(key, new String[] {value});
054 }
055
056 public byte[] getBytes() {
057 return _bytes;
058 }
059
060 public long getCompanyId() {
061 return _user.getCompanyId();
062 }
063
064 public HttpServletRequest getHttpServletRequest() {
065 return _request;
066 }
067
068 public HttpServletResponse getHttpServletResponse() {
069 return _response;
070 }
071
072 public String getParameterValue(String name) {
073 String[] values = _params.get(name);
074
075 if (ArrayUtil.isNotEmpty(values)) {
076 return GetterUtil.getString(_params.get(name)[0]);
077 }
078 else {
079 return StringPool.BLANK;
080 }
081 }
082
083 public String getRootPath() {
084 return _rootPath;
085 }
086
087 public SharepointStorage getSharepointStorage() {
088 return _storage;
089 }
090
091 public User getUser() {
092 return _user;
093 }
094
095 public long getUserId() {
096 return _user.getUserId();
097 }
098
099 public void setBytes(byte[] bytes) {
100 _bytes = bytes;
101 }
102
103 public void setRootPath(String rootPath) {
104 _rootPath = SharepointUtil.replaceBackSlashes(rootPath);
105 }
106
107 public void setSharepointStorage(SharepointStorage storage) {
108 _storage = storage;
109 }
110
111 protected void addParams() throws SharepointException {
112 String contentType = _request.getContentType();
113
114 if (!contentType.equals(SharepointUtil.VEERMER_URLENCODED)) {
115 return;
116 }
117
118 try {
119 InputStream is = _request.getInputStream();
120
121 UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
122 new UnsyncByteArrayOutputStream();
123
124 StreamUtil.transfer(is, unsyncByteArrayOutputStream);
125
126 byte[] bytes = unsyncByteArrayOutputStream.toByteArray();
127
128 UnsyncBufferedReader unsyncBufferedReader =
129 new UnsyncBufferedReader(
130 new InputStreamReader(new ByteArrayInputStream(bytes)));
131
132 String url = unsyncBufferedReader.readLine();
133
134 String[] params = url.split(StringPool.AMPERSAND);
135
136 for (String param : params) {
137 String[] kvp = param.split(StringPool.EQUAL);
138
139 String key = HttpUtil.decodeURL(kvp[0]);
140 String value = StringPool.BLANK;
141
142 if (kvp.length > 1) {
143 value = HttpUtil.decodeURL(kvp[1]);
144 }
145
146 addParam(key, value);
147 }
148
149 bytes = ArrayUtil.subset(bytes, url.length() + 1, bytes.length);
150
151 setBytes(bytes);
152 }
153 catch (Exception e) {
154 throw new SharepointException(e);
155 }
156 }
157
158 private SharepointRequest(
159 HttpServletRequest request, HttpServletResponse response, User user,
160 String rootPath)
161 throws SharepointException {
162
163 _request = request;
164 _response = response;
165 _user = user;
166 _rootPath = rootPath;
167
168 _params.putAll(request.getParameterMap());
169
170 addParams();
171 }
172
173 private byte[] _bytes;
174 private final Map<String, String[]> _params = new HashMap<>();
175 private final HttpServletRequest _request;
176 private final HttpServletResponse _response;
177 private String _rootPath = StringPool.BLANK;
178 private SharepointStorage _storage;
179 private final User _user;
180
181 }