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 RestcProxies}
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
037   RemoteMethodArg(int index, HttpPartType partType, HttpPartSchema schema) {
038      this.index = index;
039      this.partType = partType;
040      this.serializer = createSerializer(partType, schema);
041      this.schema = schema;
042   }
043
044   private static HttpPartSerializer createSerializer(HttpPartType partType, HttpPartSchema schema) {
045      return castOrCreate(HttpPartSerializer.class, schema.getSerializer());
046   }
047
048   /**
049    * Returns the name of the HTTP part.
050    *
051    * @return The name of the HTTP part.
052    */
053   public String getName() {
054      return schema.getName();
055   }
056
057   /**
058    * Returns whether the <c>skipIfEmpty</c> flag was found in the schema.
059    *
060    * @return <jk>true</jk> if the <c>skipIfEmpty</c> flag was found in the schema.
061    */
062   public boolean isSkipIfEmpty() {
063      return schema.isSkipIfEmpty();
064   }
065
066   /**
067    * Returns the method argument index.
068    *
069    * @return The method argument index.
070    */
071   public int getIndex() {
072      return index;
073   }
074
075   /**
076    * Returns the HTTP part type.
077    *
078    * @return The HTTP part type.  Never <jk>null</jk>.
079    */
080   public HttpPartType getPartType() {
081      return partType;
082   }
083
084   /**
085    * Returns the HTTP part serializer to use for serializing this part.
086    *
087    * @param _default The default serializer to use if the serializer was not defined via annotations.
088    * @return The HTTP part serializer, or the default if not specified.
089    */
090   public HttpPartSerializerSession getSerializer(HttpPartSerializerSession _default) {
091      return serializer == null ? _default : serializer.createPartSession(null);
092   }
093
094   /**
095    * Returns the HTTP part schema information about this part.
096    *
097    * @return The HTTP part schema information, or <jk>null</jk> if not found.
098    */
099   public HttpPartSchema getSchema() {
100      return schema;
101   }
102
103   static RemoteMethodArg create(ParamInfo mpi) {
104      int i = mpi.getIndex();
105      if (mpi.hasAnnotation(Header.class)) {
106         return new RemoteMethodArg(i, HEADER, HttpPartSchema.create(Header.class, mpi));
107      } else if (mpi.hasAnnotation(Query.class)) {
108         return new RemoteMethodArg(i, QUERY, HttpPartSchema.create(Query.class, mpi));
109      } else if (mpi.hasAnnotation(FormData.class)) {
110         return new RemoteMethodArg(i, FORMDATA, HttpPartSchema.create(FormData.class, mpi));
111      } else if (mpi.hasAnnotation(Path.class)) {
112         return new RemoteMethodArg(i, PATH, HttpPartSchema.create(Path.class, mpi));
113      } else if (mpi.hasAnnotation(Body.class)) {
114         return new RemoteMethodArg(i, BODY, HttpPartSchema.create(Body.class, mpi));
115      }
116      return null;
117   }
118}