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.httppart.bean;
014
015import static org.apache.juneau.internal.ClassUtils.*;
016import static org.apache.juneau.httppart.bean.Utils.*;
017
018import java.lang.annotation.*;
019import java.lang.reflect.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.http.annotation.*;
023import org.apache.juneau.httppart.*;
024import org.apache.juneau.internal.*;
025
026/**
027 * Represents the metadata gathered from a getter method of a class annotated with {@link Request}.
028 */
029public class RequestBeanPropertyMeta {
030
031   static RequestBeanPropertyMeta.Builder create(HttpPartType partType, Class<? extends Annotation> c, Method m) {
032      HttpPartSchemaBuilder sb = HttpPartSchema.create().name(getPropertyName(m));
033      for (Annotation a : getAnnotationsParentFirst(c, m))
034         sb.apply(a);
035      return new Builder().partType(partType).schema(sb.build()).getter(m);
036   }
037
038   //-----------------------------------------------------------------------------------------------------------------
039   // Instance
040   //-----------------------------------------------------------------------------------------------------------------
041
042   private final Method getter;
043   private final HttpPartType partType;
044   private final HttpPartSerializer serializer;
045   private final HttpPartParser parser;
046   private final HttpPartSchema schema;
047
048   RequestBeanPropertyMeta(Builder b, HttpPartSerializer serializer, HttpPartParser parser) {
049      this.partType = b.partType;
050      this.schema = b.schema;
051      this.getter = b.getter;
052      this.serializer = schema.getSerializer() == null ? serializer : ClassUtils.newInstance(HttpPartSerializer.class, schema.getSerializer(), true, b.ps);
053      this.parser = schema.getParser() == null ? parser : ClassUtils.newInstance(HttpPartParser.class, schema.getParser(), true, b.ps);
054   }
055
056   static class Builder {
057      HttpPartType partType;
058      HttpPartSchema schema;
059      Method getter;
060      PropertyStore ps = PropertyStore.DEFAULT;
061
062      Builder getter(Method value) {
063         getter = value;
064         return this;
065      }
066
067      Builder partType(HttpPartType value) {
068         partType = value;
069         return this;
070      }
071
072      Builder schema(HttpPartSchema value) {
073         schema = value;
074         return this;
075      }
076
077      RequestBeanPropertyMeta build(HttpPartSerializer serializer, HttpPartParser parser) {
078         return new RequestBeanPropertyMeta(this, serializer, parser);
079      }
080   }
081
082   /**
083    * Returns the HTTP part name for this property (query parameter name for example).
084    *
085    * @return The HTTP part name, or <jk>null</jk> if it doesn't have a part name.
086    */
087   public String getPartName() {
088      return schema == null ? null : schema.getName();
089   }
090
091   /**
092    * Returns the name of the Java method getter that defines this property.
093    *
094    * @return
095    *    The name of the Java method getter that defines this property.
096    *    <br>Never <jk>null</jk>.
097    */
098   public Method getGetter() {
099      return getter;
100   }
101
102   /**
103    * Returns the HTTP part type for this property (query parameter for example).
104    *
105    * @return
106    *    The HTTP part type for this property.
107    *    <br>Never <jk>null</jk>.
108    */
109   public HttpPartType getPartType() {
110      return partType;
111   }
112
113   /**
114    * Returns the serializer to use for serializing the bean property value.
115    *
116    * @param _default The default serializer to use if not defined on the annotation.
117    * @return The serializer to use for serializing the bean property value.
118    */
119   public HttpPartSerializer getSerializer(HttpPartSerializer _default) {
120      return serializer == null ? _default : serializer;
121   }
122
123   /**
124    * Returns the parser to use for parsing the bean property value.
125    *
126    * @param _default The default parsing to use if not defined on the annotation.
127    * @return The parsing to use for serializing the bean property value.
128    */
129   public HttpPartParser getParser(HttpPartParser _default) {
130      return parser == null ? _default : parser;
131   }
132
133   /**
134    * Returns the schema information gathered from annotations on the method and return type.
135    *
136    * @return
137    *    The schema information gathered from annotations on the method and return type.
138    *    <br>Never <jk>null</jk>.
139    */
140   public HttpPartSchema getSchema() {
141      return schema;
142   }
143}