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.ContentTypes;
022    import com.liferay.portal.kernel.util.StringBundler;
023    
024    import javax.servlet.http.HttpServlet;
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    /**
029     * @author Bruno Farache
030     */
031    public class SharepointWebServicesServlet extends HttpServlet {
032    
033            @Override
034            protected void doPost(
035                    HttpServletRequest request, HttpServletResponse response) {
036    
037                    if (_log.isInfoEnabled()) {
038                            _log.info(
039                                    request.getHeader(HttpHeaders.USER_AGENT) + " " +
040                                            request.getMethod() + " " + request.getRequestURI());
041                    }
042    
043                    try {
044                            String uri = request.getRequestURI();
045    
046                            if (uri.equals("/_vti_bin/webs.asmx")) {
047                                    vtiBinWebsAsmx(request, response);
048                            }
049                    }
050                    catch (Exception e) {
051                            _log.error(e, e);
052                    }
053            }
054    
055            protected void vtiBinWebsAsmx(
056                            HttpServletRequest request, HttpServletResponse response)
057                    throws Exception {
058    
059                    StringBundler sb = new StringBundler(12);
060    
061                    String url =
062                            "http://" + request.getLocalAddr() + ":" + request.getServerPort() +
063                                    "/sharepoint";
064    
065                    sb.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"");
066                    sb.append("http://schemas.xmlsoap.org/soap/envelope/\">");
067                    sb.append("<SOAP-ENV:Header/>");
068                    sb.append("<SOAP-ENV:Body>");
069                    sb.append("<WebUrlFromPageUrlResponse xmlns=\"");
070                    sb.append("http://schemas.microsoft.com/sharepoint/soap/\">");
071                    sb.append("<WebUrlFromPageUrlResult>");
072                    sb.append(url);
073                    sb.append("</WebUrlFromPageUrlResult>");
074                    sb.append("</WebUrlFromPageUrlResponse>");
075                    sb.append("</SOAP-ENV:Body>");
076                    sb.append("</SOAP-ENV:Envelope>");
077    
078                    response.setContentType(ContentTypes.TEXT_XML_UTF8);
079    
080                    ServletResponseUtil.write(response, sb.toString());
081            }
082    
083            private static Log _log = LogFactoryUtil.getLog(
084                    SharepointWebServicesServlet.class);
085    
086    }