Search in sources :

Example 1 with HttpPatch

use of org.apache.http.client.methods.HttpPatch in project elasticsearch by elastic.

the class RestClientSingleHostTests method performRandomRequest.

private HttpUriRequest performRandomRequest(String method) throws Exception {
    String uriAsString = "/" + randomStatusCode(getRandom());
    URIBuilder uriBuilder = new URIBuilder(uriAsString);
    final Map<String, String> params = new HashMap<>();
    boolean hasParams = randomBoolean();
    if (hasParams) {
        int numParams = randomIntBetween(1, 3);
        for (int i = 0; i < numParams; i++) {
            String paramKey = "param-" + i;
            String paramValue = randomAsciiOfLengthBetween(3, 10);
            params.put(paramKey, paramValue);
            uriBuilder.addParameter(paramKey, paramValue);
        }
    }
    if (randomBoolean()) {
        //randomly add some ignore parameter, which doesn't get sent as part of the request
        String ignore = Integer.toString(randomFrom(RestClientTestUtil.getAllErrorStatusCodes()));
        if (randomBoolean()) {
            ignore += "," + Integer.toString(randomFrom(RestClientTestUtil.getAllErrorStatusCodes()));
        }
        params.put("ignore", ignore);
    }
    URI uri = uriBuilder.build();
    HttpUriRequest request;
    switch(method) {
        case "DELETE":
            request = new HttpDeleteWithEntity(uri);
            break;
        case "GET":
            request = new HttpGetWithEntity(uri);
            break;
        case "HEAD":
            request = new HttpHead(uri);
            break;
        case "OPTIONS":
            request = new HttpOptions(uri);
            break;
        case "PATCH":
            request = new HttpPatch(uri);
            break;
        case "POST":
            request = new HttpPost(uri);
            break;
        case "PUT":
            request = new HttpPut(uri);
            break;
        case "TRACE":
            request = new HttpTrace(uri);
            break;
        default:
            throw new UnsupportedOperationException("method not supported: " + method);
    }
    HttpEntity entity = null;
    boolean hasBody = request instanceof HttpEntityEnclosingRequest && getRandom().nextBoolean();
    if (hasBody) {
        entity = new StringEntity(randomAsciiOfLengthBetween(10, 100), ContentType.APPLICATION_JSON);
        ((HttpEntityEnclosingRequest) request).setEntity(entity);
    }
    Header[] headers = new Header[0];
    final Set<String> uniqueNames = new HashSet<>();
    if (randomBoolean()) {
        headers = RestClientTestUtil.randomHeaders(getRandom(), "Header");
        for (Header header : headers) {
            request.addHeader(header);
            uniqueNames.add(header.getName());
        }
    }
    for (Header defaultHeader : defaultHeaders) {
        // request level headers override default headers
        if (uniqueNames.contains(defaultHeader.getName()) == false) {
            request.addHeader(defaultHeader);
        }
    }
    try {
        if (hasParams == false && hasBody == false && randomBoolean()) {
            restClient.performRequest(method, uriAsString, headers);
        } else if (hasBody == false && randomBoolean()) {
            restClient.performRequest(method, uriAsString, params, headers);
        } else {
            restClient.performRequest(method, uriAsString, params, entity, headers);
        }
    } catch (ResponseException e) {
    //all good
    }
    return request;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) HttpOptions(org.apache.http.client.methods.HttpOptions) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HashSet(java.util.HashSet) URIBuilder(org.apache.http.client.utils.URIBuilder) HttpTrace(org.apache.http.client.methods.HttpTrace) Header(org.apache.http.Header)

Example 2 with HttpPatch

use of org.apache.http.client.methods.HttpPatch in project elasticsearch by elastic.

the class RestClient method createHttpRequest.

private static HttpRequestBase createHttpRequest(String method, URI uri, HttpEntity entity) {
    switch(method.toUpperCase(Locale.ROOT)) {
        case HttpDeleteWithEntity.METHOD_NAME:
            return addRequestBody(new HttpDeleteWithEntity(uri), entity);
        case HttpGetWithEntity.METHOD_NAME:
            return addRequestBody(new HttpGetWithEntity(uri), entity);
        case HttpHead.METHOD_NAME:
            return addRequestBody(new HttpHead(uri), entity);
        case HttpOptions.METHOD_NAME:
            return addRequestBody(new HttpOptions(uri), entity);
        case HttpPatch.METHOD_NAME:
            return addRequestBody(new HttpPatch(uri), entity);
        case HttpPost.METHOD_NAME:
            HttpPost httpPost = new HttpPost(uri);
            addRequestBody(httpPost, entity);
            return httpPost;
        case HttpPut.METHOD_NAME:
            return addRequestBody(new HttpPut(uri), entity);
        case HttpTrace.METHOD_NAME:
            return addRequestBody(new HttpTrace(uri), entity);
        default:
            throw new UnsupportedOperationException("http method not supported: " + method);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpTrace(org.apache.http.client.methods.HttpTrace) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpHead(org.apache.http.client.methods.HttpHead) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut)

Example 3 with HttpPatch

use of org.apache.http.client.methods.HttpPatch in project linuxtools by eclipse.

the class OSIORestPatchRequest method createHttpRequestBase.

@Override
protected HttpRequestBase createHttpRequestBase(String url) {
    HttpPatch request = new HttpPatch(url);
    request.setHeader(CONTENT_TYPE, APPLICATION_VND_JSON);
    return request;
}
Also used : HttpPatch(org.apache.http.client.methods.HttpPatch)

Example 4 with HttpPatch

use of org.apache.http.client.methods.HttpPatch in project linuxtools by eclipse.

the class OSIORestPatchUpdateTask method addHttpRequestEntities.

@SuppressWarnings("deprecation")
@Override
protected void addHttpRequestEntities(HttpRequestBase request) throws OSIORestException {
    super.addHttpRequestEntities(request);
    try {
        Gson gson = new GsonBuilder().registerTypeAdapter(OldAttributes.class, new TaskAttributeTypeAdapter(getClient().getLocation())).create();
        StringEntity requestEntity = new StringEntity(gson.toJson(oldAttributes));
        ((HttpPatch) request).setEntity(requestEntity);
    } catch (UnsupportedEncodingException e) {
        Throwables.propagate(new CoreException(// $NON-NLS-1$
        new Status(IStatus.ERROR, OSIORestCore.ID_PLUGIN, "Can not build HttpRequest", e)));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) StringEntity(org.apache.http.entity.StringEntity) CoreException(org.eclipse.core.runtime.CoreException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpPatch(org.apache.http.client.methods.HttpPatch)

Example 5 with HttpPatch

use of org.apache.http.client.methods.HttpPatch in project pwm by pwm-project.

the class PwmHttpClient method executeRequest.

private HttpResponse executeRequest(final PwmHttpClientRequest clientRequest) throws IOException, PwmUnrecoverableException {
    final String requestBody = clientRequest.getBody();
    final HttpRequestBase httpRequest;
    switch(clientRequest.getMethod()) {
        case POST:
            {
                try {
                    httpRequest = new HttpPost(new URI(clientRequest.getUrl()).toString());
                    if (requestBody != null && !requestBody.isEmpty()) {
                        ((HttpPost) httpRequest).setEntity(new StringEntity(requestBody, PwmConstants.DEFAULT_CHARSET));
                    }
                } catch (URISyntaxException e) {
                    throw PwmUnrecoverableException.newException(PwmError.ERROR_UNKNOWN, "malformed url: " + clientRequest.getUrl() + ", error: " + e.getMessage());
                }
            }
            break;
        case PUT:
            httpRequest = new HttpPut(clientRequest.getUrl());
            if (clientRequest.getBody() != null && !clientRequest.getBody().isEmpty()) {
                ((HttpPut) httpRequest).setEntity(new StringEntity(requestBody, PwmConstants.DEFAULT_CHARSET));
            }
            break;
        case PATCH:
            httpRequest = new HttpPatch(clientRequest.getUrl());
            if (clientRequest.getBody() != null && !clientRequest.getBody().isEmpty()) {
                ((HttpPatch) httpRequest).setEntity(new StringEntity(requestBody, PwmConstants.DEFAULT_CHARSET));
            }
            break;
        case GET:
            httpRequest = new HttpGet(clientRequest.getUrl());
            break;
        case DELETE:
            httpRequest = new HttpDelete(clientRequest.getUrl());
            break;
        default:
            throw new IllegalStateException("http method not yet implemented");
    }
    if (clientRequest.getHeaders() != null) {
        for (final String key : clientRequest.getHeaders().keySet()) {
            final String value = clientRequest.getHeaders().get(key);
            httpRequest.addHeader(key, value);
        }
    }
    final HttpClient httpClient = getHttpClient(pwmApplication.getConfig(), pwmHttpClientConfiguration);
    return httpClient.execute(httpRequest);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpClient(org.apache.http.client.HttpClient) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) HttpPatch(org.apache.http.client.methods.HttpPatch)

Aggregations

HttpPatch (org.apache.http.client.methods.HttpPatch)24 HttpPost (org.apache.http.client.methods.HttpPost)11 HttpPut (org.apache.http.client.methods.HttpPut)11 HttpDelete (org.apache.http.client.methods.HttpDelete)8 HttpGet (org.apache.http.client.methods.HttpGet)8 StringEntity (org.apache.http.entity.StringEntity)7 HttpHead (org.apache.http.client.methods.HttpHead)6 IOException (java.io.IOException)5 URI (java.net.URI)5 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)5 HttpOptions (org.apache.http.client.methods.HttpOptions)4 HttpTrace (org.apache.http.client.methods.HttpTrace)4 InputStreamEntity (org.apache.http.entity.InputStreamEntity)4 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 Header (org.apache.http.Header)3 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)3 HttpResponse (org.apache.http.HttpResponse)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2