Last active
April 5, 2019 13:11
-
-
Save perchrh/581106df4e6b3b3e73e33dce2dd28b14 to your computer and use it in GitHub Desktop.
Modern Java 11 Http Client implementation of org.glassfish.jersey.client.internal.HttpUrlConnector._apply
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.digitalsprouts.example.http_client; | |
import org.glassfish.jersey.client.ClientProperties; | |
import org.glassfish.jersey.client.ClientResponse; | |
import org.glassfish.jersey.message.internal.Statuses; | |
import javax.ws.rs.core.Response; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.http.HttpClient; | |
import java.net.http.HttpRequest; | |
import java.net.http.HttpResponse; | |
public class JerseyExampleIementation { | |
//alternative to org.glassfish.jersey.client.internal.HttpUrlConnector._apply | |
private ClientResponse _apply(final HttpRequest request) throws IOException, InterruptedException { | |
HttpClient.Builder builder = HttpClient.newBuilder(); | |
builder.followRedirects(resolveProperty(ClientProperties.FOLLOW_REDIRECTS, true)); | |
builder.connectTimeout(resolveProperty(ClientProperties.CONNECT_TIMEOUT, defaultTimeout)); | |
HttpClient httpClient = builder.build(); | |
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); | |
final int code = response.statusCode(); | |
final Response.StatusType status = Statuses.from(code); | |
ClientResponse responseContext = new ClientResponse(status, request); | |
responseContext.headers(response.headers().map()); | |
responseContext.setEntityStream(response.body()); | |
return responseContext; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment