001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.webdav.methods;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.InstancePool;
019    import com.liferay.portal.kernel.webdav.WebDAVException;
020    import com.liferay.portal.util.PropsUtil;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import javax.servlet.http.HttpServletRequest;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class MethodFactory {
031    
032            public static Method create(HttpServletRequest request)
033                    throws WebDAVException {
034    
035                    return _instance._create(request);
036            }
037    
038            private MethodFactory() {
039                    _methods = new HashMap<String, Object>();
040    
041                    _methods.put("COPY", InstancePool.get(_COPY_METHOD_IMPL));
042                    _methods.put("DELETE", InstancePool.get(_DELETE_METHOD_IMPL));
043                    _methods.put("GET", InstancePool.get(_GET_METHOD_IMPL));
044                    _methods.put("HEAD", InstancePool.get(_HEAD_METHOD_IMPL));
045                    _methods.put("LOCK", InstancePool.get(_LOCK_METHOD_IMPL));
046                    _methods.put("MKCOL", InstancePool.get(_MKCOL_METHOD_IMPL));
047                    _methods.put("MOVE", InstancePool.get(_MOVE_METHOD_IMPL));
048                    _methods.put("OPTIONS", InstancePool.get(_OPTIONS_METHOD_IMPL));
049                    _methods.put("PROPFIND", InstancePool.get(_PROPFIND_METHOD_IMPL));
050                    _methods.put("PROPPATCH", InstancePool.get(_PROPPATCH_METHOD_IMPL));
051                    _methods.put("PUT", InstancePool.get(_PUT_METHOD_IMPL));
052                    _methods.put("UNLOCK", InstancePool.get(_UNLOCK_METHOD_IMPL));
053            }
054    
055            private Method _create(HttpServletRequest request) throws WebDAVException {
056                    String method = request.getMethod();
057    
058                    Method methodImpl = (Method)_methods.get(method.toUpperCase());
059    
060                    if (methodImpl == null) {
061                            throw new WebDAVException(
062                                    "Method " + method + " is not implemented");
063                    }
064    
065                    return methodImpl;
066            }
067    
068            private static final String _COPY_METHOD_IMPL = GetterUtil.getString(
069                    PropsUtil.get(MethodFactory.class.getName() + ".COPY"),
070                    CopyMethodImpl.class.getName());
071    
072            private static final String _DELETE_METHOD_IMPL = GetterUtil.getString(
073                    PropsUtil.get(MethodFactory.class.getName() + ".DELETE"),
074                    DeleteMethodImpl.class.getName());
075    
076            private static final String _GET_METHOD_IMPL = GetterUtil.getString(
077                    PropsUtil.get(MethodFactory.class.getName() + ".GET"),
078                    GetMethodImpl.class.getName());
079    
080            private static final String _HEAD_METHOD_IMPL = GetterUtil.getString(
081                    PropsUtil.get(MethodFactory.class.getName() + ".HEAD"),
082                    HeadMethodImpl.class.getName());
083    
084            private static final String _LOCK_METHOD_IMPL = GetterUtil.getString(
085                    PropsUtil.get(MethodFactory.class.getName() + ".LOCK"),
086                    LockMethodImpl.class.getName());
087    
088            private static final String _MKCOL_METHOD_IMPL = GetterUtil.getString(
089                    PropsUtil.get(MethodFactory.class.getName() + ".MKCOL"),
090                    MkcolMethodImpl.class.getName());
091    
092            private static final String _MOVE_METHOD_IMPL = GetterUtil.getString(
093                    PropsUtil.get(MethodFactory.class.getName() + ".MOVE"),
094                    MoveMethodImpl.class.getName());
095    
096            private static final String _OPTIONS_METHOD_IMPL = GetterUtil.getString(
097                    PropsUtil.get(MethodFactory.class.getName() + ".OPTIONS"),
098                    OptionsMethodImpl.class.getName());
099    
100            private static final String _PROPFIND_METHOD_IMPL = GetterUtil.getString(
101                    PropsUtil.get(MethodFactory.class.getName() + ".PROPFIND"),
102                    PropfindMethodImpl.class.getName());
103    
104            private static final String _PROPPATCH_METHOD_IMPL = GetterUtil.getString(
105                    PropsUtil.get(MethodFactory.class.getName() + ".PROPPATCH"),
106                    ProppatchMethodImpl.class.getName());
107    
108            private static final String _PUT_METHOD_IMPL = GetterUtil.getString(
109                    PropsUtil.get(MethodFactory.class.getName() + ".PUT"),
110                    PutMethodImpl.class.getName());
111    
112            private static final String _UNLOCK_METHOD_IMPL = GetterUtil.getString(
113                    PropsUtil.get(MethodFactory.class.getName() + ".UNLOCK"),
114                    UnlockMethodImpl.class.getName());
115    
116            private static MethodFactory _instance = new MethodFactory();
117    
118            private Map<String, Object> _methods;
119    
120    }