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.http.annotation;
018
019import static java.lang.annotation.ElementType.*;
020import static java.lang.annotation.RetentionPolicy.*;
021import static org.apache.juneau.internal.ArrayUtils.*;
022
023import java.lang.annotation.*;
024
025import org.apache.juneau.*;
026import org.apache.juneau.annotation.*;
027import org.apache.juneau.httppart.*;
028import org.apache.juneau.reflect.*;
029import org.apache.juneau.svl.*;
030
031/**
032 * Utility classes and methods for the {@link Request @Request} annotation.
033 *
034 * <h5 class='section'>See Also:</h5><ul>
035 * </ul>
036 */
037public class RequestAnnotation {
038
039   //-----------------------------------------------------------------------------------------------------------------
040   // Static
041   //-----------------------------------------------------------------------------------------------------------------
042
043   /** Default value */
044   public static final Request DEFAULT = create().build();
045
046   /**
047    * Instantiates a new builder for this class.
048    *
049    * @return A new builder object.
050    */
051   public static Builder create() {
052      return new Builder();
053   }
054
055   /**
056    * Instantiates a new builder for this class.
057    *
058    * @param on The targets this annotation applies to.
059    * @return A new builder object.
060    */
061   public static Builder create(Class<?>...on) {
062      return create().on(on);
063   }
064
065   /**
066    * Instantiates a new builder for this class.
067    *
068    * @param on The targets this annotation applies to.
069    * @return A new builder object.
070    */
071   public static Builder create(String...on) {
072      return create().on(on);
073   }
074
075   //-----------------------------------------------------------------------------------------------------------------
076   // Builder
077   //-----------------------------------------------------------------------------------------------------------------
078
079   /**
080    * Builder class.
081    *
082    * <h5 class='section'>See Also:</h5><ul>
083    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
084    * </ul>
085    */
086   public static class Builder extends TargetedAnnotationTBuilder<Builder> {
087
088      Class<? extends HttpPartParser> parser = HttpPartParser.Void.class;
089      Class<? extends HttpPartSerializer> serializer = HttpPartSerializer.Void.class;
090
091      /**
092       * Constructor.
093       */
094      protected Builder() {
095         super(Request.class);
096      }
097
098      /**
099       * Instantiates a new {@link Request @Request} object initialized with this builder.
100       *
101       * @return A new {@link Request @Request} object.
102       */
103      public Request build() {
104         return new Impl(this);
105      }
106
107      /**
108       * Sets the {@link Request#parser} property on this annotation.
109       *
110       * @param value The new value for this property.
111       * @return This object.
112       */
113      public Builder parser(Class<? extends HttpPartParser> value) {
114         this.parser = value;
115         return this;
116      }
117
118      /**
119       * Sets the {@link Request#serializer} property on this annotation.
120       *
121       * @param value The new value for this property.
122       * @return This object.
123       */
124      public Builder serializer(Class<? extends HttpPartSerializer> value) {
125         this.serializer = value;
126         return this;
127      }
128
129   }
130
131   //-----------------------------------------------------------------------------------------------------------------
132   // Implementation
133   //-----------------------------------------------------------------------------------------------------------------
134
135   private static class Impl extends TargetedAnnotationTImpl implements Request {
136
137      private final Class<? extends HttpPartParser> parser;
138      private final Class<? extends HttpPartSerializer> serializer;
139
140      Impl(Builder b) {
141         super(b);
142         this.parser = b.parser;
143         this.serializer = b.serializer;
144         postConstruct();
145      }
146
147      @Override /* Request */
148      public Class<? extends HttpPartParser> parser() {
149         return parser;
150      }
151
152      @Override /* Request */
153      public Class<? extends HttpPartSerializer> serializer() {
154         return serializer;
155      }
156   }
157
158   //-----------------------------------------------------------------------------------------------------------------
159   // Appliers
160   //-----------------------------------------------------------------------------------------------------------------
161
162   /**
163    * Applies targeted {@link Request} annotations to a {@link org.apache.juneau.BeanContext.Builder}.
164    */
165   public static class Applier extends AnnotationApplier<Request,BeanContext.Builder> {
166
167      /**
168       * Constructor.
169       *
170       * @param vr The resolver for resolving values in annotations.
171       */
172      public Applier(VarResolverSession vr) {
173         super(Request.class, BeanContext.Builder.class, vr);
174      }
175
176      @Override
177      public void apply(AnnotationInfo<Request> ai, BeanContext.Builder b) {
178         Request a = ai.inner();
179         if (isEmptyArray(a.on(), a.onClass()))
180            return;
181         b.annotations(a);
182      }
183   }
184
185   //-----------------------------------------------------------------------------------------------------------------
186   // Other
187   //-----------------------------------------------------------------------------------------------------------------
188
189   /**
190    * A collection of {@link Request @Request annotations}.
191    */
192   @Documented
193   @Target({METHOD,TYPE})
194   @Retention(RUNTIME)
195   @Inherited
196   public static @interface Array {
197
198      /**
199       * The child annotations.
200       *
201       * @return The annotation value.
202       */
203      Request[] value();
204   }
205}