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