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.webdav.methods;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.webdav.Resource;
20  import com.liferay.portal.webdav.WebDAVException;
21  import com.liferay.portal.webdav.WebDAVRequest;
22  import com.liferay.portal.webdav.WebDAVStorage;
23  import com.liferay.util.servlet.ServletResponseUtil;
24  
25  import java.io.InputStream;
26  
27  import javax.servlet.http.HttpServletResponse;
28  
29  /**
30   * <a href="GetMethodImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   * @author Alexander Chow
34   */
35  public class GetMethodImpl implements Method {
36  
37      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
38          InputStream is = null;
39  
40          try {
41              WebDAVStorage storage = webDavRequest.getWebDAVStorage();
42              HttpServletResponse response =
43                  webDavRequest.getHttpServletResponse();
44  
45              Resource resource = storage.getResource(webDavRequest);
46  
47              if (resource != null) {
48                  try {
49                      is = resource.getContentAsStream();
50                  }
51                  catch (Exception e) {
52                      if (_log.isErrorEnabled()) {
53                          _log.error(e.getMessage());
54                      }
55                  }
56              }
57  
58              int status = HttpServletResponse.SC_NOT_FOUND;
59  
60              if (is != null) {
61                  try {
62                      response.setContentType(resource.getContentType());
63  
64                      ServletResponseUtil.write(response, is);
65                  }
66                  catch (Exception e) {
67                      if (_log.isWarnEnabled()) {
68                          _log.warn(e);
69                      }
70                  }
71  
72                  status = HttpServletResponse.SC_OK;
73              }
74  
75              return status;
76          }
77          catch (Exception e) {
78              throw new WebDAVException(e);
79          }
80      }
81  
82      private static Log _log = LogFactoryUtil.getLog(GetMethodImpl.class);
83  
84  }