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

Jersey connector to Helidon WebClient #1932

Merged
merged 2 commits into from
Jun 5, 2020
Merged
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
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
<artifactId>helidon-jersey-client</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.jersey</groupId>
<artifactId>helidon-jersey-connector</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.jersey</groupId>
<artifactId>helidon-jersey-media-jsonp</artifactId>
Expand Down
16 changes: 16 additions & 0 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,22 @@
<artifactId>reactive-streams-tck</artifactId>
<version>${version.lib.reactive-streams-tck}</version>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.26.3</version>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<!-- imported boms -->
<dependency>
<groupId>org.glassfish.jersey</groupId>
Expand Down
48 changes: 48 additions & 0 deletions jersey/connector/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>helidon-jersey-project</artifactId>
<groupId>io.helidon.jersey</groupId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>helidon-jersey-connector</artifactId>
<name>Helidon WebClient Jersey Connector</name>

<dependencies>
<dependency>
<groupId>io.helidon.jersey</groupId>
<artifactId>helidon-jersey-client</artifactId>
<!-- for use with plain Jersey -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.jersey.connector;

import io.helidon.common.Version;
import io.helidon.webclient.WebClient;
import io.helidon.webclient.WebClientRequestBuilder;
import io.helidon.webclient.WebClientResponse;
import org.glassfish.jersey.client.ClientAsyncExecutorLiteral;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.client.JerseyClient;
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.internal.util.PropertiesHelper;
import org.glassfish.jersey.spi.ExecutorServiceProvider;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Response;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.AccessController;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import java.util.logging.Logger;

/**
* A {@link Connector} that utilizes the Helidon HTTP Client to send and receive
* HTTP request and responses.
*/
class HelidonConnector implements Connector {

private static final String helidonVersion = "Helidon/" + Version.VERSION + " (java " + AccessController
.doPrivileged(PropertiesHelper.getSystemProperty("java.runtime.version")) + ")";
static final Logger LOGGER = Logger.getLogger(HelidonConnector.class.getName());

private final WebClient webClient;

private final ExecutorServiceKeeper executorServiceKeeper;
private final HelidonEntity.HelidonEntityType entityType;

private static final InputStream NO_CONTENT_INPUT_STREAM = new InputStream() {
@Override
public int read() throws IOException {
return -1;
}
};

// internal implementation entity type, can be removed in the future
// settable for testing purposes
// see LargeDataTest

static final String INTERNAL_ENTITY_TYPE = "jersey.connector.helidon.entity.type";

HelidonConnector(final Client client, final Configuration config) {
executorServiceKeeper = new ExecutorServiceKeeper(client);
entityType = getEntityType(config);

final WebClient.Builder webClientBuilder = WebClient.builder();

webClientBuilder.addReader(HelidonStructures.createInputStreamBodyReader());
HelidonEntity.helidonWriter(entityType).ifPresent(webClientBuilder::addWriter);

HelidonStructures.createProxy(config).ifPresent(webClientBuilder::proxy);

HelidonStructures.helidonConfig(config).ifPresent(webClientBuilder::config);

webClientBuilder.connectTimeout(ClientProperties.getValue(config.getProperties(),
ClientProperties.CONNECT_TIMEOUT, 10000), TimeUnit.MILLISECONDS);

HelidonStructures.createSSL(client.getSslContext()).ifPresent(webClientBuilder::ssl);

webClient = webClientBuilder.build();
}

@Override
public ClientResponse apply(ClientRequest request) {
try {
return applyInternal(request).toCompletableFuture().get();
} catch (InterruptedException | ExecutionException e) {
throw new ProcessingException(e);
}
}

@Override
public Future<?> apply(ClientRequest request, AsyncConnectorCallback callback) {
final BiConsumer<? super ClientResponse, ? super Throwable> action = (r, th) -> {
if (th == null) callback.response(r);
else callback.failure(th);
};
return applyInternal(request)
.whenCompleteAsync(action, executorServiceKeeper.getExecutorService(request))
.toCompletableFuture();
}

@Override
public String getName() {
return helidonVersion;
}

@Override
public void close() {

}

private CompletionStage<ClientResponse> applyInternal(ClientRequest request) {
final WebClientRequestBuilder webClientRequestBuilder = webClient.method(request.getMethod());
webClientRequestBuilder.uri(request.getUri());

webClientRequestBuilder.headers(HelidonStructures.createHeaders(request.getRequestHeaders()));

for (String propertyName : request.getConfiguration().getPropertyNames()) {
Object property = request.getConfiguration().getProperty(propertyName);
if (!propertyName.startsWith("jersey") && String.class.isInstance(property)) {
webClientRequestBuilder.property(propertyName, (String) property);
}
}

for (String propertyName : request.getPropertyNames()) {
Object property = request.resolveProperty(propertyName, Object.class);
if (!propertyName.startsWith("jersey") && String.class.isInstance(property)) {
webClientRequestBuilder.property(propertyName, (String) property);
}
}

// TODO
// HelidonStructures.createProxy(request).ifPresent(webClientRequestBuilder::proxy);

webClientRequestBuilder.followRedirects(request.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, true));
webClientRequestBuilder.readTimeout(request.resolveProperty(ClientProperties.READ_TIMEOUT, 10000), TimeUnit.MILLISECONDS);

CompletionStage<WebClientResponse> responseStage = null;

if (request.hasEntity()) {
responseStage = HelidonEntity.submit(
entityType, request, webClientRequestBuilder, executorServiceKeeper.getExecutorService(request)
);
} else {
responseStage = webClientRequestBuilder.submit();
}

return responseStage.thenCompose((a) -> convertResponse(request, a));
}

private CompletionStage<ClientResponse> convertResponse(final ClientRequest requestContext,
final WebClientResponse webClientResponse) {

final ClientResponse responseContext = new ClientResponse(new Response.StatusType() {
@Override
public int getStatusCode() {
return webClientResponse.status().code();
}

@Override
public Response.Status.Family getFamily() {
return Response.Status.Family.familyOf(getStatusCode());
}

@Override
public String getReasonPhrase() {
return webClientResponse.status().reasonPhrase();
}
}, requestContext);

for (Map.Entry<String, List<String>> entry : webClientResponse.headers().toMap().entrySet()) {
for (String value : entry.getValue()) {
responseContext.getHeaders().add(entry.getKey(), value);
}
}

responseContext.setResolvedRequestUri(webClientResponse.lastEndpointURI());

final CompletionStage<InputStream> stream = HelidonStructures.hasEntity(webClientResponse)
? webClientResponse.content().as(InputStream.class)
: CompletableFuture.supplyAsync(() -> NO_CONTENT_INPUT_STREAM);

return stream.thenApply((a) -> {
responseContext.setEntityStream(new FilterInputStream(a) {
private final AtomicBoolean closed = new AtomicBoolean(false);

@Override
public void close() throws IOException {
// Avoid idempotent close in the underlying input stream
if (!closed.compareAndSet(false, true)) {
super.close();
}
}
});
return responseContext;
});
}

private static HelidonEntity.HelidonEntityType getEntityType(final Configuration config) {
final String helidonType = ClientProperties.getValue(config.getProperties(),
INTERNAL_ENTITY_TYPE, HelidonEntity.HelidonEntityType.READABLE_BYTE_CHANNEL.name());
final HelidonEntity.HelidonEntityType entityType = HelidonEntity.HelidonEntityType.valueOf(helidonType);

return entityType;
}

private static class ExecutorServiceKeeper {
private Optional<ExecutorService> executorService;

private ExecutorServiceKeeper(Client client) {
final ClientConfig config = ((JerseyClient) client).getConfiguration();
executorService = Optional.ofNullable(config.getExecutorService());
}

private ExecutorService getExecutorService(ClientRequest request) {
if (!executorService.isPresent()) {
// cache for multiple requests
executorService = Optional.ofNullable(request.getInjectionManager()
.getInstance(ExecutorServiceProvider.class, ClientAsyncExecutorLiteral.INSTANCE).getExecutorService());
}

return executorService.get();
}
}
}
Loading