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

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

Introduction

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

Prototype

String METHOD_NAME

To view the source code for org.apache.http.client.methods HttpDelete METHOD_NAME.

Click Source Link

Usage

From source file:retsys.client.http.HttpHelper.java

public String executeHttpRequest(HttpClient client, HttpUriRequest req) throws IOException {
    String response = null;/* ww w .  j  a  va 2s .  com*/
    HttpResponse httpResponse = client.execute(req);
    int httpStatus = httpResponse.getStatusLine().getStatusCode();
    if (httpStatus >= 200 && httpStatus < 300) {
        if (!HttpDelete.METHOD_NAME.equals(req.getMethod()) && !HttpPut.METHOD_NAME.equals(req.getMethod())) {
            response = readFromStream(httpResponse.getEntity().getContent());
        }
    } else if (httpStatus == 409) {
        response = readFromStream(httpResponse.getEntity().getContent());
        System.out.println("response:  " + response);
        response = "!ERROR!";
    } else {
        response = "!ERROR!";
    }

    return response;
}

From source file:com.evolveum.midpoint.report.impl.ReportNodeUtils.java

public static InputStream executeOperation(String host, String fileName, String intraClusterHttpUrlPattern,
        String operation) throws CommunicationException, SecurityViolationException, ObjectNotFoundException,
        ConfigurationException, IOException {
    fileName = fileName.replaceAll("\\s", SPACE);
    InputStream inputStream = null;
    InputStream entityContent = null;
    LOGGER.trace("About to initiate connection with {}", host);
    try {//from ww  w . jav a2  s.  c  o m
        if (StringUtils.isNotEmpty(intraClusterHttpUrlPattern)) {
            LOGGER.trace("The cluster uri pattern: {} ", intraClusterHttpUrlPattern);
            URI requestUri = buildURI(intraClusterHttpUrlPattern, host, fileName);
            fileName = URLDecoder.decode(fileName, ReportTypeUtil.URLENCODING);

            LOGGER.debug("Sending request to the following uri: {} ", requestUri);
            HttpRequestBase httpRequest = buildHttpRequest(operation);
            httpRequest.setURI(requestUri);
            httpRequest.setHeader("User-Agent", ReportTypeUtil.HEADER_USERAGENT);
            HttpClient client = HttpClientBuilder.create().build();
            try (CloseableHttpResponse response = (CloseableHttpResponse) client.execute(httpRequest)) {
                HttpEntity entity = response.getEntity();
                Integer statusCode = response.getStatusLine().getStatusCode();

                if (statusCode == HttpStatus.SC_OK) {
                    LOGGER.debug("Response OK, the file successfully returned by the cluster peer. ");
                    if (entity != null) {
                        entityContent = entity.getContent();
                        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                        byte[] buffer = new byte[1024];
                        int len;
                        while ((len = entityContent.read(buffer)) > -1) {
                            arrayOutputStream.write(buffer, 0, len);
                        }
                        arrayOutputStream.flush();
                        inputStream = new ByteArrayInputStream(arrayOutputStream.toByteArray());
                    }
                } else if (statusCode == HttpStatus.SC_NO_CONTENT) {
                    if (HttpDelete.METHOD_NAME.equals(operation)) {
                        LOGGER.info("Deletion of the file {} was successful.", fileName);
                    }
                } else if (statusCode == HttpStatus.SC_FORBIDDEN) {
                    LOGGER.error("The access to the report with the name {} is forbidden.", fileName);
                    String error = "The access to the report " + fileName + " is forbidden.";
                    throw new SecurityViolationException(error);
                } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
                    String error = "The report file " + fileName
                            + " was not found on the originating nodes filesystem.";
                    throw new ObjectNotFoundException(error);
                }
            } catch (ClientProtocolException e) {
                String error = "An exception with the communication protocol has occurred during a query to the cluster peer. "
                        + e.getLocalizedMessage();
                throw new CommunicationException(error);
            }
        } else {
            LOGGER.error(
                    "Cluster pattern parameters is empty, please refer to the documentation and set up the parameter value accordingly");
            throw new ConfigurationException(
                    "Cluster pattern parameters is empty, please refer to the documentation and set up the parameter value accordingly");
        }
    } catch (URISyntaxException e1) {
        throw new CommunicationException("Invalid uri syntax: " + e1.getLocalizedMessage());
    } catch (UnsupportedEncodingException e) {
        LOGGER.error("Unhandled exception when listing nodes");
        LoggingUtils.logUnexpectedException(LOGGER, "Unhandled exception when listing nodes", e);
    } finally {
        IOUtils.closeQuietly(entityContent);
    }

    return inputStream;
}

From source file:org.deviceconnect.message.http.impl.factory.AbstractHttpMessageFactory.java

/**
 * HTTP 1???dConnect??.//w  w w  . j a  va 2  s  .  c  o m
 * @param message HTTP
 * @return dConnect
 */
protected DConnectMessage parseFirstLine(final M message) {
    mLogger.entering(getClass().getName(), "parseFirstLine", message);
    DConnectMessage dmessage = null;

    if (message instanceof HttpRequest) {

        // put action
        String method = ((HttpRequest) message).getRequestLine().getMethod();
        mLogger.fine("HTTP request method: " + method);

        DConnectRequestMessage drequest = new DConnectRequestMessage();
        if (HttpGet.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_GET);
        } else if (HttpPut.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_PUT);
        } else if (HttpPost.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_POST);
        } else if (HttpDelete.METHOD_NAME.equals(method)) {
            drequest.setMethod(DConnectRequestMessage.METHOD_DELETE);
        } else {
            throw new IllegalArgumentException("invalid http request mehtod: " + method);
        }

        dmessage = drequest;
    } else if (message instanceof HttpResponse) {
        dmessage = new DConnectResponseMessage();
    } else {
        throw new IllegalArgumentException(
                "unkown http message class instance: " + message.getClass().getName());
    }

    mLogger.exiting(getClass().getName(), "parseFirstLine", dmessage);
    return dmessage;
}

From source file:com.uploader.Vimeo.java

public VimeoResponse removeVideo(String videoEndpoint) throws IOException {
    return apiRequest(videoEndpoint, HttpDelete.METHOD_NAME, null, null);
}

From source file:eu.over9000.cathode.Dispatcher.java

public <ResponseType> Result<ResponseType> performDelete(final Class<ResponseType> resultClass,
        final String path, final Parameter... parameters) {
    return performInternal(HttpDelete.METHOD_NAME, resultClass, path, null, parameters);
}

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

public Q delete() {
    return method(HttpDelete.METHOD_NAME);
}

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  va  2  s  . c  om
            throw LOG.unknownHttpMethod(method);
        }
    } else {
        throw LOG.requestUrlRequired();
    }
}

From source file:org.apache.edgent.connectors.http.HttpStreams.java

/**
 * Make an HTTP DELETE request with JsonObject. <br>
 * //  w w w .  j  a v  a  2s . c o m
 * Method specifically works with JsonObjects. For each JsonObject in the
 * stream, HTTP DELETE request is executed on provided uri. As a result,
 * Response is added to the response TStream. <br>
 * 
 * Sample usage:<br>
 * 
 * <pre>
 * {@code
 *     DirectProvider ep = new DirectProvider();
 *     Topology topology = ep.newTopology();
 *     final String url = "http://httpbin.org/delete?";
 * 
 *     JsonObject request = new JsonObject();
 *     request.addProperty("a", "abc");
 *     request.addProperty("b", "42");
 * 
 *     TStream<JsonObject> stream = topology.collection(Arrays.asList(request));
 *     TStream<JsonObject> rc = HttpStreams.deleteJson(stream,
 *             HttpClients::noAuthentication,
 *             t -> url + "a=" + t.get("a").getAsString() + "&b="
 *                     + t.get("b").getAsString());
 * }
 * </pre>
 * 
 * <br>
 * See <i>HttpTest</i> for example. <br>
 * 
 * @param stream - JsonObject TStream.
 * @param clientCreator - CloseableHttpClient supplier preferably created using {@link HttpClients}
 * @param uri - URI function which returns URI string
 * @return TStream of JsonObject which contains responses of DELETE requests
 * 
 * @see HttpStreams#requests(TStream, Supplier, Function, Function, BiFunction)
 */
public static TStream<JsonObject> deleteJson(TStream<JsonObject> stream,
        Supplier<CloseableHttpClient> clientCreator, Function<JsonObject, String> uri) {

    return HttpStreams.<JsonObject, JsonObject>requests(stream, clientCreator, t -> HttpDelete.METHOD_NAME, uri,
            HttpResponders.json());
}

From source file:io.wcm.caravan.io.http.impl.RequestUtilTest.java

@Test
public void testBuildHttpRequest_Delete() {
    RequestTemplate template = new RequestTemplate().method("delete").append("/path");
    HttpUriRequest request = RequestUtil.buildHttpRequest("http://host", template.request());

    assertEquals("http://host/path", request.getURI().toString());
    assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
}

From source file:com.googlecode.webutilities.servlets.WebProxyServlet.java

private HttpUriRequest getRequest(String method, String url) {
    if (HttpPost.METHOD_NAME.equals(method)) {
        return new HttpPost(url);
    } else if (HttpPut.METHOD_NAME.equals(method)) {
        return new HttpPut(url);
    } else if (HttpDelete.METHOD_NAME.equals(method)) {
        return new HttpDelete(url);
    } else if (HttpOptions.METHOD_NAME.equals(method)) {
        return new HttpOptions(url);
    } else if (HttpHead.METHOD_NAME.equals(method)) {
        return new HttpHead(url);
    }// w w w.  j av a  2 s . co  m
    return new HttpGet(url);
}