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