1   /**
2    * Copyright (c) 2000-2007 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  import com.liferay.util.CollectionFactory;
30  
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 req)
47          throws WebDAVException {
48  
49          return _instance._create(req);
50      }
51  
52      private MethodFactory() {
53          _methods = CollectionFactory.getHashMap();
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("PUT", InstancePool.get(_PUT_METHOD_IMPL));
68      }
69  
70      private Method _create(HttpServletRequest req) throws WebDAVException {
71          String method = req.getMethod();
72  
73          if (_log.isDebugEnabled()) {
74              _log.debug("Get method " + method);
75          }
76  
77          Method methodImpl = (Method)_methods.get(method.toUpperCase());
78  
79          if (methodImpl == null) {
80              throw new WebDAVException(
81                  "Method " + method + " is not implemented");
82          }
83          else {
84              if (_log.isDebugEnabled()) {
85                  _log.debug(
86                      "Method " + method + " is mapped to " +
87                          methodImpl.getClass().getName());
88              }
89          }
90  
91          return methodImpl;
92      }
93  
94      private static final String _COPY_METHOD_IMPL = GetterUtil.getString(
95          PropsUtil.get(MethodFactory.class.getName() + ".COPY"),
96          CopyMethodImpl.class.getName());
97  
98      private static final String _DELETE_METHOD_IMPL = GetterUtil.getString(
99          PropsUtil.get(MethodFactory.class.getName() + ".DELETE"),
100         DeleteMethodImpl.class.getName());
101 
102     private static final String _GET_METHOD_IMPL = GetterUtil.getString(
103         PropsUtil.get(MethodFactory.class.getName() + ".GET"),
104         GetMethodImpl.class.getName());
105 
106     private static final String _HEAD_METHOD_IMPL = GetterUtil.getString(
107         PropsUtil.get(MethodFactory.class.getName() + ".HEAD"),
108         HeadMethodImpl.class.getName());
109 
110     private static final String _MKCOL_METHOD_IMPL = GetterUtil.getString(
111         PropsUtil.get(MethodFactory.class.getName() + ".MKCOL"),
112         MkcolMethodImpl.class.getName());
113 
114     private static final String _MOVE_METHOD_IMPL = GetterUtil.getString(
115         PropsUtil.get(MethodFactory.class.getName() + ".MOVE"),
116         MoveMethodImpl.class.getName());
117 
118     private static final String _OPTIONS_METHOD_IMPL = GetterUtil.getString(
119         PropsUtil.get(MethodFactory.class.getName() + ".OPTIONS"),
120         OptionsMethodImpl.class.getName());
121 
122     private static final String _PROPFIND_METHOD_IMPL = GetterUtil.getString(
123         PropsUtil.get(MethodFactory.class.getName() + ".PROPFIND"),
124         PropfindMethodImpl.class.getName());
125 
126     private static final String _PUT_METHOD_IMPL = GetterUtil.getString(
127         PropsUtil.get(MethodFactory.class.getName() + ".PUT"),
128         PutMethodImpl.class.getName());
129 
130     private static Log _log = LogFactory.getLog(MethodFactory.class);
131 
132     private static MethodFactory _instance = new MethodFactory();
133 
134     private Map _methods;
135 
136 }