1   /**
2    * Copyright (c) 2000-2008 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.webdav.methods;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.InstancePool;
27  import com.liferay.portal.util.PropsUtil;
28  import com.liferay.portal.webdav.WebDAVException;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import javax.servlet.http.HttpServletRequest;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  /**
39   * <a href="MethodFactory.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class MethodFactory {
45  
46      public static Method create(HttpServletRequest request)
47          throws WebDAVException {
48  
49          return _instance._create(request);
50      }
51  
52      private MethodFactory() {
53          _methods = new HashMap<String, Object>();
54  
55          if (_log.isDebugEnabled()) {
56              _log.debug("Delete method implementation " + _DELETE_METHOD_IMPL);
57          }
58  
59          _methods.put("COPY", InstancePool.get(_COPY_METHOD_IMPL));
60          _methods.put("DELETE", InstancePool.get(_DELETE_METHOD_IMPL));
61          _methods.put("GET", InstancePool.get(_GET_METHOD_IMPL));
62          _methods.put("HEAD", InstancePool.get(_HEAD_METHOD_IMPL));
63          _methods.put("MKCOL", InstancePool.get(_MKCOL_METHOD_IMPL));
64          _methods.put("MOVE", InstancePool.get(_MOVE_METHOD_IMPL));
65          _methods.put("OPTIONS", InstancePool.get(_OPTIONS_METHOD_IMPL));
66          _methods.put("PROPFIND", InstancePool.get(_PROPFIND_METHOD_IMPL));
67          _methods.put("PROPPATCH", InstancePool.get(_PROPPATCH_METHOD_IMPL));
68          _methods.put("PUT", InstancePool.get(_PUT_METHOD_IMPL));
69      }
70  
71      private Method _create(HttpServletRequest request) throws WebDAVException {
72          String method = request.getMethod();
73  
74          if (_log.isDebugEnabled()) {
75              _log.debug("Get method " + method);
76          }
77  
78          Method methodImpl = (Method)_methods.get(method.toUpperCase());
79  
80          if (methodImpl == null) {
81              throw new WebDAVException(
82                  "Method " + method + " is not implemented");
83          }
84          else {
85              if (_log.isDebugEnabled()) {
86                  _log.debug(
87                      "Method " + method + " is mapped to " +
88                          methodImpl.getClass().getName());
89              }
90          }
91  
92          return methodImpl;
93      }
94  
95      private static final String _COPY_METHOD_IMPL = GetterUtil.getString(
96          PropsUtil.get(MethodFactory.class.getName() + ".COPY"),
97          CopyMethodImpl.class.getName());
98  
99      private static final String _DELETE_METHOD_IMPL = GetterUtil.getString(
100         PropsUtil.get(MethodFactory.class.getName() + ".DELETE"),
101         DeleteMethodImpl.class.getName());
102 
103     private static final String _GET_METHOD_IMPL = GetterUtil.getString(
104         PropsUtil.get(MethodFactory.class.getName() + ".GET"),
105         GetMethodImpl.class.getName());
106 
107     private static final String _HEAD_METHOD_IMPL = GetterUtil.getString(
108         PropsUtil.get(MethodFactory.class.getName() + ".HEAD"),
109         HeadMethodImpl.class.getName());
110 
111     private static final String _MKCOL_METHOD_IMPL = GetterUtil.getString(
112         PropsUtil.get(MethodFactory.class.getName() + ".MKCOL"),
113         MkcolMethodImpl.class.getName());
114 
115     private static final String _MOVE_METHOD_IMPL = GetterUtil.getString(
116         PropsUtil.get(MethodFactory.class.getName() + ".MOVE"),
117         MoveMethodImpl.class.getName());
118 
119     private static final String _OPTIONS_METHOD_IMPL = GetterUtil.getString(
120         PropsUtil.get(MethodFactory.class.getName() + ".OPTIONS"),
121         OptionsMethodImpl.class.getName());
122 
123     private static final String _PROPFIND_METHOD_IMPL = GetterUtil.getString(
124         PropsUtil.get(MethodFactory.class.getName() + ".PROPFIND"),
125         PropfindMethodImpl.class.getName());
126 
127     private static final String _PROPPATCH_METHOD_IMPL = GetterUtil.getString(
128         PropsUtil.get(MethodFactory.class.getName() + ".PROPPATCH"),
129         ProppatchMethodImpl.class.getName());
130 
131     private static final String _PUT_METHOD_IMPL = GetterUtil.getString(
132         PropsUtil.get(MethodFactory.class.getName() + ".PUT"),
133         PutMethodImpl.class.getName());
134 
135     private static Log _log = LogFactory.getLog(MethodFactory.class);
136 
137     private static MethodFactory _instance = new MethodFactory();
138 
139     private Map<String, Object> _methods;
140 
141 }