001
014
015 package com.liferay.portal.jsonwebservice;
016
017 import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionMapping;
018 import com.liferay.portal.kernel.util.GetterUtil;
019 import com.liferay.portal.kernel.util.MethodParameter;
020 import com.liferay.portal.kernel.util.MethodParametersResolverUtil;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.Validator;
024
025 import java.lang.reflect.Method;
026
027 import java.util.Objects;
028
029
033 public class JSONWebServiceActionConfig
034 implements Comparable<JSONWebServiceActionConfig>,
035 JSONWebServiceActionMapping {
036
037 public JSONWebServiceActionConfig(
038 String contextName, String contextPath, Class<?> actionClass,
039 Method actionMethod, String path, String method) {
040
041 this(
042 contextName, contextPath, null, actionClass, actionMethod, path,
043 method);
044 }
045
046 public JSONWebServiceActionConfig(
047 String contextName, String contextPath, Object actionObject,
048 Class<?> actionClass, Method actionMethod, String path, String method) {
049
050 _contextName = GetterUtil.getString(contextName);
051 _contextPath = GetterUtil.getString(contextPath);
052 _actionObject = actionObject;
053 _actionClass = actionClass;
054
055 Method newActionMethod = actionMethod;
056
057 if (actionObject != null) {
058 try {
059 Class<?> actionObjectClass = actionObject.getClass();
060
061 newActionMethod = actionObjectClass.getMethod(
062 actionMethod.getName(), actionMethod.getParameterTypes());
063 }
064 catch (NoSuchMethodException nsme) {
065 throw new IllegalArgumentException(nsme);
066 }
067 }
068
069 _actionMethod = newActionMethod;
070
071 if (Validator.isNotNull(_contextName)) {
072 path = path.substring(1);
073
074 path = StringPool.SLASH.concat(_contextName).concat(
075 StringPool.PERIOD).concat(path);
076 }
077
078 _path = path;
079
080 _method = method;
081
082 Deprecated deprecated = actionMethod.getAnnotation(Deprecated.class);
083
084 if (deprecated != null) {
085 _deprecated = true;
086 }
087 else {
088 _deprecated = false;
089 }
090
091 _methodParameters =
092 MethodParametersResolverUtil.resolveMethodParameters(actionMethod);
093
094 Method realActionMethod = null;
095
096 try {
097 realActionMethod = _actionClass.getDeclaredMethod(
098 actionMethod.getName(), actionMethod.getParameterTypes());
099 }
100 catch (NoSuchMethodException nsme) {
101 }
102
103 _realActionMethod = realActionMethod;
104
105 StringBundler sb = new StringBundler(_methodParameters.length * 2 + 3);
106
107 sb.append(_path);
108 sb.append(StringPool.MINUS);
109 sb.append(_methodParameters.length);
110
111 for (MethodParameter methodParameter : _methodParameters) {
112 sb.append(StringPool.MINUS);
113 sb.append(methodParameter.getName());
114 }
115
116 _signature = sb.toString();
117 }
118
119 @Override
120 public int compareTo(
121 JSONWebServiceActionConfig jsonWebServiceActionConfig) {
122
123 return _signature.compareTo(jsonWebServiceActionConfig._signature);
124 }
125
126 @Override
127 public boolean equals(Object object) {
128 if (this == object) {
129 return true;
130 }
131
132 if (!(object instanceof JSONWebServiceActionConfig)) {
133 return false;
134 }
135
136 JSONWebServiceActionConfig jsonWebServiceActionConfig =
137 (JSONWebServiceActionConfig)object;
138
139 if (Objects.equals(_signature, jsonWebServiceActionConfig._signature)) {
140 return true;
141 }
142
143 return false;
144 }
145
146 @Override
147 public Class<?> getActionClass() {
148 return _actionClass;
149 }
150
151 @Override
152 public Method getActionMethod() {
153 return _actionMethod;
154 }
155
156 @Override
157 public Object getActionObject() {
158 return _actionObject;
159 }
160
161 @Override
162 public String getContextName() {
163 return _contextName;
164 }
165
166 @Override
167 public String getContextPath() {
168 return _contextPath;
169 }
170
171 @Override
172 public String getMethod() {
173 return _method;
174 }
175
176 @Override
177 public MethodParameter[] getMethodParameters() {
178 return _methodParameters;
179 }
180
181 @Override
182 public String getPath() {
183 return _path;
184 }
185
186 @Override
187 public Method getRealActionMethod() {
188 return _realActionMethod;
189 }
190
191 @Override
192 public String getSignature() {
193 return _signature;
194 }
195
196 @Override
197 public int hashCode() {
198 return _signature.hashCode();
199 }
200
201 @Override
202 public boolean isDeprecated() {
203 return _deprecated;
204 }
205
206 @Override
207 public String toString() {
208 StringBundler sb = new StringBundler(21);
209
210 sb.append("{actionClass=");
211 sb.append(_actionClass);
212 sb.append(", actionMethod=");
213 sb.append(_actionMethod);
214 sb.append(", contextName=");
215 sb.append(_contextName);
216 sb.append(", contextPath=");
217 sb.append(_contextPath);
218 sb.append(", deprecated=");
219 sb.append(_deprecated);
220 sb.append(", method=");
221 sb.append(_method);
222 sb.append(", methodParameters=");
223 sb.append(_methodParameters);
224 sb.append(", path=");
225 sb.append(_path);
226 sb.append(", realActionMethod=");
227 sb.append(_realActionMethod);
228 sb.append(", signature=");
229 sb.append(_signature);
230 sb.append("}");
231
232 return sb.toString();
233 }
234
235 private final Class<?> _actionClass;
236 private final Method _actionMethod;
237 private final Object _actionObject;
238 private final String _contextName;
239 private final String _contextPath;
240 private final boolean _deprecated;
241 private final String _method;
242 private final MethodParameter[] _methodParameters;
243 private final String _path;
244 private final Method _realActionMethod;
245 private final String _signature;
246
247 }