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.common.internal.StringUtils.*; 016import static org.apache.juneau.internal.CollectionUtils.*; 017 018import java.lang.reflect.*; 019import java.util.*; 020 021import org.apache.juneau.*; 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 Remote @Remote} annotations for caching and reuse. 029 * 030 * <h5 class='section'>See Also:</h5><ul> 031 * <li class='link'><a class="doclink" href="../../../../../index.html#jrs.RestRpc">REST/RPC</a> 032 * </ul> 033 */ 034public class RrpcInterfaceMeta { 035 036 private final Map<Method,RrpcInterfaceMethodMeta> methods; 037 private final Map<String,RrpcInterfaceMethodMeta> 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 Remote @Remote} 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 RrpcInterfaceMeta(Class<?> c, String uri) { 052 this.c = c; 053 Value<String> path = Value.of(""); 054 ClassInfo ci = ClassInfo.of(c); 055 056 ci.forEachAnnotation(Remote.class, x -> isNotEmpty(x.path()), x -> path.set(trimSlashes(x.path()))); 057 058 Map<Method,RrpcInterfaceMethodMeta> methods = map(); 059 ci.forEachPublicMethod( 060 x -> true, 061 x -> methods.put(x.inner(), new RrpcInterfaceMethodMeta(uri, x.inner())) 062 ); 063 064 Map<String,RrpcInterfaceMethodMeta> methodsByPath = map(); 065 methods.values().forEach(x -> methodsByPath.put(x.getPath(), x)); 066 067 this.methods = unmodifiable(methods); 068 this.methodsByPath = unmodifiable(methodsByPath); 069 this.path = path.get(); 070 } 071 072 /** 073 * Returns a map of all methods on this interface proxy keyed by HTTP path. 074 * 075 * @return 076 * A map of all methods on this remote interface keyed by HTTP path. 077 * <br>The keys never have leading slashes. 078 * <br>The map is never <jk>null</jk>. 079 */ 080 public Map<String,RrpcInterfaceMethodMeta> getMethodsByPath() { 081 return methodsByPath; 082 } 083 084 /** 085 * Returns the metadata about the specified method on this interface proxy. 086 * 087 * @param m The method to look up. 088 * @return Metadata about the method or <jk>null</jk> if no metadata was found. 089 */ 090 public RrpcInterfaceMethodMeta getMethodMeta(Method m) { 091 return methods.get(m); 092 } 093 094 /** 095 * Returns the metadata about the specified method on this interface proxy by the path defined on the method. 096 * 097 * @param p The HTTP path to look for. 098 * @return Metadata about the method or <jk>null</jk> if no metadata was found. 099 */ 100 public RrpcInterfaceMethodMeta getMethodMetaByPath(String p) { 101 return methodsByPath.get(p); 102 } 103 104 /** 105 * Returns the Java class of this interface. 106 * 107 * @return 108 * The Java class of this interface. 109 * <br>Never <jk>null</jk>. 110 */ 111 public Class<?> getJavaClass() { 112 return c; 113 } 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() { 124 return path; 125 } 126}