001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
037     * @author Bruno Farache
038     */
039    public class SharepointRequest {
040    
041            public SharepointRequest(
042                            HttpServletRequest request, HttpServletResponse response, User user)
043                    throws SharepointException {
044    
045                    _request = request;
046                    _response = response;
047                    _user = user;
048    
049                    _params.putAll(request.getParameterMap());
050    
051                    addParams();
052            }
053    
054            public SharepointRequest(String rootPath) {
055                    _rootPath = rootPath;
056            }
057    
058            public void addParam(String key, String value) {
059                    _params.put(key, new String[] {value});
060            }
061    
062            public byte[] getBytes() {
063                    return _bytes;
064            }
065    
066            public long getCompanyId() {
067                    return _user.getCompanyId();
068            }
069    
070            public HttpServletRequest getHttpServletRequest() {
071                    return _request;
072            }
073    
074            public HttpServletResponse getHttpServletResponse() {
075                    return _response;
076            }
077    
078            public String getParameterValue(String name) {
079                    String[] values = _params.get(name);
080    
081                    if (ArrayUtil.isNotEmpty(values)) {
082                            return GetterUtil.getString(_params.get(name)[0]);
083                    }
084                    else {
085                            return StringPool.BLANK;
086                    }
087            }
088    
089            public String getRootPath() {
090                    return _rootPath;
091            }
092    
093            public SharepointStorage getSharepointStorage() {
094                    return _storage;
095            }
096    
097            public User getUser() {
098                    return _user;
099            }
100    
101            public long getUserId() {
102                    return _user.getUserId();
103            }
104    
105            public void setBytes(byte[] bytes) {
106                    _bytes = bytes;
107            }
108    
109            public void setRootPath(String rootPath) {
110                    _rootPath = SharepointUtil.replaceBackSlashes(rootPath);
111            }
112    
113            public void setSharepointStorage(SharepointStorage storage) {
114                    _storage = storage;
115            }
116    
117            protected void addParams() throws SharepointException {
118                    String contentType = _request.getContentType();
119    
120                    if (!contentType.equals(SharepointUtil.VEERMER_URLENCODED)) {
121                            return;
122                    }
123    
124                    try {
125                            InputStream is = _request.getInputStream();
126    
127                            UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
128                                    new UnsyncByteArrayOutputStream();
129    
130                            StreamUtil.transfer(is, unsyncByteArrayOutputStream);
131    
132                            byte[] bytes = unsyncByteArrayOutputStream.toByteArray();
133    
134                            UnsyncBufferedReader unsyncBufferedReader =
135                                    new UnsyncBufferedReader(
136                                            new InputStreamReader(new ByteArrayInputStream(bytes)));
137    
138                            String url = unsyncBufferedReader.readLine();
139    
140                            String[] params = url.split(StringPool.AMPERSAND);
141    
142                            for (String param : params) {
143                                    String[] kvp = param.split(StringPool.EQUAL);
144    
145                                    String key = HttpUtil.decodeURL(kvp[0]);
146                                    String value = StringPool.BLANK;
147    
148                                    if (kvp.length > 1) {
149                                            value = HttpUtil.decodeURL(kvp[1]);
150                                    }
151    
152                                    addParam(key, value);
153                            }
154    
155                            bytes = ArrayUtil.subset(bytes, url.length() + 1, bytes.length);
156    
157                            setBytes(bytes);
158                    }
159                    catch (Exception e) {
160                            throw new SharepointException(e);
161                    }
162            }
163    
164            private byte[] _bytes;
165            private Map<String, String[]> _params = new HashMap<String, String[]>();
166            private HttpServletRequest _request;
167            private HttpServletResponse _response;
168            private String _rootPath = StringPool.BLANK;
169            private SharepointStorage _storage;
170            private User _user;
171    
172    }