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.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            public String getMethodName() {
038                    return _METHOD_NAME;
039            }
040    
041            @Override
042            public String getRootPath(SharepointRequest sharepointRequest) {
043                    return sharepointRequest.getParameterValue("document_name");
044            }
045    
046            @Override
047            protected void doProcess(SharepointRequest sharepointRequest)
048                    throws Exception {
049    
050                    SharepointStorage storage = sharepointRequest.getSharepointStorage();
051    
052                    StringBuilder sb = getResponseBuffer(sharepointRequest);
053    
054                    sb.append(StringPool.NEW_LINE);
055    
056                    InputStream is = storage.getDocumentInputStream(sharepointRequest);
057    
058                    byte[] bytes = ArrayUtil.append(
059                            sb.toString().getBytes(), FileUtil.getBytes(is));
060    
061                    ServletResponseUtil.write(
062                            sharepointRequest.getHttpServletResponse(), bytes);
063            }
064    
065            @Override
066            protected List<ResponseElement> getElements(
067                            SharepointRequest sharepointRequest)
068                    throws Exception {
069    
070                    List<ResponseElement> elements = new ArrayList<ResponseElement>();
071    
072                    SharepointStorage storage = sharepointRequest.getSharepointStorage();
073    
074                    elements.add(new Property("message", StringPool.BLANK));
075    
076                    Tree documentTree = storage.getDocumentTree(sharepointRequest);
077    
078                    Property documentProperty = new Property("document", documentTree);
079    
080                    elements.add(documentProperty);
081    
082                    return elements;
083            }
084    
085            private static final String _METHOD_NAME = "get document";
086    
087    }