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:org.sonatype.nexus.testsuite.repo.nexus4548.Nexus4548RepoTargetPermissionMatchesPathInRepoIT.java

private HttpResponse put(final String gavPath, final int code) throws Exception {
    HttpPut putMethod = new HttpPut(getNexusTestRepoUrl() + gavPath);
    putMethod.setEntity(new FileEntity(getTestFile("pom-a.pom"), "text/xml"));

    final HttpResponse response = RequestFacade.executeHTTPClientMethod(putMethod);
    assertThat(response.getStatusLine().getStatusCode(), Matchers.is(code));
    return response;
}

From source file:com.zlk.bigdemo.android.volley.toolbox.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from  ww  w  .ja  v a 2  s  . co  m*/
@SuppressWarnings("deprecation")
/* protected */ static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    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:com.uber.stream.kafka.mirrormaker.common.utils.HttpClientUtils.java

public static int putData(final HttpClient httpClient, final RequestConfig requestConfig, final String host,
        final int port, final String path, final JSONObject entity) throws IOException, URISyntaxException {
    URI uri = new URIBuilder().setScheme("http").setHost(host).setPort(port).setPath(path).build();

    HttpPut httpPut = new HttpPut(uri);
    httpPut.setConfig(requestConfig);/*from w  ww  .  j  a v  a  2 s.  co  m*/

    StringEntity params = new StringEntity(entity.toJSONString());
    httpPut.setEntity(params);

    return httpClient.execute(httpPut, HttpClientUtils.createResponseCodeExtractor());
}

From source file:com.urbancode.ud.client.ResourceClient.java

public void addResourceToTeam(String resource, String team, String type) throws IOException {
    String uri = url + "/cli/resource/teams?team=" + encodePath(team) + "&type=" + encodePath(type)
            + "&resource=" + encodePath(resource);
    HttpPut method = new HttpPut(uri);
    invokeMethod(method);//from  w  w w .j  a v a2s  . c om
}

From source file:com.robustaweb.library.rest.client.implementation.AndroidRestClient.java

/**
 * {@inheritDoc }/*from  w  ww .  j  ava2  s  .com*/
 */
@Override
protected void executeMethod(final HttpMethod method, final String url, final String requestBody,
        final Callback callback) throws HttpException {

    requestThread = new Thread() {

        @Override
        public void run() {

            HttpRequestBase meth = null;
            try {
                switch (method) {
                case GET:
                    meth = new HttpGet(url);

                    break;
                case POST:
                    meth = new HttpPost(url);
                    break;
                case DELETE:
                    meth = new HttpDelete(url);
                    break;
                case PUT:
                    meth = new HttpPut(url);
                    break;
                default:
                    meth = new HttpEntityEnclosingRequestBase() {
                        @Override
                        public String getMethod() {
                            return method.getMethod();
                        }
                    };
                    break;
                }
                // this.builder = new RequestBuilder(meth, url);

                if (contentType != null && !contentType.isEmpty()) {

                    meth.addHeader("Content-Type", contentType);
                }
                if (AndroidRestClient.authorizationValue != null
                        && AndroidRestClient.authorizationValue.length() > 0) {
                    meth.addHeader("Authorization", AndroidRestClient.authorizationValue);
                }

                HttpContext localContext = new BasicHttpContext();
                HttpResponse response = client.execute(meth, localContext);
                callback.onSuccess(response.toString());

                // headers response
                HeaderIterator it = response.headerIterator();
                while (it.hasNext()) {
                    Header header = it.nextHeader();
                    responseHeaders.put(header.getName(), header.getValue());
                }

            } catch (Exception ex) {
                callback.onException(ex);
            } finally {
                clean();
            }
        }
    };
}

From source file:ai.eve.volley.stack.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from w  w w  . j  a v a2 s  .  c o  m*/
private static HttpUriRequest createHttpRequest(Request<?> request) throws AuthFailureError {
    switch (request.getMethod()) {
    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(HTTP.CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HTTP.CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    case Method.HEAD:
        return new HttpHead(request.getUrl());
    case Method.OPTIONS:
        return new HttpOptions(request.getUrl());
    case Method.TRACE:
        return new HttpTrace(request.getUrl());
    case Method.PATCH: {
        HttpPatch patchRequest = new HttpPatch(request.getUrl());
        patchRequest.addHeader(HTTP.CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(patchRequest, request);
        return patchRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

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

/**
 * deviceId?????./*ww  w.  j ava  2 s. c o  m*/
 * <pre>
 * ?HTTP
 * Method: PUT
 * Path: /vibration/vibrate?deviceId=
 * </pre>
 * <pre>
 * ??
 * result?1???????
 * </pre>
 */
public void testPutVibrateEmptyDeviceId() {
    URIBuilder builder = TestURIBuilder.createURIBuilder();
    builder.setProfile(VibrationProfileConstants.PROFILE_NAME);
    builder.setAttribute(VibrationProfileConstants.ATTRIBUTE_VIBRATE);
    builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, "");
    builder.addParameter(AuthorizationProfileConstants.PARAM_ACCESS_TOKEN, getAccessToken());
    try {
        HttpUriRequest request = new HttpPut(builder.toString());
        JSONObject root = sendRequest(request);
        assertResultError(ErrorCode.NOT_FOUND_DEVICE.getCode(), root);
    } catch (JSONException e) {
        fail("Exception in JSONObject." + e.getMessage());
    }
}

From source file:com.robustaweb.library.rest.client.implementation.ApacheRestClient.java

@Override
protected String executeMethod(HttpMethod method, String url, String requestBody) throws HttpException {
    assert url.startsWith("http");

    try {/*from  w  w  w . j a  v  a  2s.c o m*/
        client = createClient();
        HttpUriRequest httpMethod = null;

        switch (method) {
        case GET:
            httpMethod = new HttpGet(url);
            break;
        case DELETE:
            httpMethod = new HttpDelete(url);
            break;

        case POST:
            httpMethod = new HttpPost(url);
            ((HttpPost) httpMethod).setEntity(new StringEntity(this.requestBody));
            break;
        case PUT:
            httpMethod = new HttpPut(url);
            ((HttpPut) httpMethod).setEntity(new StringEntity(this.requestBody));
            break;
        default:
            throw new IllegalStateException("Can't execute this method : " + method);
        }

        //Adding headers
        if (this.contentType == null) {
            this.contentType = SynchronousRestClient.xmlContentType;
        }
        httpMethod.addHeader("Content-type", this.contentType);
        if (authorizationValue != null) {
            httpMethod.addHeader("Authorization", ApacheRestClient.authorizationValue);
        }

        //Executing
        HttpResponse httpResponse = client.execute(httpMethod);

        //parsing responseHeaders

        HeaderIterator it = httpResponse.headerIterator();
        while (it.hasNext()) {
            Header header = it.nextHeader();
            responseHeaders.put(header.getName(), header.getValue());
        }

        //Parsing response
        this.response = FileUtils.readInputStream(httpResponse.getEntity().getContent());

        return this.response;

    } catch (IOException ex) {
        throw new HttpException("IO Exception : " + ex.getMessage(), ex);
    } finally {
        clear();
    }
}

From source file:org.aerogear.android.impl.core.HttpRestProvider.java

/**
 * {@inheritDoc}//from   w ww . jav a  2 s .  c o  m
 */
@Override
public HeaderAndBody put(String id, String data) throws RuntimeException {
    HttpPut put = new HttpPut(appendIdToURL(id));
    addBodyRequest(put, data);
    try {
        return execute(put);
    } catch (IOException e) {
        Log.e(TAG, "Error on PUT of " + url, e);
        throw new RuntimeException(e);
    }
}

From source file:it.larusba.integration.couchbase.document.transformer.RemoteNeo4jTransformer.java

/**
 * It perform the REST call to the URL configured in the
 * transofrmer.properties file//ww  w . jav  a 2s .  c o  m
 * 
 * @throws IOException
 */
private void transformJson2Cypher(String documentKey, String documentType, String jsonDocument)
        throws IOException {

    Properties prop = new Properties();
    String propFileName = "transformer.properties";

    inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

    if (inputStream != null) {
        prop.load(inputStream);
    } else {
        throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
    }

    // get the URL property value
    String urlJson2Cypher = prop.getProperty("transformer.url");

    JsonDocument documentToTransform = new JsonDocument(documentKey, jsonDocument, documentType);

    HttpClient httpClient = HttpClientBuilder.create().build();

    HttpPut putRequest = new HttpPut(urlJson2Cypher);
    putRequest.addHeader("content-type", MediaType.APPLICATION_JSON);

    JSONObject json = new JSONObject(documentToTransform);

    StringEntity entity = new StringEntity(json.toString());

    putRequest.setEntity(entity);

    HttpResponse response = httpClient.execute(putRequest);

    if (response.getStatusLine().getStatusCode() != 200) {

        throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
    }
}