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.rest.client.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.http.remote.*;
021import org.apache.juneau.reflect.*;
022
023/**
024 * Contains the meta-data about a REST proxy class.
025 *
026 * <p>
027 * Captures the information in {@link org.apache.juneau.http.remote.Remote @Remote} and {@link org.apache.juneau.http.remote.RemoteMethod @RemoteMethod} annotations for
028 * caching and reuse.
029 *
030 * <ul class='seealso'>
031 *    <li class='link'>{@doc juneau-rest-client.RestProxies}
032 * </ul>
033 */
034public class RemoteMeta {
035
036   private final Map<Method,RemoteMethodMeta> methods;
037   private final String path;
038
039   /**
040    * Constructor.
041    *
042    * @param c The interface class annotated with a {@link org.apache.juneau.http.remote.Remote @Remote} annotation (optional).
043    */
044   @SuppressWarnings("deprecation")
045   public RemoteMeta(Class<?> c) {
046      String path = "";
047
048      ClassInfo ci = ClassInfo.of(c);
049      for (RemoteResource r : ci.getAnnotationsParentFirst(RemoteResource.class))
050         if (! r.path().isEmpty())
051            path = trimSlashes(r.path());
052      for (org.apache.juneau.http.remote.RemoteResource r : ci.getAnnotationsParentFirst(org.apache.juneau.http.remote.RemoteResource.class))
053         if (! r.path().isEmpty())
054            path = trimSlashes(r.path());
055      for (Remote r : ci.getAnnotationsParentFirst(Remote.class))
056         if (! r.path().isEmpty())
057            path = trimSlashes(r.path());
058
059      Map<Method,RemoteMethodMeta> methods = new LinkedHashMap<>();
060      for (MethodInfo m : ci.getPublicMethods())
061         if (m.isPublic())
062            methods.put(m.inner(), new RemoteMethodMeta(path, m.inner(), false, "GET"));
063
064      this.methods = unmodifiableMap(methods);
065      this.path = path;
066   }
067
068   /**
069    * Returns the metadata about the specified method on this resource proxy.
070    *
071    * @param m The method to look up.
072    * @return Metadata about the method or <jk>null</jk> if no metadata was found.
073    */
074   public RemoteMethodMeta getMethodMeta(Method m) {
075      return methods.get(m);
076   }
077
078   /**
079    * Returns the HTTP path of this interface.
080    *
081    * @return
082    *    The HTTP path of this interface.
083    *    <br>Never <jk>null</jk>.
084    *    <br>Never has leading or trailing slashes.
085    */
086   public String getPath() {
087      return path;
088   }
089}