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:io.djigger.monitoring.java.instrumentation.subscription.HttpClientTracerTest.java

private CloseableHttpResponse callPut() throws Exception {
    CloseableHttpClient client = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {//from  w ww  . jav a2 s  .c  o m
        HttpPut put = new HttpPut("http://localhost:12298");
        response = client.execute(put);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

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

public String addTagToApplication(String appName, String tagName) throws IOException {
    String result = null;/*from  w w w  .  j a  v a 2  s.  c o m*/

    String uri = url + "/cli/application/tag?application=" + encodePath(appName) + "&tag="
            + encodePath(tagName);

    HttpPut method = new HttpPut(uri);
    HttpResponse response = invokeMethod(method);
    result = getBody(response);
    return result;
}

From source file:it.polimi.tower4clouds.rules.actions.RestCall.java

@Override
public void execute(String resourceId, String value, String timestamp) {
    getLogger().info("Action requested. Input data: {}, {}, {}", resourceId, value, timestamp);
    try {/*from w ww .java2 s.  c  om*/
        CloseableHttpClient client = HttpClients.createDefault();
        HttpUriRequest request;
        String url = getParameters().get(URL);
        String method = getParameters().get(METHOD).toUpperCase();
        switch (method) {
        case POST:
            request = new HttpPost(url);
            break;
        case PUT:
            request = new HttpPut(url);
            break;
        case DELETE:
            request = new HttpDelete(url);
            break;
        case GET:
            request = new HttpGet(url);
            break;

        default:
            getLogger().error("Unknown method {}", method);
            return;
        }
        request.setHeader("Cache-Control", "no-cache");
        CloseableHttpResponse response = client.execute(request);
        getLogger().info("Rest call executed");
        response.close();
    } catch (Exception e) {
        getLogger().error("Error executing rest call", e);
    }

}

From source file:io.apiman.manager.api.gateway.rest.GatewayClient.java

/**
 * @see io.apiman.gateway.api.rest.contract.IApplicationResource#register(io.apiman.gateway.engine.beans.Application)
 *//*from w  w  w.j a va  2 s.co m*/
public void register(Application application) throws RegistrationException, NotAuthorizedException {
    try {
        URI uri = new URI(this.endpoint + APPLICATIONS);
        HttpPut put = new HttpPut(uri);
        put.setHeader("Content-Type", "application/json"); //$NON-NLS-1$ //$NON-NLS-2$
        String jsonPayload = mapper.writer().writeValueAsString(application);
        HttpEntity entity = new StringEntity(jsonPayload);
        put.setEntity(entity);
        HttpResponse response = httpClient.execute(put);
        int actualStatusCode = response.getStatusLine().getStatusCode();
        if (actualStatusCode >= 300) {
            throw new Exception("Application registration failed: " + actualStatusCode); //$NON-NLS-1$
        }
    } catch (Exception e) {
        // TODO log this error
        throw new RuntimeException(e);
    }
}

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

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

    HttpPut putRequest = null;/*from w w w.j a v  a2 s  .  co  m*/
    try {
        putRequest = new HttpPut(resourcePath);

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

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

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

public String addTagToResource(String resourceName, String tagName) throws IOException {
    String result = null;/*from  w  w  w  . j av a  2s .c o m*/

    String uri = url + "/cli/resource/tag?resource=" + encodePath(resourceName) + "&tag=" + encodePath(tagName);

    HttpPut method = new HttpPut(uri);
    HttpResponse response = invokeMethod(method);
    result = getBody(response);
    return result;
}

From source file:cycronix.ctlib.CThttp.java

private HttpResponse httpput(String SourceChan, byte[] body) throws IOException {

    String url = CTwebhost + "/" + SourceChan;
    final HttpPut put = new HttpPut(url);
    if (userpass != null)
        put.setHeader("Authorization", "Basic " + userpass);

    if (body != null) {
        CTinfo.debugPrint("PUT: " + url + ", userpass: " + userpass);
        put.setEntity(new ByteArrayEntity(body));
    }/* w  ww.j ava 2s .c  o  m*/
    return httpclient.execute(put);
}

From source file:com.meltmedia.cadmium.cli.AuthCommand.java

@Override
public void execute() throws Exception {
    if (args == null || args.size() != 2) {
        System.err.println("Please specify action (list|add|remove) and site.");
        System.exit(1);//  w w w.  ja  v  a2 s .  c  om
    }
    String action = args.get(0);
    String site = this.getSecureBaseUrl(args.get(1) + SERVICE_PATH);
    HttpUriRequest request = null;
    int expectedStatus = HttpStatus.SC_OK;
    if (action.equalsIgnoreCase("list")) {
        System.out.println("Listing site (" + args.get(1) + ") specific users.");
        request = new HttpGet(site);
    } else if (action.equalsIgnoreCase("add")) {
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
            expectedStatus = HttpStatus.SC_CREATED;
            String passwordHash = hashPasswordForShiro();
            System.out.println("Adding User [" + username + "] with password hash [" + passwordHash + "]");
            request = new HttpPut(site + "/" + username);
            ((HttpPut) request).setEntity(new StringEntity(passwordHash));
        } else {
            System.err.println("Both username and password are required to add a user.");
            System.exit(1);
        }
    } else if (action.equalsIgnoreCase("remove")) {
        if (StringUtils.isNotBlank(username)) {
            expectedStatus = HttpStatus.SC_GONE;
            System.out.println("Removing User [" + username + "]");
            request = new HttpDelete(site + "/" + username);
        } else {
            System.err.println("The username of the user to remove is required.");
            System.exit(1);
        }
    }
    sendRequest(request, expectedStatus);
}

From source file:org.androidx.frames.libs.volley.HttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *///from  w  w w . j av a2 s.  c  om
/* 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;
    }
    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(HEADER_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.FailProximityProfileTestCase.java

/**
 * deviceId??ondeviceproximity???./*from w w  w.  j a v a  2s .c  o  m*/
 * <pre>
 * ?HTTP
 * Method: PUT
 * Path: /proximity/ondeviceproximity?deviceId=&sessionKey=xxxx
 * </pre>
 * <pre>
 * ??
 * result?1???????
 * </pre>
 */
public void testPutOnDeviceProximityChangeEmptyDeviceId() {
    URIBuilder builder = TestURIBuilder.createURIBuilder();
    builder.setProfile(ProximityProfileConstants.PROFILE_NAME);
    builder.setAttribute(ProximityProfileConstants.ATTRIBUTE_ON_DEVICE_PROXIMITY);
    builder.addParameter(DConnectProfileConstants.PARAM_DEVICE_ID, "");
    builder.addParameter(DConnectMessage.EXTRA_SESSION_KEY, getClientId());
    builder.addParameter(DConnectMessage.EXTRA_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());
    }
}