001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.annotation; 018 019import static org.apache.juneau.commons.utils.CollectionUtils.*; 020 021import java.lang.annotation.*; 022import java.nio.charset.*; 023 024import org.apache.juneau.*; 025import org.apache.juneau.encoders.*; 026import org.apache.juneau.http.*; 027import org.apache.juneau.commons.annotation.*; 028import org.apache.juneau.commons.reflect.*; 029import org.apache.juneau.rest.*; 030import org.apache.juneau.rest.converter.*; 031import org.apache.juneau.rest.guard.*; 032import org.apache.juneau.rest.httppart.*; 033import org.apache.juneau.rest.matcher.*; 034import org.apache.juneau.serializer.*; 035import org.apache.juneau.svl.*; 036 037/** 038 * Utility classes and methods for the {@link RestPatch @RestPatch} annotation. 039 * 040 * <h5 class='section'>See Also:</h5><ul> 041 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestOpAnnotatedMethodBasics">@RestOp-Annotated Method Basics</a> 042 * </ul> 043 */ 044public class RestPatchAnnotation { 045 /** 046 * Builder class. 047 * 048 * <h5 class='section'>See Also:</h5><ul> 049 * <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)} 050 * </ul> 051 */ 052 @SuppressWarnings("unchecked") 053 public static class Builder extends AppliedAnnotationObject.BuilderM { 054 055 private Class<? extends RestConverter>[] converters = new Class[0]; 056 private Class<? extends RestGuard>[] guards = new Class[0]; 057 private Class<? extends RestMatcher>[] matchers = new Class[0]; 058 private Class<? extends Encoder>[] encoders = new Class[0]; 059 private Class<? extends Serializer>[] serializers = new Class[0]; 060 private Class<?>[] parsers = {}; 061 private OpSwagger swagger = OpSwaggerAnnotation.DEFAULT; 062 private String clientVersion = "", debug = "", defaultAccept = "", defaultCharset = "", defaultContentType = "", maxInput = "", rolesDeclared = "", roleGuard = "", summary = "", value = ""; 063 private String[] consumes = {}, defaultRequestFormData = {}, defaultRequestQueryData = {}, defaultRequestAttributes = {}, defaultRequestHeaders = {}, defaultResponseHeaders = {}, 064 description = {}, path = {}, produces = {}; 065 066 /** 067 * Constructor. 068 */ 069 protected Builder() { 070 super(RestPatch.class); 071 } 072 073 /** 074 * Instantiates a new {@link RestPatch @RestPatch} object initialized with this builder. 075 * 076 * @return A new {@link RestPatch @RestPatch} object. 077 */ 078 public RestPatch build() { 079 return new Object(this); 080 } 081 082 /** 083 * Sets the description property on this annotation. 084 * 085 * @param value The new value for this property. 086 * @return This object. 087 */ 088 public Builder description(String...value) { 089 description = value; 090 return this; 091 } 092 093 /** 094 * Sets the {@link RestPatch#clientVersion()} property on this annotation. 095 * 096 * @param value The new value for this property. 097 * @return This object. 098 */ 099 public Builder clientVersion(String value) { 100 clientVersion = value; 101 return this; 102 } 103 104 /** 105 * Sets the {@link RestPatch#consumes()} property on this annotation. 106 * 107 * @param value The new value for this property. 108 * @return This object. 109 */ 110 public Builder consumes(String...value) { 111 consumes = value; 112 return this; 113 } 114 115 /** 116 * Sets the {@link RestPatch#converters()} property on this annotation. 117 * 118 * @param value The new value for this property. 119 * @return This object. 120 */ 121 @SafeVarargs 122 public final Builder converters(Class<? extends RestConverter>...value) { 123 converters = value; 124 return this; 125 } 126 127 /** 128 * Sets the {@link RestPatch#debug()} property on this annotation. 129 * 130 * @param value The new value for this property. 131 * @return This object. 132 */ 133 public Builder debug(String value) { 134 debug = value; 135 return this; 136 } 137 138 /** 139 * Sets the {@link RestPatch#defaultAccept()} property on this annotation. 140 * 141 * @param value The new value for this property. 142 * @return This object. 143 */ 144 public Builder defaultAccept(String value) { 145 defaultAccept = value; 146 return this; 147 } 148 149 /** 150 * Sets the {@link RestPatch#defaultCharset()} property on this annotation. 151 * 152 * @param value The new value for this property. 153 * @return This object. 154 */ 155 public Builder defaultCharset(String value) { 156 defaultCharset = value; 157 return this; 158 } 159 160 /** 161 * Sets the {@link RestPatch#defaultContentType()} property on this annotation. 162 * 163 * @param value The new value for this property. 164 * @return This object. 165 */ 166 public Builder defaultContentType(String value) { 167 defaultContentType = value; 168 return this; 169 } 170 171 /** 172 * Sets the {@link RestPatch#defaultRequestAttributes()} property on this annotation. 173 * 174 * @param value The new value for this property. 175 * @return This object. 176 */ 177 public Builder defaultRequestAttributes(String...value) { 178 defaultRequestAttributes = value; 179 return this; 180 } 181 182 /** 183 * Sets the {@link RestPatch#defaultRequestFormData()} property on this annotation. 184 * 185 * @param value The new value for this property. 186 * @return This object. 187 */ 188 public Builder defaultRequestFormData(String...value) { 189 defaultRequestFormData = value; 190 return this; 191 } 192 193 /** 194 * Sets the {@link RestPatch#defaultRequestHeaders()} property on this annotation. 195 * 196 * @param value The new value for this property. 197 * @return This object. 198 */ 199 public Builder defaultRequestHeaders(String...value) { 200 defaultRequestHeaders = value; 201 return this; 202 } 203 204 /** 205 * Sets the {@link RestPatch#defaultRequestQueryData()} property on this annotation. 206 * 207 * @param value The new value for this property. 208 * @return This object. 209 */ 210 public Builder defaultRequestQueryData(String...value) { 211 defaultRequestQueryData = value; 212 return this; 213 } 214 215 /** 216 * Sets the {@link RestPatch#defaultResponseHeaders()} property on this annotation. 217 * 218 * @param value The new value for this property. 219 * @return This object. 220 */ 221 public Builder defaultResponseHeaders(String...value) { 222 defaultResponseHeaders = value; 223 return this; 224 } 225 226 /** 227 * Sets the {@link RestPatch#encoders()} property on this annotation. 228 * 229 * @param value The new value for this property. 230 * @return This object. 231 */ 232 @SafeVarargs 233 public final Builder encoders(Class<? extends Encoder>...value) { 234 encoders = value; 235 return this; 236 } 237 238 /** 239 * Sets the {@link RestPatch#guards()} property on this annotation. 240 * 241 * @param value The new value for this property. 242 * @return This object. 243 */ 244 @SafeVarargs 245 public final Builder guards(Class<? extends RestGuard>...value) { 246 guards = value; 247 return this; 248 } 249 250 /** 251 * Sets the {@link RestPatch#matchers()} property on this annotation. 252 * 253 * @param value The new value for this property. 254 * @return This object. 255 */ 256 @SafeVarargs 257 public final Builder matchers(Class<? extends RestMatcher>...value) { 258 matchers = value; 259 return this; 260 } 261 262 /** 263 * Sets the {@link RestPatch#maxInput()} property on this annotation. 264 * 265 * @param value The new value for this property. 266 * @return This object. 267 */ 268 public Builder maxInput(String value) { 269 maxInput = value; 270 return this; 271 } 272 273 /** 274 * Sets the {@link RestPatch#parsers()} property on this annotation. 275 * 276 * @param value The new value for this property. 277 * @return This object. 278 */ 279 public Builder parsers(Class<?>...value) { 280 parsers = value; 281 return this; 282 } 283 284 /** 285 * Sets the {@link RestPatch#path()} property on this annotation. 286 * 287 * @param value The new value for this property. 288 * @return This object. 289 */ 290 public Builder path(String...value) { 291 path = value; 292 return this; 293 } 294 295 /** 296 * Sets the {@link RestPatch#produces()} property on this annotation. 297 * 298 * @param value The new value for this property. 299 * @return This object. 300 */ 301 public Builder produces(String...value) { 302 produces = value; 303 return this; 304 } 305 306 /** 307 * Sets the {@link RestPatch#roleGuard()} property on this annotation. 308 * 309 * @param value The new value for this property. 310 * @return This object. 311 */ 312 public Builder roleGuard(String value) { 313 roleGuard = value; 314 return this; 315 } 316 317 /** 318 * Sets the {@link RestPatch#rolesDeclared()} property on this annotation. 319 * 320 * @param value The new value for this property. 321 * @return This object. 322 */ 323 public Builder rolesDeclared(String value) { 324 rolesDeclared = value; 325 return this; 326 } 327 328 /** 329 * Sets the {@link RestPatch#serializers()} property on this annotation. 330 * 331 * @param value The new value for this property. 332 * @return This object. 333 */ 334 @SafeVarargs 335 public final Builder serializers(Class<? extends Serializer>...value) { 336 serializers = value; 337 return this; 338 } 339 340 /** 341 * Sets the {@link RestPatch#summary()} property on this annotation. 342 * 343 * @param value The new value for this property. 344 * @return This object. 345 */ 346 public Builder summary(String value) { 347 summary = value; 348 return this; 349 } 350 351 /** 352 * Sets the {@link RestPatch#swagger()} property on this annotation. 353 * 354 * @param value The new value for this property. 355 * @return This object. 356 */ 357 public Builder swagger(OpSwagger value) { 358 swagger = value; 359 return this; 360 } 361 362 /** 363 * Sets the {@link RestPatch#value()} property on this annotation. 364 * 365 * @param value The new value for this property. 366 * @return This object. 367 */ 368 public Builder value(String value) { 369 this.value = value; 370 return this; 371 } 372 373 @Override /* Overridden from AppliedAnnotationObject.Builder */ 374 public Builder on(String...value) { 375 super.on(value); 376 return this; 377 } 378 379 @Override /* Overridden from AppliedAnnotationObject.BuilderM */ 380 public Builder on(java.lang.reflect.Method...value) { 381 super.on(value); 382 return this; 383 } 384 385 @Override /* Overridden from AppliedAnnotationObject.BuilderM */ 386 public Builder on(MethodInfo...value) { 387 super.on(value); 388 return this; 389 } 390 391 } 392 393 /** 394 * Applies {@link RestPatch} annotations to a {@link org.apache.juneau.rest.RestOpContext.Builder}. 395 */ 396 public static class RestOpContextApply extends AnnotationApplier<RestPatch,RestOpContext.Builder> { 397 398 /** 399 * Constructor. 400 * 401 * @param vr The resolver for resolving values in annotations. 402 */ 403 public RestOpContextApply(VarResolverSession vr) { 404 super(RestPatch.class, RestOpContext.Builder.class, vr); 405 } 406 407 @Override 408 public void apply(AnnotationInfo<RestPatch> ai, RestOpContext.Builder b) { 409 RestPatch a = ai.inner(); 410 411 b.httpMethod("patch"); 412 413 classes(a.serializers()).ifPresent(x -> b.serializers().set(x)); 414 classes(a.parsers()).ifPresent(x -> b.parsers().set(x)); 415 classes(a.encoders()).ifPresent(x -> b.encoders().set(x)); 416 stream(a.produces()).map(MediaType::of).forEach(x -> b.produces(x)); 417 stream(a.consumes()).map(MediaType::of).forEach(x -> b.consumes(x)); 418 stream(a.defaultRequestHeaders()).map(HttpHeaders::stringHeader).forEach(x -> b.defaultRequestHeaders().setDefault(x)); 419 stream(a.defaultResponseHeaders()).map(HttpHeaders::stringHeader).forEach(x -> b.defaultResponseHeaders().setDefault(x)); 420 stream(a.defaultRequestAttributes()).map(BasicNamedAttribute::ofPair).forEach(x -> b.defaultRequestAttributes().add(x)); 421 stream(a.defaultRequestQueryData()).map(HttpParts::basicPart).forEach(x -> b.defaultRequestQueryData().setDefault(x)); 422 stream(a.defaultRequestFormData()).map(HttpParts::basicPart).forEach(x -> b.defaultRequestFormData().setDefault(x)); 423 string(a.defaultAccept()).map(HttpHeaders::accept).ifPresent(x -> b.defaultRequestHeaders().setDefault(x)); 424 string(a.defaultContentType()).map(HttpHeaders::contentType).ifPresent(x -> b.defaultRequestHeaders().setDefault(x)); 425 b.converters().append(a.converters()); 426 b.guards().append(a.guards()); 427 b.matchers().append(a.matchers()); 428 string(a.clientVersion()).ifPresent(x -> b.clientVersion(x)); 429 string(a.defaultCharset()).map(Charset::forName).ifPresent(x -> b.defaultCharset(x)); 430 string(a.maxInput()).ifPresent(x -> b.maxInput(x)); 431 stream(a.path()).forEach(x -> b.path(x)); 432 string(a.value()).ifPresent(x -> b.path(x)); 433 cdl(a.rolesDeclared()).forEach(x -> b.rolesDeclared(x)); 434 string(a.roleGuard()).ifPresent(x -> b.roleGuard(x)); 435 string(a.debug()).map(Enablement::fromString).ifPresent(x -> b.debug(x)); 436 } 437 438 } 439 440 private static class Object extends AppliedAnnotationObject implements RestPatch { 441 442 private final String[] description; 443 private final Class<? extends RestConverter>[] converters; 444 private final Class<? extends RestGuard>[] guards; 445 private final Class<? extends RestMatcher>[] matchers; 446 private final Class<? extends Encoder>[] encoders; 447 private final Class<? extends Serializer>[] serializers; 448 private final Class<?>[] parsers; 449 private final OpSwagger swagger; 450 private final String clientVersion, debug, defaultAccept, defaultCharset, defaultContentType, maxInput, rolesDeclared, roleGuard, summary, value; 451 private final String[] consumes, defaultRequestFormData, defaultRequestQueryData, defaultRequestAttributes, defaultRequestHeaders, defaultResponseHeaders, path, produces; 452 453 Object(RestPatchAnnotation.Builder b) { 454 super(b); 455 description = copyOf(b.description); 456 clientVersion = b.clientVersion; 457 consumes = copyOf(b.consumes); 458 converters = copyOf(b.converters); 459 debug = b.debug; 460 defaultAccept = b.defaultAccept; 461 defaultCharset = b.defaultCharset; 462 defaultContentType = b.defaultContentType; 463 defaultRequestFormData = copyOf(b.defaultRequestFormData); 464 defaultRequestQueryData = copyOf(b.defaultRequestQueryData); 465 defaultRequestAttributes = copyOf(b.defaultRequestAttributes); 466 defaultRequestHeaders = copyOf(b.defaultRequestHeaders); 467 defaultResponseHeaders = copyOf(b.defaultResponseHeaders); 468 encoders = copyOf(b.encoders); 469 guards = copyOf(b.guards); 470 matchers = copyOf(b.matchers); 471 maxInput = b.maxInput; 472 parsers = copyOf(b.parsers); 473 path = copyOf(b.path); 474 produces = copyOf(b.produces); 475 roleGuard = b.roleGuard; 476 rolesDeclared = b.rolesDeclared; 477 serializers = copyOf(b.serializers); 478 summary = b.summary; 479 swagger = b.swagger; 480 value = b.value; 481 } 482 483 @Override /* Overridden from RestPatch */ 484 public String clientVersion() { 485 return clientVersion; 486 } 487 488 @Override /* Overridden from RestPatch */ 489 public String[] consumes() { 490 return consumes; 491 } 492 493 @Override /* Overridden from RestPatch */ 494 public Class<? extends RestConverter>[] converters() { 495 return converters; 496 } 497 498 @Override /* Overridden from RestPatch */ 499 public String debug() { 500 return debug; 501 } 502 503 @Override /* Overridden from RestPatch */ 504 public String defaultAccept() { 505 return defaultAccept; 506 } 507 508 @Override /* Overridden from RestPatch */ 509 public String defaultCharset() { 510 return defaultCharset; 511 } 512 513 @Override /* Overridden from RestPatch */ 514 public String defaultContentType() { 515 return defaultContentType; 516 } 517 518 @Override /* Overridden from RestPatch */ 519 public String[] defaultRequestAttributes() { 520 return defaultRequestAttributes; 521 } 522 523 @Override /* Overridden from RestPatch */ 524 public String[] defaultRequestFormData() { 525 return defaultRequestFormData; 526 } 527 528 @Override /* Overridden from RestPatch */ 529 public String[] defaultRequestHeaders() { 530 return defaultRequestHeaders; 531 } 532 533 @Override /* Overridden from RestPatch */ 534 public String[] defaultRequestQueryData() { 535 return defaultRequestQueryData; 536 } 537 538 @Override /* Overridden from RestPatch */ 539 public String[] defaultResponseHeaders() { 540 return defaultResponseHeaders; 541 } 542 543 @Override /* Overridden from RestPatch */ 544 public Class<? extends Encoder>[] encoders() { 545 return encoders; 546 } 547 548 @Override /* Overridden from RestPatch */ 549 public Class<? extends RestGuard>[] guards() { 550 return guards; 551 } 552 553 @Override /* Overridden from RestPatch */ 554 public Class<? extends RestMatcher>[] matchers() { 555 return matchers; 556 } 557 558 @Override /* Overridden from RestPatch */ 559 public String maxInput() { 560 return maxInput; 561 } 562 563 @Override /* Overridden from RestPatch */ 564 public Class<?>[] parsers() { 565 return parsers; 566 } 567 568 @Override /* Overridden from RestPatch */ 569 public String[] path() { 570 return path; 571 } 572 573 @Override /* Overridden from RestPatch */ 574 public String[] produces() { 575 return produces; 576 } 577 578 @Override /* Overridden from RestPatch */ 579 public String roleGuard() { 580 return roleGuard; 581 } 582 583 @Override /* Overridden from RestPatch */ 584 public String rolesDeclared() { 585 return rolesDeclared; 586 } 587 588 @Override /* Overridden from RestPatch */ 589 public Class<? extends Serializer>[] serializers() { 590 return serializers; 591 } 592 593 @Override /* Overridden from RestPatch */ 594 public String summary() { 595 return summary; 596 } 597 598 @Override /* Overridden from RestPatch */ 599 public OpSwagger swagger() { 600 return swagger; 601 } 602 603 @Override /* Overridden from RestPatch */ 604 public String value() { 605 return value; 606 } 607 608 @Override /* Overridden from annotation */ 609 public String[] description() { 610 return description; 611 } 612 } 613 614 /** Default value */ 615 public static final RestPatch DEFAULT = create().build(); 616 617 /** 618 * Instantiates a new builder for this class. 619 * 620 * @return A new builder object. 621 */ 622 public static Builder create() { 623 return new Builder(); 624 } 625}