1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.sharepoint.methods;
16  
17  import com.liferay.portal.kernel.util.ArrayUtil;
18  import com.liferay.portal.kernel.util.FileUtil;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.sharepoint.Property;
21  import com.liferay.portal.sharepoint.ResponseElement;
22  import com.liferay.portal.sharepoint.SharepointRequest;
23  import com.liferay.portal.sharepoint.SharepointStorage;
24  import com.liferay.portal.sharepoint.Tree;
25  import com.liferay.util.servlet.ServletResponseUtil;
26  
27  import java.io.InputStream;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * <a href="GetDocumentMethodImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Bruno Farache
36   */
37  public class GetDocumentMethodImpl extends BaseMethodImpl {
38  
39      public String getMethodName() {
40          return _METHOD_NAME;
41      }
42  
43      public String getRootPath(SharepointRequest sharepointRequest) {
44          return sharepointRequest.getParameterValue("document_name");
45      }
46  
47      protected void doProcess(SharepointRequest sharepointRequest)
48          throws Exception {
49  
50          SharepointStorage storage = sharepointRequest.getSharepointStorage();
51  
52          StringBuilder sb = getResponseBuffer(sharepointRequest);
53  
54          sb.append(StringPool.NEW_LINE);
55  
56          InputStream is = storage.getDocumentInputStream(sharepointRequest);
57  
58          byte[] bytes = ArrayUtil.append(
59              sb.toString().getBytes(), FileUtil.getBytes(is));
60  
61          ServletResponseUtil.write(
62              sharepointRequest.getHttpServletResponse(), bytes);
63      }
64  
65      protected List<ResponseElement> getElements(
66              SharepointRequest sharepointRequest)
67          throws Exception {
68  
69          List<ResponseElement> elements = new ArrayList<ResponseElement>();
70  
71          SharepointStorage storage = sharepointRequest.getSharepointStorage();
72  
73          elements.add(new Property("message", StringPool.BLANK));
74  
75          Tree documentTree = storage.getDocumentTree(sharepointRequest);
76  
77          Property documentProperty = new Property("document", documentTree);
78  
79          elements.add(documentProperty);
80  
81          return elements;
82      }
83  
84      private static final String _METHOD_NAME = "get document";
85  
86  }