001    /**
002     * Copyright (c) 2000-present 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.convertMethodToHttpMethod(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.convertMethodToPath(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.convertServiceClassToPath(
079                                    clazz);
080                    }
081    
082                    if (!pathFromClass.startsWith(StringPool.SLASH)) {
083                            pathFromClass = StringPool.SLASH + pathFromClass;
084                    }
085    
086                    return pathFromClass + path;
087            }
088    
089            private final JSONWebServiceNaming _jsonWebServiceNaming;
090    
091    }