1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }