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.common.internal.StringUtils.*;
016import static org.apache.juneau.http.HttpHeaders.*;
017import static org.apache.juneau.internal.ClassUtils.*;
018import static org.apache.juneau.internal.CollectionUtils.*;
019
020import java.lang.reflect.*;
021import java.util.*;
022
023import org.apache.juneau.*;
024import org.apache.juneau.http.header.*;
025import org.apache.juneau.http.remote.*;
026import org.apache.juneau.reflect.*;
027import org.apache.juneau.svl.*;
028
029/**
030 * Contains the meta-data about a REST proxy class.
031 *
032 * <p>
033 * Captures the information in {@link org.apache.juneau.http.remote.Remote @Remote} and {@link org.apache.juneau.http.remote.RemoteOp @RemoteOp} annotations for
034 * caching and reuse.
035 *
036 * <h5 class='section'>See Also:</h5><ul>
037 *    <li class='link'><a class="doclink" href="../../../../../../index.html#jrc.Proxies">REST Proxies</a>
038 *    <li class='link'><a class="doclink" href="../../../../../../index.html#juneau-rest-client">juneau-rest-client</a>
039 * </ul>
040 */
041public class RemoteMeta {
042
043   private final Map<Method,RemoteOperationMeta> operations;
044   private final HeaderList headers;
045
046   /**
047    * Constructor.
048    *
049    * @param c The interface class annotated with a {@link org.apache.juneau.http.remote.Remote @Remote} annotation (optional).
050    */
051   public RemoteMeta(Class<?> c) {
052      String path = "";
053
054      ClassInfo ci = ClassInfo.of(c);
055      List<Remote> remotes = ci.getAnnotations(Remote.class);
056
057      String versionHeader = "Client-Version", clientVersion = null;
058      HeaderList headers = HeaderList.create().resolving();
059
060      for (Remote r : remotes) {
061         if (isNotEmpty(r.path()))
062            path = trimSlashes(resolve(r.path()));
063         for (String h : r.headers())
064            headers.append(stringHeader(resolve(h)));
065         if (isNotEmpty(r.version()))
066            clientVersion = resolve(r.version());
067         if (isNotEmpty(r.versionHeader()))
068            versionHeader = resolve(r.versionHeader());
069         if (isNotVoid(r.headerList())) {
070            try {
071               headers.append(r.headerList().getDeclaredConstructor().newInstance().getAll());
072            } catch (Exception e) {
073               throw new BasicRuntimeException(e, "Could not instantiate HeaderSupplier class");
074            }
075         }
076      }
077
078      if (clientVersion != null)
079         headers.append(stringHeader(versionHeader, clientVersion));
080
081      Map<Method,RemoteOperationMeta> operations = map();
082      String path2 = path;
083      ci.forEachPublicMethod(
084         x -> true,
085         x -> operations.put(x.inner(), new RemoteOperationMeta(path2, x.inner(), "GET"))
086      );
087
088      this.operations = unmodifiable(operations);
089      this.headers = headers;
090   }
091
092   /**
093    * Returns the metadata about the specified operation on this resource proxy.
094    *
095    * @param m The method to look up.
096    * @return Metadata about the method or <jk>null</jk> if no metadata was found.
097    */
098   public RemoteOperationMeta getOperationMeta(Method m) {
099      return operations.get(m);
100   }
101
102   /**
103    * Returns the headers to set on all requests.
104    *
105    * @return The headers to set on all requests.
106    */
107   public HeaderList getHeaders() {
108      return headers;
109   }
110
111   //------------------------------------------------------------------------------------------------------------------
112   // Helper methods.
113   //------------------------------------------------------------------------------------------------------------------
114
115   private static String resolve(String s) {
116      return VarResolver.DEFAULT.resolve(s);
117   }
118}