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.stripOfficeExtension(rootPath);
091                                    rootPath = SharepointUtil.stripService(rootPath, true);
092    
093                                    if (_log.isInfoEnabled()) {
094                                            _log.info("Modified root path " + rootPath);
095                                    }
096    
097                                    sharepointRequest.setRootPath(rootPath);
098    
099                                    SharepointStorage storage = SharepointUtil.getStorage(rootPath);
100    
101                                    sharepointRequest.setSharepointStorage(storage);
102    
103                                    if (_log.isInfoEnabled()) {
104                                            _log.info(
105                                                    request.getHeader(HttpHeaders.USER_AGENT) + " " +
106                                                            method.getMethodName() + " " + uri + " " +
107                                                                    rootPath);
108                                    }
109    
110                                    method.process(sharepointRequest);
111                            }
112                            else {
113                                    if (_log.isInfoEnabled()) {
114                                            _log.info(
115                                                    request.getHeader(HttpHeaders.USER_AGENT) + " " +
116                                                            request.getMethod() + " " + uri);
117                                    }
118                            }
119                    }
120                    catch (SharepointException se) {
121                            _log.error(se, se);
122                    }
123            }
124    
125            protected void vtiInfHtml(HttpServletResponse response) throws Exception {
126                    StringBundler sb = new StringBundler(13);
127    
128                    sb.append("<!-- FrontPage Configuration Information");
129                    sb.append(StringPool.NEW_LINE);
130                    sb.append(" FPVersion=\"6.0.2.9999\"");
131                    sb.append(StringPool.NEW_LINE);
132                    sb.append("FPShtmlScriptUrl=\"_vti_bin/shtml.dll/_vti_rpc\"");
133                    sb.append(StringPool.NEW_LINE);
134                    sb.append("FPAuthorScriptUrl=\"_vti_bin/_vti_aut/author.dll\"");
135                    sb.append(StringPool.NEW_LINE);
136                    sb.append("FPAdminScriptUrl=\"_vti_bin/_vti_adm/admin.dll\"");
137                    sb.append(StringPool.NEW_LINE);
138                    sb.append("TPScriptUrl=\"_vti_bin/owssvr.dll\"");
139                    sb.append(StringPool.NEW_LINE);
140                    sb.append("-->");
141    
142                    ServletResponseUtil.write(response, sb.toString());
143            }
144    
145            private static Log _log = LogFactoryUtil.getLog(SharepointServlet.class);
146    
147    }