001
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.PortalUtil;
022 import com.liferay.portal.kernel.util.PropsKeys;
023 import com.liferay.portal.kernel.util.PropsUtil;
024 import com.liferay.portal.kernel.util.SetUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
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
038 public class JSONWebServiceNaming {
039
040 public String convertMethodToHttpMethod(Method method) {
041 String methodName = method.getName();
042
043 String methodNamePrefix = getMethodNamePrefix(methodName);
044
045 if (prefixes.contains(methodNamePrefix)) {
046 return HttpMethods.GET;
047 }
048
049 return HttpMethods.POST;
050 }
051
052 public String convertMethodToPath(Method method) {
053 return CamelCaseUtil.fromCamelCase(method.getName());
054 }
055
056 public String convertModelClassToImplClassName(Class<?> clazz) {
057 String className = clazz.getName();
058
059 className =
060 StringUtil.replace(className, ".model.", ".model.impl.") +
061 "ModelImpl";
062
063 return className;
064 }
065
066 public String convertServiceClassToPath(Class<?> clazz) {
067 String className = convertServiceClassToSimpleName(clazz);
068
069 return StringUtil.toLowerCase(className);
070 }
071
072 public String convertServiceClassToSimpleName(Class<?> clazz) {
073 String className = clazz.getSimpleName();
074
075 className = StringUtil.replace(className, "Impl", StringPool.BLANK);
076 className = StringUtil.replace(className, "Service", StringPool.BLANK);
077
078 return className;
079 }
080
081 public String convertServiceImplClassToUtilClassName(Class<?> clazz) {
082 String className = clazz.getName();
083
084 if (className.endsWith("Impl")) {
085 className = className.substring(0, className.length() - 4);
086 }
087
088 return StringUtil.replace(
089 className + "Util", ".impl.", StringPool.PERIOD);
090 }
091
092 public boolean isIncludedMethod(Method method) {
093 if ((excludedMethodNames != null) &&
094 excludedMethodNames.contains(method.getName())) {
095
096 return false;
097 }
098
099 if (excludedTypesNames == null) {
100 return true;
101 }
102
103 Class<?> returnType = method.getReturnType();
104
105 if (returnType.isArray()) {
106 returnType = returnType.getComponentType();
107 }
108
109 String returnTypeName = returnType.getName();
110
111 for (String excludedTypesName : excludedTypesNames) {
112 if (excludedTypesName.startsWith(returnTypeName)) {
113 return false;
114 }
115 }
116
117 MethodParameter[] methodParameters =
118 MethodParametersResolverUtil.resolveMethodParameters(method);
119
120 Class<?>[] parameterTypes = method.getParameterTypes();
121
122 for (int i = 0; i < parameterTypes.length; i++) {
123 MethodParameter methodParameter = methodParameters[i];
124
125 Class<?> parameterType = parameterTypes[i];
126
127 if (parameterType.isArray()) {
128 parameterType = parameterType.getComponentType();
129 }
130
131 String parameterTypeName = parameterType.getName();
132
133 for (String excludedTypesName : excludedTypesNames) {
134 if (parameterTypeName.startsWith(excludedTypesName)) {
135 return false;
136 }
137
138 Class<?>[] genericTypes = methodParameter.getGenericTypes();
139
140 if (genericTypes != null) {
141 for (Class<?> genericType : genericTypes) {
142 String genericName = genericType.getName();
143
144 if (genericName.startsWith(excludedTypesName)) {
145 return false;
146 }
147 }
148 }
149 }
150 }
151
152 return true;
153 }
154
155 public boolean isIncludedPath(String contextPath, String path) {
156 String portalContextPath = PortalUtil.getPathContext();
157
158 if (!contextPath.equals(portalContextPath)) {
159 path = contextPath + StringPool.PERIOD + path.substring(1);
160 }
161
162 for (String excludedPath : excludedPaths) {
163 if (StringUtil.wildcardMatches(
164 path, excludedPath, '?', '*', '\\', false)) {
165
166 return false;
167 }
168 }
169
170 if (includedPaths.length == 0) {
171 return true;
172 }
173
174 for (String includedPath : includedPaths) {
175 if (StringUtil.wildcardMatches(
176 path, includedPath, '?', '*', '\\', false)) {
177
178 return true;
179 }
180 }
181
182 return false;
183 }
184
185 public boolean isValidHttpMethod(String httpMethod) {
186 if (invalidHttpMethods.contains(httpMethod)) {
187 return false;
188 }
189
190 return true;
191 }
192
193 protected String getMethodNamePrefix(String methodName) {
194 int i = 0;
195
196 while (i < methodName.length()) {
197 if (Character.isUpperCase(methodName.charAt(i))) {
198 break;
199 }
200
201 i++;
202 }
203
204 return methodName.substring(0, i);
205 }
206
207 protected Set<String> excludedMethodNames = SetUtil.fromArray(
208 PropsUtil.getArray(PropsKeys.JSON_SERVICE_INVALID_METHOD_NAMES));
209 protected String[] excludedPaths = PropsUtil.getArray(
210 PropsKeys.JSONWS_WEB_SERVICE_PATHS_EXCLUDES);
211 protected String[] excludedTypesNames =
212 {InputStream.class.getName(), OutputStream.class.getName(), "javax."};
213 protected String[] includedPaths = PropsUtil.getArray(
214 PropsKeys.JSONWS_WEB_SERVICE_PATHS_INCLUDES);
215 protected Set<String> invalidHttpMethods = SetUtil.fromArray(
216 PropsUtil.getArray(PropsKeys.JSONWS_WEB_SERVICE_INVALID_HTTP_METHODS));
217 protected Set<String> prefixes = SetUtil.fromArray(
218 new String[] {"get", "has", "is"});
219
220 }