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