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

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

Introduction

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

Prototype

public DeleteMethod(String paramString) 

Source Link

Usage

From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java

/**
 * Initializes the http client with correct httpmethod. Adds Authorization header to request.
 * @param client/*from www  .  j a  v a 2  s .  co  m*/
 * @param uri
 * @param methodType
 * @return
 */
protected HttpMethod getInitializedHttpMethod(HttpClient client, String uri, HttpMethodType methodType) {
    Calendar now = GregorianCalendar.getInstance();
    _apiSession.setLastRequestDateTimeUtc(new Date(now.getTimeInMillis()));
    _apiSession.setHash(CryptographyHelper.computeHash(_apiSession, _sharedSecret));

    String authHeader = AuthorizationHelper.toAuthorizationHeader(_apiSession);

    HttpMethod method;
    switch (methodType) {
    case GET:
        method = new GetMethod(uri);
        break;
    case PUT:
        method = new PutMethod(uri);
        method.setRequestHeader("Content-Type", "text/xml");
        break;
    case POST:
        method = new PostMethod(uri);
        method.setRequestHeader("Content-Type", "text/xml");
        break;
    case DELETE:
        method = new DeleteMethod(uri);
        break;
    default:
        method = new GetMethod(uri);
    }
    HttpParams params = DefaultHttpParams.getDefaultParams();
    method.setRequestHeader("Authorization", authHeader);
    client.setParams((HttpClientParams) params);

    return method;
}

From source file:nl.nn.adapterframework.http.HttpSender.java

protected HttpMethod getMethod(URI uri, String message, ParameterValueList parameters,
        Map<String, String> headersParamsMap) throws SenderException {
    try {/*from  ww w.j a v  a 2 s .  c o  m*/
        boolean queryParametersAppended = false;
        if (isEncodeMessages()) {
            message = URLEncoder.encode(message);
        }

        StringBuffer path = new StringBuffer(uri.getPath());
        if (!StringUtils.isEmpty(uri.getQuery())) {
            path.append("?" + uri.getQuery());
            queryParametersAppended = true;
        }

        if (getMethodType().equals("GET")) {
            if (parameters != null) {
                queryParametersAppended = appendParameters(queryParametersAppended, path, parameters,
                        headersParamsMap);
                if (log.isDebugEnabled())
                    log.debug(getLogPrefix() + "path after appending of parameters [" + path.toString() + "]");
            }
            GetMethod result = new GetMethod(path + (parameters == null ? message : ""));
            for (String param : headersParamsMap.keySet()) {
                result.addRequestHeader(param, headersParamsMap.get(param));
            }
            if (log.isDebugEnabled())
                log.debug(
                        getLogPrefix() + "HttpSender constructed GET-method [" + result.getQueryString() + "]");
            return result;
        } else if (getMethodType().equals("POST")) {
            PostMethod postMethod = new PostMethod(path.toString());
            if (StringUtils.isNotEmpty(getContentType())) {
                postMethod.setRequestHeader("Content-Type", getContentType());
            }
            if (parameters != null) {
                StringBuffer msg = new StringBuffer(message);
                appendParameters(true, msg, parameters, headersParamsMap);
                if (StringUtils.isEmpty(message) && msg.length() > 1) {
                    message = msg.substring(1);
                } else {
                    message = msg.toString();
                }
            }
            for (String param : headersParamsMap.keySet()) {
                postMethod.addRequestHeader(param, headersParamsMap.get(param));
            }
            postMethod.setRequestBody(message);

            return postMethod;
        }
        if (getMethodType().equals("PUT")) {
            PutMethod putMethod = new PutMethod(path.toString());
            if (StringUtils.isNotEmpty(getContentType())) {
                putMethod.setRequestHeader("Content-Type", getContentType());
            }
            if (parameters != null) {
                StringBuffer msg = new StringBuffer(message);
                appendParameters(true, msg, parameters, headersParamsMap);
                if (StringUtils.isEmpty(message) && msg.length() > 1) {
                    message = msg.substring(1);
                } else {
                    message = msg.toString();
                }
            }
            putMethod.setRequestBody(message);
            return putMethod;
        }
        if (getMethodType().equals("DELETE")) {
            DeleteMethod deleteMethod = new DeleteMethod(path.toString());
            if (StringUtils.isNotEmpty(getContentType())) {
                deleteMethod.setRequestHeader("Content-Type", getContentType());
            }
            return deleteMethod;
        }
        if (getMethodType().equals("HEAD")) {
            HeadMethod headMethod = new HeadMethod(path.toString());
            if (StringUtils.isNotEmpty(getContentType())) {
                headMethod.setRequestHeader("Content-Type", getContentType());
            }
            return headMethod;
        }
        if (getMethodType().equals("REPORT")) {
            Element element = XmlUtils.buildElement(message, true);
            ReportInfo reportInfo = new ReportInfo(element, 0);
            ReportMethod reportMethod = new ReportMethod(path.toString(), reportInfo);
            if (StringUtils.isNotEmpty(getContentType())) {
                reportMethod.setRequestHeader("Content-Type", getContentType());
            }
            return reportMethod;
        }
        throw new SenderException(
                "unknown methodtype [" + getMethodType() + "], must be either POST, GET, PUT or DELETE");
    } catch (URIException e) {
        throw new SenderException(getLogPrefix() + "cannot find path from url [" + getUrl() + "]", e);
    } catch (DavException e) {
        throw new SenderException(e);
    } catch (DomBuilderException e) {
        throw new SenderException(e);
    } catch (IOException e) {
        throw new SenderException(e);
    }
}

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 {//from  ww w . j  av a2 s. c om
        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.custom.rest.httpconnector.RemoteClientImpl.java

private org.apache.commons.httpclient.HttpMethod createRequestMethod(HttpMethod requestMethod,
        URI redirectURL) {/*from   ww  w .  j  a v a 2s .  c  om*/
    org.apache.commons.httpclient.HttpMethod method = null;

    switch (requestMethod) {
    default:
    case GET:
        method = new GetMethod(redirectURL.toString());
        break;
    case PUT:
        method = new PutMethod(redirectURL.toString());
        break;
    case POST:
        method = new PostMethod(redirectURL.toString());
        break;
    case DELETE:
        method = new DeleteMethod(redirectURL.toString());
        break;
    case HEAD:
        method = new HeadMethod(redirectURL.toString());
        break;
    }

    // Switch off automatic redirect handling as we want to process them
    // ourselves and maintain cookies
    method.setFollowRedirects(false);

    return method;
}

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 av a2 s. com
 * @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.alfresco.module.vti.handler.alfresco.ShareUtils.java

/**
 * Deletes component from site//from   www  . j a  va 2  s  .  c o m
 * 
 * @param httpClient HTTP client
 * @param siteName name of the site
 * @param user current user
 * @param componentName name of the component
 */
private void deleteSiteComponent(HttpClient httpClient, String siteName, SessionUser user,
        String componentName) {
    DeleteMethod deleteTitleMethod = new DeleteMethod(getAlfrescoHostWithPort() + getAlfrescoContext()
            + "/s/remoteadm/delete/alfresco/site-data/components/page." + componentName + ".site~" + siteName
            + "~dashboard.xml?s=sitestore&alf_ticket=" + user.getTicket());
    try {
        if (logger.isDebugEnabled())
            logger.debug("Trying to delete site component with name: " + siteName);

        int status = httpClient.executeMethod(deleteTitleMethod);
        deleteTitleMethod.getResponseBody();

        if (logger.isDebugEnabled())
            logger.debug("Delete site component method returned status: " + status);
    } catch (Exception e) {
        if (logger.isDebugEnabled())
            logger.debug("Fail to delete component from site with name: " + siteName);
        throw new RuntimeException(e);
    } finally {
        deleteTitleMethod.releaseConnection();
    }
}

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

/**
 * Deletes site dashboard/*from ww w.  j a  v  a 2 s .  c om*/
 * 
 * @param httpClient HTTP client
 * @param siteName name of the site
 * @param user current user
 */
private void deleteSiteDashboard(HttpClient httpClient, String siteName, SessionUser user) {
    DeleteMethod deleteDashboardMethod = new DeleteMethod(getAlfrescoHostWithPort() + getAlfrescoContext()
            + "/s/remoteadm/delete/alfresco/site-data/pages/site/" + siteName
            + "/dashboard.xml?s=sitestore&alf_ticket=" + user.getTicket());
    try {
        if (logger.isDebugEnabled())
            logger.debug("Trying to delete dashboard from site with name: " + siteName);

        int status = httpClient.executeMethod(deleteDashboardMethod);
        deleteDashboardMethod.getResponseBody();

        if (logger.isDebugEnabled())
            logger.debug("Delete dashboard from site method returned status: " + status);
    } catch (Exception e) {
        if (logger.isDebugEnabled())
            logger.debug("Fail to delete dashboard from site with name: " + siteName);
        throw new RuntimeException(e);
    } finally {
        deleteDashboardMethod.releaseConnection();
    }
}

From source file:org.alfresco.repo.remoteconnector.RemoteConnectorRequestImpl.java

protected static HttpMethodBase buildHttpClientMethod(String url, String method) {
    if ("GET".equals(method)) {
        return new GetMethod(url);
    }//w w  w .  java2s .co m
    if ("POST".equals(method)) {
        return new PostMethod(url);
    }
    if ("PUT".equals(method)) {
        return new PutMethod(url);
    }
    if ("DELETE".equals(method)) {
        return new DeleteMethod(url);
    }
    if (TestingMethod.METHOD_NAME.equals(method)) {
        return new TestingMethod(url);
    }
    throw new UnsupportedOperationException("Method '" + method + "' not supported");
}

From source file:org.alfresco.repo.web.scripts.BaseWebScriptTest.java

/**
 * Send Remote Request to stand-alone Web Script Server
 * /* w  ww.ja  v a2 s  .com*/
 * @param req Request
 * @param expectedStatus int
 * @return response
 * @throws IOException
 */
protected Response sendRemoteRequest(Request req, int expectedStatus) throws IOException {
    String uri = req.getFullUri();
    if (!uri.startsWith("http")) {
        uri = remoteServer.baseAddress + uri;
    }

    // construct method
    HttpMethod httpMethod = null;
    String method = req.getMethod();
    if (method.equalsIgnoreCase("GET")) {
        GetMethod get = new GetMethod(req.getFullUri());
        httpMethod = get;
    } else if (method.equalsIgnoreCase("POST")) {
        PostMethod post = new PostMethod(req.getFullUri());
        post.setRequestEntity(new ByteArrayRequestEntity(req.getBody(), req.getType()));
        httpMethod = post;
    } else if (method.equalsIgnoreCase("PATCH")) {
        PatchMethod post = new PatchMethod(req.getFullUri());
        post.setRequestEntity(new ByteArrayRequestEntity(req.getBody(), req.getType()));
        httpMethod = post;
    } else if (method.equalsIgnoreCase("PUT")) {
        PutMethod put = new PutMethod(req.getFullUri());
        put.setRequestEntity(new ByteArrayRequestEntity(req.getBody(), req.getType()));
        httpMethod = put;
    } else if (method.equalsIgnoreCase("DELETE")) {
        DeleteMethod del = new DeleteMethod(req.getFullUri());
        httpMethod = del;
    } else {
        throw new AlfrescoRuntimeException("Http Method " + method + " not supported");
    }
    if (req.getHeaders() != null) {
        for (Map.Entry<String, String> header : req.getHeaders().entrySet()) {
            httpMethod.setRequestHeader(header.getKey(), header.getValue());
        }
    }

    // execute method
    httpClient.executeMethod(httpMethod);
    return new HttpMethodResponse(httpMethod);
}

From source file:org.alfresco.rest.api.tests.client.PublicApiHttpClient.java

public HttpResponse delete(final RequestContext rq, Binding cmisBinding, String version, String cmisOperation)
        throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), cmisBinding, version, cmisOperation,
            null);/*from  w ww  .  j a v  a 2 s  . co  m*/
    String url = endpoint.getUrl();

    DeleteMethod req = new DeleteMethod(url.toString());
    return submitRequest(req, rq);
}