1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.sharepoint;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
18  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.HttpUtil;
22  import com.liferay.portal.kernel.util.StreamUtil;
23  import com.liferay.portal.kernel.util.StringBundler;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.sharepoint.methods.Method;
27  import com.liferay.portal.sharepoint.methods.MethodFactory;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.util.servlet.ServletResponseUtil;
30  
31  import java.io.InputStream;
32  import java.io.InputStreamReader;
33  
34  import javax.servlet.http.HttpServlet;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  /**
39   * <a href="SharepointServlet.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Bruno Farache
42   */
43  public class SharepointServlet extends HttpServlet {
44  
45      public void doGet(
46          HttpServletRequest request, HttpServletResponse response) {
47  
48          try {
49              String uri = request.getRequestURI();
50  
51              if (uri.equals("/_vti_inf.html")) {
52                  vtiInfHtml(response);
53              }
54          }
55          catch (Exception e) {
56              _log.error(e, e);
57          }
58      }
59  
60      public void doPost(
61          HttpServletRequest request, HttpServletResponse response) {
62  
63          try {
64              String uri = request.getRequestURI();
65  
66              if (uri.equals("/_vti_bin/shtml.dll/_vti_rpc") ||
67                  uri.equals("/sharepoint/_vti_bin/_vti_aut/author.dll")) {
68  
69                  User user = (User)request.getSession().getAttribute(
70                      WebKeys.USER);
71  
72                  SharepointRequest sharepointRequest = new SharepointRequest(
73                      request, response, user);
74  
75                  addParams(request, sharepointRequest);
76  
77                  Method method = MethodFactory.create(sharepointRequest);
78  
79                  String rootPath = method.getRootPath(sharepointRequest);
80  
81                  sharepointRequest.setRootPath(rootPath);
82  
83                  SharepointStorage storage = SharepointUtil.getStorage(rootPath);
84  
85                  sharepointRequest.setSharepointStorage(storage);
86  
87                  method.process(sharepointRequest);
88              }
89          }
90          catch (SharepointException se) {
91              _log.error(se, se);
92          }
93      }
94  
95      protected void addParams(
96              HttpServletRequest request, SharepointRequest sharepointRequest)
97          throws SharepointException {
98  
99          String contentType = request.getContentType();
100 
101         if (!contentType.equals(SharepointUtil.VEERMER_URLENCODED)) {
102             return;
103         }
104 
105         try {
106             InputStream is = request.getInputStream();
107 
108             UnsyncBufferedReader unsyncBufferedReader =
109                 new UnsyncBufferedReader(new InputStreamReader(is));
110 
111             String url = unsyncBufferedReader.readLine();
112 
113             String[] params = url.split(StringPool.AMPERSAND);
114 
115             for (String param : params) {
116                 String[] kvp = param.split(StringPool.EQUAL);
117 
118                 String key = HttpUtil.decodeURL(kvp[0]);
119                 String value = StringPool.BLANK;
120 
121                 if (kvp.length > 1) {
122                     value = HttpUtil.decodeURL(kvp[1]);
123                 }
124 
125                 sharepointRequest.addParam(key, value);
126             }
127 
128             UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
129                 new UnsyncByteArrayOutputStream();
130 
131             StreamUtil.transfer(is, unsyncByteArrayOutputStream);
132 
133             sharepointRequest.setBytes(
134                 unsyncByteArrayOutputStream.toByteArray());
135         }
136         catch (Exception e) {
137             throw new SharepointException(e);
138         }
139     }
140 
141     protected void vtiInfHtml(HttpServletResponse response) throws Exception {
142         StringBundler sb = new StringBundler(13);
143 
144         sb.append("<!-- FrontPage Configuration Information");
145         sb.append(StringPool.NEW_LINE);
146         sb.append(" FPVersion=\"6.0.2.9999\"");
147         sb.append(StringPool.NEW_LINE);
148         sb.append("FPShtmlScriptUrl=\"_vti_bin/shtml.dll/_vti_rpc\"");
149         sb.append(StringPool.NEW_LINE);
150         sb.append("FPAuthorScriptUrl=\"_vti_bin/_vti_aut/author.dll\"");
151         sb.append(StringPool.NEW_LINE);
152         sb.append("FPAdminScriptUrl=\"_vti_bin/_vti_adm/admin.dll\"");
153         sb.append(StringPool.NEW_LINE);
154         sb.append("TPScriptUrl=\"_vti_bin/owssvr.dll\"");
155         sb.append(StringPool.NEW_LINE);
156         sb.append("-->");
157 
158         ServletResponseUtil.write(response, sb.toString());
159     }
160 
161     private static Log _log = LogFactoryUtil.getLog(SharepointServlet.class);
162 
163 }