001    /**
002     * Copyright (c) 2000-2012 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.servlet.HttpMethods;
018    import com.liferay.portal.kernel.util.CamelCaseUtil;
019    import com.liferay.portal.kernel.util.SetUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import java.lang.reflect.Method;
024    
025    import java.util.Set;
026    
027    /**
028     * @author Igor Spasic
029     */
030    public class JSONWebServiceMappingResolver {
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                    String methodName = method.getName();
047    
048                    String methodNamePrefix = _cutPrefix(methodName);
049    
050                    return _prefixToHttpMethod(methodNamePrefix);
051            }
052    
053            public String resolvePath(Class<?> clazz, Method method) {
054                    JSONWebService jsonWebServiceAnnotation = method.getAnnotation(
055                            JSONWebService.class);
056    
057                    String path = null;
058    
059                    if (jsonWebServiceAnnotation != null) {
060                            path = jsonWebServiceAnnotation.value().trim();
061                    }
062    
063                    if ((path == null) || (path.length() == 0)) {
064                            path = CamelCaseUtil.fromCamelCase(method.getName());
065                    }
066    
067                    if (!path.startsWith(StringPool.SLASH)) {
068                            path = StringPool.SLASH + path;
069    
070                            String pathFromClass = null;
071    
072                            jsonWebServiceAnnotation = clazz.getAnnotation(
073                                    JSONWebService.class);
074    
075                            if (jsonWebServiceAnnotation != null) {
076                                    pathFromClass = jsonWebServiceAnnotation.value().trim();
077                            }
078    
079                            if ((pathFromClass == null) || (pathFromClass.length() == 0)) {
080                                    pathFromClass = _classNameToPath(clazz);
081                            }
082    
083                            if (!pathFromClass.startsWith(StringPool.SLASH)) {
084                                    pathFromClass = StringPool.SLASH + pathFromClass;
085                            }
086    
087                            path = pathFromClass + path;
088                    }
089    
090                    return path;
091            }
092    
093            private String _classNameToPath(Class<?> clazz) {
094                    String className = clazz.getSimpleName();
095    
096                    className = StringUtil.replace(className, "Impl", StringPool.BLANK);
097                    className = StringUtil.replace(className, "Service", StringPool.BLANK);
098    
099                    return className.toLowerCase();
100            }
101    
102            private String _cutPrefix(String methodName) {
103                    int i = 0;
104    
105                    while (i < methodName.length()) {
106                            if (Character.isUpperCase(methodName.charAt(i))) {
107                                    break;
108                            }
109    
110                            i++;
111                    }
112    
113                    return methodName.substring(0, i);
114            }
115    
116            private String _prefixToHttpMethod(String prefix) {
117                    if (_prefixes.contains(prefix)) {
118                            return HttpMethods.GET;
119                    }
120    
121                    return HttpMethods.POST;
122            }
123    
124            private static Set<String> _prefixes = SetUtil.fromArray(
125                    new String[] {"get", "has", "is"});
126    
127    }