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