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.methods;
016    
017    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.sharepoint.Property;
022    import com.liferay.portal.sharepoint.ResponseElement;
023    import com.liferay.portal.sharepoint.SharepointRequest;
024    import com.liferay.portal.sharepoint.SharepointStorage;
025    import com.liferay.portal.sharepoint.Tree;
026    
027    import java.io.InputStream;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * @author Bruno Farache
034     */
035    public class GetDocumentMethodImpl extends BaseMethodImpl {
036    
037            @Override
038            public String getMethodName() {
039                    return _METHOD_NAME;
040            }
041    
042            @Override
043            public String getRootPath(SharepointRequest sharepointRequest) {
044                    return sharepointRequest.getParameterValue("document_name");
045            }
046    
047            @Override
048            protected void doProcess(SharepointRequest sharepointRequest)
049                    throws Exception {
050    
051                    SharepointStorage storage = sharepointRequest.getSharepointStorage();
052    
053                    String html = getResponse(sharepointRequest, true);
054    
055                    InputStream is = storage.getDocumentInputStream(sharepointRequest);
056    
057                    byte[] bytes = ArrayUtil.append(html.getBytes(), FileUtil.getBytes(is));
058    
059                    ServletResponseUtil.write(
060                            sharepointRequest.getHttpServletResponse(), bytes);
061            }
062    
063            @Override
064            protected List<ResponseElement> getElements(
065                            SharepointRequest sharepointRequest)
066                    throws Exception {
067    
068                    List<ResponseElement> elements = new ArrayList<ResponseElement>();
069    
070                    SharepointStorage storage = sharepointRequest.getSharepointStorage();
071    
072                    elements.add(new Property("message", StringPool.BLANK));
073    
074                    Tree documentTree = storage.getDocumentTree(sharepointRequest);
075    
076                    Property documentProperty = new Property("document", documentTree);
077    
078                    elements.add(documentProperty);
079    
080                    return elements;
081            }
082    
083            private static final String _METHOD_NAME = "get document";
084    
085    }