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