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