Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicroProfile REST Client v1.1 support #3844

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<!--

Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2018 Payara Foundation and/or its affiliates.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -178,6 +179,11 @@
<artifactId>jersey-proxy-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-microprofile-rest-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-servlet-portability</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ClientBinding;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.Uri;
import org.glassfish.jersey.uri.Uri;
import org.glassfish.jersey.test.JerseyTest;

import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ClientBinding;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.Uri;
import org.glassfish.jersey.uri.Uri;
import org.glassfish.jersey.test.JerseyTest;

import org.junit.Test;
Expand Down
6 changes: 5 additions & 1 deletion containers/jersey-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<!--

Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2018 Payara Foundation and/or its affiliates.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -39,7 +40,10 @@
<version>${servlet3.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.eclipse.microprofile.rest.client</groupId>
<artifactId>microprofile-rest-client-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -38,6 +39,7 @@
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.HandlesTypes;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
Expand Down Expand Up @@ -111,6 +113,8 @@ public void onStartup(Set<Class<?>> classes, final ServletContext servletContext
if (classes == null) {
classes = Collections.emptySet();
}
classes.removeIf(clazz -> clazz.isAnnotationPresent(RegisterRestClient.class));

// PRE INIT
for (final ServletContainerProvider servletContainerProvider : allServletContainerProviders) {
servletContainerProvider.preInit(servletContext, classes);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2018 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.client;


import org.glassfish.jersey.internal.BootstrapBag;
import org.glassfish.jersey.client.inject.ParameterInserterProvider;

/**
* {@inheritDoc}
* <p>
* This bootstrap bag is specialized for client part of Jersey.
*
* @author Gaurav Gupta ([email protected])
*/
public class ClientBootstrapBag extends BootstrapBag {

private ParameterInserterProvider parameterInserterProvider;

public ParameterInserterProvider getParameterInserterProvider() {
requireNonNull(parameterInserterProvider, ParameterInserterProvider.class);
return parameterInserterProvider;
}

public void setParameterInserterProvider(ParameterInserterProvider provider) {
this.parameterInserterProvider = provider;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -34,6 +35,7 @@
import org.glassfish.jersey.CommonProperties;
import org.glassfish.jersey.ExtendedConfig;
import org.glassfish.jersey.client.internal.LocalizationMessages;
import org.glassfish.jersey.client.internal.inject.ParameterInserterConfigurator;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.client.spi.ConnectorProvider;
import org.glassfish.jersey.internal.AutoDiscoverableConfigurator;
Expand All @@ -56,6 +58,7 @@
import org.glassfish.jersey.model.internal.ComponentBag;
import org.glassfish.jersey.model.internal.ManagedObjectsFinalizer;
import org.glassfish.jersey.process.internal.RequestScope;
import org.glassfish.jersey.internal.inject.ParamConverterConfigurator;

/**
* Jersey externalized implementation of client-side JAX-RS {@link javax.ws.rs.core.Configurable
Expand All @@ -64,6 +67,7 @@
* @author Marek Potociar (marek.potociar at oracle.com)
* @author Martin Matula
* @author Libor Kramolis (libor.kramolis at oracle.com)
* @author Gaurav Gupta ([email protected])
*/
public class ClientConfig implements Configurable<ClientConfig>, ExtendedConfig {
/**
Expand Down Expand Up @@ -408,10 +412,11 @@ private ClientRuntime initRuntime() {
InjectionManager injectionManager = Injections.createInjectionManager();
injectionManager.register(new ClientBinder(runtimeCfgState.getProperties()));

BootstrapBag bootstrapBag = new BootstrapBag();
BootstrapBag bootstrapBag = new ClientBootstrapBag();
bootstrapBag.setManagedObjectsFinalizer(new ManagedObjectsFinalizer(injectionManager));
List<BootstrapConfigurator> bootstrapConfigurators = Arrays.asList(
new RequestScope.RequestScopeConfigurator(),
List<BootstrapConfigurator> bootstrapConfigurators = Arrays.asList(new RequestScope.RequestScopeConfigurator(),
new ParamConverterConfigurator(),
new ParameterInserterConfigurator(),
new RuntimeConfigConfigurator(runtimeCfgState),
new ContextResolverFactory.ContextResolversConfigurator(),
new MessageBodyFactory.MessageBodyWorkersConfigurator(),
Expand Down Expand Up @@ -798,7 +803,7 @@ public ScheduledExecutorService getScheduledExecutorService() {
*
* @return configured runtime.
*/
ClientRuntime getRuntime() {
public ClientRuntime getRuntime() {
return state.runtime.get();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -57,13 +58,15 @@
* Client-side request processing runtime.
*
* @author Marek Potociar (marek.potociar at oracle.com)
* @author Gaurav Gupta ([email protected])
*/
class ClientRuntime implements JerseyClient.ShutdownHook, ClientExecutor {
public class ClientRuntime implements JerseyClient.ShutdownHook, ClientExecutor {

private static final Logger LOG = Logger.getLogger(ClientRuntime.class.getName());

private final Stage<ClientRequest> requestProcessingRoot;
private final Stage<ClientResponse> responseProcessingRoot;
private final Stage<ClientResponse> responseExceptionMapperProcessingRoot;

private final Connector connector;
private final ClientConfig config;
Expand Down Expand Up @@ -102,6 +105,11 @@ public ClientRuntime(final ClientConfig config, final Connector connector, final
ChainableStage<ClientResponse> responseFilteringStage = ClientFilteringStages.createResponseFilteringStage(
injectionManager);
this.responseProcessingRoot = responseFilteringStage != null ? responseFilteringStage : Stages.identity();

ChainableStage<ClientResponse> responseExceptionMapperStage =
ResponseExceptionMappingStages.createResponseExceptionMappingStage(injectionManager);
this.responseExceptionMapperProcessingRoot = responseExceptionMapperStage != null
? responseExceptionMapperStage : Stages.identity();
this.managedObjectsFinalizer = bootstrapBag.getManagedObjectsFinalizer();
this.config = config;
this.connector = connector;
Expand Down Expand Up @@ -256,7 +264,8 @@ public ClientResponse invoke(final ClientRequest request) {
response = aborted.getAbortResponse();
}

return Stages.process(response, responseProcessingRoot);
ClientResponse processedResponse = Stages.process(response, responseProcessingRoot);
return Stages.process(processedResponse, responseExceptionMapperProcessingRoot);
} catch (final ProcessingException pe) {
throw pe;
} catch (final Throwable t) {
Expand Down Expand Up @@ -347,7 +356,7 @@ public Connector getConnector() {
*
* @return injection manager.
*/
InjectionManager getInjectionManager() {
public InjectionManager getInjectionManager() {
return injectionManager;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,14 +17,21 @@

package org.glassfish.jersey.client;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutorService;

import java.util.function.Supplier;
import javax.ws.rs.client.CompletionStageRxInvoker;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.GenericType;
import org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptor;
import org.eclipse.microprofile.rest.client.ext.AsyncInvocationInterceptorFactory;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.Providers;
import org.glassfish.jersey.model.internal.RankedComparator;

/**
* Implementation of Reactive Invoker for {@code CompletionStage}.
Expand All @@ -33,25 +41,65 @@
*/
public class JerseyCompletionStageRxInvoker extends AbstractRxInvoker<CompletionStage> implements CompletionStageRxInvoker {

JerseyCompletionStageRxInvoker(Invocation.Builder builder, ExecutorService executor) {
private final InjectionManager injectionManager;

JerseyCompletionStageRxInvoker(Invocation.Builder builder, ExecutorService executor, InjectionManager injectionManager) {
super(builder, executor);
this.injectionManager = injectionManager;
}

@Override
public <T> CompletionStage<T> method(final String name, final Entity<?> entity, final Class<T> responseType) {
final ExecutorService executorService = getExecutorService();
List<AsyncInvocationInterceptor> asyncInvocationInterceptors = getAsyncInvocationInterceptors();

Supplier invoker = () -> {
asyncInvocationInterceptors.forEach(AsyncInvocationInterceptor::applyContext);
return getSyncInvoker().method(name, entity, responseType);
};

return executorService == null
? CompletableFuture.supplyAsync(() -> getSyncInvoker().method(name, entity, responseType))
: CompletableFuture.supplyAsync(() -> getSyncInvoker().method(name, entity, responseType), executorService);
? CompletableFuture.supplyAsync(invoker)
: CompletableFuture.supplyAsync(invoker, executorService);
}

@Override
public <T> CompletionStage<T> method(final String name, final Entity<?> entity, final GenericType<T> responseType) {
final ExecutorService executorService = getExecutorService();
List<AsyncInvocationInterceptor> asyncInvocationInterceptors = getAsyncInvocationInterceptors();

Supplier invoker = () -> {
asyncInvocationInterceptors.forEach(AsyncInvocationInterceptor::applyContext);
return getSyncInvoker().method(name, entity, responseType);
};

return executorService == null
? CompletableFuture.supplyAsync(() -> getSyncInvoker().method(name, entity, responseType))
: CompletableFuture.supplyAsync(() -> getSyncInvoker().method(name, entity, responseType), executorService);
? CompletableFuture.supplyAsync(invoker)
: CompletableFuture.supplyAsync(invoker, executorService);
}

private List<AsyncInvocationInterceptor> getAsyncInvocationInterceptors() {
RankedComparator<AsyncInvocationInterceptorFactory> comparator
= new RankedComparator<>(RankedComparator.Order.DESCENDING);
Iterable<AsyncInvocationInterceptorFactory> asyncInvocationInterceptorFactories
= Providers.getAllProviders(getInjectionManager(), AsyncInvocationInterceptorFactory.class, comparator);

List<AsyncInvocationInterceptor> asyncInvocationInterceptors = new ArrayList<>();
asyncInvocationInterceptorFactories.forEach(factory -> {
AsyncInvocationInterceptor asyncInvocationInterceptor = factory.newInterceptor();
asyncInvocationInterceptors.add(asyncInvocationInterceptor);
asyncInvocationInterceptor.prepareContext();
});

return asyncInvocationInterceptors;
}

/**
* Return injection manager this reactive invoker was initialized with.
*
* @return non-null injection manager.
*/
protected InjectionManager getInjectionManager() {
return injectionManager;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2011, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -59,6 +60,7 @@

import org.glassfish.jersey.client.internal.LocalizationMessages;
import org.glassfish.jersey.internal.MapPropertiesDelegate;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.Providers;
import org.glassfish.jersey.internal.util.Producer;
import org.glassfish.jersey.internal.util.PropertiesHelper;
Expand Down Expand Up @@ -451,10 +453,9 @@ public Builder property(final String name, final Object value) {

@Override
public CompletionStageRxInvoker rx() {
ExecutorServiceProvider instance = this.requestContext.getInjectionManager()
.getInstance(ExecutorServiceProvider.class);

return new JerseyCompletionStageRxInvoker(this, instance.getExecutorService());
InjectionManager injectionManager = this.requestContext.getInjectionManager();
ExecutorServiceProvider instance = injectionManager.getInstance(ExecutorServiceProvider.class);
return new JerseyCompletionStageRxInvoker(this, instance.getExecutorService(), injectionManager);
}

@Override
Expand Down
Loading