Skip to content

Commit

Permalink
new InvocationBuilderListener SPI
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Supol <[email protected]>
  • Loading branch information
jansupol committed Nov 13, 2019
1 parent 4c10f29 commit ec4235d
Show file tree
Hide file tree
Showing 4 changed files with 442 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (c) 2011, 2019 Oracle 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.client.spi.InvocationBuilderListener;
import org.glassfish.jersey.internal.inject.InjectionManager;
import org.glassfish.jersey.internal.inject.Providers;
import org.glassfish.jersey.model.internal.RankedComparator;

import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import java.util.Iterator;
import java.util.Locale;

/**
* Client Request processing stage. During a request completion, when the {@link Invocation.Builder}
* would create an {@link Invocation}, this class is utilized.
*/
/* package */ class InvocationBuilderListenerStage {
final Iterator<InvocationBuilderListener> invocationBuilderListenerIterator;

/* package */ InvocationBuilderListenerStage(InjectionManager injectionManager) {
final RankedComparator<InvocationBuilderListener> comparator =
new RankedComparator<>(RankedComparator.Order.ASCENDING);
invocationBuilderListenerIterator = Providers
.getAllProviders(injectionManager, InvocationBuilderListener.class, comparator).iterator();
}

/* package */ void invokeListener(Invocation.Builder builder) {
while (invocationBuilderListenerIterator.hasNext()) {
invocationBuilderListenerIterator.next().onInvocation(new InvocationBuilderContextImpl(builder));
}
}

private static class InvocationBuilderContextImpl implements InvocationBuilderListener.InvocationBuilderContext {
private final Invocation.Builder builder;

private InvocationBuilderContextImpl(Invocation.Builder builder) {
this.builder = builder;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext accept(String... mediaTypes) {
builder.accept(mediaTypes);
return this;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext accept(MediaType... mediaTypes) {
builder.accept(mediaTypes);
return this;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext acceptLanguage(Locale... locales) {
builder.acceptLanguage(locales);
return this;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext acceptLanguage(String... locales) {
builder.acceptLanguage(locales);
return this;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext acceptEncoding(String... encodings) {
builder.acceptEncoding(encodings);
return this;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext cookie(Cookie cookie) {
builder.cookie(cookie);
return this;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext cookie(String name, String value) {
builder.cookie(name, value);
return this;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext cacheControl(CacheControl cacheControl) {
builder.cacheControl(cacheControl);
return this;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext header(String name, Object value) {
builder.header(name, value);
return this;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext headers(MultivaluedMap<String, Object> headers) {
builder.headers(headers);
return this;
}

@Override
public InvocationBuilderListener.InvocationBuilderContext property(String name, Object value) {
builder.property(name, value);
return this;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -178,45 +178,45 @@ private void storeEntity(final Entity<?> entity) {
@Override
public JerseyInvocation build(final String method) {
requestContext.setMethod(method);
return new JerseyInvocation(this, true);
return new JerseyInvocation(onInvocation(this), true);
}

@Override
public JerseyInvocation build(final String method, final Entity<?> entity) {
requestContext.setMethod(method);
storeEntity(entity);
return new JerseyInvocation(this, true);
return new JerseyInvocation(onInvocation(this), true);
}

@Override
public JerseyInvocation buildGet() {
requestContext.setMethod("GET");
return new JerseyInvocation(this, true);
return new JerseyInvocation(onInvocation(this), true);
}

@Override
public JerseyInvocation buildDelete() {
requestContext.setMethod("DELETE");
return new JerseyInvocation(this, true);
return new JerseyInvocation(onInvocation(this), true);
}

@Override
public JerseyInvocation buildPost(final Entity<?> entity) {
requestContext.setMethod("POST");
storeEntity(entity);
return new JerseyInvocation(this, true);
return new JerseyInvocation(onInvocation(this), true);
}

@Override
public JerseyInvocation buildPut(final Entity<?> entity) {
requestContext.setMethod("PUT");
storeEntity(entity);
return new JerseyInvocation(this, true);
return new JerseyInvocation(onInvocation(this), true);
}

@Override
public javax.ws.rs.client.AsyncInvoker async() {
return new AsyncInvoker(this);
return new AsyncInvoker(onInvocation(this));
}

@Override
Expand Down Expand Up @@ -392,7 +392,7 @@ public <T> T trace(final GenericType<T> responseType) throws ProcessingException
@Override
public Response method(final String name) throws ProcessingException {
requestContext.setMethod(name);
return new JerseyInvocation(this).invoke();
return new JerseyInvocation(onInvocation(this)).invoke();
}

@Override
Expand All @@ -401,7 +401,7 @@ public <T> T method(final String name, final Class<T> responseType) throws Proce
throw new IllegalArgumentException(LocalizationMessages.RESPONSE_TYPE_IS_NULL());
}
requestContext.setMethod(name);
return new JerseyInvocation(this).invoke(responseType);
return new JerseyInvocation(onInvocation(this)).invoke(responseType);
}

@Override
Expand All @@ -411,14 +411,14 @@ public <T> T method(final String name, final GenericType<T> responseType)
throw new IllegalArgumentException(LocalizationMessages.RESPONSE_TYPE_IS_NULL());
}
requestContext.setMethod(name);
return new JerseyInvocation(this).invoke(responseType);
return new JerseyInvocation(onInvocation(this)).invoke(responseType);
}

@Override
public Response method(final String name, final Entity<?> entity) throws ProcessingException {
requestContext.setMethod(name);
storeEntity(entity);
return new JerseyInvocation(this).invoke();
return new JerseyInvocation(onInvocation(this)).invoke();
}

@Override
Expand All @@ -429,7 +429,7 @@ public <T> T method(final String name, final Entity<?> entity, final Class<T> re
}
requestContext.setMethod(name);
storeEntity(entity);
return new JerseyInvocation(this).invoke(responseType);
return new JerseyInvocation(onInvocation(this)).invoke(responseType);
}

@Override
Expand All @@ -440,7 +440,7 @@ public <T> T method(final String name, final Entity<?> entity, final GenericType
}
requestContext.setMethod(name);
storeEntity(entity);
return new JerseyInvocation(this).invoke(responseType);
return new JerseyInvocation(onInvocation(this)).invoke(responseType);
}

@Override
Expand All @@ -451,13 +451,13 @@ public Builder property(final String name, final Object value) {

@Override
public CompletionStageRxInvoker rx() {
return new JerseyCompletionStageRxInvoker(this);
return new JerseyCompletionStageRxInvoker(onInvocation(this));
}

@Override
public <T extends RxInvoker> T rx(Class<T> clazz) {
if (clazz == JerseyCompletionStageRxInvoker.class) {
return (T) new JerseyCompletionStageRxInvoker(this);
return (T) new JerseyCompletionStageRxInvoker(onInvocation(this));
}
return createRxInvoker(clazz, executorService());
}
Expand Down Expand Up @@ -508,7 +508,7 @@ private <T extends RxInvoker> T createRxInvoker(Class<? extends RxInvoker> clazz
for (RxInvokerProvider invokerProvider : allProviders) {
if (invokerProvider.isProviderFor(clazz)) {

RxInvoker rxInvoker = invokerProvider.getRxInvoker(this, executorService);
RxInvoker rxInvoker = invokerProvider.getRxInvoker(onInvocation(this), executorService);

if (rxInvoker == null) {
throw new IllegalStateException(LocalizationMessages.CLIENT_RX_PROVIDER_NULL());
Expand All @@ -521,6 +521,11 @@ private <T extends RxInvoker> T createRxInvoker(Class<? extends RxInvoker> clazz
throw new IllegalStateException(
LocalizationMessages.CLIENT_RX_PROVIDER_NOT_REGISTERED(clazz.getSimpleName()));
}

private static JerseyInvocation.Builder onInvocation(JerseyInvocation.Builder builder) {
new InvocationBuilderListenerStage(builder.requestContext.getInjectionManager()).invokeListener(builder);
return builder;
}
}

/* package */ static class AsyncInvoker extends CompletableFutureAsyncInvoker implements javax.ws.rs.client.AsyncInvoker {
Expand Down
Loading

0 comments on commit ec4235d

Please sign in to comment.