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