Search in sources :

Example 1 with HttpDelete

use of org.keycloak.client.admin.cli.httpcomponents.HttpDelete in project keycloak by keycloak.

the class HttpUtil method doRequest.

public static HeadersBodyStatus doRequest(String type, String url, HeadersBody request) throws IOException {
    HttpRequestBase req;
    switch(type) {
        case "get":
            req = new HttpGet(url);
            break;
        case "post":
            req = new HttpPost(url);
            break;
        case "put":
            req = new HttpPut(url);
            break;
        case "delete":
            req = new HttpDelete(url);
            break;
        case "options":
            req = new HttpOptions(url);
            break;
        case "head":
            req = new HttpHead(url);
            break;
        default:
            throw new RuntimeException("Method not supported: " + type);
    }
    addHeaders(req, request.getHeaders());
    if (request.getBody() != null) {
        if (req instanceof HttpEntityEnclosingRequestBase == false) {
            throw new RuntimeException("Request type does not support body: " + type);
        }
        ((HttpEntityEnclosingRequestBase) req).setEntity(new InputStreamEntity(request.getBody()));
    }
    HttpResponse res = getHttpClient().execute(req);
    InputStream responseStream = null;
    if (res.getEntity() != null) {
        responseStream = res.getEntity().getContent();
    } else {
        responseStream = new InputStream() {

            @Override
            public int read() throws IOException {
                return -1;
            }
        };
    }
    Headers headers = new Headers();
    HeaderIterator it = res.headerIterator();
    while (it.hasNext()) {
        org.apache.http.Header header = it.nextHeader();
        headers.add(header.getName(), header.getValue());
    }
    return new HeadersBodyStatus(res.getStatusLine().toString(), headers, responseStream);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpDelete(org.keycloak.client.admin.cli.httpcomponents.HttpDelete) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpHeaders(org.apache.http.HttpHeaders) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) InputStreamEntity(org.apache.http.entity.InputStreamEntity) HeaderIterator(org.apache.http.HeaderIterator)

Example 2 with HttpDelete

use of org.keycloak.client.admin.cli.httpcomponents.HttpDelete in project keycloak by keycloak.

the class HttpUtil method doDelete.

public static void doDelete(String url, String authorization) {
    try {
        HttpDelete request = new HttpDelete(url);
        doRequest(authorization, request);
    } catch (IOException e) {
        throw new RuntimeException("Failed to send request - " + e.getMessage(), e);
    }
}
Also used : HttpDelete(org.keycloak.client.admin.cli.httpcomponents.HttpDelete) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 HttpDelete (org.keycloak.client.admin.cli.httpcomponents.HttpDelete)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HeaderIterator (org.apache.http.HeaderIterator)1 HttpHeaders (org.apache.http.HttpHeaders)1 HttpResponse (org.apache.http.HttpResponse)1 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpHead (org.apache.http.client.methods.HttpHead)1 HttpOptions (org.apache.http.client.methods.HttpOptions)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpPut (org.apache.http.client.methods.HttpPut)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1