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