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("LOCK", InstancePool.get(_LOCK_METHOD_IMPL));
64          _methods.put("MKCOL", InstancePool.get(_MKCOL_METHOD_IMPL));
65          _methods.put("MOVE", InstancePool.get(_MOVE_METHOD_IMPL));
66          _methods.put("OPTIONS", InstancePool.get(_OPTIONS_METHOD_IMPL));
67          _methods.put("PROPFIND", InstancePool.get(_PROPFIND_METHOD_IMPL));
68          _methods.put("PROPPATCH", InstancePool.get(_PROPPATCH_METHOD_IMPL));
69          _methods.put("PUT", InstancePool.get(_PUT_METHOD_IMPL));
70          _methods.put("UNLOCK", InstancePool.get(_UNLOCK_METHOD_IMPL));
71      }
72  
73      private Method _create(HttpServletRequest request) throws WebDAVException {
74          String method = request.getMethod();
75  
76          if (_log.isDebugEnabled()) {
77              _log.debug("Get method " + method);
78          }
79  
80          Method methodImpl = (Method)_methods.get(method.toUpperCase());
81  
82          if (methodImpl == null) {
83              throw new WebDAVException(
84                  "Method " + method + " is not implemented");
85          }
86          else {
87              if (_log.isDebugEnabled()) {
88                  _log.debug(
89                      "Method " + method + " is mapped to " +
90                          methodImpl.getClass().getName());
91              }
92          }
93  
94          return methodImpl;
95      }
96  
97      private static final String _COPY_METHOD_IMPL = GetterUtil.getString(
98          PropsUtil.get(MethodFactory.class.getName() + ".COPY"),
99          CopyMethodImpl.class.getName());
100 
101     private static final String _DELETE_METHOD_IMPL = GetterUtil.getString(
102         PropsUtil.get(MethodFactory.class.getName() + ".DELETE"),
103         DeleteMethodImpl.class.getName());
104 
105     private static final String _GET_METHOD_IMPL = GetterUtil.getString(
106         PropsUtil.get(MethodFactory.class.getName() + ".GET"),
107         GetMethodImpl.class.getName());
108 
109     private static final String _HEAD_METHOD_IMPL = GetterUtil.getString(
110         PropsUtil.get(MethodFactory.class.getName() + ".HEAD"),
111         HeadMethodImpl.class.getName());
112 
113     private static final String _LOCK_METHOD_IMPL = GetterUtil.getString(
114         PropsUtil.get(MethodFactory.class.getName() + ".LOCK"),
115         LockMethodImpl.class.getName());
116 
117     private static final String _MKCOL_METHOD_IMPL = GetterUtil.getString(
118         PropsUtil.get(MethodFactory.class.getName() + ".MKCOL"),
119         MkcolMethodImpl.class.getName());
120 
121     private static final String _MOVE_METHOD_IMPL = GetterUtil.getString(
122         PropsUtil.get(MethodFactory.class.getName() + ".MOVE"),
123         MoveMethodImpl.class.getName());
124 
125     private static final String _OPTIONS_METHOD_IMPL = GetterUtil.getString(
126         PropsUtil.get(MethodFactory.class.getName() + ".OPTIONS"),
127         OptionsMethodImpl.class.getName());
128 
129     private static final String _PROPFIND_METHOD_IMPL = GetterUtil.getString(
130         PropsUtil.get(MethodFactory.class.getName() + ".PROPFIND"),
131         PropfindMethodImpl.class.getName());
132 
133     private static final String _PROPPATCH_METHOD_IMPL = GetterUtil.getString(
134         PropsUtil.get(MethodFactory.class.getName() + ".PROPPATCH"),
135         ProppatchMethodImpl.class.getName());
136 
137     private static final String _PUT_METHOD_IMPL = GetterUtil.getString(
138         PropsUtil.get(MethodFactory.class.getName() + ".PUT"),
139         PutMethodImpl.class.getName());
140 
141     private static final String _UNLOCK_METHOD_IMPL = GetterUtil.getString(
142         PropsUtil.get(MethodFactory.class.getName() + ".UNLOCK"),
143         UnlockMethodImpl.class.getName());
144 
145     private static Log _log = LogFactory.getLog(MethodFactory.class);
146 
147     private static MethodFactory _instance = new MethodFactory();
148 
149     private Map<String, Object> _methods;
150 
151 }