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

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

Introduction

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

Prototype

public PostMethod(String paramString) 

Source Link

Usage

From source file:net.bioclipse.opentox.api.MolecularDescriptorAlgorithm.java

public static String calculate(String service, String descriptor, String dataSetURI, IProgressMonitor monitor)
        throws HttpException, IOException, InterruptedException, GeneralSecurityException {
    if (monitor == null)
        monitor = new NullProgressMonitor();

    HttpClient client = new HttpClient();
    dataSetURI = Dataset.normalizeURI(dataSetURI);
    PostMethod method = new PostMethod(descriptor);

    HttpMethodHelper.addMethodHeaders(method, new HashMap<String, String>() {
        {//from   w w w .j a  v  a2  s.  co  m
            put("Accept", "text/uri-list");
        }
    });
    method.setParameter("dataset_uri", dataSetURI);
    method.setParameter("dataset_service", service + "dataset");
    logger.debug("Calculating: " + descriptor);
    logger.debug("  with data set: " + dataSetURI);
    client.executeMethod(method);
    int status = method.getStatusCode();
    logger.debug("  -> return status: " + status);
    String dataset = "";
    // FIXME: I should really start using the RDF response...
    String responseString = method.getResponseBodyAsString();
    int tailing = 1;
    if (status == 200 || status == 202) {
        if (responseString.contains("/task/")) {
            // OK, we got a task... let's wait until it is done
            String task = responseString;
            logger.debug("OK, we got a task assigned: " + task);
            Thread.sleep(andABit(500)); // let's be friendly, and wait 1 sec
            TaskState state = Task.getState(task);
            while (!state.isFinished() && !monitor.isCanceled()) {
                // let's be friendly, and wait 2 secs and a bit and increase
                // that time after each wait
                int waitingTime = andABit(2000 * tailing);
                logger.debug("Waiting " + waitingTime + "ms.");
                waitUnlessInterrupted(waitingTime, monitor);
                state = Task.getState(task);
                if (state.isRedirected()) {
                    task = state.getResults();
                    logger.debug("  new task, new task!!: " + task);
                }
                // but wait at most 20 secs and a bit
                if (tailing < 10)
                    tailing++;
            }
            if (monitor.isCanceled())
                Task.delete(task);
            // OK, it should be finished now
            dataset = state.getResults();
        } else {
            // OK, that was quick!
            dataset = responseString;
        }
    } else if (status == 401) {
        throw new GeneralSecurityException("Not authenticated");
    } else if (status == 403) {
        throw new GeneralSecurityException("Not authorized");
    } else {
        throw new IllegalStateException("Service error: " + status);
    }
    method.releaseConnection();
    dataset = dataset.replaceAll("\n", "");
    return dataset;
}

From source file:es.carebear.rightmanagement.client.group.RemoveUserFromGroup.java

public void RemoveUser(String groupName, String targetName, String authName) throws IOException,
        UnauthorizedException, BadRequestException, UnknownResponseException, InternalServerErrorException {
    PostMethod postMethod = new PostMethod(
            baseUri + "/client/groups/remove/user/" + groupName + "/" + targetName);
    postMethod.addParameter("authName", authName);
    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_OK:
        break;//from w  w w  . j ava 2  s  .c  om
    case HttpStatus.SC_FORBIDDEN:
        throw new UnauthorizedException();
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        throw new InternalServerErrorException();
    default:
        throw new UnknownResponseException((new Integer(responseCode)).toString());
    }
}

From source file:net.jadler.JadlerTimeoutIntegrationTest.java

@Test(timeout = 10000L)
public void timeout() throws IOException {
    onRequest().respond().withStatus(201);

    final PostMethod method = new PostMethod("http://localhost:" + port());
    method.setRequestEntity(new StringRequestEntity("postbody", null, null));

    int status = client.executeMethod(method);
    assertThat(status, is(201));/*from ww w .  j a  va2 s.  co  m*/
}

From source file:es.carebear.rightmanagement.client.group.GrantGroupRight.java

public void GrantRight(String groupName, String targetName, String authName, String rightMask)
        throws IOException, UnauthorizedException, BadRequestException, UnknownResponseException,
        InternalServerErrorException {//from ww  w . ja v a  2 s . c  o  m
    PostMethod postMethod = new PostMethod(baseUri + "/client/groups/grant/" + groupName + "/" + targetName);
    postMethod.addParameter("authName", authName);
    postMethod.addParameter("rightMask", rightMask);
    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_OK:
        break;
    case HttpStatus.SC_FORBIDDEN:
        throw new UnauthorizedException();
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        throw new InternalServerErrorException();
    default:
        throw new UnknownResponseException((new Integer(responseCode)).toString());
    }
}

From source file:es.carebear.rightmanagement.client.group.RemoveGroupRight.java

public void RemoveRight(String targetGroup, String removedGroup, String authName) throws IOException,
        UnauthorizedException, BadRequestException, UnknownResponseException, InternalServerErrorException {
    PostMethod postMethod = new PostMethod(
            baseUri + "/client/groups/remove/group/" + targetGroup + "/" + removedGroup);
    postMethod.addParameter("authName", authName);
    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_OK:
        break;// w w  w. jav  a  2  s  . c o  m
    case HttpStatus.SC_FORBIDDEN:
        throw new UnauthorizedException();
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        throw new InternalServerErrorException();
    default:
        throw new UnknownResponseException((new Integer(responseCode)).toString());
    }
}

From source file:es.carebear.rightmanagement.client.admin.DeleteGroup.java

public void deleteGroup(String groupName, String authName) throws IOException, IllegalArgumentException,
        InternalServerErrorException, BadRequestException, UnauthorizedException, UnknownResponseException {
    PostMethod postMethod = new PostMethod(baseUri + "/client/groups/delete/" + groupName);
    postMethod.addParameter("authName", authName);
    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_OK:
        break;/*  w ww .j  av  a  2 s . c  o  m*/
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        throw new InternalServerErrorException();
    case HttpStatus.SC_UNAUTHORIZED:
        throw new UnauthorizedException();
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    default:
        throw new UnknownResponseException((new Integer(responseCode)).toString());

    }

}

From source file:es.carebear.rightmanagement.client.user.RegisterUser.java

public User register(String username, String password) throws BadRequestException, InternalServerErrorException,
        IOException, UnauthorizedException, UnknownResponseException {
    PostMethod postMethod = new PostMethod(baseUri + "/client/users/register/" + username);
    postMethod.addParameter("password", password);
    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_CREATED:
        String response = postMethod.getResponseBodyAsString();
        return XMLHelper.fromXML(response, User.class);
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        throw new InternalServerErrorException();
    default://from   w  ww. j  a  va2s.c o  m
        throw new UnknownResponseException((new Integer(responseCode)).toString());
    }
}

From source file:com.tops.hotelmanager.util.CommonHttpClient.java

public static String executePostRequest(String url, Map<String, String> map, Map<String, String> header,
        int timeOut) {
    HttpClient client = new HttpClient();

    // HostConfiguration configuration = new HostConfiguration();
    // configuration.setProxy("localhost", 8888);
    // client.setHostConfiguration(configuration);

    client.getHttpConnectionManager().getParams().setSoTimeout(timeOut);
    client.getHttpConnectionManager().getParams().setConnectionTimeout(timeOut);
    PostMethod post = new PostMethod(url);
    try {//from  w w  w.  ja  v a  2  s.c om
        if (map != null && map.size() > 0) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                post.addParameter(entry.getKey(), entry.getValue());
            }
        }
        if (header != null && header.size() > 0) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                post.addRequestHeader(entry.getKey(), entry.getValue());
            }
        }
        int i = client.executeMethod(post);
        if (i != -1) {
            return post.getResponseBodyAsString();
        }
    } catch (Exception e) {
        logger.error("HttpClient post method error url: " + url + ", Parameters: " + map, e);
    } finally {
        if (post != null) {
            post.releaseConnection();
        }
    }

    return "error~Request Failed";

}

From source file:es.carebear.rightmanagement.client.admin.CreateGroup.java

public Group createGroup(String authName, String groupName) throws IOException, IllegalArgumentException,
        InternalServerErrorException, BadRequestException, UnauthorizedException, UnknownResponseException {
    PostMethod postMethod = new PostMethod(baseUri + "/client/groups/create/" + groupName);
    postMethod.addParameter("creatorName", authName);

    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_CREATED:
        String response = postMethod.getResponseBodyAsString();
        return XMLHelper.fromXML(response, Group.class);
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        throw new InternalServerErrorException();
    case HttpStatus.SC_UNAUTHORIZED:
        throw new UnauthorizedException();
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    default:/*  w w w.  j  a va2  s . c  o  m*/
        throw new UnknownResponseException((new Integer(responseCode)).toString());

    }

}

From source file:es.carebear.rightmanagement.client.user.AddAllowedUserId.java

public void AddUser(String authName, String target, String rightMask) throws BadRequestException,
        InternalServerErrorException, IOException, UnknownResponseException, UnauthorizedException {
    PostMethod postMethod = new PostMethod(baseUri + "/client/users/change/allowed");
    postMethod.addParameter("authName", authName);
    postMethod.addParameter("target", target);
    postMethod.addParameter("rightMask", rightMask);
    int responseCode = httpClient.executeMethod(postMethod);

    switch (responseCode) {
    case HttpStatus.SC_OK:
        break;/*from w ww. j  a  v a2  s  .  c o  m*/
    case HttpStatus.SC_BAD_REQUEST:
        throw new BadRequestException();
    case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        throw new InternalServerErrorException();
    case HttpStatus.SC_UNAUTHORIZED:
        throw new UnauthorizedException();
    case HttpStatus.SC_FORBIDDEN:
        throw new ForbiddenException();
    default:
        throw new UnknownResponseException((new Integer(responseCode)).toString());
    }
}