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.rest.client.remote;
014
015import static org.apache.juneau.internal.ClassUtils.*;
016import static org.apache.juneau.httppart.HttpPartType.*;
017
018import java.lang.reflect.*;
019
020import org.apache.juneau.http.annotation.*;
021import org.apache.juneau.httppart.*;
022
023/**
024 * Represents the metadata about an annotated argument of a method on a REST proxy class.
025 *
026 * <h5 class='section'>See Also:</h5>
027 * <ul class='doctree'>
028 *    <li class='link'>{@doc juneau-rest-client.RestProxies}
029 * </ul>
030 */
031public final class RemoteMethodArg {
032
033   private final int index;
034   private final HttpPartType partType;
035   private final HttpPartSerializer serializer;
036   private final HttpPartSchema schema;
037   private final String name;
038   private final boolean skipIfEmpty;
039
040   RemoteMethodArg(int index, HttpPartType partType, HttpPartSchema schema) {
041      this.index = index;
042      this.partType = partType;
043      this.serializer = createSerializer(partType, schema);
044      this.schema = schema;
045      this.name = schema == null ? null : schema.getName();
046      this.skipIfEmpty = schema == null ? false : schema.isSkipIfEmpty();
047   }
048
049   private static HttpPartSerializer createSerializer(HttpPartType partType, HttpPartSchema schema) {
050      if (schema == null)
051         return null;
052      return newInstance(HttpPartSerializer.class, schema.getSerializer());
053   }
054
055   /**
056    * Returns the name of the HTTP part.
057    *
058    * @return The name of the HTTP part.
059    */
060   public String getName() {
061      return name;
062   }
063
064   /**
065    * Returns whether the <code>skipIfEmpty</code> flag was found in the schema.
066    *
067    * @return <jk>true</jk> if the <code>skipIfEmpty</code> flag was found in the schema.
068    */
069   public boolean isSkipIfEmpty() {
070      return skipIfEmpty;
071   }
072
073   /**
074    * Returns the method argument index.
075    *
076    * @return The method argument index.
077    */
078   public int getIndex() {
079      return index;
080   }
081
082   /**
083    * Returns the HTTP part type.
084    *
085    * @return The HTTP part type.  Never <jk>null</jk>.
086    */
087   public HttpPartType getPartType() {
088      return partType;
089   }
090
091   /**
092    * Returns the HTTP part serializer to use for serializing this part.
093    *
094    * @param _default The default serializer to use if the serializer was not defined via annotations.
095    * @return The HTTP part serializer, or <jk>null</jk> if not specified.
096    */
097   public HttpPartSerializer getSerializer(HttpPartSerializer _default) {
098      return serializer == null ? _default : serializer;
099   }
100
101   /**
102    * Returns the HTTP part schema information about this part.
103    *
104    * @return The HTTP part schema information, or <jk>null</jk> if not found.
105    */
106   public HttpPartSchema getSchema() {
107      return schema;
108   }
109
110   @SuppressWarnings("deprecation")
111   static RemoteMethodArg create(Method m, int i) {
112      if (hasAnnotation(Header.class, m, i)) {
113         return new RemoteMethodArg(i, HEADER, HttpPartSchema.create(Header.class, m, i));
114      } else if (hasAnnotation(Query.class, m, i)) {
115         return new RemoteMethodArg(i, QUERY, HttpPartSchema.create(Query.class, m, i));
116      } else if (hasAnnotation(FormData.class, m, i)) {
117         return new RemoteMethodArg(i, FORMDATA, HttpPartSchema.create(FormData.class, m, i));
118      } else if (hasAnnotation(Path.class, m, i)) {
119         return new RemoteMethodArg(i, PATH, HttpPartSchema.create(Path.class, m, i));
120      } else if (hasAnnotation(Body.class, m, i)) {
121         return new RemoteMethodArg(i, BODY, HttpPartSchema.create(Body.class, m, i));
122      } else if (hasAnnotation(org.apache.juneau.remoteable.Header.class, m, i)) {
123         return new RemoteMethodArg(i, HEADER, HttpPartSchema.create(org.apache.juneau.remoteable.Header.class, m, i));
124      } else if (hasAnnotation(org.apache.juneau.remoteable.Query.class, m, i)) {
125         return new RemoteMethodArg(i, QUERY, HttpPartSchema.create(org.apache.juneau.remoteable.Query.class, m, i));
126      } else if (hasAnnotation(org.apache.juneau.remoteable.FormData.class, m, i)) {
127         return new RemoteMethodArg(i, FORMDATA, HttpPartSchema.create(org.apache.juneau.remoteable.FormData.class, m, i));
128      } else if (hasAnnotation(org.apache.juneau.remoteable.Path.class, m, i)) {
129         return new RemoteMethodArg(i, PATH, HttpPartSchema.create(org.apache.juneau.remoteable.Path.class, m, i));
130      } else if (hasAnnotation(org.apache.juneau.remoteable.Body.class, m, i)) {
131         return new RemoteMethodArg(i, BODY, HttpPartSchema.create(org.apache.juneau.remoteable.Body.class, m, i));
132      }
133      return null;
134   }
135}