001    /**
002     * Copyright (c) 2000-2010 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.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.FileUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.sharepoint.Property;
021    import com.liferay.portal.sharepoint.ResponseElement;
022    import com.liferay.portal.sharepoint.SharepointRequest;
023    import com.liferay.portal.sharepoint.SharepointStorage;
024    import com.liferay.portal.sharepoint.Tree;
025    import com.liferay.util.servlet.ServletResponseUtil;
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            public String getRootPath(SharepointRequest sharepointRequest) {
042                    return sharepointRequest.getParameterValue("document_name");
043            }
044    
045            protected void doProcess(SharepointRequest sharepointRequest)
046                    throws Exception {
047    
048                    SharepointStorage storage = sharepointRequest.getSharepointStorage();
049    
050                    StringBuilder sb = getResponseBuffer(sharepointRequest);
051    
052                    sb.append(StringPool.NEW_LINE);
053    
054                    InputStream is = storage.getDocumentInputStream(sharepointRequest);
055    
056                    byte[] bytes = ArrayUtil.append(
057                            sb.toString().getBytes(), FileUtil.getBytes(is));
058    
059                    ServletResponseUtil.write(
060                            sharepointRequest.getHttpServletResponse(), bytes);
061            }
062    
063            protected List<ResponseElement> getElements(
064                            SharepointRequest sharepointRequest)
065                    throws Exception {
066    
067                    List<ResponseElement> elements = new ArrayList<ResponseElement>();
068    
069                    SharepointStorage storage = sharepointRequest.getSharepointStorage();
070    
071                    elements.add(new Property("message", StringPool.BLANK));
072    
073                    Tree documentTree = storage.getDocumentTree(sharepointRequest);
074    
075                    Property documentProperty = new Property("document", documentTree);
076    
077                    elements.add(documentProperty);
078    
079                    return elements;
080            }
081    
082            private static final String _METHOD_NAME = "get document";
083    
084    }