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