001    /**
002     * Copyright (c) 2000-2012 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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.HttpUtil;
024    import com.liferay.portal.kernel.util.StreamUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.sharepoint.methods.Method;
029    import com.liferay.portal.sharepoint.methods.MethodFactory;
030    import com.liferay.portal.util.WebKeys;
031    
032    import java.io.ByteArrayInputStream;
033    import java.io.InputStream;
034    import java.io.InputStreamReader;
035    
036    import javax.servlet.http.HttpServlet;
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    /**
041     * @author Bruno Farache
042     */
043    public class SharepointServlet extends HttpServlet {
044    
045            @Override
046            public void doGet(
047                    HttpServletRequest request, HttpServletResponse response) {
048    
049                    try {
050                            String uri = request.getRequestURI();
051    
052                            if (uri.equals("/_vti_inf.html")) {
053                                    vtiInfHtml(response);
054                            }
055                    }
056                    catch (Exception e) {
057                            _log.error(e, e);
058                    }
059            }
060    
061            @Override
062            public void doPost(
063                    HttpServletRequest request, HttpServletResponse response) {
064    
065                    try {
066                            String uri = request.getRequestURI();
067    
068                            if (uri.equals("/_vti_bin/shtml.dll/_vti_rpc") ||
069                                    uri.equals("/sharepoint/_vti_bin/_vti_aut/author.dll")) {
070    
071                                    User user = (User)request.getSession().getAttribute(
072                                            WebKeys.USER);
073    
074                                    SharepointRequest sharepointRequest = new SharepointRequest(
075                                            request, response, user);
076    
077                                    addParams(request, sharepointRequest);
078    
079                                    Method method = MethodFactory.create(sharepointRequest);
080    
081                                    String rootPath = method.getRootPath(sharepointRequest);
082    
083                                    if (rootPath == null) {
084                                            throw new SharepointException("Unabled to get root path");
085                                    }
086    
087                                    int pos = rootPath.lastIndexOf("sharepoint/");
088    
089                                    if (pos != -1) {
090                                            rootPath = rootPath.substring(pos + 11);
091                                    }
092    
093                                    // LPS-12922
094    
095                                    pos = rootPath.lastIndexOf("webdav/");
096    
097                                    if (pos != -1) {
098                                            rootPath = rootPath.substring(pos + 7);
099                                    }
100    
101                                    sharepointRequest.setRootPath(rootPath);
102    
103                                    SharepointStorage storage = SharepointUtil.getStorage(rootPath);
104    
105                                    sharepointRequest.setSharepointStorage(storage);
106    
107                                    method.process(sharepointRequest);
108                            }
109                    }
110                    catch (SharepointException se) {
111                            _log.error(se, se);
112                    }
113            }
114    
115            protected void addParams(
116                            HttpServletRequest request, SharepointRequest sharepointRequest)
117                    throws SharepointException {
118    
119                    String contentType = request.getContentType();
120    
121                    if (!contentType.equals(SharepointUtil.VEERMER_URLENCODED)) {
122                            return;
123                    }
124    
125                    try {
126                            InputStream is = request.getInputStream();
127    
128                            UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
129                                    new UnsyncByteArrayOutputStream();
130    
131                            StreamUtil.transfer(is, unsyncByteArrayOutputStream);
132    
133                            byte[] bytes = unsyncByteArrayOutputStream.toByteArray();
134    
135                            UnsyncBufferedReader unsyncBufferedReader =
136                                    new UnsyncBufferedReader(
137                                            new InputStreamReader(new ByteArrayInputStream(bytes)));
138    
139                            String url = unsyncBufferedReader.readLine();
140    
141                            String[] params = url.split(StringPool.AMPERSAND);
142    
143                            for (String param : params) {
144                                    String[] kvp = param.split(StringPool.EQUAL);
145    
146                                    String key = HttpUtil.decodeURL(kvp[0]);
147                                    String value = StringPool.BLANK;
148    
149                                    if (kvp.length > 1) {
150                                            value = HttpUtil.decodeURL(kvp[1]);
151                                    }
152    
153                                    sharepointRequest.addParam(key, value);
154                            }
155    
156                            bytes = ArrayUtil.subset(bytes, url.length() + 1, bytes.length);
157    
158                            sharepointRequest.setBytes(bytes);
159                    }
160                    catch (Exception e) {
161                            throw new SharepointException(e);
162                    }
163            }
164    
165            protected void vtiInfHtml(HttpServletResponse response) throws Exception {
166                    StringBundler sb = new StringBundler(13);
167    
168                    sb.append("<!-- FrontPage Configuration Information");
169                    sb.append(StringPool.NEW_LINE);
170                    sb.append(" FPVersion=\"6.0.2.9999\"");
171                    sb.append(StringPool.NEW_LINE);
172                    sb.append("FPShtmlScriptUrl=\"_vti_bin/shtml.dll/_vti_rpc\"");
173                    sb.append(StringPool.NEW_LINE);
174                    sb.append("FPAuthorScriptUrl=\"_vti_bin/_vti_aut/author.dll\"");
175                    sb.append(StringPool.NEW_LINE);
176                    sb.append("FPAdminScriptUrl=\"_vti_bin/_vti_adm/admin.dll\"");
177                    sb.append(StringPool.NEW_LINE);
178                    sb.append("TPScriptUrl=\"_vti_bin/owssvr.dll\"");
179                    sb.append(StringPool.NEW_LINE);
180                    sb.append("-->");
181    
182                    ServletResponseUtil.write(response, sb.toString());
183            }
184    
185            private static Log _log = LogFactoryUtil.getLog(SharepointServlet.class);
186    
187    }