org.glassfish.jersey.microprofile.restclient.InterfaceModel Maven / Gradle / Ivy
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.microprofile.restclient;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.ParamConverterProvider;
import org.eclipse.microprofile.rest.client.RestClientDefinitionException;
import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam;
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.ext.ClientHeadersFactory;
import org.glassfish.jersey.client.inject.ParameterUpdater;
import org.glassfish.jersey.client.inject.ParameterUpdaterProvider;
import org.glassfish.jersey.internal.inject.Providers;
import org.glassfish.jersey.model.Parameter;
/**
* Model of interface and its annotation.
*
* @author David Kral
* @author Patrik Dudits
* @author Tomas Langer
*/
class InterfaceModel {
private static final Logger LOGGER = Logger.getLogger(InterfaceModel.class.getName());
private final Class> restClientClass;
private final String[] produces;
private final String[] consumes;
private final String path;
private final ClientHeadersFactory clientHeadersFactory;
private final CreationalContext> creationalContext;
private final RestClientContext context;
private final List clientHeaders;
private final Set interceptorAnnotations;
/**
* Creates new model based on interface class. Interface is parsed according to specific annotations.
*
* @param context RestClient data context
* @return new model instance
*/
static InterfaceModel from(RestClientContext context) {
return new Builder(context).build();
}
private InterfaceModel(Builder builder) {
this.restClientClass = builder.restClientClass;
this.context = builder.context;
this.path = builder.pathValue;
this.produces = builder.produces;
this.consumes = builder.consumes;
this.clientHeaders = builder.clientHeaders;
this.clientHeadersFactory = builder.clientHeadersFactory;
this.interceptorAnnotations = builder.interceptorAnnotations;
this.creationalContext = builder.creationalContext;
}
/**
* Returns rest client interface class.
*
* @return interface class
*/
Class> getRestClientClass() {
return restClientClass;
}
/**
* Returns defined produces media types.
*
* @return produces
*/
String[] getProduces() {
return produces;
}
/**
* Returns defined consumes media types.
*
* @return consumes
*/
String[] getConsumes() {
return consumes;
}
/**
* Returns path value defined on interface level.
*
* @return path value
*/
String getPath() {
return path;
}
/**
* Returns registered instance of {@link ClientHeadersFactory}.
*
* @return registered factory
*/
Optional getClientHeadersFactory() {
return Optional.ofNullable(clientHeadersFactory);
}
/**
* Returns {@link List} of processed annotation {@link ClientHeaderParam} to {@link ClientHeaderParamModel}
*
* @return registered factories
*/
List getClientHeaders() {
return clientHeaders;
}
/**
* Return context of the RestClient.
*
* @return context
*/
RestClientContext context() {
return context;
}
/**
* Returns {@link Set} of interceptor annotations
*
* @return interceptor annotations
*/
Set getInterceptorAnnotations() {
return interceptorAnnotations;
}
/**
* Context bound to this model.
*
* @return context
*/
CreationalContext> getCreationalContext() {
return creationalContext;
}
/**
* Resolves value of the method argument.
*
* @param arg actual argument value
* @return converted value of argument
*/
Object resolveParamValue(Object arg, Parameter parameter) {
final Iterable parameterUpdaterProviders
= Providers.getAllProviders(context.injectionManager(), ParameterUpdaterProvider.class);
for (final ParameterUpdaterProvider parameterUpdaterProvider : parameterUpdaterProviders) {
if (parameterUpdaterProvider != null) {
ParameterUpdater