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.Utils.*;
020import static org.apache.juneau.internal.CollectionUtils.map;
021
022import java.lang.reflect.*;
023import java.util.*;
024
025import org.apache.juneau.*;
026import org.apache.juneau.common.utils.*;
027import org.apache.juneau.reflect.*;
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 final Map<Method,RrpcInterfaceMethodMeta> methods;
042   private final Map<String,RrpcInterfaceMethodMeta> methodsByPath;
043   private final String path;
044   private final Class<?> c;
045
046   /**
047    * Constructor.
048    *
049    * @param c
050    *    The interface class annotated with a {@link Remote @Remote} annotation.
051    *    <br>Note that the annotations are optional.
052    * @param uri
053    *    The absolute URL of the remote REST interface that implements this proxy interface.
054    *    <br>This is only used on the client side.
055    */
056   public RrpcInterfaceMeta(Class<?> c, String uri) {
057      this.c = c;
058      Value<String> path = Value.of("");
059      ClassInfo ci = ClassInfo.of(c);
060
061      ci.forEachAnnotation(Remote.class, x -> isNotEmpty(x.path()), x -> path.set(StringUtils.trimSlashes(x.path())));
062
063      Map<Method,RrpcInterfaceMethodMeta> methods = map();
064      ci.forEachPublicMethod(
065         x -> true,
066         x -> methods.put(x.inner(), new RrpcInterfaceMethodMeta(uri, x.inner()))
067      );
068
069      Map<String,RrpcInterfaceMethodMeta> methodsByPath = map();
070      methods.values().forEach(x -> methodsByPath.put(x.getPath(), x));
071
072      this.methods = u(methods);
073      this.methodsByPath = u(methodsByPath);
074      this.path = path.get();
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}