| 1 |
package org.jboss.resteasy.spi.metadata; |
| 2 |
|
| 3 |
import java.security.AccessController; |
| 4 |
import java.security.PrivilegedActionException; |
| 5 |
import java.security.PrivilegedExceptionAction; |
| 6 |
import org.jboss.resteasy.annotations.Body; |
| 7 |
import org.jboss.resteasy.annotations.Form; |
| 8 |
import org.jboss.resteasy.annotations.Query; |
| 9 |
import org.jboss.resteasy.annotations.Suspend; |
| 10 |
import org.jboss.resteasy.resteasy_jaxrs.i18n.LogMessages; |
| 11 |
import org.jboss.resteasy.resteasy_jaxrs.i18n.Messages; |
| 12 |
import org.jboss.resteasy.specimpl.ResteasyUriBuilder; |
| 13 |
import org.jboss.resteasy.spi.ResteasyDeployment; |
| 14 |
import org.jboss.resteasy.spi.ResteasyProviderFactory; |
| 15 |
import org.jboss.resteasy.util.IsHttpMethod; |
| 16 |
import org.jboss.resteasy.util.MediaTypeHelper; |
| 17 |
import org.jboss.resteasy.util.MethodHashing; |
| 18 |
import org.jboss.resteasy.util.PickConstructor; |
| 19 |
import org.jboss.resteasy.util.Types; |
| 20 |
|
| 21 |
import javax.ws.rs.BeanParam; |
| 22 |
import javax.ws.rs.Consumes; |
| 23 |
import javax.ws.rs.CookieParam; |
| 24 |
import javax.ws.rs.DefaultValue; |
| 25 |
import javax.ws.rs.Encoded; |
| 26 |
import javax.ws.rs.FormParam; |
| 27 |
import javax.ws.rs.HeaderParam; |
| 28 |
import javax.ws.rs.HttpMethod; |
| 29 |
import javax.ws.rs.MatrixParam; |
| 30 |
import javax.ws.rs.Path; |
| 31 |
import javax.ws.rs.PathParam; |
| 32 |
import javax.ws.rs.Produces; |
| 33 |
import javax.ws.rs.QueryParam; |
| 34 |
import javax.ws.rs.container.Suspended; |
| 35 |
import javax.ws.rs.core.Context; |
| 36 |
import javax.ws.rs.core.GenericType; |
| 37 |
import javax.ws.rs.core.MediaType; |
| 38 |
|
| 39 |
import java.lang.annotation.Annotation; |
| 40 |
import java.lang.reflect.AccessibleObject; |
| 41 |
import java.lang.reflect.Constructor; |
| 42 |
import java.lang.reflect.Field; |
| 43 |
import java.lang.reflect.Method; |
| 44 |
import java.lang.reflect.Modifier; |
| 45 |
import java.lang.reflect.Type; |
| 46 |
import java.util.ArrayList; |
| 47 |
import java.util.HashSet; |
| 48 |
import java.util.List; |
| 49 |
import java.util.Set; |
| 50 |
|
| 51 |
import static org.jboss.resteasy.util.FindAnnotation.findAnnotation; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
@SuppressWarnings(value = "unchecked") |
| 58 |
public class ResourceBuilder |
| 59 |
{ |
| 60 |
public static class ResourceClassBuilder |
| 61 |
{ |
| 62 |
final ResourceClass resourceClass; |
| 63 |
List<FieldParameter> fields = new ArrayList<FieldParameter>(); |
| 64 |
List<SetterParameter> setters = new ArrayList<SetterParameter>(); |
| 65 |
List<ResourceMethod> resourceMethods = new ArrayList<ResourceMethod>(); |
| 66 |
List<ResourceLocator> resourceLocators = new ArrayList<ResourceLocator>(); |
| 67 |
|
| 68 |
public ResourceClassBuilder(Class<?> root, String path) |
| 69 |
{ |
| 70 |
this.resourceClass = new ResourceClass(root, path); |
| 71 |
} |
| 72 |
|
| 73 |
public ResourceMethodBuilder method(Method method) |
| 74 |
{ |
| 75 |
return new ResourceMethodBuilder(this, method, method); |
| 76 |
} |
| 77 |
|
| 78 |
public ResourceMethodBuilder method(Method method, Method annotatedMethod) |
| 79 |
{ |
| 80 |
return new ResourceMethodBuilder(this, method, annotatedMethod); |
| 81 |
} |
| 82 |
|
| 83 |
public ResourceLocatorBuilder locator(Method method) |
| 84 |
{ |
| 85 |
return new ResourceLocatorBuilder(this, method, method); |
| 86 |
} |
| 87 |
|
| 88 |
public ResourceLocatorBuilder locator(Method method, Method annotatedMethod) |
| 89 |
{ |
| 90 |
return new ResourceLocatorBuilder(this, method, annotatedMethod); |
| 91 |
} |
| 92 |
|
| 93 |
public FieldParameterBuilder field(Field field) |
| 94 |
{ |
| 95 |
FieldParameter param = new FieldParameter(resourceClass, field); |
| 96 |
return new FieldParameterBuilder(this, param); |
| 97 |
} |
| 98 |
|
| 99 |
public SetterParameterBuilder setter(Method method) |
| 100 |
{ |
| 101 |
SetterParameter param = new SetterParameter(resourceClass, method, method); |
| 102 |
return new SetterParameterBuilder(this, param); |
| 103 |
} |
| 104 |
|
| 105 |
public ResourceConstructorBuilder constructor(Constructor constructor) |
| 106 |
{ |
| 107 |
return new ResourceConstructorBuilder(this, constructor); |
| 108 |
} |
| 109 |
|
| 110 |
public ResourceClass buildClass() |
| 111 |
{ |
| 112 |
resourceClass.fields = fields.toArray(new FieldParameter[fields.size()]); |
| 113 |
resourceClass.setters = setters.toArray(new SetterParameter[setters.size()]); |
| 114 |
resourceClass.resourceMethods = resourceMethods.toArray(new ResourceMethod[resourceMethods.size()]); |
| 115 |
resourceClass.resourceLocators = resourceLocators.toArray(new ResourceLocator[resourceLocators.size()]); |
| 116 |
|
| 117 |
return resourceClass; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public static class ParameterBuilder<T extends ParameterBuilder<T>> |
| 122 |
{ |
| 123 |
final Parameter parameter; |
| 124 |
|
| 125 |
public ParameterBuilder(Parameter parameter) |
| 126 |
{ |
| 127 |
this.parameter = parameter; |
| 128 |
} |
| 129 |
|
| 130 |
public T type(Class<?> type) |
| 131 |
{ |
| 132 |
parameter.type = type; |
| 133 |
return (T)this; |
| 134 |
} |
| 135 |
|
| 136 |
public T genericType(Type type) |
| 137 |
{ |
| 138 |
parameter.genericType = type; |
| 139 |
return (T)this; |
| 140 |
} |
| 141 |
|
| 142 |
public T type(GenericType type) |
| 143 |
{ |
| 144 |
parameter.type = type.getRawType(); |
| 145 |
parameter.genericType = type.getType(); |
| 146 |
return (T)this; |
| 147 |
} |
| 148 |
|
| 149 |
public T beanParam() |
| 150 |
{ |
| 151 |
parameter.paramType = Parameter.ParamType.BEAN_PARAM; |
| 152 |
return (T)this; |
| 153 |
} |
| 154 |
|
| 155 |
public T context() |
| 156 |
{ |
| 157 |
parameter.paramType = Parameter.ParamType.CONTEXT; |
| 158 |
return (T)this; |
| 159 |
} |
| 160 |
|
| 161 |
public T messageBody() |
| 162 |
{ |
| 163 |
parameter.paramType = Parameter.ParamType.MESSAGE_BODY; |
| 164 |
return (T)this; |
| 165 |
} |
| 166 |
|
| 167 |
public T encoded() |
| 168 |
{ |
| 169 |
parameter.encoded = true; |
| 170 |
return (T)this; |
| 171 |
} |
| 172 |
|
| 173 |
public T defaultValue(String defaultValue) |
| 174 |
{ |
| 175 |
parameter.defaultValue = defaultValue; |
| 176 |
return (T)this; |
| 177 |
} |
| 178 |
|
| 179 |
public T cookieParam(String name) |
| 180 |
{ |
| 181 |
parameter.paramType = Parameter.ParamType.COOKIE_PARAM; |
| 182 |
parameter.paramName = name; |
| 183 |
return (T)this; |
| 184 |
} |
| 185 |
|
| 186 |
public T formParam(String name) |
| 187 |
{ |
| 188 |
parameter.paramType = Parameter.ParamType.FORM_PARAM; |
| 189 |
parameter.paramName = name; |
| 190 |
return (T)this; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public T form(String prefix) |
| 200 |
{ |
| 201 |
parameter.paramType = Parameter.ParamType.FORM; |
| 202 |
parameter.paramName = prefix; |
| 203 |
return (T)this; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public T form() |
| 212 |
{ |
| 213 |
parameter.paramType = Parameter.ParamType.FORM; |
| 214 |
parameter.paramName = ""; |
| 215 |
return (T)this; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
public T headerParam(String name) |
| 220 |
{ |
| 221 |
parameter.paramType = Parameter.ParamType.HEADER_PARAM; |
| 222 |
parameter.paramName = name; |
| 223 |
return (T)this; |
| 224 |
} |
| 225 |
|
| 226 |
public T matrixParam(String name) |
| 227 |
{ |
| 228 |
parameter.paramType = Parameter.ParamType.MATRIX_PARAM; |
| 229 |
parameter.paramName = name; |
| 230 |
return (T)this; |
| 231 |
} |
| 232 |
|
| 233 |
public T pathParam(String name) |
| 234 |
{ |
| 235 |
parameter.paramType = Parameter.ParamType.PATH_PARAM; |
| 236 |
parameter.paramName = name; |
| 237 |
return (T)this; |
| 238 |
} |
| 239 |
|
| 240 |
public T queryParam(String name) |
| 241 |
{ |
| 242 |
parameter.paramType = Parameter.ParamType.QUERY_PARAM; |
| 243 |
parameter.paramName = name; |
| 244 |
return (T)this; |
| 245 |
} |
| 246 |
|
| 247 |
public T fromAnnotations() |
| 248 |
{ |
| 249 |
Annotation[] annotations = parameter.getAnnotations(); |
| 250 |
AccessibleObject injectTarget = parameter.getAccessibleObject(); |
| 251 |
Class<?> type = parameter.getResourceClass().getClazz(); |
| 252 |
|
| 253 |
parameter.encoded = findAnnotation(annotations, Encoded.class) != null || injectTarget.isAnnotationPresent(Encoded.class) || type.isAnnotationPresent(Encoded.class); |
| 254 |
DefaultValue defaultValue = findAnnotation(annotations, DefaultValue.class); |
| 255 |
if (defaultValue != null) parameter.defaultValue = defaultValue.value(); |
| 256 |
|
| 257 |
QueryParam queryParam; |
| 258 |
Query query; |
| 259 |
HeaderParam header; |
| 260 |
MatrixParam matrix; |
| 261 |
PathParam uriParam; |
| 262 |
CookieParam cookie; |
| 263 |
FormParam formParam; |
| 264 |
Form form; |
| 265 |
Suspend suspend; |
| 266 |
Suspended suspended; |
| 267 |
|
| 268 |
|
| 269 |
if ((queryParam = findAnnotation(annotations, QueryParam.class)) != null) |
| 270 |
{ |
| 271 |
parameter.paramType = Parameter.ParamType.QUERY_PARAM; |
| 272 |
parameter.paramName = queryParam.value(); |
| 273 |
} |
| 274 |
else if(( query = findAnnotation(annotations, Query.class))!= null) |
| 275 |
{ |
| 276 |
parameter.paramType = Parameter.ParamType.QUERY; |
| 277 |
parameter.paramName = ""; |
| 278 |
} |
| 279 |
else if ((header = findAnnotation(annotations, HeaderParam.class)) != null) |
| 280 |
{ |
| 281 |
parameter.paramType = Parameter.ParamType.HEADER_PARAM; |
| 282 |
parameter.paramName = header.value(); |
| 283 |
} |
| 284 |
else if ((formParam = findAnnotation(annotations, FormParam.class)) != null) |
| 285 |
{ |
| 286 |
parameter.paramType = Parameter.ParamType.FORM_PARAM; |
| 287 |
parameter.paramName = formParam.value(); |
| 288 |
} |
| 289 |
else if ((cookie = findAnnotation(annotations, CookieParam.class)) != null) |
| 290 |
{ |
| 291 |
parameter.paramType = Parameter.ParamType.COOKIE_PARAM; |
| 292 |
parameter.paramName = cookie.value(); |
| 293 |
} |
| 294 |
else if ((uriParam = findAnnotation(annotations, PathParam.class)) != null) |
| 295 |
{ |
| 296 |
parameter.paramType = Parameter.ParamType.PATH_PARAM; |
| 297 |
parameter.paramName = uriParam.value(); |
| 298 |
} |
| 299 |
else if ((form = findAnnotation(annotations, Form.class)) != null) |
| 300 |
{ |
| 301 |
parameter.paramType = Parameter.ParamType.FORM; |
| 302 |
parameter.paramName = form.prefix(); |
| 303 |
} |
| 304 |
else if (findAnnotation(annotations, BeanParam.class) != null) |
| 305 |
{ |
| 306 |
parameter.paramType = Parameter.ParamType.BEAN_PARAM; |
| 307 |
} |
| 308 |
else if ((matrix = findAnnotation(annotations, MatrixParam.class)) != null) |
| 309 |
{ |
| 310 |
parameter.paramType = Parameter.ParamType.MATRIX_PARAM; |
| 311 |
parameter.paramName = matrix.value(); |
| 312 |
} |
| 313 |
else if ((suspend = findAnnotation(annotations, Suspend.class)) != null) |
| 314 |
{ |
| 315 |
parameter.paramType = Parameter.ParamType.SUSPEND; |
| 316 |
parameter.suspendTimeout = suspend.value(); |
| 317 |
} |
| 318 |
else if (findAnnotation(annotations, Context.class) != null) |
| 319 |
{ |
| 320 |
parameter.paramType = Parameter.ParamType.CONTEXT; |
| 321 |
} |
| 322 |
else if ((suspended = findAnnotation(annotations, Suspended.class)) != null) |
| 323 |
{ |
| 324 |
parameter.paramType = Parameter.ParamType.SUSPENDED; |
| 325 |
} |
| 326 |
else if (javax.ws.rs.container.AsyncResponse.class.isAssignableFrom(type)) |
| 327 |
{ |
| 328 |
parameter.paramType = Parameter.ParamType.SUSPENDED; |
| 329 |
} |
| 330 |
else if (findAnnotation(annotations, Body.class) != null) |
| 331 |
{ |
| 332 |
parameter.paramType = Parameter.ParamType.MESSAGE_BODY; |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
parameter.paramType = Parameter.ParamType.UNKNOWN; |
| 337 |
} |
| 338 |
return (T)this; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
public static class ConstructorParameterBuilder extends ParameterBuilder<ConstructorParameterBuilder> |
| 343 |
{ |
| 344 |
final ResourceConstructorBuilder constructor; |
| 345 |
final ConstructorParameter param; |
| 346 |
|
| 347 |
public ConstructorParameterBuilder(ResourceConstructorBuilder builder, ConstructorParameter param) |
| 348 |
{ |
| 349 |
super(param); |
| 350 |
this.constructor = builder; |
| 351 |
this.param = param; |
| 352 |
} |
| 353 |
|
| 354 |
public ConstructorParameterBuilder param(int i) |
| 355 |
{ |
| 356 |
return constructor.param(i); |
| 357 |
} |
| 358 |
|
| 359 |
public ResourceClassBuilder buildConstructor() |
| 360 |
{ |
| 361 |
return constructor.buildConstructor(); |
| 362 |
} |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
public static class LocatorMethodParameterBuilder<T extends LocatorMethodParameterBuilder<T>> extends ParameterBuilder<T> |
| 367 |
{ |
| 368 |
final ResourceLocatorBuilder locator; |
| 369 |
final MethodParameter param; |
| 370 |
|
| 371 |
public LocatorMethodParameterBuilder(ResourceLocatorBuilder method, MethodParameter param) |
| 372 |
{ |
| 373 |
super(param); |
| 374 |
this.locator = method; |
| 375 |
this.param = param; |
| 376 |
} |
| 377 |
|
| 378 |
public T param(int i) |
| 379 |
{ |
| 380 |
return (T)locator.param(i); |
| 381 |
} |
| 382 |
|
| 383 |
public ResourceClassBuilder buildMethod() |
| 384 |
{ |
| 385 |
return locator.buildMethod(); |
| 386 |
} |
| 387 |
|
| 388 |
} |
| 389 |
|
| 390 |
public static class ResourceMethodParameterBuilder extends LocatorMethodParameterBuilder<ResourceMethodParameterBuilder> |
| 391 |
{ |
| 392 |
final ResourceMethodBuilder method; |
| 393 |
|
| 394 |
public ResourceMethodParameterBuilder(ResourceMethodBuilder method, MethodParameter param) |
| 395 |
{ |
| 396 |
super(method, param); |
| 397 |
this.method = method; |
| 398 |
} |
| 399 |
|
| 400 |
public ResourceMethodParameterBuilder suspended() |
| 401 |
{ |
| 402 |
method.method.asynchronous = true; |
| 403 |
parameter.paramType = Parameter.ParamType.SUSPENDED; |
| 404 |
return this; |
| 405 |
} |
| 406 |
|
| 407 |
public ResourceMethodParameterBuilder suspend(long timeout) |
| 408 |
{ |
| 409 |
method.method.asynchronous = true; |
| 410 |
parameter.paramType = Parameter.ParamType.SUSPEND; |
| 411 |
parameter.suspendTimeout = timeout; |
| 412 |
return this; |
| 413 |
} |
| 414 |
|
| 415 |
@Override |
| 416 |
public ResourceMethodParameterBuilder fromAnnotations() |
| 417 |
{ |
| 418 |
super.fromAnnotations(); |
| 419 |
if (param.paramType == Parameter.ParamType.SUSPEND || param.paramType == Parameter.ParamType.SUSPENDED) |
| 420 |
{ |
| 421 |
method.method.asynchronous = true; |
| 422 |
} |
| 423 |
else if (param.paramType == Parameter.ParamType.UNKNOWN) |
| 424 |
{ |
| 425 |
param.paramType = Parameter.ParamType.MESSAGE_BODY; |
| 426 |
} |
| 427 |
return this; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
public static class ResourceConstructorBuilder |
| 432 |
{ |
| 433 |
|
| 434 |
ResourceConstructor constructor; |
| 435 |
ResourceClassBuilder resourceClassBuilder; |
| 436 |
|
| 437 |
public ResourceConstructorBuilder(ResourceClassBuilder resourceClassBuilder, Constructor constructor) |
| 438 |
{ |
| 439 |
this.resourceClassBuilder = resourceClassBuilder; |
| 440 |
this.constructor = new ResourceConstructor(resourceClassBuilder.resourceClass, constructor); |
| 441 |
} |
| 442 |
public ConstructorParameterBuilder param(int i) |
| 443 |
{ |
| 444 |
return new ConstructorParameterBuilder(this, constructor.getParams()[i]); |
| 445 |
} |
| 446 |
|
| 447 |
public ResourceClassBuilder buildConstructor() |
| 448 |
{ |
| 449 |
resourceClassBuilder.resourceClass.constructor = constructor; |
| 450 |
return resourceClassBuilder; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
public static class ResourceLocatorBuilder<T extends ResourceLocatorBuilder<T>> |
| 455 |
{ |
| 456 |
|
| 457 |
ResourceLocator locator; |
| 458 |
ResourceClassBuilder resourceClassBuilder; |
| 459 |
|
| 460 |
ResourceLocatorBuilder() |
| 461 |
{ |
| 462 |
} |
| 463 |
|
| 464 |
public ResourceLocatorBuilder(ResourceClassBuilder resourceClassBuilder, Method method, Method annotatedMethod) |
| 465 |
{ |
| 466 |
this.resourceClassBuilder = resourceClassBuilder; |
| 467 |
this.locator = new ResourceLocator(resourceClassBuilder.resourceClass, method, annotatedMethod); |
| 468 |
} |
| 469 |
|
| 470 |
public T returnType(Class<?> type) |
| 471 |
{ |
| 472 |
locator.returnType = type; |
| 473 |
return (T)this; |
| 474 |
} |
| 475 |
|
| 476 |
public T genericReturnType(Type type) |
| 477 |
{ |
| 478 |
locator.genericReturnType = type; |
| 479 |
return (T)this; |
| 480 |
} |
| 481 |
|
| 482 |
public T returnType(GenericType type) |
| 483 |
{ |
| 484 |
locator.returnType = type.getRawType(); |
| 485 |
locator.genericReturnType = type.getType(); |
| 486 |
return (T)this; |
| 487 |
} |
| 488 |
|
| 489 |
public LocatorMethodParameterBuilder param(int i) |
| 490 |
{ |
| 491 |
return new LocatorMethodParameterBuilder(this, locator.getParams()[i]); |
| 492 |
} |
| 493 |
|
| 494 |
public ResourceClassBuilder buildMethod() |
| 495 |
{ |
| 496 |
ResteasyUriBuilder builder = new ResteasyUriBuilder(); |
| 497 |
if (locator.resourceClass.path != null) builder.path(locator.resourceClass.path); |
| 498 |
if (locator.path != null) builder.path(locator.path); |
| 499 |
String pathExpression = builder.getPath(); |
| 500 |
if (pathExpression == null) |
| 501 |
pathExpression = ""; |
| 502 |
locator.fullpath = pathExpression; |
| 503 |
if (locator.resourceClass.getClazz().isAnonymousClass()) |
| 504 |
{ |
| 505 |
locator.getMethod().setAccessible(true); |
| 506 |
} |
| 507 |
resourceClassBuilder.resourceLocators.add(locator); |
| 508 |
return resourceClassBuilder; |
| 509 |
} |
| 510 |
|
| 511 |
public T path(String path) |
| 512 |
{ |
| 513 |
locator.path = path; |
| 514 |
return (T)this; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
public static class ResourceMethodBuilder extends ResourceLocatorBuilder<ResourceMethodBuilder> |
| 519 |
{ |
| 520 |
ResourceMethod method; |
| 521 |
|
| 522 |
ResourceMethodBuilder(ResourceClassBuilder resourceClassBuilder, Method method, Method annotatedMethod) |
| 523 |
{ |
| 524 |
this.method = new ResourceMethod(resourceClassBuilder.resourceClass, method, annotatedMethod); |
| 525 |
this.locator = this.method; |
| 526 |
this.resourceClassBuilder = resourceClassBuilder; |
| 527 |
} |
| 528 |
|
| 529 |
public ResourceMethodBuilder httpMethod(String httpMethod) |
| 530 |
{ |
| 531 |
method.httpMethods.add(httpMethod.toUpperCase()); |
| 532 |
return this; |
| 533 |
} |
| 534 |
|
| 535 |
public ResourceMethodBuilder get() |
| 536 |
{ |
| 537 |
method.httpMethods.add(HttpMethod.GET); |
| 538 |
return this; |
| 539 |
} |
| 540 |
|
| 541 |
public ResourceMethodBuilder put() |
| 542 |
{ |
| 543 |
method.httpMethods.add(HttpMethod.PUT); |
| 544 |
return this; |
| 545 |
} |
| 546 |
|
| 547 |
public ResourceMethodBuilder post() |
| 548 |
{ |
| 549 |
method.httpMethods.add(HttpMethod.POST); |
| 550 |
return this; |
| 551 |
} |
| 552 |
|
| 553 |
public ResourceMethodBuilder delete() |
| 554 |
{ |
| 555 |
method.httpMethods.add(HttpMethod.DELETE); |
| 556 |
return this; |
| 557 |
} |
| 558 |
|
| 559 |
public ResourceMethodBuilder options() |
| 560 |
{ |
| 561 |
method.httpMethods.add(HttpMethod.OPTIONS); |
| 562 |
return this; |
| 563 |
} |
| 564 |
|
| 565 |
public ResourceMethodBuilder head() |
| 566 |
{ |
| 567 |
method.httpMethods.add(HttpMethod.HEAD); |
| 568 |
return this; |
| 569 |
} |
| 570 |
|
| 571 |
public ResourceMethodBuilder produces(MediaType... produces) |
| 572 |
{ |
| 573 |
method.produces = produces; |
| 574 |
return this; |
| 575 |
} |
| 576 |
|
| 577 |
public ResourceMethodBuilder produces(String... produces) |
| 578 |
{ |
| 579 |
MediaType[] types = parseMediaTypes(produces); |
| 580 |
method.produces = types; |
| 581 |
for (MediaType mt : types) |
| 582 |
{ |
| 583 |
if (!mt.getParameters().containsKey(MediaType.CHARSET_PARAMETER)) |
| 584 |
{ |
| 585 |
if (MediaTypeHelper.isTextLike(mt)) |
| 586 |
{ |
| 587 |
ResteasyDeployment deployment = ResteasyProviderFactory.getContextData(ResteasyDeployment.class); |
| 588 |
if (deployment != null && !deployment.isAddCharset()) |
| 589 |
{ |
| 590 |
LogMessages.LOGGER.mediaTypeLacksCharset(mt, method.getMethod().getName()); |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
return this; |
| 596 |
} |
| 597 |
|
| 598 |
protected MediaType[] parseMediaTypes(String[] produces) |
| 599 |
{ |
| 600 |
List<MediaType> mediaTypes = new ArrayList<MediaType>(); |
| 601 |
for (String produce : produces) |
| 602 |
{ |
| 603 |
String[] split = produce.split(","); |
| 604 |
for (String s : split) mediaTypes.add(MediaType.valueOf(s)); |
| 605 |
} |
| 606 |
MediaType[] types = new MediaType[mediaTypes.size()]; |
| 607 |
types = mediaTypes.toArray(types); |
| 608 |
return types; |
| 609 |
} |
| 610 |
|
| 611 |
public ResourceMethodBuilder consumes(MediaType... consumes) |
| 612 |
{ |
| 613 |
method.consumes = consumes; |
| 614 |
return this; |
| 615 |
} |
| 616 |
|
| 617 |
public ResourceMethodBuilder consumes(String... consumes) |
| 618 |
{ |
| 619 |
MediaType[] types = parseMediaTypes(consumes); |
| 620 |
method.consumes = types; |
| 621 |
return this; |
| 622 |
} |
| 623 |
|
| 624 |
public ResourceMethodParameterBuilder param(int i) |
| 625 |
{ |
| 626 |
return new ResourceMethodParameterBuilder(this, locator.getParams()[i]); |
| 627 |
} |
| 628 |
|
| 629 |
public ResourceClassBuilder buildMethod() |
| 630 |
{ |
| 631 |
ResteasyUriBuilder builder = new ResteasyUriBuilder(); |
| 632 |
if (method.resourceClass.path != null) builder.path(method.resourceClass.path); |
| 633 |
if (method.path != null) builder.path(method.path); |
| 634 |
String pathExpression = builder.getPath(); |
| 635 |
if (pathExpression == null) |
| 636 |
pathExpression = ""; |
| 637 |
method.fullpath = pathExpression; |
| 638 |
if (method.resourceClass.getClazz().isAnonymousClass()) |
| 639 |
{ |
| 640 |
method.getMethod().setAccessible(true); |
| 641 |
} |
| 642 |
resourceClassBuilder.resourceMethods.add(method); |
| 643 |
return resourceClassBuilder; |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
public static class FieldParameterBuilder extends ParameterBuilder<FieldParameterBuilder> |
| 648 |
{ |
| 649 |
FieldParameter field; |
| 650 |
ResourceClassBuilder resourceClassBuilder; |
| 651 |
FieldParameterBuilder(ResourceClassBuilder resourceClassBuilder, FieldParameter parameter) |
| 652 |
{ |
| 653 |
super(parameter); |
| 654 |
this.field = parameter; |
| 655 |
this.resourceClassBuilder = resourceClassBuilder; |
| 656 |
} |
| 657 |
|
| 658 |
public ResourceClassBuilder buildField() |
| 659 |
{ |
| 660 |
field.field.setAccessible(true); |
| 661 |
resourceClassBuilder.fields.add(field); |
| 662 |
return resourceClassBuilder; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
public static class SetterParameterBuilder extends ParameterBuilder<SetterParameterBuilder> |
| 667 |
{ |
| 668 |
SetterParameter setter; |
| 669 |
ResourceClassBuilder resourceClassBuilder; |
| 670 |
SetterParameterBuilder(ResourceClassBuilder resourceClassBuilder, SetterParameter parameter) |
| 671 |
{ |
| 672 |
super(parameter); |
| 673 |
this.setter = parameter; |
| 674 |
this.resourceClassBuilder = resourceClassBuilder; |
| 675 |
} |
| 676 |
|
| 677 |
public ResourceClassBuilder buildSetter() |
| 678 |
{ |
| 679 |
setter.setter.setAccessible(true); |
| 680 |
resourceClassBuilder.setters.add(setter); |
| 681 |
return resourceClassBuilder; |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
public static ResourceClassBuilder rootResource(Class<?> root) |
| 687 |
{ |
| 688 |
return new ResourceClassBuilder(root, "/"); |
| 689 |
} |
| 690 |
|
| 691 |
public static ResourceClassBuilder rootResource(Class<?> root, String path) |
| 692 |
{ |
| 693 |
return new ResourceClassBuilder(root, path); |
| 694 |
} |
| 695 |
|
| 696 |
public static ResourceClassBuilder locator(Class<?> root) |
| 697 |
{ |
| 698 |
return new ResourceClassBuilder(root, null); |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
public static ResourceConstructor constructor(Class<?> annotatedResourceClass) |
| 709 |
{ |
| 710 |
Constructor constructor = PickConstructor.pickPerRequestConstructor(annotatedResourceClass); |
| 711 |
if (constructor == null) |
| 712 |
{ |
| 713 |
throw new RuntimeException(Messages.MESSAGES.couldNotFindConstructor(annotatedResourceClass.getName())); |
| 714 |
} |
| 715 |
ResourceConstructorBuilder builder = rootResource(annotatedResourceClass).constructor(constructor); |
| 716 |
if (constructor.getParameterTypes() != null) |
| 717 |
{ |
| 718 |
for (int i = 0; i < constructor.getParameterTypes().length; i++) builder.param(i).fromAnnotations(); |
| 719 |
} |
| 720 |
return builder.buildConstructor().buildClass().getConstructor(); |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
public static ResourceClass rootResourceFromAnnotations(Class<?> clazz) |
| 729 |
{ |
| 730 |
return fromAnnotations(false, clazz); |
| 731 |
} |
| 732 |
|
| 733 |
public static ResourceClass locatorFromAnnotations(Class<?> clazz) |
| 734 |
{ |
| 735 |
return fromAnnotations(true, clazz); |
| 736 |
} |
| 737 |
|
| 738 |
private static ResourceClass fromAnnotations(boolean isLocator, Class<?> clazz) |
| 739 |
{ |
| 740 |
|
| 741 |
if (!clazz.isInterface() && clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Object.class) && clazz.isSynthetic()) |
| 742 |
{ |
| 743 |
clazz = clazz.getSuperclass(); |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
ResourceClassBuilder builder = null; |
| 748 |
if (isLocator) builder = locator(clazz); |
| 749 |
else |
| 750 |
{ |
| 751 |
Path path = clazz.getAnnotation(Path.class); |
| 752 |
if (path == null) builder = rootResource(clazz, null); |
| 753 |
else builder = rootResource(clazz, path.value()); |
| 754 |
} |
| 755 |
for (Method method : clazz.getMethods()) |
| 756 |
{ |
| 757 |
if(!method.isSynthetic() && !method.getDeclaringClass().equals(Object.class)) |
| 758 |
processMethod(isLocator, builder, clazz, method); |
| 759 |
|
| 760 |
} |
| 761 |
if (!clazz.isInterface()) |
| 762 |
{ |
| 763 |
processFields(builder, clazz); |
| 764 |
} |
| 765 |
processSetters(builder, clazz); |
| 766 |
return builder.buildClass(); |
| 767 |
} |
| 768 |
|
| 769 |
private static Method findAnnotatedInterfaceMethod(Class<?> root, Class<?> iface, Method implementation) |
| 770 |
{ |
| 771 |
for (Method method : iface.getMethods()) |
| 772 |
{ |
| 773 |
if (method.isSynthetic()) continue; |
| 774 |
|
| 775 |
if (!method.getName().equals(implementation.getName())) continue; |
| 776 |
if (method.getParameterTypes().length != implementation.getParameterTypes().length) continue; |
| 777 |
|
| 778 |
Method actual = Types.getImplementingMethod(root, method); |
| 779 |
if (!actual.equals(implementation)) continue; |
| 780 |
|
| 781 |
if (method.isAnnotationPresent(Path.class) || IsHttpMethod.getHttpMethods(method) != null) |
| 782 |
return method; |
| 783 |
|
| 784 |
} |
| 785 |
for (Class<?> extended : iface.getInterfaces()) |
| 786 |
{ |
| 787 |
Method m = findAnnotatedInterfaceMethod(root, extended, implementation); |
| 788 |
if(m != null) |
| 789 |
return m; |
| 790 |
} |
| 791 |
return null; |
| 792 |
} |
| 793 |
|
| 794 |
private static Method findAnnotatedMethod(Class<?> root, Method implementation) |
| 795 |
{ |
| 796 |
|
| 797 |
if (implementation.isAnnotationPresent(Path.class) || IsHttpMethod.getHttpMethods(implementation) != null) |
| 798 |
return implementation; |
| 799 |
|
| 800 |
if (implementation.isAnnotationPresent(Produces.class) |
| 801 |
|| implementation.isAnnotationPresent(Consumes.class)) |
| 802 |
{ |
| 803 |
|
| 804 |
return null; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
for (Class<?> clazz = implementation.getDeclaringClass().getSuperclass(); clazz != null; clazz = clazz.getSuperclass()) |
| 812 |
{ |
| 813 |
try |
| 814 |
{ |
| 815 |
Method method = clazz.getDeclaredMethod(implementation.getName(), implementation.getParameterTypes()); |
| 816 |
if (method.isAnnotationPresent(Path.class) || IsHttpMethod.getHttpMethods(method) != null) |
| 817 |
return method; |
| 818 |
if (method.isAnnotationPresent(Produces.class) |
| 819 |
|| method.isAnnotationPresent(Consumes.class)) |
| 820 |
{ |
| 821 |
|
| 822 |
return null; |
| 823 |
} |
| 824 |
} |
| 825 |
catch (NoSuchMethodException e) |
| 826 |
{ |
| 827 |
|
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
for (Class<?> clazz = root; clazz != null; clazz = clazz.getSuperclass()) |
| 835 |
{ |
| 836 |
Method method = null; |
| 837 |
for (Class<?> iface : clazz.getInterfaces()) |
| 838 |
{ |
| 839 |
Method m = findAnnotatedInterfaceMethod(root, iface, implementation); |
| 840 |
if (m != null) |
| 841 |
{ |
| 842 |
if(method != null && !m.equals(method)) |
| 843 |
throw new RuntimeException(Messages.MESSAGES.ambiguousInheritedAnnotations(implementation)); |
| 844 |
method = m; |
| 845 |
} |
| 846 |
} |
| 847 |
if (method != null) |
| 848 |
return method; |
| 849 |
} |
| 850 |
return null; |
| 851 |
} |
| 852 |
|
| 853 |
protected static void processFields(ResourceClassBuilder resourceClassBuilder, Class<?> root) |
| 854 |
{ |
| 855 |
do |
| 856 |
{ |
| 857 |
processDeclaredFields(resourceClassBuilder, root); |
| 858 |
root = root.getSuperclass(); |
| 859 |
|
| 860 |
} while (root != null && !root.equals(Object.class)); |
| 861 |
} |
| 862 |
|
| 863 |
protected static void processSetters(ResourceClassBuilder resourceClassBuilder, Class<?> root) |
| 864 |
{ |
| 865 |
HashSet<Long> hashes = new HashSet<Long>(); |
| 866 |
do |
| 867 |
{ |
| 868 |
processDeclaredSetters(resourceClassBuilder, root, hashes); |
| 869 |
root = root.getSuperclass(); |
| 870 |
} while (root != null && !root.equals(Object.class)); |
| 871 |
} |
| 872 |
|
| 873 |
protected static void processDeclaredFields(ResourceClassBuilder resourceClassBuilder, final Class<?> root) |
| 874 |
{ |
| 875 |
Field[] fieldList = new Field[0]; |
| 876 |
try { |
| 877 |
if (System.getSecurityManager() == null) { |
| 878 |
fieldList = root.getDeclaredFields(); |
| 879 |
} else { |
| 880 |
fieldList = AccessController.doPrivileged(new PrivilegedExceptionAction<Field[]>() { |
| 881 |
@Override |
| 882 |
public Field[] run() throws Exception { |
| 883 |
return root.getDeclaredFields(); |
| 884 |
} |
| 885 |
}); |
| 886 |
} |
| 887 |
} catch (PrivilegedActionException pae) { |
| 888 |
|
| 889 |
} |
| 890 |
|
| 891 |
for (Field field : fieldList) |
| 892 |
{ |
| 893 |
FieldParameterBuilder builder = resourceClassBuilder.field(field).fromAnnotations(); |
| 894 |
if (builder.field.paramType == Parameter.ParamType.MESSAGE_BODY && !field.isAnnotationPresent(Body.class)) continue; |
| 895 |
if (builder.field.paramType == Parameter.ParamType.UNKNOWN) continue; |
| 896 |
builder.buildField(); |
| 897 |
} |
| 898 |
} |
| 899 |
protected static void processDeclaredSetters(ResourceClassBuilder resourceClassBuilder, final Class<?> root, Set<Long> visitedHashes) |
| 900 |
{ |
| 901 |
Method[] methodList = new Method[0]; |
| 902 |
try { |
| 903 |
if (System.getSecurityManager() == null) { |
| 904 |
methodList = root.getDeclaredMethods(); |
| 905 |
} else { |
| 906 |
methodList = AccessController.doPrivileged(new PrivilegedExceptionAction<Method[]>() { |
| 907 |
@Override |
| 908 |
public Method[] run() throws Exception { |
| 909 |
return root.getDeclaredMethods(); |
| 910 |
} |
| 911 |
}); |
| 912 |
} |
| 913 |
} catch (PrivilegedActionException pae) { |
| 914 |
|
| 915 |
} |
| 916 |
|
| 917 |
for (Method method : methodList) |
| 918 |
{ |
| 919 |
if (!method.getName().startsWith("set")) continue; |
| 920 |
if (method.getParameterTypes().length != 1) continue; |
| 921 |
long hash = 0; |
| 922 |
try |
| 923 |
{ |
| 924 |
hash = MethodHashing.methodHash(method); |
| 925 |
} |
| 926 |
catch (Exception e) |
| 927 |
{ |
| 928 |
throw new RuntimeException(e); |
| 929 |
} |
| 930 |
if (!Modifier.isPrivate(method.getModifiers()) && visitedHashes.contains(hash)) continue; |
| 931 |
visitedHashes.add(hash); |
| 932 |
SetterParameterBuilder builder = resourceClassBuilder.setter(method).fromAnnotations(); |
| 933 |
if (builder.setter.paramType == Parameter.ParamType.MESSAGE_BODY && !method.isAnnotationPresent(Body.class)) continue; |
| 934 |
if (builder.setter.paramType == Parameter.ParamType.UNKNOWN) continue; |
| 935 |
builder.buildSetter(); |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
protected static void processMethod(boolean isLocator, ResourceClassBuilder resourceClassBuilder, Class<?> root, Method implementation) |
| 940 |
{ |
| 941 |
Method method = findAnnotatedMethod(root, implementation); |
| 942 |
if (method != null) |
| 943 |
{ |
| 944 |
Set<String> httpMethods = IsHttpMethod.getHttpMethods(method); |
| 945 |
|
| 946 |
ResourceLocatorBuilder resourceLocatorBuilder; |
| 947 |
|
| 948 |
if (httpMethods == null) |
| 949 |
{ |
| 950 |
resourceLocatorBuilder = resourceClassBuilder.locator(implementation, method); |
| 951 |
} |
| 952 |
else |
| 953 |
{ |
| 954 |
ResourceMethodBuilder resourceMethodBuilder = resourceClassBuilder.method(implementation, method); |
| 955 |
resourceLocatorBuilder = resourceMethodBuilder; |
| 956 |
|
| 957 |
for (String httpMethod : httpMethods) |
| 958 |
{ |
| 959 |
if (httpMethod.equalsIgnoreCase(HttpMethod.GET)) resourceMethodBuilder.get(); |
| 960 |
else if (httpMethod.equalsIgnoreCase(HttpMethod.PUT)) resourceMethodBuilder.put(); |
| 961 |
else if (httpMethod.equalsIgnoreCase(HttpMethod.POST)) resourceMethodBuilder.post(); |
| 962 |
else if (httpMethod.equalsIgnoreCase(HttpMethod.DELETE)) resourceMethodBuilder.delete(); |
| 963 |
else if (httpMethod.equalsIgnoreCase(HttpMethod.OPTIONS)) resourceMethodBuilder.options(); |
| 964 |
else if (httpMethod.equalsIgnoreCase(HttpMethod.HEAD)) resourceMethodBuilder.head(); |
| 965 |
else resourceMethodBuilder.httpMethod(httpMethod); |
| 966 |
} |
| 967 |
Produces produces = method.getAnnotation(Produces.class); |
| 968 |
if (produces == null) produces = resourceClassBuilder.resourceClass.getClazz().getAnnotation(Produces.class); |
| 969 |
if (produces == null) produces = method.getDeclaringClass().getAnnotation(Produces.class); |
| 970 |
if (produces != null) resourceMethodBuilder.produces(produces.value()); |
| 971 |
|
| 972 |
Consumes consumes = method.getAnnotation(Consumes.class); |
| 973 |
if (consumes == null) consumes = resourceClassBuilder.resourceClass.getClazz().getAnnotation(Consumes.class); |
| 974 |
if (consumes == null) consumes = method.getDeclaringClass().getAnnotation(Consumes.class); |
| 975 |
if (consumes != null) resourceMethodBuilder.consumes(consumes.value()); |
| 976 |
} |
| 977 |
Path methodPath = method.getAnnotation(Path.class); |
| 978 |
if (methodPath != null) resourceLocatorBuilder.path(methodPath.value()); |
| 979 |
for (int i = 0; i < resourceLocatorBuilder.locator.params.length; i++) |
| 980 |
{ |
| 981 |
resourceLocatorBuilder.param(i).fromAnnotations(); |
| 982 |
} |
| 983 |
resourceLocatorBuilder.buildMethod(); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
} |