1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.webdav.methods;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.InstancePool;
19  import com.liferay.portal.util.PropsUtil;
20  import com.liferay.portal.webdav.WebDAVException;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  /**
28   * <a href="MethodFactory.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class MethodFactory {
33  
34      public static Method create(HttpServletRequest request)
35          throws WebDAVException {
36  
37          return _instance._create(request);
38      }
39  
40      private MethodFactory() {
41          _methods = new HashMap<String, Object>();
42  
43          _methods.put("COPY", InstancePool.get(_COPY_METHOD_IMPL));
44          _methods.put("DELETE", InstancePool.get(_DELETE_METHOD_IMPL));
45          _methods.put("GET", InstancePool.get(_GET_METHOD_IMPL));
46          _methods.put("HEAD", InstancePool.get(_HEAD_METHOD_IMPL));
47          _methods.put("LOCK", InstancePool.get(_LOCK_METHOD_IMPL));
48          _methods.put("MKCOL", InstancePool.get(_MKCOL_METHOD_IMPL));
49          _methods.put("MOVE", InstancePool.get(_MOVE_METHOD_IMPL));
50          _methods.put("OPTIONS", InstancePool.get(_OPTIONS_METHOD_IMPL));
51          _methods.put("PROPFIND", InstancePool.get(_PROPFIND_METHOD_IMPL));
52          _methods.put("PROPPATCH", InstancePool.get(_PROPPATCH_METHOD_IMPL));
53          _methods.put("PUT", InstancePool.get(_PUT_METHOD_IMPL));
54          _methods.put("UNLOCK", InstancePool.get(_UNLOCK_METHOD_IMPL));
55      }
56  
57      private Method _create(HttpServletRequest request) throws WebDAVException {
58          String method = request.getMethod();
59  
60          Method methodImpl = (Method)_methods.get(method.toUpperCase());
61  
62          if (methodImpl == null) {
63              throw new WebDAVException(
64                  "Method " + method + " is not implemented");
65          }
66  
67          return methodImpl;
68      }
69  
70      private static final String _COPY_METHOD_IMPL = GetterUtil.getString(
71          PropsUtil.get(MethodFactory.class.getName() + ".COPY"),
72          CopyMethodImpl.class.getName());
73  
74      private static final String _DELETE_METHOD_IMPL = GetterUtil.getString(
75          PropsUtil.get(MethodFactory.class.getName() + ".DELETE"),
76          DeleteMethodImpl.class.getName());
77  
78      private static final String _GET_METHOD_IMPL = GetterUtil.getString(
79          PropsUtil.get(MethodFactory.class.getName() + ".GET"),
80          GetMethodImpl.class.getName());
81  
82      private static final String _HEAD_METHOD_IMPL = GetterUtil.getString(
83          PropsUtil.get(MethodFactory.class.getName() + ".HEAD"),
84          HeadMethodImpl.class.getName());
85  
86      private static final String _LOCK_METHOD_IMPL = GetterUtil.getString(
87          PropsUtil.get(MethodFactory.class.getName() + ".LOCK"),
88          LockMethodImpl.class.getName());
89  
90      private static final String _MKCOL_METHOD_IMPL = GetterUtil.getString(
91          PropsUtil.get(MethodFactory.class.getName() + ".MKCOL"),
92          MkcolMethodImpl.class.getName());
93  
94      private static final String _MOVE_METHOD_IMPL = GetterUtil.getString(
95          PropsUtil.get(MethodFactory.class.getName() + ".MOVE"),
96          MoveMethodImpl.class.getName());
97  
98      private static final String _OPTIONS_METHOD_IMPL = GetterUtil.getString(
99          PropsUtil.get(MethodFactory.class.getName() + ".OPTIONS"),
100         OptionsMethodImpl.class.getName());
101 
102     private static final String _PROPFIND_METHOD_IMPL = GetterUtil.getString(
103         PropsUtil.get(MethodFactory.class.getName() + ".PROPFIND"),
104         PropfindMethodImpl.class.getName());
105 
106     private static final String _PROPPATCH_METHOD_IMPL = GetterUtil.getString(
107         PropsUtil.get(MethodFactory.class.getName() + ".PROPPATCH"),
108         ProppatchMethodImpl.class.getName());
109 
110     private static final String _PUT_METHOD_IMPL = GetterUtil.getString(
111         PropsUtil.get(MethodFactory.class.getName() + ".PUT"),
112         PutMethodImpl.class.getName());
113 
114     private static final String _UNLOCK_METHOD_IMPL = GetterUtil.getString(
115         PropsUtil.get(MethodFactory.class.getName() + ".UNLOCK"),
116         UnlockMethodImpl.class.getName());
117 
118     private static MethodFactory _instance = new MethodFactory();
119 
120     private Map<String, Object> _methods;
121 
122 }