Example usage for org.apache.commons.httpclient.methods DeleteMethod getResponseBodyAsString

List of usage examples for org.apache.commons.httpclient.methods DeleteMethod getResponseBodyAsString

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods DeleteMethod getResponseBodyAsString.

Prototype

@Override
public String getResponseBodyAsString() throws IOException 

Source Link

Document

Returns the response body of the HTTP method, if any, as a String .

Usage

From source file:org.activiti.kickstart.service.alfresco.AlfrescoKickstartServiceImpl.java

protected void deleteWorkflowInstance(String workflowInstanceId) {
    HttpState state = new HttpState();
    state.setCredentials(new AuthScope(null, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(cmisUser, cmisPassword));

    // Only fetching one, we're only interested in the paging information, after all
    String url = ALFRESCO_BASE_URL + "api/workflow-instances/" + workflowInstanceId + "?forced=true";
    DeleteMethod deleteMethod = new DeleteMethod(url);
    LOGGER.info("Executing DELETE '" + url + "'");

    try {//www.  j a  v a  2  s . com
        HttpClient httpClient = new HttpClient();
        int result = httpClient.executeMethod(null, deleteMethod, state);

        // Display Response
        String responseJson = deleteMethod.getResponseBodyAsString();
        LOGGER.info("Response status code: " + result);
        LOGGER.info("Response body: " + responseJson);

    } catch (Throwable t) {
        System.err.println("Error: " + t.getMessage());
        t.printStackTrace();
    } finally {
        deleteMethod.releaseConnection();
    }
}

From source file:org.alfresco.module.vti.handler.alfresco.ShareUtils.java

/**
 * Deletes site using REST API, http method is sent to appropriate web script
 * /*  w w w .j a  va  2 s .co m*/
 * @param user current user
 * @param shortName shortName of site we are going to delete
 * @throws HttpException
 * @throws IOException
 */
public void deleteSite(SessionUser user, String shortName) throws HttpException, IOException {
    HttpClient httpClient = new HttpClient();
    DeleteMethod deleteSiteMethod = new DeleteMethod(getAlfrescoHostWithPort() + getAlfrescoContext()
            + "/s/api/sites/" + shortName + "?alf_ticket=" + user.getTicket());
    try {
        if (logger.isDebugEnabled())
            logger.debug("Trying to delete site with name: " + shortName);

        int status = httpClient.executeMethod(deleteSiteMethod);
        if (logger.isDebugEnabled())
            logger.debug("Delete site method returned status: " + status);
        if (status != HttpStatus.SC_OK) {
            throw new RuntimeException(
                    "Failed to delete site with name: " + shortName + ". Returned status is: " + status
                            + ". \n Response from server :\n" + deleteSiteMethod.getResponseBodyAsString());
        }
        deleteSiteMethod.getResponseBody();
    } catch (Exception e) {
        if (logger.isDebugEnabled())
            logger.debug("Fail to delete site with name: " + shortName);
        throw new RuntimeException(e);
    } finally {
        deleteSiteMethod.releaseConnection();
    }

    // deletes site dashboard
    deleteSiteDashboard(httpClient, shortName, user);

    // deletes title component
    deleteSiteComponent(httpClient, shortName, user, "title");

    // deletes navigation component
    deleteSiteComponent(httpClient, shortName, user, "navigation");

    // deletes component-2-2 component
    deleteSiteComponent(httpClient, shortName, user, "component-2-2");

    // deletes component-1-1 component
    deleteSiteComponent(httpClient, shortName, user, "component-1-1");

    // deletes component-2-1 component
    deleteSiteComponent(httpClient, shortName, user, "component-2-1");

    // deletes component-1-2 component
    deleteSiteComponent(httpClient, shortName, user, "component-1-2");
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsNullConditionsTest.java

/**
 * Tests that a <code>WebApplicationException</code> constructed with a
 * cause and response status will return the response status and empty
 * response body by default./*from  w w w  .  ja v  a 2 s  .co  m*/
 * 
 * @throws Exception
 */
public void testWebExceptionWithCauseAndResponseStatus() throws Exception {
    HttpClient client = new HttpClient();

    DeleteMethod deleteMethod = new DeleteMethod(getBaseURI() + "/webappexceptionwithcauseandresponsestatus");
    try {
        client.executeMethod(deleteMethod);
        assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), deleteMethod.getStatusCode());
        ServerContainerAssertions.assertExceptionBodyFromServer(400, deleteMethod.getResponseBodyAsString());
    } finally {
        deleteMethod.releaseConnection();
    }
}

From source file:org.apache.zeppelin.rest.InterpreterRestApiTest.java

@Test
public void testSettingsCRUD() throws IOException {
    // when: call create setting API
    String rawRequest = "{\"name\":\"md2\",\"group\":\"md\",\"properties\":{\"propname\":\"propvalue\"},"
            + "\"interpreterGroup\":[{\"class\":\"org.apache.zeppelin.markdown.Markdown\",\"name\":\"md\"}],"
            + "\"dependencies\":[]," + "\"option\": { \"remote\": true, \"session\": false }}";
    JsonObject jsonRequest = gson.fromJson(rawRequest, JsonElement.class).getAsJsonObject();
    PostMethod post = httpPost("/interpreter/setting/", jsonRequest.toString());
    String postResponse = post.getResponseBodyAsString();
    LOG.info("testSettingCRUD create response\n" + post.getResponseBodyAsString());
    InterpreterSetting created = convertResponseToInterpreterSetting(postResponse);
    String newSettingId = created.getId();
    // then : call create setting API
    assertThat("test create method:", post, isAllowed());
    post.releaseConnection();//www  .  j a  va  2 s  .c o  m

    // when: call read setting API
    GetMethod get = httpGet("/interpreter/setting/" + newSettingId);
    String getResponse = get.getResponseBodyAsString();
    LOG.info("testSettingCRUD get response\n" + getResponse);
    InterpreterSetting previouslyCreated = convertResponseToInterpreterSetting(getResponse);
    // then : read Setting API
    assertThat("Test get method:", get, isAllowed());
    assertEquals(newSettingId, previouslyCreated.getId());
    get.releaseConnection();

    // when: call update setting API
    jsonRequest.getAsJsonObject("properties").addProperty("propname2", "this is new prop");
    PutMethod put = httpPut("/interpreter/setting/" + newSettingId, jsonRequest.toString());
    LOG.info("testSettingCRUD update response\n" + put.getResponseBodyAsString());
    // then: call update setting API
    assertThat("test update method:", put, isAllowed());
    put.releaseConnection();

    // when: call delete setting API
    DeleteMethod delete = httpDelete("/interpreter/setting/" + newSettingId);
    LOG.info("testSettingCRUD delete response\n" + delete.getResponseBodyAsString());
    // then: call delete setting API
    assertThat("Test delete method:", delete, isAllowed());
    delete.releaseConnection();
}

From source file:org.apache.zeppelin.rest.ZeppelinRestApiTest.java

@Test
public void testSettingsCRUD() throws IOException {
    // Call Create Setting REST API
    String jsonRequest = "{\"name\":\"md2\",\"group\":\"md\",\"properties\":{\"propname\":\"propvalue\"},\""
            + "interpreterGroup\":[{\"class\":\"org.apache.zeppelin.markdown.Markdown\",\"name\":\"md\"}]}";
    PostMethod post = httpPost("/interpreter/setting/", jsonRequest);
    LOG.info("testSettingCRUD create response\n" + post.getResponseBodyAsString());
    assertThat("test create method:", post, isCreated());

    Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(),
            new TypeToken<Map<String, Object>>() {
            }.getType());//ww  w.j a v a2  s.c o  m
    Map<String, Object> body = (Map<String, Object>) resp.get("body");
    //extract id from body string {id=2AWMQDNX7, name=md2, group=md,
    String newSettingId = body.toString().split(",")[0].split("=")[1];
    post.releaseConnection();

    // Call Update Setting REST API
    jsonRequest = "{\"name\":\"md2\",\"group\":\"md\",\"properties\":{\"propname\":\"Otherpropvalue\"},\""
            + "interpreterGroup\":[{\"class\":\"org.apache.zeppelin.markdown.Markdown\",\"name\":\"md\"}]}";
    PutMethod put = httpPut("/interpreter/setting/" + newSettingId, jsonRequest);
    LOG.info("testSettingCRUD update response\n" + put.getResponseBodyAsString());
    assertThat("test update method:", put, isAllowed());
    put.releaseConnection();

    // Call Delete Setting REST API
    DeleteMethod delete = httpDelete("/interpreter/setting/" + newSettingId);
    LOG.info("testSettingCRUD delete response\n" + delete.getResponseBodyAsString());
    assertThat("Test delete method:", delete, isAllowed());
    delete.releaseConnection();
}

From source file:org.apache.zeppelin.rest.ZeppelinRestApiTest.java

private void testDeleteNotebook(String notebookId) throws IOException {

    DeleteMethod delete = httpDelete(("/notebook/" + notebookId));
    LOG.info("testDeleteNotebook delete response\n" + delete.getResponseBodyAsString());
    assertThat("Test delete method:", delete, isAllowed());
    delete.releaseConnection();/*w  w w .  j  a v a2 s . com*/
    // make sure note is deleted
    if (!notebookId.isEmpty()) {
        Note deletedNote = ZeppelinServer.notebook.getNote(notebookId);
        assertNull("Deleted note should be null", deletedNote);
    }
}

From source file:org.cloudata.core.rest.CRestTable.java

public String dropTable(String tableName) throws IOException {
    HttpClient client = new HttpClient();
    DeleteMethod method = new DeleteMethod(selectRestServer() + "/table/" + tableName);

    int result = client.executeMethod(method);
    if (result != 200) {
        throw new IOException(method.getResponseBodyAsString());
    }/*w  w w .  j a v  a 2s . co m*/
    return method.getResponseBodyAsString();
}

From source file:org.cloudata.core.rest.CRestTable.java

public String delete(String tableName, String rowKey) throws IOException {
    HttpClient client = new HttpClient();
    DeleteMethod method = new DeleteMethod(selectRestServer() + "/" + tableName + "/" + rowKey);
    int result = client.executeMethod(method);
    if (result != 200) {
        throw new IOException(method.getResponseBodyAsString());
    }//  w w w  .  j  a v a  2  s. com
    return method.getResponseBodyAsString();
}

From source file:org.collectionspace.services.IntegrationTests.xmlreplay.XmlReplayTransport.java

public static ServiceResult doDELETE(String urlString, String authForTest, String testID, String fromTestID)
        throws Exception {
    ServiceResult pr = new ServiceResult();
    pr.failureReason = "";
    pr.method = "DELETE";
    pr.fullURL = urlString;/*from  w  ww.  ja v  a  2 s  .c  o m*/
    pr.fromTestID = fromTestID;
    if (Tools.isEmpty(urlString)) {
        pr.error = "url was empty.  Check the result for fromTestID: " + fromTestID + ". currentTest: "
                + testID;
        return pr;
    }
    HttpClient client = new HttpClient();
    DeleteMethod deleteMethod = new DeleteMethod(urlString);
    deleteMethod.setRequestHeader("Accept", "multipart/mixed");
    deleteMethod.addRequestHeader("Accept", "application/xml");
    deleteMethod.setRequestHeader("Authorization", "Basic " + authForTest);
    deleteMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);
    int statusCode1 = 0;
    String res = "";
    try {
        statusCode1 = client.executeMethod(deleteMethod);
        pr.responseCode = statusCode1;
        //System.out.println("statusCode: "+statusCode1+" statusLine ==>" + deleteMethod.getStatusLine());
        pr.responseMessage = deleteMethod.getStatusText();
        res = deleteMethod.getResponseBodyAsString();
        deleteMethod.releaseConnection();
    } catch (Throwable t) {
        pr.error = t.toString();
    }
    pr.result = res;
    pr.responseCode = statusCode1;
    return pr;
}

From source file:org.dasein.persist.riak.RiakCache.java

@Override
public void remove(Transaction xaction, T item) throws PersistenceException {
    startCall("remove");
    try {/* w ww .  j a va 2s  .  c  o  m*/
        StringBuilder url = new StringBuilder();

        url.append(getEndpoint());
        url.append("buckets/");
        url.append(getBucket());
        url.append("/keys/");
        url.append(getKeyValue(item));

        HttpClient client = getClient();
        DeleteMethod delete = new DeleteMethod(url.toString());
        int code;

        try {
            if (wire.isDebugEnabled()) {
                try {
                    wire.debug(delete.getName() + " " + url.toString());
                    wire.debug("");
                    for (Header h : delete.getRequestHeaders()) {
                        wire.debug(h.getName() + ": " + h.getValue());
                    }
                    wire.debug("");
                } catch (Throwable ignore) {
                    // ignore
                }
            }
            code = client.executeMethod(delete);
        } catch (HttpException e) {
            throw new PersistenceException("HttpException during GET: " + e.getMessage());
        } catch (IOException e) {
            throw new PersistenceException("IOException during GET: " + e.getMessage());
        }
        try {
            String body = delete.getResponseBodyAsString();

            if (wire.isDebugEnabled()) {
                try {
                    wire.debug("----------------------------------------");
                    wire.debug("");
                    wire.debug(delete.getStatusLine().getStatusCode() + " "
                            + delete.getStatusLine().getReasonPhrase());
                    wire.debug("");
                    if (body != null) {
                        wire.debug(body);
                        wire.debug("");
                    }
                } catch (Throwable ignore) {
                    // ignore
                }
            }
            if (code != HttpStatus.SC_NO_CONTENT && code != HttpStatus.SC_NOT_FOUND) {
                throw new PersistenceException(code + ": " + body);
            }
            getCache().release(item);
        } catch (IOException e) {
            throw new PersistenceException(e);
        }
    } finally {
        endCall("remove");
    }
}