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.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    }