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