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