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

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

Introduction

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

Prototype

public HttpDelete(final String uri) 

Source Link

Usage

From source file:net.oauth.client.httpclient4.HttpClient4.java

public HttpResponseMessage execute(HttpMessage request, Map<String, Object> parameters) throws IOException {
    final String method = request.method;
    final String url = request.url.toExternalForm();
    final InputStream body = request.getBody();
    final boolean isDelete = DELETE.equalsIgnoreCase(method);
    final boolean isPost = POST.equalsIgnoreCase(method);
    final boolean isPut = PUT.equalsIgnoreCase(method);
    byte[] excerpt = null;
    HttpRequestBase httpRequest;//w  w  w . j a  v a  2  s. c  o  m
    if (isPost || isPut) {
        HttpEntityEnclosingRequestBase entityEnclosingMethod = isPost ? new HttpPost(url) : new HttpPut(url);
        if (body != null) {
            ExcerptInputStream e = new ExcerptInputStream(body);
            excerpt = e.getExcerpt();
            String length = request.removeHeaders(HttpMessage.CONTENT_LENGTH);
            entityEnclosingMethod
                    .setEntity(new InputStreamEntity(e, (length == null) ? -1 : Long.parseLong(length)));
        }
        httpRequest = entityEnclosingMethod;
    } else if (isDelete) {
        httpRequest = new HttpDelete(url);
    } else {
        httpRequest = new HttpGet(url);
    }
    for (Map.Entry<String, String> header : request.headers) {
        httpRequest.addHeader(header.getKey(), header.getValue());
    }
    HttpParams params = httpRequest.getParams();
    for (Map.Entry<String, Object> p : parameters.entrySet()) {
        String name = p.getKey();
        String value = p.getValue().toString();
        if (FOLLOW_REDIRECTS.equals(name)) {
            params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.parseBoolean(value));
        } else if (READ_TIMEOUT.equals(name)) {
            params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, Integer.parseInt(value));
        } else if (CONNECT_TIMEOUT.equals(name)) {
            params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Integer.parseInt(value));
        }
    }
    HttpClient client = clientPool.getHttpClient(new URL(httpRequest.getURI().toString()));
    HttpResponse httpResponse = client.execute(httpRequest);
    return new HttpMethodResponse(httpRequest, httpResponse, excerpt, request.getContentCharset());
}

From source file:org.sensapp.android.sensappdroid.restrequests.RestRequest.java

public static String deleteSensor(Uri uri, Sensor sensor) throws RequestErrorException {
    URI target;//www. j a  v  a 2s.com
    try {
        target = new URI(uri.toString() + SENSOR_PATH + "/" + sensor.getName());
    } catch (URISyntaxException e) {
        e.printStackTrace();
        throw new RequestErrorException(e.getMessage());
    }
    HttpClient client = new DefaultHttpClient();
    HttpDelete request = new HttpDelete(target);
    request.setHeader("Content-type", "application/json");
    String response = null;
    try {
        response = resolveResponse(client.execute(request));
    } catch (Exception e) {
        throw new RequestErrorException(e.getMessage());
    }
    return response;
}

From source file:com.servioticy.restclient.RestClient.java

public FutureRestResponse restRequest(String url, String body, int method, Map<String, String> headers)
        throws RestClientException, RestClientErrorCodeException {
    HttpRequestBase httpMethod;//from   w  w w .j a v a2 s . c  om
    StringEntity input;

    switch (method) {
    case POST:
        try {
            input = new StringEntity(body);
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
            throw new RestClientException(e.getMessage());
        }
        input.setContentType("application/json");
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(input);
        httpMethod = httpPost;
        break;
    case PUT:
        try {
            input = new StringEntity(body);
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
            throw new RestClientException(e.getMessage());
        }
        input.setContentType("application/json");
        HttpPut httpPut = new HttpPut(url);
        httpPut.setEntity(input);
        httpMethod = httpPut;
        break;
    case DELETE:
        httpMethod = new HttpDelete(url);
        break;
    case GET:
        httpMethod = new HttpGet(url);
        break;
    default:
        return null;
    }

    if (headers != null) {
        for (Map.Entry<String, String> header : headers.entrySet()) {
            httpMethod.addHeader(header.getKey(), header.getValue());
        }
    }

    Future<HttpResponse> response;
    try {
        response = httpClient.execute(httpMethod, null);
    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new RestClientException(e.getMessage());
    }

    // TODO Check the errors nicely
    FutureRestResponse rr = new FutureRestResponse(response);

    return rr;

}

From source file:org.apache.edgent.connectors.http.runtime.HttpRequester.java

@Override
public R apply(T t) {

    if (client == null)
        client = clientCreator.get();//ww w.j a  v a2  s.c o  m

    String m = method.apply(t);
    String uri = url.apply(t);
    HttpUriRequest request;

    switch (m) {

    case HttpGet.METHOD_NAME:
        request = new HttpGet(uri);
        break;
    case HttpDelete.METHOD_NAME:
        request = new HttpDelete(uri);
        break;
    case HttpPost.METHOD_NAME:
        request = new HttpPost(uri);
        break;
    case HttpPut.METHOD_NAME:
        request = new HttpPut(uri);
        break;

    default:
        throw new IllegalArgumentException();
    }

    // If entity is not null means http request should have a body
    if (entity != null) {

        HttpEntity body = entity.apply(t);

        if (request instanceof HttpEntityEnclosingRequest == false) {
            throw new IllegalArgumentException("Http request does not support body");
        }

        ((HttpEntityEnclosingRequest) request).setEntity(body);
    }

    try {
        try (CloseableHttpResponse response = client.execute(request)) {
            return responseProcessor.apply(t, response);
        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:me.xiaopan.android.gohttp.HttpClientNetManager.java

private HttpUriRequest httpRequest2HttpUriRequest(HttpRequest httpRequest) {
    HttpUriRequest httpUriRequest = null;
    switch (httpRequest.getMethod()) {
    case GET:/*from w  ww  . j  a  v  a2 s  . c  o  m*/
        String url = getUrlByParams(true, httpRequest.getUrl(), httpRequest.getParams());
        HttpGet httGet = new HttpGet(url);
        appendHeaders(httGet, httpRequest.getHeaders());
        httpUriRequest = httGet;
        break;
    case POST:
        HttpPost httPost = new HttpPost(httpRequest.getUrl());
        appendHeaders(httPost, httpRequest.getHeaders());

        HttpEntity httpPostEntity = httpRequest.getHttpEntity();
        if (httpPostEntity == null && httpRequest.getParams() != null) {
            httpPostEntity = httpRequest.getParams().getEntity();
        }
        if (httpPostEntity != null) {
            httPost.setEntity(httpPostEntity);
        }
        httpUriRequest = httPost;
        break;
    case DELETE:
        String deleteUrl = getUrlByParams(true, httpRequest.getUrl(), httpRequest.getParams());
        HttpDelete httDelete = new HttpDelete(deleteUrl);
        appendHeaders(httDelete, httpRequest.getHeaders());
        httpUriRequest = httDelete;
        break;
    case PUT:
        HttpPut httpPut = new HttpPut(httpRequest.getUrl());
        appendHeaders(httpPut, httpRequest.getHeaders());

        HttpEntity httpPutEntity = httpRequest.getHttpEntity();
        if (httpPutEntity == null && httpRequest.getParams() != null) {
            httpPutEntity = httpRequest.getParams().getEntity();
        }
        if (httpPutEntity != null) {
            httpPut.setEntity(httpPutEntity);
        }
        httpUriRequest = httpPut;
        break;
    }
    return httpUriRequest;
}

From source file:org.fcrepo.integration.webhooks.FedoraWebhooksIT.java

public void deleteWebhookTest() throws Exception {
    final HttpPost method = new HttpPost(serverAddress + "/fcr:webhooks/callback_id");

    final List<NameValuePair> formparams = new ArrayList<NameValuePair>();

    formparams.add(new BasicNameValuePair("callbackUrl", RESTAPI_NAMESPACE + "/fake:url"));
    final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
    method.setEntity(entity);/*from   www  .  j ava  2s.c o m*/

    assertEquals(201, getStatus(method));

    final HttpDelete delete_method = new HttpDelete(serverAddress + "/fcr:webhooks/callback_id");

    assertEquals(204, getStatus(delete_method));
}

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

@Test
public void testShouldHandleDeleteRequests() throws IOException {
    // Given a mock server configured to respond to a DELETE /test
    responseProvider.expect(Method.DELETE, "/test").respondWith(204, "text/plain", "");

    // When a request for DELETE /test arrives
    final HttpDelete req = new HttpDelete(baseUrl + "/test");
    final HttpResponse response = client.execute(req);

    // Then the response status is 204
    Assert.assertEquals(204, response.getStatusLine().getStatusCode());
}

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

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from   w  ww.j ava2s . co m*/
/* 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:com.hoccer.api.Linccer.java

public void disconnect() throws UpdateException {
    HttpResponse response;// w w  w .  j  av  a  2s . com
    try {
        String uri = mConfig.getClientUri() + "/environment";
        HttpDelete request = new HttpDelete(sign(uri));
        response = getHttpClient().execute(request);
    } catch (Exception e) {
        throw new UpdateException(
                "could not update gps measurement for " + mConfig.getClientUri() + " because of " + e);
    }

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new UpdateException("could not delete environment because server responded with status "
                + response.getStatusLine().getStatusCode());
    }
}

From source file:org.bibimbap.shortcutlink.RestClient.java

/**
 * Execute the REST subtasks/*from   w  w w  . j av a  2  s .  com*/
 */
protected RestClientRequest[] doInBackground(RestClientRequest... params) {
    // HttpClient that is configured with reasonable default settings and registered schemes for Android
    final AndroidHttpClient httpClient = AndroidHttpClient.newInstance(RestClient.class.getSimpleName());

    for (int index = 0; index < params.length; index++) {
        RestClientRequest rcr = params[index];
        HttpUriRequest httprequest = null;
        try {
            HttpResponse httpresponse = null;
            HttpEntity httpentity = null;

            // initiating
            publishProgress(params.length, index, RestfulState.DS_INITIATING);

            switch (rcr.getHttpRequestType()) {
            case HTTP_PUT:
                httprequest = new HttpPut(rcr.getURI());
                break;
            case HTTP_POST:
                httprequest = new HttpPost(rcr.getURI());
                break;
            case HTTP_DELETE:
                httprequest = new HttpDelete(rcr.getURI());
                break;
            case HTTP_GET:
            default:
                // default to HTTP_GET
                httprequest = new HttpGet(rcr.getURI());
                break;
            }

            // resting
            publishProgress(params.length, index, RestfulState.DS_ONGOING);

            if ((httpresponse = httpClient.execute(httprequest)) != null) {
                if ((httpentity = httpresponse.getEntity()) != null) {
                    rcr.setResponse(EntityUtils.toByteArray(httpentity));
                }
            }

            // success
            publishProgress(params.length, index, RestfulState.DS_SUCCESS);
        } catch (Exception ioe) {
            Log.i(TAG, ioe.getClass().getSimpleName());

            // clear out the response
            rcr.setResponse(null);

            // abort the request on failure
            httprequest.abort();

            // failed
            publishProgress(params.length, index, RestfulState.DS_FAILED);
        }
    }

    // close the connection
    if (httpClient != null)
        httpClient.close();

    return params;
}