Search in sources :

Example 6 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 7 with HttpRequest

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

the class HttpRDF method httpDeleteGraph.

public static void httpDeleteGraph(HttpClient httpClient, String url) {
    URI uri = toRequestURI(url);
    HttpRequest requestData = HttpLib.requestBuilderFor(url).DELETE().uri(uri).build();
    HttpResponse<InputStream> response = execute(httpClient, requestData);
    handleResponseNoBody(response);
}
Also used : HttpRequest(java.net.http.HttpRequest) InputStream(java.io.InputStream) URI(java.net.URI)

Example 8 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)

Example 9 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 10 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)

Aggregations

HttpRequest (java.net.http.HttpRequest)38 InputStream (java.io.InputStream)15 HttpClient (java.net.http.HttpClient)10 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)10 URI (java.net.URI)9 IOException (java.io.IOException)7 HttpResponse (java.net.http.HttpResponse)7 Duration (java.time.Duration)6 Test (org.junit.Test)5 HttpException (org.apache.jena.atlas.web.HttpException)4 SerializedClassRunner (io.pravega.test.common.SerializedClassRunner)3 TestUtils (io.pravega.test.common.TestUtils)3 URISyntaxException (java.net.URISyntaxException)3 Pattern (java.util.regex.Pattern)3 Cleanup (lombok.Cleanup)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 RunWith (org.junit.runner.RunWith)3 JsonParser (com.google.gson.JsonParser)2 Counter (io.pravega.shared.metrics.Counter)2 MetricsConfig (io.pravega.shared.metrics.MetricsConfig)2