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 java.lang.reflect.*; 018import java.util.*; 019 020import org.apache.juneau.reflect.*; 021 022/** 023 * Contains the meta-data about a remote proxy REST interface. 024 * 025 * <p> 026 * Captures the information in {@link RemoteInterface @RemoteInterface} annotations for caching and reuse. 027 * 028 * <ul class='seealso'> 029 * <li class='link'>{@doc juneau-rest-server.restRPC} 030 * </ul> 031 */ 032public class RemoteInterfaceMeta { 033 034 private final Map<Method,RemoteInterfaceMethod> methods; 035 private final Map<String,RemoteInterfaceMethod> methodsByPath; 036 private final String path; 037 private final Class<?> c; 038 039 /** 040 * Constructor. 041 * 042 * @param c 043 * The interface class annotated with a {@link RemoteInterface @RemoteInterface} annotation. 044 * <br>Note that the annotations are optional. 045 * @param uri 046 * The absolute URL of the remote REST interface that implements this proxy interface. 047 * <br>This is only used on the client side. 048 */ 049 public RemoteInterfaceMeta(Class<?> c, String uri) { 050 this.c = c; 051 String path = ""; 052 ClassInfo ci = ClassInfo.of(c); 053 List<RemoteInterface> rr = ci.getAnnotationsParentFirst(RemoteInterface.class); 054 for (RemoteInterface r : rr) 055 if (! r.path().isEmpty()) 056 path = trimSlashes(r.path()); 057 058 Map<Method,RemoteInterfaceMethod> methods = new LinkedHashMap<>(); 059 for (MethodInfo m : ci.getPublicMethods()) 060 if (m.isPublic()) 061 methods.put(m.inner(), new RemoteInterfaceMethod(uri, m.inner())); 062 063 Map<String,RemoteInterfaceMethod> methodsByPath = new LinkedHashMap<>(); 064 for (RemoteInterfaceMethod rmm : methods.values()) 065 methodsByPath.put(rmm.getPath(), rmm); 066 067 this.methods = unmodifiableMap(methods); 068 this.methodsByPath = unmodifiableMap(methodsByPath); 069 this.path = path; 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,RemoteInterfaceMethod> 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 RemoteInterfaceMethod 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 RemoteInterfaceMethod 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}