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