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