Example usage for org.apache.http.client.methods HttpPut HttpPut

List of usage examples for org.apache.http.client.methods HttpPut HttpPut

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPut HttpPut.

Prototype

public HttpPut(final String uri) 

Source Link

Usage

From source file:com.github.tomakehurst.wiremock.http.HttpClientFactory.java

public static HttpUriRequest getHttpRequestFor(RequestMethod method, String url) {
    notifier().info("Proxying: " + method + " " + url);

    if (method.equals(GET))
        return new HttpGet(url);
    else if (method.equals(POST))
        return new HttpPost(url);
    else if (method.equals(PUT))
        return new HttpPut(url);
    else if (method.equals(DELETE))
        return new HttpDelete(url);
    else if (method.equals(HEAD))
        return new HttpHead(url);
    else if (method.equals(OPTIONS))
        return new HttpOptions(url);
    else if (method.equals(TRACE))
        return new HttpTrace(url);
    else if (method.equals(PATCH))
        return new HttpPatch(url);
    else//  www .ja v a  2 s  .c  om
        return new GenericHttpUriRequest(method.toString(), url);
}

From source file:org.apache.stratos.kubernetes.client.rest.RestClient.java

public KubernetesResponse doPut(URI resourcePath, String jsonParamString) throws Exception {

    HttpPut putRequest = null;//from w ww .  j  a  va2  s.  c o  m
    try {
        putRequest = new HttpPut(resourcePath);

        StringEntity input = new StringEntity(jsonParamString);
        input.setContentType("application/json");
        putRequest.setEntity(input);

        return httpClient.execute(putRequest, new KubernetesResponseHandler());
    } finally {
        releaseConnection(putRequest);
    }
}

From source file:com.restqueue.framework.client.messageupdate.BasicMessageUpdater.java

/**
 * This method updates the message given the headers and body that you want to update have been set.
 * @return The result of the operation giving you access to the http response code and error information
 *///w ww  . ja v  a 2s  .c  om
public ConditionalPutResult updateMessage() {
    Object messageBody = null;
    if (stringBody == null && objectBody == null) {
        throw new IllegalArgumentException("String body and Object body cannot both be null.");
    } else {
        if (stringBody != null) {
            messageBody = stringBody;
        }
        if (objectBody != null) {
            messageBody = objectBody;
        }
    }

    if (urlLocation == null) {
        throw new IllegalArgumentException("The Channel Endpoint must be set.");
    }
    if (eTag == null) {
        throw new ChannelClientException("Must set the eTag value to update a message.",
                ChannelClientException.ExceptionType.MISSING_DATA);
    }

    if (messageBody instanceof String && asType == null) {
        throw new IllegalArgumentException("The type must be set when using a String body.");
    }

    final HttpPut httpPut = new HttpPut(urlLocation);

    try {
        if (messageBody instanceof String) {
            httpPut.setEntity(new StringEntity((String) messageBody));
            httpPut.setHeader(HttpHeaders.CONTENT_TYPE, asType.toString());
        } else {
            httpPut.setEntity(
                    new StringEntity(new Serializer().toType(messageBody, MediaType.APPLICATION_JSON)));
            httpPut.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
        }
    } catch (UnsupportedEncodingException e) {
        httpPut.setEntity(new BasicHttpEntity());
    }

    for (Map.Entry<CustomHeaders, List<String>> entry : headerMap.entrySet()) {
        for (String headerValue : entry.getValue()) {
            httpPut.addHeader(entry.getKey().getName(), headerValue);
        }
    }

    httpPut.addHeader(CustomHeaders.IF_MATCH.getName(), eTag);

    DefaultHttpClient client = new DefaultHttpClient(params);
    try {
        final HttpResponse response = client.execute(httpPut);
        return new ResultsFactory().conditionalPutResultFromHttpPutResponse(response);
    } catch (HttpHostConnectException e) {
        throw new ChannelClientException(
                "Exception connecting to server. "
                        + "Ensure server is running and configured using the right ip address and port.",
                e, ChannelClientException.ExceptionType.CONNECTION);
    } catch (ClientProtocolException e) {
        throw new ChannelClientException("Exception communicating with server.", e,
                ChannelClientException.ExceptionType.TRANSPORT_PROTOCOL);
    } catch (Exception e) {
        throw new ChannelClientException("Unknown exception occurred when trying to update the message:", e,
                ChannelClientException.ExceptionType.UNKNOWN);
    }

}

From source file:guru.nidi.ramltester.HttpCommonsTest.java

@Test
public void emptyPostRequest() throws IOException {
    final HttpPut put = new HttpPut(url("base"));
    client.execute(put);/* w  w w.j  a v a2 s  . c  o m*/
    assertThat(client.getLastReport(), checks());
}

From source file:com.ettrema.httpclient.TransferService.java

public HttpResult put(String encodedUrl, InputStream content, Long contentLength, String contentType,
        ProgressListener listener) {//  w ww  .j av  a  2  s  .c o m
    LogUtils.trace(log, "put: ", encodedUrl);
    notifyStartRequest();
    String s = encodedUrl;
    HttpPut p = new HttpPut(s);

    NotifyingFileInputStream notifyingIn = null;
    try {
        notifyingIn = new NotifyingFileInputStream(content, contentLength, s, listener);
        HttpEntity requestEntity;
        if (contentLength == null) {
            throw new RuntimeException("Content length for input stream is null, you must provide a length");
        } else {
            requestEntity = new InputStreamEntity(notifyingIn, contentLength);
        }
        p.setEntity(requestEntity);
        return Utils.executeHttpWithResult(client, p, null);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeQuietly(notifyingIn);
        notifyFinishRequest();
    }
}

From source file:com.github.kristofa.test.http.MockHttpServerTestNG.java

@Test
public void testShouldHandlePutRequests() throws IOException {
    // Given a mock server configured to respond to a DELETE /test
    responseProvider.expect(Method.PUT, "/test", "text/plain; charset=UTF-8", "Hello World").respondWith(200,
            "text/plain", "Welcome");

    // When a request for DELETE /test arrives
    final HttpPut req = new HttpPut(baseUrl + "/test");
    req.setEntity(new StringEntity("Hello World", UTF_8));
    final HttpResponse response = client.execute(req);

    final String responseBody = IOUtils.toString(response.getEntity().getContent());
    final int statusCode = response.getStatusLine().getStatusCode();

    // Then the response status is 204
    Assert.assertEquals(200, statusCode);
    Assert.assertEquals("Welcome", responseBody);
}

From source file:org.fcrepo.integration.AbstractResourceIT.java

protected static HttpPut putDSMethod(final String pid, final String ds, final String content)
        throws UnsupportedEncodingException {
    final HttpPut put = new HttpPut(serverAddress + pid + "/" + ds + "/fcr:content");

    put.setEntity(new StringEntity(content));
    return put;/*www  . ja  va 2s . c  om*/
}

From source file:org.camunda.connect.httpclient.impl.AbstractHttpConnector.java

@SuppressWarnings("unchecked")
protected <T extends HttpRequestBase> T createHttpRequestBase(Q request) {
    String url = request.getUrl();
    if (url != null && !url.trim().isEmpty()) {
        String method = request.getMethod();
        if (HttpGet.METHOD_NAME.equals(method)) {
            return (T) new HttpGet(url);
        } else if (HttpPost.METHOD_NAME.equals(method)) {
            return (T) new HttpPost(url);
        } else if (HttpPut.METHOD_NAME.equals(method)) {
            return (T) new HttpPut(url);
        } else if (HttpDelete.METHOD_NAME.equals(method)) {
            return (T) new HttpDelete(url);
        } else if (HttpPatch.METHOD_NAME.equals(method)) {
            return (T) new HttpPatch(url);
        } else if (HttpHead.METHOD_NAME.equals(method)) {
            return (T) new HttpHead(url);
        } else if (HttpOptions.METHOD_NAME.equals(method)) {
            return (T) new HttpOptions(url);
        } else if (HttpTrace.METHOD_NAME.equals(method)) {
            return (T) new HttpTrace(url);
        } else {//from w  w w .ja  v  a  2 s.co  m
            throw LOG.unknownHttpMethod(method);
        }
    } else {
        throw LOG.requestUrlRequired();
    }
}

From source file:com.ab.network.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *///from   w w  w  .  j a va  2  s. com
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for backwards compatibility.
        // If the request's post body is null, then the assumption is that the request is
        // GET.  Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:org.deviceconnect.android.profile.restful.test.FailMediaPlayerProfileTestCase.java

/**
 * deviceId????????./*from  w  w  w.ja  va 2  s  .c o  m*/
 * <pre>
 * ?HTTP
 * Method: PUT
 * Path: /media_player/media?mediaId=xxxx
 * </pre>
 * <pre>
 * ??
 * result?1???????
 * </pre>
 */
public void testPutMediaNoDeviceId() {
    URIBuilder builder = TestURIBuilder.createURIBuilder();
    builder.setProfile(MediaPlayerProfileConstants.PROFILE_NAME);
    builder.setAttribute(MediaPlayerProfileConstants.ATTRIBUTE_MEDIA);
    builder.addParameter(MediaPlayerProfileConstants.PARAM_MEDIA_ID, TEST_MEDIA_ID);
    builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
    try {
        HttpUriRequest request = new HttpPut(builder.toString());
        JSONObject root = sendRequest(request);
        assertResultError(ErrorCode.EMPTY_DEVICE_ID.getCode(), root);
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}