1   /**
2    * Copyright (c) 2000-2009 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  /**
36   * <a href="MethodFactory.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class MethodFactory {
42  
43      public static Method create(HttpServletRequest request)
44          throws WebDAVException {
45  
46          return _instance._create(request);
47      }
48  
49      private MethodFactory() {
50          _methods = new HashMap<String, Object>();
51  
52          _methods.put("COPY", InstancePool.get(_COPY_METHOD_IMPL));
53          _methods.put("DELETE", InstancePool.get(_DELETE_METHOD_IMPL));
54          _methods.put("GET", InstancePool.get(_GET_METHOD_IMPL));
55          _methods.put("HEAD", InstancePool.get(_HEAD_METHOD_IMPL));
56          _methods.put("LOCK", InstancePool.get(_LOCK_METHOD_IMPL));
57          _methods.put("MKCOL", InstancePool.get(_MKCOL_METHOD_IMPL));
58          _methods.put("MOVE", InstancePool.get(_MOVE_METHOD_IMPL));
59          _methods.put("OPTIONS", InstancePool.get(_OPTIONS_METHOD_IMPL));
60          _methods.put("PROPFIND", InstancePool.get(_PROPFIND_METHOD_IMPL));
61          _methods.put("PROPPATCH", InstancePool.get(_PROPPATCH_METHOD_IMPL));
62          _methods.put("PUT", InstancePool.get(_PUT_METHOD_IMPL));
63          _methods.put("UNLOCK", InstancePool.get(_UNLOCK_METHOD_IMPL));
64      }
65  
66      private Method _create(HttpServletRequest request) throws WebDAVException {
67          String method = request.getMethod();
68  
69          Method methodImpl = (Method)_methods.get(method.toUpperCase());
70  
71          if (methodImpl == null) {
72              throw new WebDAVException(
73                  "Method " + method + " is not implemented");
74          }
75  
76          return methodImpl;
77      }
78  
79      private static final String _COPY_METHOD_IMPL = GetterUtil.getString(
80          PropsUtil.get(MethodFactory.class.getName() + ".COPY"),
81          CopyMethodImpl.class.getName());
82  
83      private static final String _DELETE_METHOD_IMPL = GetterUtil.getString(
84          PropsUtil.get(MethodFactory.class.getName() + ".DELETE"),
85          DeleteMethodImpl.class.getName());
86  
87      private static final String _GET_METHOD_IMPL = GetterUtil.getString(
88          PropsUtil.get(MethodFactory.class.getName() + ".GET"),
89          GetMethodImpl.class.getName());
90  
91      private static final String _HEAD_METHOD_IMPL = GetterUtil.getString(
92          PropsUtil.get(MethodFactory.class.getName() + ".HEAD"),
93          HeadMethodImpl.class.getName());
94  
95      private static final String _LOCK_METHOD_IMPL = GetterUtil.getString(
96          PropsUtil.get(MethodFactory.class.getName() + ".LOCK"),
97          LockMethodImpl.class.getName());
98  
99      private static final String _MKCOL_METHOD_IMPL = GetterUtil.getString(
100         PropsUtil.get(MethodFactory.class.getName() + ".MKCOL"),
101         MkcolMethodImpl.class.getName());
102 
103     private static final String _MOVE_METHOD_IMPL = GetterUtil.getString(
104         PropsUtil.get(MethodFactory.class.getName() + ".MOVE"),
105         MoveMethodImpl.class.getName());
106 
107     private static final String _OPTIONS_METHOD_IMPL = GetterUtil.getString(
108         PropsUtil.get(MethodFactory.class.getName() + ".OPTIONS"),
109         OptionsMethodImpl.class.getName());
110 
111     private static final String _PROPFIND_METHOD_IMPL = GetterUtil.getString(
112         PropsUtil.get(MethodFactory.class.getName() + ".PROPFIND"),
113         PropfindMethodImpl.class.getName());
114 
115     private static final String _PROPPATCH_METHOD_IMPL = GetterUtil.getString(
116         PropsUtil.get(MethodFactory.class.getName() + ".PROPPATCH"),
117         ProppatchMethodImpl.class.getName());
118 
119     private static final String _PUT_METHOD_IMPL = GetterUtil.getString(
120         PropsUtil.get(MethodFactory.class.getName() + ".PUT"),
121         PutMethodImpl.class.getName());
122 
123     private static final String _UNLOCK_METHOD_IMPL = GetterUtil.getString(
124         PropsUtil.get(MethodFactory.class.getName() + ".UNLOCK"),
125         UnlockMethodImpl.class.getName());
126 
127     private static MethodFactory _instance = new MethodFactory();
128 
129     private Map<String, Object> _methods;
130 
131 }