Search in sources :

Example 11 with HttpException

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

the class FusekiTest method execOptions.

/** Do an HTTP Options. */
public static String execOptions(String url) {
    // Prepare and execute
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpUriRequest request = new HttpOptions(url);
        HttpResponse response = httpClient.execute(request, (HttpContext) null);
        // Response
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (HttpSC.isClientError(statusCode) || HttpSC.isServerError(statusCode)) {
            // Error responses can have bodies so it is important to clear up.
            String contentPayload = "";
            if (response.getEntity() != null)
                contentPayload = EntityUtils.toString(response.getEntity());
            throw new HttpException(statusCode, statusLine.getReasonPhrase(), contentPayload);
        }
        HttpResponseLib.nullResponse.handle(url, response);
        return response.getFirstHeader(HttpNames.hAllow).getValue();
    } catch (IOException ex) {
        throw new HttpException(ex);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.jena.atlas.web.HttpException) IOException(java.io.IOException)

Example 12 with HttpException

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

the class TestAdmin method add_delete_dataset_2.

// Try to add twice
@Test
public void add_delete_dataset_2() {
    checkNotThere(dsTest);
    File f = new File(fileBase + "config-ds-1.ttl");
    {
        org.apache.http.entity.ContentType ct = org.apache.http.entity.ContentType.parse(WebContent.contentTypeTurtle + "; charset=" + WebContent.charsetUTF8);
        HttpEntity e = new FileEntity(f, ct);
        execHttpPost(ServerCtl.urlRoot() + "$/" + opDatasets, e);
    }
    // Check exists.
    checkExists(dsTest);
    try {
        org.apache.http.entity.ContentType ct = org.apache.http.entity.ContentType.parse(WebContent.contentTypeTurtle + "; charset=" + WebContent.charsetUTF8);
        HttpEntity e = new FileEntity(f, ct);
        execHttpPost(ServerCtl.urlRoot() + "$/" + opDatasets, e);
    } catch (HttpException ex) {
        assertEquals(HttpSC.CONFLICT_409, ex.getResponseCode());
    }
    // Check exists.
    checkExists(dsTest);
    deleteDataset(dsTest);
}
Also used : FileEntity(org.apache.http.entity.FileEntity) HttpEntity(org.apache.http.HttpEntity) HttpException(org.apache.jena.atlas.web.HttpException) File(java.io.File) 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