001    /**
002     * Copyright (c) 2000-2013 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.HttpHeaders;
020    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.webdav.WebDAVUtil;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.sharepoint.methods.Method;
026    import com.liferay.portal.sharepoint.methods.MethodFactory;
027    import com.liferay.portal.util.WebKeys;
028    
029    import javax.servlet.http.HttpServlet;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Bruno Farache
035     */
036    public class SharepointServlet extends HttpServlet {
037    
038            @Override
039            public void doGet(
040                    HttpServletRequest request, HttpServletResponse response) {
041    
042                    if (_log.isInfoEnabled()) {
043                            _log.info(
044                                    request.getHeader(HttpHeaders.USER_AGENT) + " " +
045                                            request.getMethod() + " " + request.getRequestURI());
046                    }
047    
048                    try {
049                            String uri = request.getRequestURI();
050    
051                            if (uri.equals("/_vti_inf.html")) {
052                                    vtiInfHtml(response);
053                            }
054                    }
055                    catch (Exception e) {
056                            _log.error(e, e);
057                    }
058            }
059    
060            @Override
061            public void doPost(
062                    HttpServletRequest request, HttpServletResponse response) {
063    
064                    try {
065                            String uri = request.getRequestURI();
066    
067                            if (uri.equals("/_vti_bin/shtml.dll/_vti_rpc") ||
068                                    uri.equals("/sharepoint/_vti_bin/_vti_aut/author.dll")) {
069    
070                                    User user = (User)request.getSession().getAttribute(
071                                            WebKeys.USER);
072    
073                                    SharepointRequest sharepointRequest = new SharepointRequest(
074                                            request, response, user);
075    
076                                    Method method = MethodFactory.create(sharepointRequest);
077    
078                                    String rootPath = method.getRootPath(sharepointRequest);
079    
080                                    if (rootPath == null) {
081                                            throw new SharepointException("Unabled to get root path");
082                                    }
083    
084                                    // LPS-12922
085    
086                                    if (_log.isInfoEnabled()) {
087                                            _log.info("Original root path " + rootPath);
088                                    }
089    
090                                    rootPath = WebDAVUtil.stripManualCheckInRequiredPath(rootPath);
091                                    rootPath = WebDAVUtil.stripOfficeExtension(rootPath);
092                                    rootPath = SharepointUtil.stripService(rootPath, true);
093    
094                                    if (_log.isInfoEnabled()) {
095                                            _log.info("Modified root path " + rootPath);
096                                    }
097    
098                                    sharepointRequest.setRootPath(rootPath);
099    
100                                    SharepointStorage storage = SharepointUtil.getStorage(rootPath);
101    
102                                    sharepointRequest.setSharepointStorage(storage);
103    
104                                    if (_log.isInfoEnabled()) {
105                                            _log.info(
106                                                    request.getHeader(HttpHeaders.USER_AGENT) + " " +
107                                                            method.getMethodName() + " " + uri + " " +
108                                                                    rootPath);
109                                    }
110    
111                                    method.process(sharepointRequest);
112                            }
113                            else {
114                                    if (_log.isInfoEnabled()) {
115                                            _log.info(
116                                                    request.getHeader(HttpHeaders.USER_AGENT) + " " +
117                                                            request.getMethod() + " " + uri);
118                                    }
119                            }
120                    }
121                    catch (SharepointException se) {
122                            _log.error(se, se);
123                    }
124            }
125    
126            protected void vtiInfHtml(HttpServletResponse response) throws Exception {
127                    StringBundler sb = new StringBundler(13);
128    
129                    sb.append("<!-- FrontPage Configuration Information");
130                    sb.append(StringPool.NEW_LINE);
131                    sb.append(" FPVersion=\"6.0.2.9999\"");
132                    sb.append(StringPool.NEW_LINE);
133                    sb.append("FPShtmlScriptUrl=\"_vti_bin/shtml.dll/_vti_rpc\"");
134                    sb.append(StringPool.NEW_LINE);
135                    sb.append("FPAuthorScriptUrl=\"_vti_bin/_vti_aut/author.dll\"");
136                    sb.append(StringPool.NEW_LINE);
137                    sb.append("FPAdminScriptUrl=\"_vti_bin/_vti_adm/admin.dll\"");
138                    sb.append(StringPool.NEW_LINE);
139                    sb.append("TPScriptUrl=\"_vti_bin/owssvr.dll\"");
140                    sb.append(StringPool.NEW_LINE);
141                    sb.append("-->");
142    
143                    ServletResponseUtil.write(response, sb.toString());
144            }
145    
146            private static Log _log = LogFactoryUtil.getLog(SharepointServlet.class);
147    
148    }