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.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.PropsKeys;
020    import com.liferay.portal.kernel.util.PropsUtil;
021    import com.liferay.portal.kernel.util.SetUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    
025    import java.lang.reflect.Method;
026    
027    import java.util.Set;
028    
029    /**
030     * @author Igor Spasic
031     */
032    public class JSONWebServiceNaming {
033    
034            public String convertClassNameToPath(Class<?> clazz) {
035                    String className = clazz.getSimpleName();
036    
037                    className = StringUtil.replace(className, "Impl", StringPool.BLANK);
038                    className = StringUtil.replace(className, "Service", StringPool.BLANK);
039    
040                    return className.toLowerCase();
041            }
042    
043            public String convertImplClassNameToUtilClassName(
044                    Class<?> implementationClass) {
045    
046                    String implementationClassName = implementationClass.getName();
047    
048                    if (implementationClassName.endsWith("Impl")) {
049                            implementationClassName = implementationClassName.substring(
050                                    0, implementationClassName.length() - 4);
051                    }
052    
053                    String utilClassName = implementationClassName + "Util";
054    
055                    utilClassName = StringUtil.replace(
056                            utilClassName, ".impl.", StringPool.PERIOD);
057    
058                    return utilClassName;
059            }
060    
061            public String convertMethodNameToHttpMethod(Method method) {
062                    String methodName = method.getName();
063    
064                    String methodNamePrefix = getMethodNamePrefix(methodName);
065    
066                    if (prefixes.contains(methodNamePrefix)) {
067                            return HttpMethods.GET;
068                    }
069    
070                    return HttpMethods.POST;
071            }
072    
073            public String convertMethodNameToPath(Method method) {
074                    return CamelCaseUtil.fromCamelCase(method.getName());
075            }
076    
077            public boolean isIncludedMethod(Method method) {
078                    if ((excludedMethodNames != null) &&
079                            excludedMethodNames.contains(method.getName())) {
080    
081                            return false;
082                    }
083    
084                    return true;
085            }
086    
087            public boolean isValidHttpMethod(String httpMethod) {
088                    if (invalidHttpMethods.contains(httpMethod)) {
089                            return false;
090                    }
091    
092                    return true;
093            }
094    
095            protected String getMethodNamePrefix(String methodName) {
096                    int i = 0;
097    
098                    while (i < methodName.length()) {
099                            if (Character.isUpperCase(methodName.charAt(i))) {
100                                    break;
101                            }
102    
103                            i++;
104                    }
105    
106                    return methodName.substring(0, i);
107            }
108    
109            protected Set<String> excludedMethodNames = SetUtil.fromArray(
110                            new String[] {"getBeanIdentifier", "setBeanIdentifier"});
111            protected Set<String> invalidHttpMethods = SetUtil.fromArray(
112                    PropsUtil.getArray(PropsKeys.JSONWS_WEB_SERVICE_INVALID_HTTP_METHODS));
113            protected Set<String> prefixes = SetUtil.fromArray(
114                    new String[] {"get", "has", "is"});
115    
116    }