001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.util.HttpUtil;
022    import com.liferay.portal.kernel.util.StreamUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.sharepoint.methods.Method;
027    import com.liferay.portal.sharepoint.methods.MethodFactory;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.util.servlet.ServletResponseUtil;
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            public void doGet(
044                    HttpServletRequest request,     HttpServletResponse response) {
045    
046                    try {
047                            String uri = request.getRequestURI();
048    
049                            if (uri.equals("/_vti_inf.html")) {
050                                    vtiInfHtml(response);
051                            }
052                    }
053                    catch (Exception e) {
054                            _log.error(e, e);
055                    }
056            }
057    
058            public void doPost(
059                    HttpServletRequest request, HttpServletResponse response) {
060    
061                    try {
062                            String uri = request.getRequestURI();
063    
064                            if (uri.equals("/_vti_bin/shtml.dll/_vti_rpc") ||
065                                    uri.equals("/sharepoint/_vti_bin/_vti_aut/author.dll")) {
066    
067                                    User user = (User)request.getSession().getAttribute(
068                                            WebKeys.USER);
069    
070                                    SharepointRequest sharepointRequest = new SharepointRequest(
071                                            request, response, user);
072    
073                                    addParams(request, sharepointRequest);
074    
075                                    Method method = MethodFactory.create(sharepointRequest);
076    
077                                    String rootPath = method.getRootPath(sharepointRequest);
078    
079                                    sharepointRequest.setRootPath(rootPath);
080    
081                                    SharepointStorage storage = SharepointUtil.getStorage(rootPath);
082    
083                                    sharepointRequest.setSharepointStorage(storage);
084    
085                                    method.process(sharepointRequest);
086                            }
087                    }
088                    catch (SharepointException se) {
089                            _log.error(se, se);
090                    }
091            }
092    
093            protected void addParams(
094                            HttpServletRequest request, SharepointRequest sharepointRequest)
095                    throws SharepointException {
096    
097                    String contentType = request.getContentType();
098    
099                    if (!contentType.equals(SharepointUtil.VEERMER_URLENCODED)) {
100                            return;
101                    }
102    
103                    try {
104                            InputStream is = request.getInputStream();
105    
106                            UnsyncBufferedReader unsyncBufferedReader =
107                                    new UnsyncBufferedReader(new InputStreamReader(is));
108    
109                            String url = unsyncBufferedReader.readLine();
110    
111                            String[] params = url.split(StringPool.AMPERSAND);
112    
113                            for (String param : params) {
114                                    String[] kvp = param.split(StringPool.EQUAL);
115    
116                                    String key = HttpUtil.decodeURL(kvp[0]);
117                                    String value = StringPool.BLANK;
118    
119                                    if (kvp.length > 1) {
120                                            value = HttpUtil.decodeURL(kvp[1]);
121                                    }
122    
123                                    sharepointRequest.addParam(key, value);
124                            }
125    
126                            UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
127                                    new UnsyncByteArrayOutputStream();
128    
129                            StreamUtil.transfer(is, unsyncByteArrayOutputStream);
130    
131                            sharepointRequest.setBytes(
132                                    unsyncByteArrayOutputStream.toByteArray());
133                    }
134                    catch (Exception e) {
135                            throw new SharepointException(e);
136                    }
137            }
138    
139            protected void vtiInfHtml(HttpServletResponse response) throws Exception {
140                    StringBundler sb = new StringBundler(13);
141    
142                    sb.append("<!-- FrontPage Configuration Information");
143                    sb.append(StringPool.NEW_LINE);
144                    sb.append(" FPVersion=\"6.0.2.9999\"");
145                    sb.append(StringPool.NEW_LINE);
146                    sb.append("FPShtmlScriptUrl=\"_vti_bin/shtml.dll/_vti_rpc\"");
147                    sb.append(StringPool.NEW_LINE);
148                    sb.append("FPAuthorScriptUrl=\"_vti_bin/_vti_aut/author.dll\"");
149                    sb.append(StringPool.NEW_LINE);
150                    sb.append("FPAdminScriptUrl=\"_vti_bin/_vti_adm/admin.dll\"");
151                    sb.append(StringPool.NEW_LINE);
152                    sb.append("TPScriptUrl=\"_vti_bin/owssvr.dll\"");
153                    sb.append(StringPool.NEW_LINE);
154                    sb.append("-->");
155    
156                    ServletResponseUtil.write(response, sb.toString());
157            }
158    
159            private static Log _log = LogFactoryUtil.getLog(SharepointServlet.class);
160    
161    }