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.kernel.jsonwebservice;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    
019    import java.lang.reflect.Method;
020    
021    /**
022     * @author Igor Spasic
023     */
024    public class JSONWebServiceMappingResolver {
025    
026            public JSONWebServiceMappingResolver(
027                    JSONWebServiceNaming jsonWebServiceNaming) {
028    
029                    _jsonWebServiceNaming = jsonWebServiceNaming;
030            }
031    
032            public String resolveHttpMethod(Method method) {
033                    JSONWebService jsonWebServiceAnnotation = method.getAnnotation(
034                            JSONWebService.class);
035    
036                    String httpMethod = null;
037    
038                    if (jsonWebServiceAnnotation != null) {
039                            httpMethod = jsonWebServiceAnnotation.method().trim();
040                    }
041    
042                    if ((httpMethod != null) && (httpMethod.length() != 0)) {
043                            return httpMethod;
044                    }
045    
046                    return _jsonWebServiceNaming.convertMethodNameToHttpMethod(method);
047            }
048    
049            public String resolvePath(Class<?> clazz, Method method) {
050                    JSONWebService jsonWebServiceAnnotation = method.getAnnotation(
051                            JSONWebService.class);
052    
053                    String path = null;
054    
055                    if (jsonWebServiceAnnotation != null) {
056                            path = jsonWebServiceAnnotation.value().trim();
057                    }
058    
059                    if ((path == null) || (path.length() == 0)) {
060                            path = _jsonWebServiceNaming.convertMethodNameToPath(method);
061                    }
062    
063                    if (path.startsWith(StringPool.SLASH)) {
064                            return path;
065                    }
066    
067                    path = StringPool.SLASH + path;
068    
069                    String pathFromClass = null;
070    
071                    jsonWebServiceAnnotation = clazz.getAnnotation(JSONWebService.class);
072    
073                    if (jsonWebServiceAnnotation != null) {
074                            pathFromClass = jsonWebServiceAnnotation.value().trim();
075                    }
076    
077                    if ((pathFromClass == null) || (pathFromClass.length() == 0)) {
078                            pathFromClass = _jsonWebServiceNaming.convertClassNameToPath(clazz);
079                    }
080    
081                    if (!pathFromClass.startsWith(StringPool.SLASH)) {
082                            pathFromClass = StringPool.SLASH + pathFromClass;
083                    }
084    
085                    return pathFromClass + path;
086            }
087    
088            private JSONWebServiceNaming _jsonWebServiceNaming;
089    
090    }