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.ClassUtils.*; 016import static org.apache.juneau.internal.CollectionUtils.*; 017import static org.apache.juneau.internal.StringUtils.*; 018 019import java.lang.reflect.*; 020import java.util.*; 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 * <h5 class='section'>See Also:</h5> 029 * <ul class='doctree'> 030 * <li class='link'>{@doc juneau-rest-server.restRPC} 031 * </ul> 032 */ 033public class RemoteInterfaceMeta { 034 035 private final Map<Method,RemoteInterfaceMethod> methods; 036 private final Map<String,RemoteInterfaceMethod> methodsByPath; 037 private final String path; 038 private final Class<?> c; 039 040 /** 041 * Constructor. 042 * 043 * @param c 044 * The interface class annotated with a {@link RemoteInterface @RemoteInterface} annotation. 045 * <br>Note that the annotations are optional. 046 * @param uri 047 * The absolute URL of the remote REST interface that implements this proxy interface. 048 * <br>This is only used on the client side. 049 */ 050 public RemoteInterfaceMeta(Class<?> c, String uri) { 051 this.c = c; 052 String path = ""; 053 List<RemoteInterface> rr = getAnnotationsParentFirst(RemoteInterface.class, c); 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 (Method m : c.getMethods()) 060 if (isPublic(m)) 061 methods.put(m, new RemoteInterfaceMethod(uri, m)); 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}