1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
51  public class SharepointServlet extends HttpServlet {
52  
53      public void doGet(
54          HttpServletRequest request, HttpServletResponse response) {
55  
56          try {
57              String uri = request.getRequestURI();
58  
59              if (uri.equals("/_vti_inf.html")) {
60                  vtiInfHtml(response);
61              }
62          }
63          catch (Exception e) {
64              _log.error(e, e);
65          }
66      }
67  
68      public void doPost(
69          HttpServletRequest request, HttpServletResponse response) {
70  
71          try {
72              String uri = request.getRequestURI();
73  
74              if (uri.equals("/_vti_bin/shtml.dll/_vti_rpc") ||
75                  uri.equals("/sharepoint/_vti_bin/_vti_aut/author.dll")) {
76  
77                  User user = (User)request.getSession().getAttribute(
78                      WebKeys.USER);
79  
80                  SharepointRequest sharepointRequest = new SharepointRequest(
81                      request, response, user);
82  
83                  addParams(request, sharepointRequest);
84  
85                  Method method = MethodFactory.create(sharepointRequest);
86  
87                  String rootPath = method.getRootPath(sharepointRequest);
88  
89                  sharepointRequest.setRootPath(rootPath);
90  
91                  SharepointStorage storage = SharepointUtil.getStorage(rootPath);
92  
93                  sharepointRequest.setSharepointStorage(storage);
94  
95                  method.process(sharepointRequest);
96              }
97          }
98          catch (SharepointException se) {
99              _log.error(se, se);
100         }
101     }
102 
103     protected void addParams(
104             HttpServletRequest request, SharepointRequest sharepointRequest)
105         throws SharepointException {
106 
107         String contentType = request.getContentType();
108 
109         if (!contentType.equals(SharepointUtil.VEERMER_URLENCODED)) {
110             return;
111         }
112 
113         try {
114             InputStream is = new BufferedInputStream(request.getInputStream());
115 
116             ByteArrayOutputStream baos = new ByteArrayOutputStream();
117             BufferedOutputStream bos = new BufferedOutputStream(baos);
118 
119             int c = is.read();
120 
121             while (c != -1) {
122                 bos.write(c);
123 
124                 if (c == CharPool.NEW_LINE) {
125                     break;
126                 }
127 
128                 c = is.read();
129             }
130 
131             bos.flush();
132             bos.close();
133 
134             String url = new String(baos.toByteArray());
135 
136             String[] params = url.split(StringPool.AMPERSAND);
137 
138             for (String param : params) {
139                 String[] kvp = param.split(StringPool.EQUAL);
140 
141                 String key = HttpUtil.decodeURL(kvp[0]);
142                 String value = StringPool.BLANK;
143 
144                 if (kvp.length > 1) {
145                     value = HttpUtil.decodeURL(kvp[1]);
146                 }
147 
148                 sharepointRequest.addParam(key, value);
149             }
150 
151             c = is.read();
152 
153             baos = new ByteArrayOutputStream();
154             bos = new BufferedOutputStream(baos);
155 
156             while (c != -1) {
157                 bos.write(c);
158 
159                 c = is.read();
160             }
161 
162             is.close();
163 
164             bos.flush();
165             bos.close();
166 
167             sharepointRequest.setBytes(baos.toByteArray());
168         }
169         catch (Exception e) {
170             throw new SharepointException(e);
171         }
172     }
173 
174     protected void vtiInfHtml(HttpServletResponse response) throws Exception {
175         StringBuilder sb = new StringBuilder();
176 
177         sb.append("<!-- FrontPage Configuration Information");
178         sb.append(StringPool.NEW_LINE);
179         sb.append(" FPVersion=\"6.0.2.9999\"");
180         sb.append(StringPool.NEW_LINE);
181         sb.append("FPShtmlScriptUrl=\"_vti_bin/shtml.dll/_vti_rpc\"");
182         sb.append(StringPool.NEW_LINE);
183         sb.append("FPAuthorScriptUrl=\"_vti_bin/_vti_aut/author.dll\"");
184         sb.append(StringPool.NEW_LINE);
185         sb.append("FPAdminScriptUrl=\"_vti_bin/_vti_adm/admin.dll\"");
186         sb.append(StringPool.NEW_LINE);
187         sb.append("TPScriptUrl=\"_vti_bin/owssvr.dll\"");
188         sb.append(StringPool.NEW_LINE);
189         sb.append("-->");
190 
191         ServletResponseUtil.write(response, sb.toString());
192     }
193 
194     private static Log _log = LogFactoryUtil.getLog(SharepointServlet.class);
195 
196 }