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