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.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
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 MethodParameter[] methodParameters =
104 MethodParametersResolverUtil.resolveMethodParameters(method);
105
106 Class<?>[] parameterTypes = method.getParameterTypes();
107
108 for (int i = 0; i < parameterTypes.length; i++) {
109 MethodParameter methodParameter = methodParameters[i];
110
111 Class<?> parameterType = parameterTypes[i];
112
113 if (parameterType.isArray()) {
114 parameterType = parameterType.getComponentType();
115 }
116
117 String parameterTypeName = parameterType.getName();
118
119 for (String excludedTypesName : excludedTypesNames) {
120 String signature = methodParameter.getSignature();
121
122 if (signature.contains(StringPool.LESS_THAN)) {
123 String excludedName = 'L' + excludedTypesName;
124
125 if (!excludedName.endsWith(StringPool.PERIOD)) {
126 excludedName = excludedName.concat(
127 StringPool.SEMICOLON);
128 }
129
130 excludedName = StringUtil.replace(excludedName, '.', '/');
131
132 if (signature.contains(excludedName)) {
133 return false;
134 }
135 }
136
137 if (parameterTypeName.startsWith(excludedTypesName)) {
138 return false;
139 }
140 }
141 }
142
143 Class<?> returnType = method.getReturnType();
144
145 if (returnType.isArray()) {
146 returnType = returnType.getComponentType();
147 }
148
149 String returnTypeName = returnType.getName();
150
151 for (String excludedTypesName : excludedTypesNames) {
152 if (excludedTypesName.startsWith(returnTypeName)) {
153 return false;
154 }
155 }
156
157 return true;
158 }
159
160 public boolean isIncludedPath(String contextPath, String path) {
161 String portalContextPath = PortalUtil.getPathContext();
162
163 if (!contextPath.equals(portalContextPath)) {
164 path = contextPath + StringPool.PERIOD + path.substring(1);
165 }
166
167 for (String excludedPath : excludedPaths) {
168 if (StringUtil.wildcardMatches(
169 path, excludedPath, '?', '*', '\\', false)) {
170
171 return false;
172 }
173 }
174
175 if (includedPaths.length == 0) {
176 return true;
177 }
178
179 for (String includedPath : includedPaths) {
180 if (StringUtil.wildcardMatches(
181 path, includedPath, '?', '*', '\\', false)) {
182
183 return true;
184 }
185 }
186
187 return false;
188 }
189
190 public boolean isValidHttpMethod(String httpMethod) {
191 if (invalidHttpMethods.contains(httpMethod)) {
192 return false;
193 }
194
195 return true;
196 }
197
198 protected String getMethodNamePrefix(String methodName) {
199 int i = 0;
200
201 while (i < methodName.length()) {
202 if (Character.isUpperCase(methodName.charAt(i))) {
203 break;
204 }
205
206 i++;
207 }
208
209 return methodName.substring(0, i);
210 }
211
212 protected Set<String> excludedMethodNames = SetUtil.fromArray(
213 PropsUtil.getArray(PropsKeys.JSON_SERVICE_INVALID_METHOD_NAMES));
214 protected String[] excludedPaths = PropsUtil.getArray(
215 PropsKeys.JSONWS_WEB_SERVICE_PATHS_EXCLUDES);
216 protected String[] excludedTypesNames =
217 {InputStream.class.getName(), OutputStream.class.getName(), "javax."};
218 protected String[] includedPaths = PropsUtil.getArray(
219 PropsKeys.JSONWS_WEB_SERVICE_PATHS_INCLUDES);
220 protected Set<String> invalidHttpMethods = SetUtil.fromArray(
221 PropsUtil.getArray(PropsKeys.JSONWS_WEB_SERVICE_INVALID_HTTP_METHODS));
222 protected Set<String> prefixes = SetUtil.fromArray(
223 new String[] {"get", "has", "is"});
224
225 }