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    import com.liferay.portal.util.PortalUtil;
025    
026    import java.io.InputStream;
027    import java.io.OutputStream;
028    
029    import java.lang.reflect.Method;
030    
031    import java.util.Set;
032    
033    /**
034     * @author Igor Spasic
035     */
036    public class JSONWebServiceNaming {
037    
038            public String convertClassNameToPath(Class<?> clazz) {
039                    String className = clazz.getSimpleName();
040    
041                    className = StringUtil.replace(className, "Impl", StringPool.BLANK);
042                    className = StringUtil.replace(className, "Service", StringPool.BLANK);
043    
044                    return StringUtil.toLowerCase(className);
045            }
046    
047            public String convertImplClassNameToUtilClassName(
048                    Class<?> implementationClass) {
049    
050                    String implementationClassName = implementationClass.getName();
051    
052                    if (implementationClassName.endsWith("Impl")) {
053                            implementationClassName = implementationClassName.substring(
054                                    0, implementationClassName.length() - 4);
055                    }
056    
057                    String utilClassName = implementationClassName + "Util";
058    
059                    utilClassName = StringUtil.replace(
060                            utilClassName, ".impl.", StringPool.PERIOD);
061    
062                    return utilClassName;
063            }
064    
065            public String convertMethodNameToHttpMethod(Method method) {
066                    String methodName = method.getName();
067    
068                    String methodNamePrefix = getMethodNamePrefix(methodName);
069    
070                    if (prefixes.contains(methodNamePrefix)) {
071                            return HttpMethods.GET;
072                    }
073    
074                    return HttpMethods.POST;
075            }
076    
077            public String convertMethodNameToPath(Method method) {
078                    return CamelCaseUtil.fromCamelCase(method.getName());
079            }
080    
081            public boolean isIncludedMethod(Method method) {
082                    if ((excludedMethodNames != null) &&
083                            excludedMethodNames.contains(method.getName())) {
084    
085                            return false;
086                    }
087    
088                    if (excludedTypes == null) {
089                            return true;
090                    }
091    
092                    Class<?>[] parameterTypes = method.getParameterTypes();
093    
094                    for (Class<?> parameterType : parameterTypes) {
095                            if (parameterType.isArray()) {
096                                    parameterType = parameterType.getComponentType();
097                            }
098    
099                            if (excludedTypes.contains(parameterType)) {
100                                    return false;
101                            }
102                    }
103    
104                    Class<?> returnType = method.getReturnType();
105    
106                    if (returnType.isArray()) {
107                            returnType = returnType.getComponentType();
108                    }
109    
110                    if (excludedTypes.contains(returnType)) {
111                            return false;
112                    }
113    
114                    return true;
115            }
116    
117            public boolean isIncludedPath(String contextPath, String path) {
118                    String portalContextPath = PortalUtil.getPathContext();
119    
120                    if (!contextPath.equals(portalContextPath)) {
121                            path = contextPath + StringPool.PERIOD + path.substring(1);
122                    }
123    
124                    for (String excludedPath : excludedPaths) {
125                            if (StringUtil.wildcardMatches(
126                                            path, excludedPath, '?', '*', '\\', false)) {
127    
128                                    return false;
129                            }
130                    }
131    
132                    if (includedPaths.length == 0) {
133                            return true;
134                    }
135    
136                    for (String includedPath : includedPaths) {
137                            if (StringUtil.wildcardMatches(
138                                            path, includedPath, '?', '*', '\\', false)) {
139    
140                                    return true;
141                            }
142                    }
143    
144                    return false;
145            }
146    
147            public boolean isValidHttpMethod(String httpMethod) {
148                    if (invalidHttpMethods.contains(httpMethod)) {
149                            return false;
150                    }
151    
152                    return true;
153            }
154    
155            protected String getMethodNamePrefix(String methodName) {
156                    int i = 0;
157    
158                    while (i < methodName.length()) {
159                            if (Character.isUpperCase(methodName.charAt(i))) {
160                                    break;
161                            }
162    
163                            i++;
164                    }
165    
166                    return methodName.substring(0, i);
167            }
168    
169            protected Set<String> excludedMethodNames = SetUtil.fromArray(
170                    new String[] {"getBeanIdentifier", "setBeanIdentifier"});
171            protected String[] excludedPaths = PropsUtil.getArray(
172                    PropsKeys.JSONWS_WEB_SERVICE_PATHS_EXCLUDES);
173            protected Set<Class<?>> excludedTypes = SetUtil.fromArray(
174                    new Class<?>[] {InputStream.class, OutputStream.class});
175            protected String[] includedPaths = PropsUtil.getArray(
176                    PropsKeys.JSONWS_WEB_SERVICE_PATHS_INCLUDES);
177            protected Set<String> invalidHttpMethods = SetUtil.fromArray(
178                    PropsUtil.getArray(PropsKeys.JSONWS_WEB_SERVICE_INVALID_HTTP_METHODS));
179            protected Set<String> prefixes = SetUtil.fromArray(
180                    new String[] {"get", "has", "is"});
181    
182    }