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