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