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