Search in sources :

Example 6 with HttpException

use of org.apache.jena.atlas.web.HttpException in project jena by apache.

the class TestAdmin method checkExistsNotActive.

private static void checkExistsNotActive(String name) {
    adminPing(name);
    try {
        askPing(name);
        fail("askPing did not cause an Http Exception");
    } catch (HttpException ex) {
    }
    JsonValue v = getDatasetDescription(name);
    assertFalse(v.getAsObject().get("ds.state").getAsBoolean().value());
}
Also used : JsonValue(org.apache.jena.atlas.json.JsonValue) HttpException(org.apache.jena.atlas.web.HttpException)

Example 7 with HttpException

use of org.apache.jena.atlas.web.HttpException in project jena by apache.

the class HttpQuery method execPost.

private InputStream execPost() throws QueryExceptionHTTP {
    URL target = null;
    try {
        target = new URL(serviceURL);
    } catch (MalformedURLException malEx) {
        throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx);
    }
    log.trace("POST " + target.toExternalForm());
    ARQ.getHttpRequestLogger().trace(target.toExternalForm());
    try {
        // Get the actual response stream
        TypedInputStream stream = HttpOp.execHttpPostFormStream(serviceURL, this, contentTypeResult, client, getContext());
        if (stream == null)
            throw new QueryExceptionHTTP(404);
        return execCommon(stream);
    } catch (HttpException httpEx) {
        throw rewrap(httpEx);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpException(org.apache.jena.atlas.web.HttpException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) URL(java.net.URL)

Example 8 with HttpException

use of org.apache.jena.atlas.web.HttpException in project jena by apache.

the class HttpOp method exec.

// ---- Perform the operation!
private static void exec(String url, HttpUriRequest request, String acceptHeader, HttpResponseHandler handler, HttpClient httpClient, HttpContext httpContext) {
    // whether we should close the client after request execution
    // only true if we built the client right here
    httpClient = firstNonNull(httpClient, getDefaultHttpClient());
    // and also only true if the handler won't close the client for us
    try {
        if (handler == null)
            // This cleans up left-behind streams
            handler = nullHandler;
        long id = counter.incrementAndGet();
        String baseURI = determineBaseIRI(url);
        if (log.isDebugEnabled())
            log.debug(format("[%d] %s %s", id, request.getMethod(), request.getURI().toString()));
        // Accept
        if (acceptHeader != null)
            request.addHeader(HttpNames.hAccept, acceptHeader);
        // User-Agent
        applyUserAgent(request);
        HttpResponse response = httpClient.execute(request, httpContext);
        // Response
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (HttpSC.isClientError(statusCode) || HttpSC.isServerError(statusCode)) {
            log.debug(format("[%d] %s %s", id, statusLine.getStatusCode(), statusLine.getReasonPhrase()));
            // Error responses can have bodies so it is important to clear
            // up.
            final String contentPayload = readPayload(response.getEntity());
            throw new HttpException(statusLine.getStatusCode(), statusLine.getReasonPhrase(), contentPayload);
        }
        if (handler != null)
            handler.handle(baseURI, response);
    } catch (IOException ex) {
        throw new HttpException(ex);
    }
}
Also used : HttpException(org.apache.jena.atlas.web.HttpException) IOException(java.io.IOException)

Example 9 with HttpException

use of org.apache.jena.atlas.web.HttpException in project jena by apache.

the class HttpQuery method execGet.

private InputStream execGet() throws QueryExceptionHTTP {
    URL target = null;
    String qs = getQueryString();
    ARQ.getHttpRequestLogger().trace(qs);
    try {
        if (count() == 0)
            target = new URL(serviceURL);
        else
            target = new URL(serviceURL + (serviceParams ? "&" : "?") + qs);
    } catch (MalformedURLException malEx) {
        throw new QueryExceptionHTTP(0, "Malformed URL: " + malEx);
    }
    log.trace("GET " + target.toExternalForm());
    try {
        try {
            // Get the actual response stream
            TypedInputStream stream = HttpOp.execHttpGet(target.toString(), contentTypeResult, client, getContext());
            if (stream == null)
                throw new QueryExceptionHTTP(404);
            return execCommon(stream);
        } catch (HttpException httpEx) {
            // Back-off and try POST if something complain about long URIs
            if (httpEx.getResponseCode() == 414)
                return execPost();
            throw httpEx;
        }
    } catch (HttpException httpEx) {
        throw rewrap(httpEx);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpException(org.apache.jena.atlas.web.HttpException) TypedInputStream(org.apache.jena.atlas.web.TypedInputStream) URL(java.net.URL)

Example 10 with HttpException

use of org.apache.jena.atlas.web.HttpException in project jena by apache.

the class TestEmbeddedFuseki method embedded_09.

@Test
public void embedded_09() {
    DatasetGraph dsg = dataset();
    int port = FusekiLib.choosePort();
    FusekiEmbeddedServer server = FusekiEmbeddedServer.create().setPort(port).setContextPath("/ABC").parseConfigFile(DIR + "config.ttl").build();
    server.start();
    try {
        try {
            query("http://localhost:" + port + "/FuTest", "ASK{}", x -> {
            });
        } catch (HttpException ex) {
            assertEquals(HttpSC.METHOD_NOT_ALLOWED_405, ex.getResponseCode());
        }
        query("http://localhost:" + port + "/ABC/FuTest", "ASK{}", x -> {
        });
    } finally {
        server.stop();
    }
}
Also used : HttpException(org.apache.jena.atlas.web.HttpException) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Aggregations

HttpException (org.apache.jena.atlas.web.HttpException)12 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)7 Test (org.junit.Test)4 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)3 HttpClient (org.apache.http.client.HttpClient)3 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 BaseTest (org.apache.jena.atlas.junit.BaseTest)2 FusekiTestAuth.assertAuthHttpException (org.apache.jena.fuseki.embedded.FusekiTestAuth.assertAuthHttpException)2 File (java.io.File)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 Credentials (org.apache.http.auth.Credentials)1 HttpOptions (org.apache.http.client.methods.HttpOptions)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 FileEntity (org.apache.http.entity.FileEntity)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1