Search in sources :

Example 1 with Builder

use of java.net.http.HttpRequest.Builder in project jena by apache.

the class HttpLib method httpPushWithResponse.

// Worker
/*package*/
static HttpResponse<InputStream> httpPushWithResponse(HttpClient httpClient, Push style, String url, Consumer<HttpRequest.Builder> modifier, BodyPublisher body) {
    URI uri = toRequestURI(url);
    HttpRequest.Builder builder = requestBuilderFor(url);
    builder.uri(uri);
    builder.method(style.method(), body);
    if (modifier != null)
        modifier.accept(builder);
    HttpResponse<InputStream> response = execute(httpClient, builder.build());
    return response;
}
Also used : HttpRequest(java.net.http.HttpRequest) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) InputStream(java.io.InputStream) URI(java.net.URI) Builder(java.net.http.HttpRequest.Builder)

Example 2 with Builder

use of java.net.http.HttpRequest.Builder in project feign by OpenFeign.

the class Http2Client method newRequestBuilder.

private Builder newRequestBuilder(Request request, Options options) throws URISyntaxException {
    URI uri = new URI(request.url());
    final BodyPublisher body;
    final byte[] data = request.body();
    if (data == null) {
        body = BodyPublishers.noBody();
    } else {
        body = BodyPublishers.ofByteArray(data);
    }
    final Builder requestBuilder = HttpRequest.newBuilder().uri(uri).timeout(Duration.ofMillis(options.readTimeoutMillis())).version(client.version());
    final Map<String, Collection<String>> headers = filterRestrictedHeaders(request.headers());
    if (!headers.isEmpty()) {
        requestBuilder.headers(asString(headers));
    }
    switch(request.httpMethod()) {
        case GET:
            return requestBuilder.GET();
        case POST:
            return requestBuilder.POST(body);
        case PUT:
            return requestBuilder.PUT(body);
        case DELETE:
            return requestBuilder.DELETE();
        default:
            // fall back scenario, http implementations may restrict some methods
            return requestBuilder.method(request.httpMethod().toString(), body);
    }
}
Also used : Builder(java.net.http.HttpRequest.Builder) BodyPublisher(java.net.http.HttpRequest.BodyPublisher) Collection(java.util.Collection) URI(java.net.URI)

Example 3 with Builder

use of java.net.http.HttpRequest.Builder in project jena by apache.

the class HttpLib method isFuseki.

/**
 * Test whether a URL identifies a Fuseki server. This operation can not guarantee to
 * detect a Fuseki server - for example, it may be behind a reverse proxy that masks
 * the signature.
 */
public static boolean isFuseki(String datasetURL) {
    HttpRequest.Builder builder = HttpRequest.newBuilder().uri(toRequestURI(datasetURL)).method(HttpNames.METHOD_HEAD, BodyPublishers.noBody());
    HttpRequest request = builder.build();
    HttpClient httpClient = HttpEnv.getDftHttpClient();
    HttpResponse<InputStream> response = execute(httpClient, request);
    handleResponseNoBody(response);
    Optional<String> value1 = response.headers().firstValue(FusekiRequestIdHeader);
    if (value1.isPresent())
        return true;
    Optional<String> value2 = response.headers().firstValue("Server");
    if (value2.isEmpty())
        return false;
    String headerValue = value2.get();
    boolean isFuseki = headerValue.startsWith("Apache Jena Fuseki") || headerValue.toLowerCase().contains("fuseki");
    return isFuseki;
}
Also used : HttpRequest(java.net.http.HttpRequest) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) InputStream(java.io.InputStream) HttpClient(java.net.http.HttpClient) Builder(java.net.http.HttpRequest.Builder)

Example 4 with Builder

use of java.net.http.HttpRequest.Builder in project jena by apache.

the class HttpLib method requestBuilder.

public static Builder requestBuilder(String url, Map<String, String> httpHeaders, long readTimeout, TimeUnit readTimeoutUnit) {
    HttpRequest.Builder builder = HttpLib.requestBuilderFor(url);
    headers(builder, httpHeaders);
    builder.uri(toRequestURI(url));
    if (readTimeout >= 0)
        builder.timeout(Duration.ofMillis(readTimeoutUnit.toMillis(readTimeout)));
    return builder;
}
Also used : HttpRequest(java.net.http.HttpRequest) Builder(java.net.http.HttpRequest.Builder)

Example 5 with Builder

use of java.net.http.HttpRequest.Builder in project jena by apache.

the class HttpLib method createBuilder.

/**
 * Create a {@code HttpRequest.Builder} from an {@code HttpRequest}.
 */
public static HttpRequest.Builder createBuilder(HttpRequest request) {
    HttpRequest.Builder builder = HttpRequest.newBuilder().expectContinue(request.expectContinue()).uri(request.uri());
    builder.method(request.method(), request.bodyPublisher().orElse(BodyPublishers.noBody()));
    request.timeout().ifPresent(builder::timeout);
    request.version().ifPresent(builder::version);
    request.headers().map().forEach((name, values) -> values.forEach(value -> builder.header(name, value)));
    return builder;
}
Also used : HttpRequest(java.net.http.HttpRequest) Context(org.apache.jena.sparql.util.Context) java.util(java.util) GZIPInputStream(java.util.zip.GZIPInputStream) IO(org.apache.jena.atlas.io.IO) BodyHandlers(java.net.http.HttpResponse.BodyHandlers) URISyntaxException(java.net.URISyntaxException) BodyPublishers(java.net.http.HttpRequest.BodyPublishers) InflaterInputStream(java.util.zip.InflaterInputStream) BodySubscribers(java.net.http.HttpResponse.BodySubscribers) BodyHandler(java.net.http.HttpResponse.BodyHandler) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) HttpNames(org.apache.jena.riot.web.HttpNames) HttpRequest(java.net.http.HttpRequest) Builder(java.net.http.HttpRequest.Builder) Duration(java.time.Duration) HttpClient(java.net.http.HttpClient) URI(java.net.URI) AuthLib(org.apache.jena.http.auth.AuthLib) HttpResponse(java.net.http.HttpResponse) RuntimeIOException(org.apache.jena.atlas.RuntimeIOException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) Params(org.apache.jena.sparql.exec.http.Params) HttpRequestModifier(org.apache.jena.http.sys.HttpRequestModifier) HttpException(org.apache.jena.atlas.web.HttpException) BodyPublisher(java.net.http.HttpRequest.BodyPublisher) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) IRILib(org.apache.jena.atlas.lib.IRILib) HttpSC(org.apache.jena.web.HttpSC) AuthEnv(org.apache.jena.http.auth.AuthEnv) ARQ(org.apache.jena.query.ARQ) RegistryRequestModifier(org.apache.jena.http.sys.RegistryRequestModifier) InputStream(java.io.InputStream) Builder(java.net.http.HttpRequest.Builder)

Aggregations

Builder (java.net.http.HttpRequest.Builder)5 HttpRequest (java.net.http.HttpRequest)4 InputStream (java.io.InputStream)3 URI (java.net.URI)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 InflaterInputStream (java.util.zip.InflaterInputStream)3 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)3 HttpClient (java.net.http.HttpClient)2 BodyPublisher (java.net.http.HttpRequest.BodyPublisher)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 URISyntaxException (java.net.URISyntaxException)1 BodyPublishers (java.net.http.HttpRequest.BodyPublishers)1 HttpResponse (java.net.http.HttpResponse)1 BodyHandler (java.net.http.HttpResponse.BodyHandler)1 BodyHandlers (java.net.http.HttpResponse.BodyHandlers)1 BodySubscribers (java.net.http.HttpResponse.BodySubscribers)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Duration (java.time.Duration)1 java.util (java.util)1