blob: db221bf5b1ae96167abb478089d16bd211d5a8fd [file] [log] [blame]
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.rest.annotation;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import static org.apache.juneau.internal.ArrayUtils.*;
import static org.apache.juneau.rest.RestContext.*;
import static org.apache.juneau.rest.RestMethodContext.*;
import static org.apache.juneau.rest.util.RestUtils.*;
import java.lang.annotation.*;
import org.apache.juneau.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.http.*;
import org.apache.juneau.http.header.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.reflect.*;
import org.apache.juneau.rest.*;
import org.apache.juneau.svl.*;
/**
* Utility classes and methods for the {@link RestMethod @RestMethod} annotation.
*/
public class RestMethodAnnotation {
/** Default value */
public static final RestMethod DEFAULT = create().build();
/**
* Instantiates a new builder for this class.
*
* @return A new builder object.
*/
public static Builder create() {
return new Builder();
}
/**
* Builder class for the {@link RestMethod} annotation.
*
* <ul class='seealso'>
* <li class='jm'>{@link BeanContextBuilder#annotations(Annotation...)}
* </ul>
*/
@SuppressWarnings("unchecked")
public static class Builder extends TargetedAnnotationMBuilder {
Class<? extends RestConverter>[] converters = new Class[0];
Class<? extends RestGuard>[] guards = new Class[0];
Class<? extends RestMatcher>[] matchers = new Class[0];
Class<? extends RestMethodContext> context = RestMethodContext.Null.class;
Class<?>[] encoders=new Class<?>[0], parsers=new Class<?>[0], serializers=new Class<?>[0];
int priority = 0;
MethodSwagger swagger = MethodSwaggerAnnotation.DEFAULT;
String clientVersion="", debug="", defaultAccept="", defaultCharset="", defaultContentType="", maxInput="", method="", rolesDeclared="", roleGuard="", summary="", value="";
String[] consumes={}, defaultFormData={}, defaultQuery={}, defaultRequestAttributes={}, defaultRequestHeaders={}, defaultResponseHeaders={}, description={}, path={}, produces={};
/**
* Constructor.
*/
public Builder() {
super(RestMethod.class);
}
/**
* Instantiates a new {@link RestMethod @RestMethod} object initialized with this builder.
*
* @return A new {@link RestMethod @RestMethod} object.
*/
public RestMethod build() {
return new Impl(this);
}
/**
* Sets the {@link RestMethod#clientVersion()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder clientVersion(String value) {
this.clientVersion = value;
return this;
}
/**
* Sets the {@link RestMethod#consumes()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder consumes(String...value) {
this.consumes = value;
return this;
}
/**
* Sets the {@link RestMethod#context()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder context(Class<? extends RestMethodContext> value) {
this.context = value;
return this;
}
/**
* Sets the {@link RestMethod#converters()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder converters(Class<? extends RestConverter>...value) {
this.converters = value;
return this;
}
/**
* Sets the {@link RestMethod#debug()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder debug(String value) {
this.debug = value;
return this;
}
/**
* Sets the {@link RestMethod#defaultAccept()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder defaultAccept(String value) {
this.defaultAccept = value;
return this;
}
/**
* Sets the {@link RestMethod#defaultCharset()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder defaultCharset(String value) {
this.defaultCharset = value;
return this;
}
/**
* Sets the {@link RestMethod#defaultContentType()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder defaultContentType(String value) {
this.defaultContentType = value;
return this;
}
/**
* Sets the {@link RestMethod#defaultFormData()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder defaultFormData(String...value) {
this.defaultFormData = value;
return this;
}
/**
* Sets the {@link RestMethod#defaultQuery()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder defaultQuery(String...value) {
this.defaultQuery = value;
return this;
}
/**
* Sets the {@link RestMethod#defaultRequestAttributes()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder defaultRequestAttributes(String...value) {
this.defaultRequestAttributes = value;
return this;
}
/**
* Sets the {@link RestMethod#defaultRequestHeaders()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder defaultRequestHeaders(String...value) {
this.defaultRequestHeaders = value;
return this;
}
/**
* Sets the {@link RestMethod#defaultResponseHeaders()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder defaultResponseHeaders(String...value) {
this.defaultResponseHeaders = value;
return this;
}
/**
* Sets the {@link RestMethod#description()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder description(String...value) {
this.description = value;
return this;
}
/**
* Sets the {@link RestMethod#encoders()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder encoders(Class<?>...value) {
this.encoders = value;
return this;
}
/**
* Sets the {@link RestMethod#guards()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder guards(Class<? extends RestGuard>...value) {
this.guards = value;
return this;
}
/**
* Sets the {@link RestMethod#matchers()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder matchers(Class<? extends RestMatcher>...value) {
this.matchers = value;
return this;
}
/**
* Sets the {@link RestMethod#maxInput()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder maxInput(String value) {
this.maxInput = value;
return this;
}
/**
* Sets the {@link RestMethod#method()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder method(String value) {
this.method = value;
return this;
}
/**
* Sets the {@link RestMethod#parsers()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder parsers(Class<?>...value) {
this.parsers = value;
return this;
}
/**
* Sets the {@link RestMethod#path()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder path(String...value) {
this.path = value;
return this;
}
/**
* Sets the {@link RestMethod#priority()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder priority(int value) {
this.priority = value;
return this;
}
/**
* Sets the {@link RestMethod#produces()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder produces(String...value) {
this.produces = value;
return this;
}
/**
* Sets the {@link RestMethod#roleGuard()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder roleGuard(String value) {
this.roleGuard = value;
return this;
}
/**
* Sets the {@link RestMethod#rolesDeclared()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder rolesDeclared(String value) {
this.rolesDeclared = value;
return this;
}
/**
* Sets the {@link RestMethod#serializers()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder serializers(Class<?>...value) {
this.serializers = value;
return this;
}
/**
* Sets the {@link RestMethod#summary()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder summary(String value) {
this.summary = value;
return this;
}
/**
* Sets the {@link RestMethod#swagger()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder swagger(MethodSwagger value) {
this.swagger = value;
return this;
}
/**
* Sets the {@link RestMethod#value()} property on this annotation.
*
* @param value The new value for this property.
* @return This object (for method chaining).
*/
public Builder value(String value) {
this.value = value;
return this;
}
// <FluentSetters>
@Override /* GENERATED - TargetedAnnotationBuilder */
public Builder on(String...values) {
super.on(values);
return this;
}
@Override /* GENERATED - TargetedAnnotationTMBuilder */
public Builder on(java.lang.reflect.Method...value) {
super.on(value);
return this;
}
// </FluentSetters>
}
private static class Impl extends TargetedAnnotationImpl implements RestMethod {
private final Class<? extends RestConverter>[] converters;
private final Class<? extends RestGuard>[] guards;
private final Class<? extends RestMatcher>[] matchers;
private final Class<? extends RestMethodContext> context;
private final Class<?>[] encoders, parsers, serializers;
private final int priority;
private final MethodSwagger swagger;
private final String clientVersion, debug, defaultAccept, defaultCharset, defaultContentType, maxInput, method, rolesDeclared, roleGuard, summary, value;
private final String[] consumes, defaultFormData, defaultQuery, defaultRequestAttributes, defaultRequestHeaders, defaultResponseHeaders, description, path, produces;
Impl(Builder b) {
super(b);
this.clientVersion = b.clientVersion;
this.consumes = copyOf(b.consumes);
this.context = b.context;
this.converters = copyOf(b.converters);
this.debug = b.debug;
this.defaultAccept = b.defaultAccept;
this.defaultCharset = b.defaultCharset;
this.defaultContentType = b.defaultContentType;
this.defaultFormData = copyOf(b.defaultFormData);
this.defaultQuery = copyOf(b.defaultQuery);
this.defaultRequestAttributes = copyOf(b.defaultRequestAttributes);
this.defaultRequestHeaders = copyOf(b.defaultRequestHeaders);
this.defaultResponseHeaders = copyOf(b.defaultResponseHeaders);
this.description = copyOf(b.description);
this.encoders = copyOf(b.encoders);
this.guards = copyOf(b.guards);
this.matchers = copyOf(b.matchers);
this.maxInput = b.maxInput;
this.method = b.method;
this.parsers = copyOf(b.parsers);
this.path = copyOf(b.path);
this.priority = b.priority;
this.produces = copyOf(b.produces);
this.roleGuard = b.roleGuard;
this.rolesDeclared = b.rolesDeclared;
this.serializers = copyOf(b.serializers);
this.summary = b.summary;
this.swagger = b.swagger;
this.value = b.value;
postConstruct();
}
@Override /* RestMethod */
public String clientVersion() {
return clientVersion;
}
@Override /* RestMethod */
public String[] consumes() {
return consumes;
}
@Override /* RestMethod */
public Class<? extends RestMethodContext> context() {
return context;
}
@Override /* RestMethod */
public Class<? extends RestConverter>[] converters() {
return converters;
}
@Override /* RestMethod */
public String debug() {
return debug;
}
@Override /* RestMethod */
public String defaultAccept() {
return defaultAccept;
}
@Override /* RestMethod */
public String defaultCharset() {
return defaultCharset;
}
@Override /* RestMethod */
public String defaultContentType() {
return defaultContentType;
}
@Override /* RestMethod */
public String[] defaultFormData() {
return defaultFormData;
}
@Override /* RestMethod */
public String[] defaultQuery() {
return defaultQuery;
}
@Override /* RestMethod */
public String[] defaultRequestAttributes() {
return defaultRequestAttributes;
}
@Override /* RestMethod */
public String[] defaultRequestHeaders() {
return defaultRequestHeaders;
}
@Override /* RestMethod */
public String[] defaultResponseHeaders() {
return defaultResponseHeaders;
}
@Override /* RestMethod */
public String[] description() {
return description;
}
@Override /* RestMethod */
public Class<?>[] encoders() {
return encoders;
}
@Override /* RestMethod */
public Class<? extends RestGuard>[] guards() {
return guards;
}
@Override /* RestMethod */
public Class<? extends RestMatcher>[] matchers() {
return matchers;
}
@Override /* RestMethod */
public String maxInput() {
return maxInput;
}
@Override /* RestMethod */
public String method() {
return method;
}
@Override /* RestMethod */
public Class<?>[] parsers() {
return parsers;
}
@Override /* RestMethod */
public String[] path() {
return path;
}
@Override /* RestMethod */
public int priority() {
return priority;
}
@Override /* RestMethod */
public String[] produces() {
return produces;
}
@Override /* RestMethod */
public String roleGuard() {
return roleGuard;
}
@Override /* RestMethod */
public String rolesDeclared() {
return rolesDeclared;
}
@Override /* RestMethod */
public Class<?>[] serializers() {
return serializers;
}
@Override /* RestMethod */
public String summary() {
return summary;
}
@Override /* RestMethod */
public MethodSwagger swagger() {
return swagger;
}
@Override /* RestMethod */
public String value() {
return value;
}
}
/**
* Applies {@link RestMethod} annotations to a {@link PropertyStoreBuilder}.
*/
public static class Apply extends ConfigApply<RestMethod> {
/**
* Constructor.
*
* @param c The annotation class.
* @param vr The resolver for resolving values in annotations.
*/
public Apply(Class<RestMethod> c, VarResolverSession vr) {
super(c, vr);
}
@Override
public void apply(AnnotationInfo<RestMethod> ai, PropertyStoreBuilder psb, VarResolverSession vr) {
RestMethod a = ai.getAnnotation();
psb.set(REST_serializers, merge(ConverterUtils.toType(psb.peek(REST_serializers), Object[].class), a.serializers()));
psb.set(REST_parsers, merge(ConverterUtils.toType(psb.peek(REST_parsers), Object[].class), a.parsers()));
psb.set(REST_encoders, merge(ConverterUtils.toType(psb.peek(REST_encoders), Object[].class), a.encoders()));
psb.setIf(a.context() != RestMethodContext.Null.class, RESTMETHOD_context, a.context());
psb.setIfNotEmpty(REST_produces, stringList(a.produces()));
psb.setIfNotEmpty(REST_consumes, stringList(a.consumes()));
stringStream(a.defaultRequestHeaders()).map(x -> BasicHeader.ofPair(x)).forEach(x -> psb.appendTo(RESTMETHOD_defaultRequestHeaders, x));
stringStream(a.defaultResponseHeaders()).map(x -> BasicHeader.ofPair(x)).forEach(x -> psb.appendTo(RESTMETHOD_defaultResponseHeaders, x));
stringStream(a.defaultRequestAttributes()).map(x -> BasicNamedAttribute.ofPair(x)).forEach(x -> psb.appendTo(RESTMETHOD_defaultRequestAttributes, x));
stringStream(a.defaultQuery()).map(x -> BasicNameValuePair.ofPair(x)).forEach(x -> psb.appendTo(RESTMETHOD_defaultQuery, x));
stringStream(a.defaultFormData()).map(x -> BasicNameValuePair.ofPair(x)).forEach(x -> psb.appendTo(RESTMETHOD_defaultFormData, x));
psb.appendToIfNotEmpty(REST_defaultRequestHeaders, Accept.of(string(a.defaultAccept())));
psb.appendToIfNotEmpty(REST_defaultRequestHeaders, ContentType.of(string(a.defaultContentType())));
psb.prependTo(REST_converters, a.converters());
psb.prependTo(REST_guards, reverse(a.guards()));
psb.prependTo(RESTMETHOD_matchers, a.matchers());
psb.setIfNotEmpty(RESTMETHOD_clientVersion, a.clientVersion());
psb.setIfNotEmpty(REST_defaultCharset, string(a.defaultCharset()));
psb.setIfNotEmpty(REST_maxInput, string(a.maxInput()));
stringStream(a.path()).forEach(x -> psb.prependTo(RESTMETHOD_path, x));
cdStream(a.rolesDeclared()).forEach(x -> psb.addTo(REST_rolesDeclared, x));
psb.addToIfNotEmpty(REST_roleGuard, string(a.roleGuard()));
psb.setIfNotEmpty(RESTMETHOD_httpMethod, string(a.method()));
psb.setIf(a.priority() != 0, RESTMETHOD_priority, a.priority());
psb.setIfNotEmpty(RESTMETHOD_debug, string(a.debug()));
String v = StringUtils.trim(string(a.value()));
if (v != null) {
int i = v.indexOf(' ');
if (i == -1) {
psb.set(RESTMETHOD_httpMethod, v);
} else {
psb.set(RESTMETHOD_httpMethod, v.substring(0, i).trim());
psb.prependTo(RESTMETHOD_path, v.substring(i).trim());
}
}
}
}
/**
* A collection of {@link RestMethod @RestMethod annotations}.
*/
@Documented
@Target({METHOD,TYPE})
@Retention(RUNTIME)
@Inherited
public static @interface Array {
/**
* The child annotations.
*/
RestMethod[] value();
}
}