Search in sources :

Example 1 with HttpRequest

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

the class RDFParser method openTypedInputStream.

@SuppressWarnings("resource")
private TypedInputStream openTypedInputStream(String urlStr, Path path) {
    // If path, use that.
    if (path != null) {
        try {
            InputStream in = Files.newInputStream(path);
            ContentType ct = RDFLanguages.guessContentType(urlStr);
            return new TypedInputStream(in, ct);
        } catch (NoSuchFileException | FileNotFoundException ex) {
            throw new RiotNotFoundException();
        } catch (IOException ex) {
            IO.exception(ex);
        }
    }
    TypedInputStream in;
    // Need more control than LocatorURL provides to get the Accept header in and the HttpCLient.
    // So map now.
    urlStr = streamManager.mapURI(urlStr);
    if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
        // HTTP
        String acceptHeader = HttpLib.dft(appAcceptHeader, WebContent.defaultRDFAcceptHeader);
        HttpRequest request = HttpLib.newGetRequest(urlStr, (b) -> {
            if (httpHeaders != null)
                httpHeaders.forEach(b::header);
            b.setHeader(HttpNames.hAccept, acceptHeader);
        });
        HttpResponse<InputStream> response = HttpLib.execute(httpClient, request);
        in = HttpLib.handleResponseTypedInputStream(response);
    } else {
        // Already mapped.
        in = streamManager.openNoMapOrNull(urlStr);
    }
    if (in == null)
        throw new RiotNotFoundException("Not found: " + urlStr);
    return in;
}
Also used : HttpRequest(java.net.http.HttpRequest) ContentType(org.apache.jena.atlas.web.ContentType) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) NoSuchFileException(java.nio.file.NoSuchFileException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream)

Example 2 with HttpRequest

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

the class TestMetrics method can_retrieve_metrics.

@Test
public void can_retrieve_metrics() {
    String r = urlRoot() + "$/metrics";
    HttpRequest request = HttpRequest.newBuilder().uri(HttpLib.toRequestURI(r)).build();
    HttpResponse<InputStream> response = HttpLib.executeJDK(HttpEnv.getDftHttpClient(), request, BodyHandlers.ofInputStream());
    String body = handleResponseRtnString(response);
    String ct = response.headers().firstValue(HttpNames.hContentType).orElse(null);
    assertTrue(ct.contains(WebContent.contentTypeTextPlain));
    assertTrue(ct.contains(WebContent.charsetUTF8));
    assertTrue(body.contains("fuseki_requests_good"));
}
Also used : HttpRequest(java.net.http.HttpRequest) InputStream(java.io.InputStream) HttpLib.handleResponseRtnString(org.apache.jena.http.HttpLib.handleResponseRtnString) Test(org.junit.Test)

Example 3 with HttpRequest

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

the class ExFusekiMain_3_FusekiModule method main.

public static void main(String... a) throws Exception {
    JenaSystem.init();
    FusekiLogging.setLogging();
    // Normally done with ServiceLoader
    // A file /META-INF/services/org.apache.jena.fuseki.main.sys.FusekiModule
    // in the jar file with contents:
    // org.apache.jena.fuseki.main.examples.ExampleModule
    // 
    // The file is typically put into the jar by having
    // src/main/resources/META-INF/services/org.apache.jena.fuseki.main.sys.FusekiModule
    FusekiModule module = new FMod_ProvidePATCH();
    FusekiModules.add(module);
    // Create server.
    FusekiServer server = FusekiServer.create().port(0).build().start();
    int port = server.getPort();
    // Client HTTP request: "PATCH /extra"
    HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:" + port + "/extra")).method("PATCH", BodyPublishers.ofString("hello world!")).build();
    HttpResponse<Void> response = HttpEnv.getDftHttpClient().send(request, BodyHandlers.discarding());
    server.stop();
}
Also used : HttpRequest(java.net.http.HttpRequest) FusekiModule(org.apache.jena.fuseki.main.sys.FusekiModule) FusekiServer(org.apache.jena.fuseki.main.FusekiServer)

Example 4 with HttpRequest

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

the class TestWebappMetrics method can_retrieve_metrics.

@Test
public void can_retrieve_metrics() {
    String r = ServerCtl.urlRoot() + "$/metrics";
    HttpRequest request = HttpRequest.newBuilder().uri(HttpLib.toRequestURI(r)).build();
    HttpResponse<InputStream> response = HttpLib.executeJDK(HttpEnv.getDftHttpClient(), request, BodyHandlers.ofInputStream());
    String body = handleResponseRtnString(response);
    String ct = response.headers().firstValue(HttpNames.hContentType).orElse(null);
    assertTrue(ct.contains(WebContent.contentTypeTextPlain));
    assertTrue(ct.contains(WebContent.charsetUTF8));
    assertTrue(body.contains("fuseki_requests_good"));
}
Also used : HttpRequest(java.net.http.HttpRequest) InputStream(java.io.InputStream) HttpLib.handleResponseRtnString(org.apache.jena.http.HttpLib.handleResponseRtnString) Test(org.junit.Test)

Example 5 with HttpRequest

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

the class AsyncHttpRDF method asyncGetToInput.

/**
 * MUST consume or close the input stream
 * @see HttpLib#finish(HttpResponse)
 */
private static CompletableFuture<HttpResponse<InputStream>> asyncGetToInput(HttpClient httpClient, String url, Consumer<HttpRequest.Builder> modifier) {
    Objects.requireNonNull(httpClient);
    Objects.requireNonNull(url);
    HttpRequest requestData = HttpLib.newGetRequest(url, modifier);
    return HttpLib.asyncExecute(httpClient, requestData);
}
Also used : HttpRequest(java.net.http.HttpRequest)

Aggregations

HttpRequest (java.net.http.HttpRequest)22 InputStream (java.io.InputStream)14 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)10 URI (java.net.URI)4 HttpClient (java.net.http.HttpClient)4 HttpException (org.apache.jena.atlas.web.HttpException)4 URISyntaxException (java.net.URISyntaxException)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 Builder (java.net.http.HttpRequest.Builder)2 HttpResponse (java.net.http.HttpResponse)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 InflaterInputStream (java.util.zip.InflaterInputStream)2 HttpLib.handleResponseRtnString (org.apache.jena.http.HttpLib.handleResponseRtnString)2 UncheckedIOException (java.io.UncheckedIOException)1 BodyPublisher (java.net.http.HttpRequest.BodyPublisher)1 BodyPublishers (java.net.http.HttpRequest.BodyPublishers)1 BodyHandler (java.net.http.HttpResponse.BodyHandler)1 BodyHandlers (java.net.http.HttpResponse.BodyHandlers)1 BodyHandlers.ofString (java.net.http.HttpResponse.BodyHandlers.ofString)1