1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.sharepoint;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.CharPool;
28  import com.liferay.portal.kernel.util.HttpUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.sharepoint.methods.Method;
32  import com.liferay.portal.sharepoint.methods.MethodFactory;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.util.servlet.ServletResponseUtil;
35  
36  import java.io.BufferedInputStream;
37  import java.io.BufferedOutputStream;
38  import java.io.ByteArrayOutputStream;
39  import java.io.InputStream;
40  
41  import javax.servlet.http.HttpServlet;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  /**
46   * <a href="SharepointServlet.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Bruno Farache
49   */
50  public class SharepointServlet extends HttpServlet {
51  
52      public void doGet(
53          HttpServletRequest request, HttpServletResponse response) {
54  
55          try {
56              String uri = request.getRequestURI();
57  
58              if (uri.equals("/_vti_inf.html")) {
59                  vtiInfHtml(response);
60              }
61          }
62          catch (Exception e) {
63              _log.error(e, e);
64          }
65      }
66  
67      public void doPost(
68          HttpServletRequest request, HttpServletResponse response) {
69  
70          try {
71              String uri = request.getRequestURI();
72  
73              if (uri.equals("/_vti_bin/shtml.dll/_vti_rpc") ||
74                  uri.equals("/sharepoint/_vti_bin/_vti_aut/author.dll")) {
75  
76                  User user = (User)request.getSession().getAttribute(
77                      WebKeys.USER);
78  
79                  SharepointRequest sharepointRequest = new SharepointRequest(
80                      request, response, user);
81  
82                  addParams(request, sharepointRequest);
83  
84                  Method method = MethodFactory.create(sharepointRequest);
85  
86                  String rootPath = method.getRootPath(sharepointRequest);
87  
88                  sharepointRequest.setRootPath(rootPath);
89  
90                  SharepointStorage storage = SharepointUtil.getStorage(rootPath);
91  
92                  sharepointRequest.setSharepointStorage(storage);
93  
94                  method.process(sharepointRequest);
95              }
96          }
97          catch (SharepointException se) {
98              _log.error(se, se);
99          }
100     }
101 
102     protected void addParams(
103             HttpServletRequest request, SharepointRequest sharepointRequest)
104         throws SharepointException {
105 
106         String contentType = request.getContentType();
107 
108         if (!contentType.equals(SharepointUtil.VEERMER_URLENCODED)) {
109             return;
110         }
111 
112         try {
113             InputStream is = new BufferedInputStream(request.getInputStream());
114 
115             ByteArrayOutputStream baos = new ByteArrayOutputStream();
116             BufferedOutputStream bos = new BufferedOutputStream(baos);
117 
118             int c = is.read();
119 
120             while (c != -1) {
121                 bos.write(c);
122 
123                 if (c == CharPool.NEW_LINE) {
124                     break;
125                 }
126 
127                 c = is.read();
128             }
129 
130             bos.flush();
131             bos.close();
132 
133             String url = new String(baos.toByteArray());
134 
135             String[] params = url.split(StringPool.AMPERSAND);
136 
137             for (String param : params) {
138                 String[] kvp = param.split(StringPool.EQUAL);
139 
140                 String key = HttpUtil.decodeURL(kvp[0]);
141                 String value = StringPool.BLANK;
142 
143                 if (kvp.length > 1) {
144                     value = HttpUtil.decodeURL(kvp[1]);
145                 }
146 
147                 sharepointRequest.addParam(key, value);
148             }
149 
150             c = is.read();
151 
152             baos = new ByteArrayOutputStream();
153             bos = new BufferedOutputStream(baos);
154 
155             while (c != -1) {
156                 bos.write(c);
157 
158                 c = is.read();
159             }
160 
161             is.close();
162 
163             bos.flush();
164             bos.close();
165 
166             sharepointRequest.setBytes(baos.toByteArray());
167         }
168         catch (Exception e) {
169             throw new SharepointException(e);
170         }
171     }
172 
173     protected void vtiInfHtml(HttpServletResponse response) throws Exception {
174         StringBuilder sb = new StringBuilder();
175 
176         sb.append("<!-- FrontPage Configuration Information");
177         sb.append(StringPool.NEW_LINE);
178         sb.append(" FPVersion=\"6.0.2.9999\"");
179         sb.append(StringPool.NEW_LINE);
180         sb.append("FPShtmlScriptUrl=\"_vti_bin/shtml.dll/_vti_rpc\"");
181         sb.append(StringPool.NEW_LINE);
182         sb.append("FPAuthorScriptUrl=\"_vti_bin/_vti_aut/author.dll\"");
183         sb.append(StringPool.NEW_LINE);
184         sb.append("FPAdminScriptUrl=\"_vti_bin/_vti_adm/admin.dll\"");
185         sb.append(StringPool.NEW_LINE);
186         sb.append("TPScriptUrl=\"_vti_bin/owssvr.dll\"");
187         sb.append(StringPool.NEW_LINE);
188         sb.append("-->");
189 
190         ServletResponseUtil.write(response, sb.toString());
191     }
192 
193     private static Log _log = LogFactoryUtil.getLog(SharepointServlet.class);
194 
195 }