001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.httppart.bean;
018
019import java.lang.reflect.*;
020import java.util.*;
021
022import org.apache.juneau.common.utils.*;
023import org.apache.juneau.cp.*;
024import org.apache.juneau.http.annotation.*;
025import org.apache.juneau.httppart.*;
026import org.apache.juneau.reflect.*;
027
028/**
029 * Represents the metadata gathered from a getter method of a class annotated with {@link Response}.
030 *
031 * <h5 class='section'>See Also:</h5><ul>
032 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HttpPartSerializersParsers">HTTP Part Serializers and Parsers</a>
033 * </ul>
034 */
035public class ResponseBeanPropertyMeta {
036
037   static ResponseBeanPropertyMeta.Builder create(HttpPartType partType, HttpPartSchema schema, MethodInfo m) {
038      return new Builder().partType(partType).schema(schema).getter(m.inner());
039   }
040
041   static ResponseBeanPropertyMeta.Builder create(HttpPartType partType, MethodInfo m) {
042      return new Builder().partType(partType).getter(m.inner());
043   }
044
045   //-----------------------------------------------------------------------------------------------------------------
046   // Instance
047   //-----------------------------------------------------------------------------------------------------------------
048
049   private final Method getter;
050   private final HttpPartType partType;
051   private final Optional<HttpPartSerializer> serializer;
052   private final Optional<HttpPartParser> parser;
053   private final HttpPartSchema schema;
054
055   ResponseBeanPropertyMeta(Builder b, Optional<HttpPartSerializer> serializer, Optional<HttpPartParser> parser) {
056      this.partType = b.partType;
057      this.schema = b.schema;
058      this.getter = b.getter;
059      this.serializer = serializer.isPresent() ? serializer : BeanCreator.of(HttpPartSerializer.class).type(schema.getSerializer()).execute();
060      this.parser = parser.isPresent() ? parser : BeanCreator.of(HttpPartParser.class).type(schema.getParser()).execute();
061   }
062
063   static class Builder {
064      HttpPartType partType;
065      HttpPartSchema schema = HttpPartSchema.DEFAULT;
066      String name;
067      Method getter;
068
069      Builder name(String value) {
070         name = value;
071         return this;
072      }
073
074      Builder getter(Method value) {
075         getter = value;
076         return this;
077      }
078
079      Builder partType(HttpPartType value) {
080         partType = value;
081         return this;
082      }
083
084      Builder schema(HttpPartSchema value) {
085         schema = value;
086         return this;
087      }
088
089      ResponseBeanPropertyMeta build(Optional<HttpPartSerializer> serializer, Optional<HttpPartParser> parser) {
090         return new ResponseBeanPropertyMeta(this, serializer, parser);
091      }
092   }
093
094   /**
095    * Returns the HTTP part name for this property (the query parameter name for example).
096    *
097    * @return The HTTP part name, or <jk>null</jk> if it doesn't have a part name.
098    */
099   public Optional<String> getPartName() {
100      return Utils.opt(schema == null ? null : schema.getName());
101   }
102
103   /**
104    * Returns the name of the Java method getter that defines this property.
105    *
106    * @return
107    *    The name of the Java method getter that defines this property.
108    *    <br>Never <jk>null</jk>.
109    */
110   public Method getGetter() {
111      return getter;
112   }
113
114   /**
115    * Returns the HTTP part type for this property (query parameter for example).
116    *
117    * @return
118    *    The HTTP part type for this property.
119    *    <br>Never <jk>null</jk>.
120    */
121   public HttpPartType getPartType() {
122      return partType;
123   }
124
125   /**
126    * Returns the serializer to use for serializing the bean property value.
127    *
128    * @return The serializer to use for serializing the bean property value.
129    */
130   public Optional<HttpPartSerializer> getSerializer() {
131      return serializer;
132   }
133
134   /**
135    * Returns the parser to use for parsing the bean property value.
136    *
137    * @return The parser to use for parsing the bean property value.
138    */
139   public Optional<HttpPartParser> getParser() {
140      return parser;
141   }
142
143   /**
144    * Returns the schema information gathered from annotations on the method and return type.
145    *
146    * @return
147    *    The schema information gathered from annotations on the method and return type.
148    *    <br>Never <jk>null</jk>.
149    */
150   public HttpPartSchema getSchema() {
151      return schema;
152   }
153}