001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http.annotation;
014
015import static java.lang.annotation.ElementType.*;
016import static java.lang.annotation.RetentionPolicy.*;
017import static org.apache.juneau.internal.ArrayUtils.*;
018
019import java.lang.annotation.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.annotation.*;
023import org.apache.juneau.httppart.*;
024import org.apache.juneau.reflect.*;
025import org.apache.juneau.svl.*;
026
027/**
028 * Utility classes and methods for the {@link Request @Request} annotation.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 * </ul>
032 */
033public class RequestAnnotation {
034
035   //-----------------------------------------------------------------------------------------------------------------
036   // Static
037   //-----------------------------------------------------------------------------------------------------------------
038
039   /** Default value */
040   public static final Request DEFAULT = create().build();
041
042   /**
043    * Instantiates a new builder for this class.
044    *
045    * @return A new builder object.
046    */
047   public static Builder create() {
048      return new Builder();
049   }
050
051   /**
052    * Instantiates a new builder for this class.
053    *
054    * @param on The targets this annotation applies to.
055    * @return A new builder object.
056    */
057   public static Builder create(Class<?>...on) {
058      return create().on(on);
059   }
060
061   /**
062    * Instantiates a new builder for this class.
063    *
064    * @param on The targets this annotation applies to.
065    * @return A new builder object.
066    */
067   public static Builder create(String...on) {
068      return create().on(on);
069   }
070
071   //-----------------------------------------------------------------------------------------------------------------
072   // Builder
073   //-----------------------------------------------------------------------------------------------------------------
074
075   /**
076    * Builder class.
077    *
078    * <h5 class='section'>See Also:</h5><ul>
079    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
080    * </ul>
081    */
082   public static class Builder extends TargetedAnnotationTBuilder {
083
084      Class<? extends HttpPartParser> parser = HttpPartParser.Void.class;
085      Class<? extends HttpPartSerializer> serializer = HttpPartSerializer.Void.class;
086
087      /**
088       * Constructor.
089       */
090      protected Builder() {
091         super(Request.class);
092      }
093
094      /**
095       * Instantiates a new {@link Request @Request} object initialized with this builder.
096       *
097       * @return A new {@link Request @Request} object.
098       */
099      public Request build() {
100         return new Impl(this);
101      }
102
103      /**
104       * Sets the {@link Request#parser} property on this annotation.
105       *
106       * @param value The new value for this property.
107       * @return This object.
108       */
109      public Builder parser(Class<? extends HttpPartParser> value) {
110         this.parser = value;
111         return this;
112      }
113
114      /**
115       * Sets the {@link Request#serializer} property on this annotation.
116       *
117       * @param value The new value for this property.
118       * @return This object.
119       */
120      public Builder serializer(Class<? extends HttpPartSerializer> value) {
121         this.serializer = value;
122         return this;
123      }
124
125      // <FluentSetters>
126
127      @Override /* GENERATED - TargetedAnnotationBuilder */
128      public Builder on(String...values) {
129         super.on(values);
130         return this;
131      }
132
133      @Override /* GENERATED - TargetedAnnotationTBuilder */
134      public Builder on(java.lang.Class<?>...value) {
135         super.on(value);
136         return this;
137      }
138
139      @Override /* GENERATED - TargetedAnnotationTBuilder */
140      public Builder onClass(java.lang.Class<?>...value) {
141         super.onClass(value);
142         return this;
143      }
144
145      // </FluentSetters>
146   }
147
148   //-----------------------------------------------------------------------------------------------------------------
149   // Implementation
150   //-----------------------------------------------------------------------------------------------------------------
151
152   private static class Impl extends TargetedAnnotationTImpl implements Request {
153
154      private final Class<? extends HttpPartParser> parser;
155      private final Class<? extends HttpPartSerializer> serializer;
156
157      Impl(Builder b) {
158         super(b);
159         this.parser = b.parser;
160         this.serializer = b.serializer;
161         postConstruct();
162      }
163
164      @Override /* Request */
165      public Class<? extends HttpPartParser> parser() {
166         return parser;
167      }
168
169      @Override /* Request */
170      public Class<? extends HttpPartSerializer> serializer() {
171         return serializer;
172      }
173   }
174
175   //-----------------------------------------------------------------------------------------------------------------
176   // Appliers
177   //-----------------------------------------------------------------------------------------------------------------
178
179   /**
180    * Applies targeted {@link Request} annotations to a {@link org.apache.juneau.BeanContext.Builder}.
181    */
182   public static class Applier extends AnnotationApplier<Request,BeanContext.Builder> {
183
184      /**
185       * Constructor.
186       *
187       * @param vr The resolver for resolving values in annotations.
188       */
189      public Applier(VarResolverSession vr) {
190         super(Request.class, BeanContext.Builder.class, vr);
191      }
192
193      @Override
194      public void apply(AnnotationInfo<Request> ai, BeanContext.Builder b) {
195         Request a = ai.inner();
196         if (isEmptyArray(a.on(), a.onClass()))
197            return;
198         b.annotations(a);
199      }
200   }
201
202   //-----------------------------------------------------------------------------------------------------------------
203   // Other
204   //-----------------------------------------------------------------------------------------------------------------
205
206   /**
207    * A collection of {@link Request @Request annotations}.
208    */
209   @Documented
210   @Target({METHOD,TYPE})
211   @Retention(RUNTIME)
212   @Inherited
213   public static @interface Array {
214
215      /**
216       * The child annotations.
217       *
218       * @return The annotation value.
219       */
220      Request[] value();
221   }
222}