001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.MethodParameter;
020    import com.liferay.portal.kernel.util.MethodParametersResolverUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.SetUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.util.PortalUtil;
027    
028    import java.io.InputStream;
029    import java.io.OutputStream;
030    
031    import java.lang.reflect.Method;
032    
033    import java.util.Set;
034    
035    /**
036     * @author Igor Spasic
037     */
038    public class JSONWebServiceNaming {
039    
040            public String convertClassNameToPath(Class<?> clazz) {
041                    String className = clazz.getSimpleName();
042    
043                    className = StringUtil.replace(className, "Impl", StringPool.BLANK);
044                    className = StringUtil.replace(className, "Service", StringPool.BLANK);
045    
046                    return StringUtil.toLowerCase(className);
047            }
048    
049            public String convertImplClassNameToUtilClassName(
050                    Class<?> implementationClass) {
051    
052                    String implementationClassName = implementationClass.getName();
053    
054                    if (implementationClassName.endsWith("Impl")) {
055                            implementationClassName = implementationClassName.substring(
056                                    0, implementationClassName.length() - 4);
057                    }
058    
059                    String utilClassName = implementationClassName + "Util";
060    
061                    utilClassName = StringUtil.replace(
062                            utilClassName, ".impl.", StringPool.PERIOD);
063    
064                    return utilClassName;
065            }
066    
067            public String convertMethodNameToHttpMethod(Method method) {
068                    String methodName = method.getName();
069    
070                    String methodNamePrefix = getMethodNamePrefix(methodName);
071    
072                    if (prefixes.contains(methodNamePrefix)) {
073                            return HttpMethods.GET;
074                    }
075    
076                    return HttpMethods.POST;
077            }
078    
079            public String convertMethodNameToPath(Method method) {
080                    return CamelCaseUtil.fromCamelCase(method.getName());
081            }
082    
083            public boolean isIncludedMethod(Method method) {
084                    if ((excludedMethodNames != null) &&
085                            excludedMethodNames.contains(method.getName())) {
086    
087                            return false;
088                    }
089    
090                    if (excludedTypesNames == null) {
091                            return true;
092                    }
093    
094                    MethodParameter[] methodParameters =
095                            MethodParametersResolverUtil.resolveMethodParameters(method);
096    
097                    Class<?>[] parameterTypes = method.getParameterTypes();
098    
099                    for (int i = 0; i < parameterTypes.length; i++) {
100                            MethodParameter methodParameter = methodParameters[i];
101    
102                            Class<?> parameterType = parameterTypes[i];
103    
104                            if (parameterType.isArray()) {
105                                    parameterType = parameterType.getComponentType();
106                            }
107    
108                            String parameterTypeName = parameterType.getName();
109    
110                            for (String excludedTypesName : excludedTypesNames) {
111                                    String signature = methodParameter.getSignature();
112    
113                                    if (signature.contains(StringPool.LESS_THAN)) {
114                                            String excludedName = 'L' + excludedTypesName;
115    
116                                            if (!excludedName.endsWith(StringPool.PERIOD)) {
117                                                    excludedName = excludedName.concat(
118                                                            StringPool.SEMICOLON);
119                                            }
120    
121                                            excludedName = StringUtil.replace(excludedName, '.', '/');
122    
123                                            if (signature.contains(excludedName)) {
124                                                    return false;
125                                            }
126                                    }
127    
128                                    if (parameterTypeName.startsWith(excludedTypesName)) {
129                                            return false;
130                                    }
131                            }
132                    }
133    
134                    Class<?> returnType = method.getReturnType();
135    
136                    if (returnType.isArray()) {
137                            returnType = returnType.getComponentType();
138                    }
139    
140                    String returnTypeName = returnType.getName();
141    
142                    for (String excludedTypesName : excludedTypesNames) {
143                            if (excludedTypesName.startsWith(returnTypeName)) {
144                                    return false;
145                            }
146                    }
147    
148                    return true;
149            }
150    
151            public boolean isIncludedPath(String contextPath, String path) {
152                    String portalContextPath = PortalUtil.getPathContext();
153    
154                    if (!contextPath.equals(portalContextPath)) {
155                            path = contextPath + StringPool.PERIOD + path.substring(1);
156                    }
157    
158                    for (String excludedPath : excludedPaths) {
159                            if (StringUtil.wildcardMatches(
160                                            path, excludedPath, '?', '*', '\\', false)) {
161    
162                                    return false;
163                            }
164                    }
165    
166                    if (includedPaths.length == 0) {
167                            return true;
168                    }
169    
170                    for (String includedPath : includedPaths) {
171                            if (StringUtil.wildcardMatches(
172                                            path, includedPath, '?', '*', '\\', false)) {
173    
174                                    return true;
175                            }
176                    }
177    
178                    return false;
179            }
180    
181            public boolean isValidHttpMethod(String httpMethod) {
182                    if (invalidHttpMethods.contains(httpMethod)) {
183                            return false;
184                    }
185    
186                    return true;
187            }
188    
189            protected String getMethodNamePrefix(String methodName) {
190                    int i = 0;
191    
192                    while (i < methodName.length()) {
193                            if (Character.isUpperCase(methodName.charAt(i))) {
194                                    break;
195                            }
196    
197                            i++;
198                    }
199    
200                    return methodName.substring(0, i);
201            }
202    
203            protected Set<String> excludedMethodNames = SetUtil.fromArray(
204                    new String[] {"getBeanIdentifier", "setBeanIdentifier"});
205            protected String[] excludedPaths = PropsUtil.getArray(
206                    PropsKeys.JSONWS_WEB_SERVICE_PATHS_EXCLUDES);
207            protected String[] excludedTypesNames =
208                    {InputStream.class.getName(), OutputStream.class.getName(), "javax."};
209            protected String[] includedPaths = PropsUtil.getArray(
210                    PropsKeys.JSONWS_WEB_SERVICE_PATHS_INCLUDES);
211            protected Set<String> invalidHttpMethods = SetUtil.fromArray(
212                    PropsUtil.getArray(PropsKeys.JSONWS_WEB_SERVICE_INVALID_HTTP_METHODS));
213            protected Set<String> prefixes = SetUtil.fromArray(
214                    new String[] {"get", "has", "is"});
215    
216    }