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.webdav.WebDAVException;
020    import com.liferay.portal.kernel.webdav.methods.Method;
021    import com.liferay.portal.kernel.webdav.methods.MethodFactory;
022    import com.liferay.portal.util.PropsUtil;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import javax.servlet.http.HttpServletRequest;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class MethodFactoryImpl implements MethodFactory {
033    
034            public MethodFactoryImpl() throws Exception {
035                    for (String methodName : Method.SUPPORTED_METHOD_NAMES) {
036                            addMethod(methodName);
037                    }
038            }
039    
040            @Override
041            public Method create(HttpServletRequest request) throws WebDAVException {
042                    String method = request.getMethod();
043    
044                    Method methodImpl = (Method)_methods.get(method.toUpperCase());
045    
046                    if (methodImpl == null) {
047                            throw new WebDAVException(
048                                    "Method " + method + " is not implemented");
049                    }
050    
051                    return methodImpl;
052            }
053    
054            protected void addMethod(String methodName) throws Exception {
055                    String defaultClassName = methodName.substring(1);
056    
057                    defaultClassName = defaultClassName.toLowerCase();
058                    defaultClassName = methodName.substring(0, 1) + defaultClassName;
059                    defaultClassName =
060                            "com.liferay.portal.webdav.methods." + defaultClassName +
061                                    "MethodImpl";
062    
063                    String className = GetterUtil.getString(
064                            PropsUtil.get(MethodFactoryImpl.class.getName() + "." + methodName),
065                            defaultClassName);
066    
067                    _methods.put(methodName, InstanceFactory.newInstance(className));
068            }
069    
070            private Map<String, Object> _methods = new HashMap<String, Object>();
071    
072    }