1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.sharepoint.methods;
24  
25  import com.liferay.portal.kernel.util.ArrayUtil;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.sharepoint.Property;
29  import com.liferay.portal.sharepoint.ResponseElement;
30  import com.liferay.portal.sharepoint.SharepointRequest;
31  import com.liferay.portal.sharepoint.SharepointStorage;
32  import com.liferay.portal.sharepoint.Tree;
33  import com.liferay.util.servlet.ServletResponseUtil;
34  
35  import java.io.InputStream;
36  
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  /**
41   * <a href="GetDocumentMethodImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Bruno Farache
44   *
45   */
46  public class GetDocumentMethodImpl extends BaseMethodImpl {
47  
48      public String getMethodName() {
49          return _METHOD_NAME;
50      }
51  
52      public String getRootPath(SharepointRequest sharepointRequest) {
53          return sharepointRequest.getParameterValue("document_name");
54      }
55  
56      protected void doProcess(SharepointRequest sharepointRequest)
57          throws Exception {
58  
59          SharepointStorage storage = sharepointRequest.getSharepointStorage();
60  
61          StringBuilder sb = getResponseBuffer(sharepointRequest);
62  
63          sb.append(StringPool.NEW_LINE);
64  
65          InputStream is = storage.getDocumentInputStream(sharepointRequest);
66  
67          byte[] bytes = ArrayUtil.append(
68              sb.toString().getBytes(), FileUtil.getBytes(is));
69  
70          ServletResponseUtil.write(
71              sharepointRequest.getHttpServletResponse(), bytes);
72      }
73  
74      protected List<ResponseElement> getElements(
75              SharepointRequest sharepointRequest)
76          throws Exception {
77  
78          List<ResponseElement> elements = new ArrayList<ResponseElement>();
79  
80          SharepointStorage storage = sharepointRequest.getSharepointStorage();
81  
82          elements.add(new Property("message", StringPool.BLANK));
83  
84          Tree documentTree = storage.getDocumentTree(sharepointRequest);
85  
86          Property documentProperty = new Property("document", documentTree);
87  
88          elements.add(documentProperty);
89  
90          return elements;
91      }
92  
93      private static final String _METHOD_NAME = "get document";
94  
95  }