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.io.unsync.UnsyncBufferedReader;
26  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.HttpUtil;
30  import com.liferay.portal.kernel.util.StreamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.sharepoint.methods.Method;
34  import com.liferay.portal.sharepoint.methods.MethodFactory;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.util.servlet.ServletResponseUtil;
37  
38  import java.io.InputStream;
39  import java.io.InputStreamReader;
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 = request.getInputStream();
114 
115             UnsyncBufferedReader unsyncBufferedReader =
116                 new UnsyncBufferedReader(new InputStreamReader(is));
117 
118             String url = unsyncBufferedReader.readLine();
119 
120             String[] params = url.split(StringPool.AMPERSAND);
121 
122             for (String param : params) {
123                 String[] kvp = param.split(StringPool.EQUAL);
124 
125                 String key = HttpUtil.decodeURL(kvp[0]);
126                 String value = StringPool.BLANK;
127 
128                 if (kvp.length > 1) {
129                     value = HttpUtil.decodeURL(kvp[1]);
130                 }
131 
132                 sharepointRequest.addParam(key, value);
133             }
134 
135             UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
136                 new UnsyncByteArrayOutputStream();
137 
138             StreamUtil.transfer(is, unsyncByteArrayOutputStream);
139 
140             sharepointRequest.setBytes(
141                 unsyncByteArrayOutputStream.toByteArray());
142         }
143         catch (Exception e) {
144             throw new SharepointException(e);
145         }
146     }
147 
148     protected void vtiInfHtml(HttpServletResponse response) throws Exception {
149         StringBuilder sb = new StringBuilder();
150 
151         sb.append("<!-- FrontPage Configuration Information");
152         sb.append(StringPool.NEW_LINE);
153         sb.append(" FPVersion=\"6.0.2.9999\"");
154         sb.append(StringPool.NEW_LINE);
155         sb.append("FPShtmlScriptUrl=\"_vti_bin/shtml.dll/_vti_rpc\"");
156         sb.append(StringPool.NEW_LINE);
157         sb.append("FPAuthorScriptUrl=\"_vti_bin/_vti_aut/author.dll\"");
158         sb.append(StringPool.NEW_LINE);
159         sb.append("FPAdminScriptUrl=\"_vti_bin/_vti_adm/admin.dll\"");
160         sb.append(StringPool.NEW_LINE);
161         sb.append("TPScriptUrl=\"_vti_bin/owssvr.dll\"");
162         sb.append(StringPool.NEW_LINE);
163         sb.append("-->");
164 
165         ServletResponseUtil.write(response, sb.toString());
166     }
167 
168     private static Log _log = LogFactoryUtil.getLog(SharepointServlet.class);
169 
170 }