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.internal.StringUtils.*;
016import java.lang.reflect.*;
017import java.util.*;
018
019import org.apache.juneau.collections.*;
020import org.apache.juneau.reflect.*;
021import org.apache.juneau.remote.*;
022
023/**
024 * Contains the meta-data about a remote proxy REST interface.
025 *
026 * <p>
027 * Captures the information in {@link Remote @Remote} annotations for caching and reuse.
028 *
029 * <ul class='seealso'>
030 *    <li class='link'>{@doc RestRpc}
031 * </ul>
032 */
033public class RrpcInterfaceMeta {
034
035   private final Map<Method,RrpcInterfaceMethodMeta> methods;
036   private final Map<String,RrpcInterfaceMethodMeta> methodsByPath;
037   private final String path;
038   private final Class<?> c;
039
040   /**
041    * Constructor.
042    *
043    * @param c
044    *    The interface class annotated with a {@link Remote @Remote} annotation.
045    *    <br>Note that the annotations are optional.
046    * @param uri
047    *    The absolute URL of the remote REST interface that implements this proxy interface.
048    *    <br>This is only used on the client side.
049    */
050   @SuppressWarnings("deprecation")
051   public RrpcInterfaceMeta(Class<?> c, String uri) {
052      this.c = c;
053      String path = "";
054      ClassInfo ci = ClassInfo.of(c);
055
056      for (RemoteInterface r : ci.getAnnotations(RemoteInterface.class))
057         if (! r.path().isEmpty())
058            path = trimSlashes(r.path());
059
060      for (Remote r : ci.getAnnotations(Remote.class))
061         if (! r.path().isEmpty())
062            path = trimSlashes(r.path());
063
064      AMap<Method,RrpcInterfaceMethodMeta> methods = AMap.of();
065      for (MethodInfo m : ci.getPublicMethods())
066         methods.put(m.inner(), new RrpcInterfaceMethodMeta(uri, m.inner()));
067
068      AMap<String,RrpcInterfaceMethodMeta> methodsByPath = AMap.of();
069      for (RrpcInterfaceMethodMeta rmm : methods.values())
070         methodsByPath.put(rmm.getPath(), rmm);
071
072      this.methods = methods.unmodifiable();
073      this.methodsByPath = methodsByPath.unmodifiable();
074      this.path = path;
075   }
076
077   /**
078    * Returns a map of all methods on this interface proxy keyed by HTTP path.
079    *
080    * @return
081    *    A map of all methods on this remote interface keyed by HTTP path.
082    *    <br>The keys never have leading slashes.
083    *    <br>The map is never <jk>null</jk>.
084    */
085   public Map<String,RrpcInterfaceMethodMeta> getMethodsByPath() {
086      return methodsByPath;
087   }
088
089   /**
090    * Returns the metadata about the specified method on this interface proxy.
091    *
092    * @param m The method to look up.
093    * @return Metadata about the method or <jk>null</jk> if no metadata was found.
094    */
095   public RrpcInterfaceMethodMeta getMethodMeta(Method m) {
096      return methods.get(m);
097   }
098
099   /**
100    * Returns the metadata about the specified method on this interface proxy by the path defined on the method.
101    *
102    * @param p The HTTP path to look for.
103    * @return Metadata about the method or <jk>null</jk> if no metadata was found.
104    */
105   public RrpcInterfaceMethodMeta getMethodMetaByPath(String p) {
106      return methodsByPath.get(p);
107   }
108
109   /**
110    * Returns the Java class of this interface.
111    *
112    * @return
113    *    The Java class of this interface.
114    *    <br>Never <jk>null</jk>.
115    */
116   public Class<?> getJavaClass() {
117      return c;
118   }
119
120   /**
121    * Returns the HTTP path of this interface.
122    *
123    * @return
124    *    The HTTP path of this interface.
125    *    <br>Never <jk>null</jk>.
126    *    <br>Never has leading or trailing slashes.
127    */
128   public String getPath() {
129      return path;
130   }
131}