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