001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.InstanceFactory;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.webdav.WebDAVException;
021    import com.liferay.portal.kernel.webdav.methods.Method;
022    import com.liferay.portal.kernel.webdav.methods.MethodFactory;
023    import com.liferay.portal.util.PropsUtil;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class MethodFactoryImpl implements MethodFactory {
034    
035            public MethodFactoryImpl() throws Exception {
036                    for (String methodName : Method.SUPPORTED_METHOD_NAMES) {
037                            addMethod(methodName);
038                    }
039            }
040    
041            @Override
042            public Method create(HttpServletRequest request) throws WebDAVException {
043                    String method = request.getMethod();
044    
045                    Method methodImpl = (Method)_methods.get(
046                            StringUtil.toUpperCase(method));
047    
048                    if (methodImpl == null) {
049                            throw new WebDAVException(
050                                    "Method " + method + " is not implemented");
051                    }
052    
053                    return methodImpl;
054            }
055    
056            protected void addMethod(String methodName) throws Exception {
057                    String defaultClassName = methodName.substring(1);
058    
059                    defaultClassName = StringUtil.toLowerCase(defaultClassName);
060                    defaultClassName = methodName.substring(0, 1) + defaultClassName;
061                    defaultClassName =
062                            "com.liferay.portal.webdav.methods." + defaultClassName +
063                                    "MethodImpl";
064    
065                    String className = GetterUtil.getString(
066                            PropsUtil.get(MethodFactoryImpl.class.getName() + "." + methodName),
067                            defaultClassName);
068    
069                    _methods.put(methodName, InstanceFactory.newInstance(className));
070            }
071    
072            private Map<String, Object> _methods = new HashMap<String, Object>();
073    
074    }