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.http.remote;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016
017import java.lang.reflect.*;
018
019import org.apache.juneau.reflect.*;
020
021/**
022 * Contains the meta-data about a Java method on a remote class.
023 *
024 * <p>
025 * Captures the information in {@link Remote @Remote} annotations for caching and reuse.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.RestRpc">REST/RPC</a>
029 * </ul>
030 */
031public class RrpcInterfaceMethodMeta {
032
033   private final String url, path;
034   private final Method method;
035
036   /**
037    * Constructor.
038    *
039    * @param restUrl The absolute URL of the REST interface backing the interface proxy.
040    * @param m The Java method.
041    */
042   public RrpcInterfaceMethodMeta(final String restUrl, Method m) {
043      this.method = m;
044      this.path =  m.getName() + '/' + getMethodArgsSignature(m);
045      this.url = trimSlashes(restUrl) + '/' + urlEncode(path);
046   }
047
048   /**
049    * Given a Java method, returns the arguments signature.
050    *
051    * @param m The Java method.
052    * @param full Whether fully-qualified names should be used for arguments.
053    * @return The arguments signature for the specified method.
054    */
055   private static String getMethodArgsSignature(Method m) {
056      StringBuilder sb = new StringBuilder(128);
057      Class<?>[] pt = m.getParameterTypes();
058      if (pt.length == 0)
059         return "";
060      sb.append('(');
061      for (int i = 0; i < pt.length; i++) {
062         ClassInfo pti = ClassInfo.of(pt[i]);
063         if (i > 0)
064            sb.append(',');
065         pti.appendFullName(sb);
066      }
067      sb.append(')');
068      return sb.toString();
069   }
070
071   /**
072    * Returns the absolute URL of the REST interface invoked by this Java method.
073    *
074    * @return The absolute URL of the REST interface, never <jk>null</jk>.
075    */
076   public String getUri() {
077      return url;
078   }
079
080   /**
081    * Returns the HTTP path of this method.
082    *
083    * @return
084    *    The HTTP path of this method relative to the parent interface.
085    *    <br>Never <jk>null</jk>.
086    *    <br>Never has leading or trailing slashes.
087    */
088   public String getPath() {
089      return path;
090   }
091
092   /**
093    * Returns the underlying Java method that this metadata is about.
094    *
095    * @return
096    *    The underlying Java method that this metadata is about.
097    *    <br>Never <jk>null</jk>.
098    */
099   public Method getJavaMethod() {
100      return method;
101   }
102}