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

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

Introduction

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

Prototype

public void addParameter(String paramString1, String paramString2) throws IllegalArgumentException 

Source Link

Usage

From source file:org.sindice.servlet.sparqlqueryservlet.SparqlQueryServletTest.java

@Test
public void testASK() throws IllegalArgumentException, HttpException, IOException {
    final String query = "ASK WHERE { ?s <http://opengraphprotocol.org/schema/firstName> ?o . }";

    PostMethod post = new PostMethod(aseBaseUrl);
    post.addParameter(Protocol.QUERY_PARAM_NAME, URLEncoder.encode(query, "UTF-8"));

    final int code = client.executeMethod(post);
    if (code == HttpStatus.SC_OK) {
        final String json = post.getResponseBodyAsString();
        final ObjectMapper mapper = new ObjectMapper();
        final HashMap<String, Object> jsonMap = mapper.readValue(json, HashMap.class);
        String ref = "{\"boolean\":\"true\",\"head\":{\"link\":[]},\"status\":\"SUCCESS\",\"message\":\"\"}";

        assertEquals(ref, json.toString());
    } else {/* w w  w. jav a  2  s  .c om*/
        fail("code=" + code);
    }
}

From source file:org.sindice.servlet.sparqlqueryservlet.SparqlQueryServletTest.java

@Test
public void testASK2() throws IllegalArgumentException, HttpException, IOException {
    final String query = "ASK WHERE { ?s <http://purl.org/dc/terms/NOTHING> ?o . }";

    PostMethod post = new PostMethod(aseBaseUrl);
    post.addParameter(Protocol.QUERY_PARAM_NAME, URLEncoder.encode(query, "UTF-8"));

    final int code = client.executeMethod(post);
    if (code == HttpStatus.SC_OK) {
        final String json = post.getResponseBodyAsString();
        final ObjectMapper mapper = new ObjectMapper();
        final HashMap<String, Object> jsonMap = mapper.readValue(json, HashMap.class);
        String ref = "{\"boolean\":\"false\",\"head\":{\"link\":[]},\"status\":\"SUCCESS\",\"message\":\"\"}";

        assertEquals(ref, json.toString());
    } else {//from www .j  av a2 s .com
        fail("code=" + code);
    }
}

From source file:org.sindice.servlet.sparqlqueryservlet.SparqlQueryServletTest.java

@Test
public void testConstruct() throws IllegalArgumentException, HttpException, IOException {
    final String query = "CONSTRUCT { ?product ?p ?o }  WHERE { " + "?product ?p ?o ." + "} LIMIT 2";

    PostMethod post = new PostMethod(aseBaseUrl);
    post.addParameter(Protocol.QUERY_PARAM_NAME, URLEncoder.encode(query, "UTF-8"));

    final int code = client.executeMethod(post);
    if (code == HttpStatus.SC_OK) {
        final String json = post.getResponseBodyAsString();
        final ObjectMapper mapper = new ObjectMapper();
        final HashMap<String, Object> jsonMap = mapper.readValue(json, HashMap.class);
        String ref = "{\"results\":{\"distinct\":\"false\",\"ordered\":\"true\"," + "\"bindings\":["
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/a\"},"
                + "\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/firstName\"},"
                + "\"o\":{\"type\":\"uri\",\"value\":\"\\\"Richard\\\"\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/a\"},"
                + "\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/type\"},"
                + "\"o\":{\"type\":\"uri\",\"value\":\"\\\"Thing\\\"\"}}]}"
                + ",\"head\":{\"link\":[],\"vars\":[\"s\",\"p\",\"o\"]},"
                + "\"status\":\"SUCCESS\",\"message\":\"\"}";

        assertEquals(ref, json.toString());
    } else {/*ww w.  j  av a 2 s  .  c  om*/
        fail("code=" + code);
    }
}

From source file:org.sindice.servlet.sparqlqueryservlet.SparqlQueryServletTest.java

@Test
public void testDescribe() throws IllegalArgumentException, HttpException, IOException {
    final String query = "DESCRIBE ?s WHERE { " + "?s <http://opengraphprotocol.org/schema/firstName> ?o "
            + "} LIMIT 1";

    PostMethod post = new PostMethod(aseBaseUrl);
    post.addParameter(Protocol.QUERY_PARAM_NAME, URLEncoder.encode(query, "UTF-8"));

    final int code = client.executeMethod(post);
    if (code == HttpStatus.SC_OK) {
        final String json = post.getResponseBodyAsString();
        final ObjectMapper mapper = new ObjectMapper();
        final HashMap<String, Object> jsonMap = mapper.readValue(json, HashMap.class);
        String ref = "{\"results\":{\"distinct\":\"false\",\"ordered\":\"true\"," + "\"bindings\":["
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/a\"},"
                + "\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/firstName\"},"
                + "\"o\":{\"type\":\"uri\",\"value\":\"\\\"Richard\\\"\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/a\"},"
                + "\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/type\"},"
                + "\"o\":{\"type\":\"uri\",\"value\":\"\\\"Thing\\\"\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/a\"},"
                + "\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/like\"},"
                + "\"o\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/c\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/e\"},"
                + "\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/type\"},"
                + "\"o\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/a\"}}]},"
                + "\"head\":{\"link\":[],\"vars\":[\"s\",\"p\",\"o\"]},"
                + "\"status\":\"SUCCESS\",\"message\":\"\"}";
        assertEquals(ref, json.toString());
    } else {//from   w  w w.  ja  v  a  2s.co  m
        fail("code=" + code);
    }
}

From source file:org.sofun.platform.legigame.LegigameServiceImpl.java

/**
 * Prepares a POST request to create or update a given a {@link Member}
 * //from w w  w  .  j  a v  a2  s. c o m
 * @param member: a {@link Member} instance
 * @param method: a {@link PostMethod} instance
 * @return a {@link PostMethod} instance with all required parameters.
 */
private PostMethod getPostForCreateOrUpdate(Member member, PostMethod method) {

    method.addParameter("Login", Configuration.getProperties().getProperty("api.login"));
    method.addParameter("Password", Configuration.getProperties().getProperty("api.password"));

    method.addParameter("PlayerID", String.valueOf(member.getId()));
    method.addParameter("Firstname", member.getFirstName());
    method.addParameter("Lastname", member.getLastName());

    final SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
    method.addParameter("BirthDate", sf.format(member.getBirthDate()));
    method.addParameter("Email", member.getEmail());

    // IBAN will be used in priority
    if (member.getIBAN() != null && member.getIBAN().getIBAN() != null && member.getIBAN().getSwift() != null
            && member.getIBAN().getIBAN().length() > 0 && member.getIBAN().getSwift().length() > 0) {
        method.addParameter("IBAN", member.getIBAN().getIBAN());
        method.addParameter("BIC", member.getIBAN().getSwift());
    } else if (member.getRIB() != null && member.getRIB().getBank() != null
            && member.getRIB().getBranch() != null && member.getRIB().getKey() != null
            && member.getRIB().getNumber() != null && member.getRIB().getBank().length() > 0
            && member.getRIB().getBranch().length() > 0 && member.getRIB().getKey().length() > 0
            && member.getRIB().getNumber().length() > 0) {
        // Legigame does not support RIB.
        // We use must convert it to IBAN.
        final String iban = member.getRIB().toIban();
        method.addParameter("IBAN", iban);
        method.addParameter("BIC", "");
    } else {
        if (member.getAccountStatus().equals(MemberAccountStatus.PENDING_VERIFIED_FR)) {
            log.error("No IBAN not RIB are available. " + "Account is pending activation though...");
        }
    }

    if (member.getBirthPlace() != null) {
        method.addParameter("CityOfBirth", member.getBirthPlace());
    }
    if (member.getBirthArea() != null) {
        method.addParameter("DepartmentOfBirth", member.getBirthArea());
    }
    if (member.getBirthCountry() != null) {
        method.addParameter("CountryOfBirth", member.getBirthCountry());
    }
    if (member.getMobilePhone() != null) {
        method.addParameter("Mobile", member.getMobilePhone());
    }
    if (member.getActivationKey() != null) {
        method.addParameter("ActivationCode", member.getActivationKey());
    }
    if (member.getPostalAddress().getStreet() != null) {
        method.addParameter("Address", member.getPostalAddress().getStreet());
    }
    if (member.getPostalAddress().getCity() != null) {
        method.addParameter("City", member.getPostalAddress().getCity());
    }
    if (member.getPostalAddress().getZipCode() != null) {
        method.addParameter("ZipCode", member.getPostalAddress().getZipCode());
    }
    if (member.getPostalAddress().getCountry() != null) {
        method.addParameter("Country", member.getPostalAddress().getCountry());
    }

    // Sofun specifics
    if (member.getAccountType().equals(MemberAccountType.GAMBLING_FR)) {
        method.addParameter("Attribut1", String.valueOf("1"));
    } else {
        method.addParameter("Attribut1", String.valueOf("0"));
    }

    if (member.getAccountStatus().equals(MemberAccountStatus.CLOSED)
            || member.getAccountStatus().equals(MemberAccountStatus.MEMBER_SUSPENDED)
            || member.getAccountStatus().equals(MemberAccountStatus.ADMIN_SUSPENDED)
            || member.getAccountStatus().equals(MemberAccountStatus.BANNED_FR)) {
        method.addParameter("ClosingDate", sf.format(member.getModified()));
    } else {
        method.addParameter("ClosingDate", "");
    }

    return method;

}

From source file:org.sofun.platform.legigame.LegigameServiceImpl.java

/**
 * Prepares a POST request to read the status of a given {@link Member}
 * /*from   ww w  .j  a va 2 s .c  o  m*/
 * @param member: a {@link Member} instance
 * @param method: a {@link PostMethod} instance
 * @return a {@link PostMethod} instance with all required parameters.
 */

private PostMethod getPostForRead(Member member, PostMethod method) {

    method.addParameter("Login", Configuration.getProperties().getProperty("api.login"));
    method.addParameter("Password", Configuration.getProperties().getProperty("api.password"));

    method.addParameter("PlayerID", String.valueOf(member.getId()));

    return method;

}

From source file:org.sonar.plugins.buildstability.ci.hudson.HudsonUtils.java

public static void doLogin(HttpClient client, String hostName, String username, String password)
        throws IOException {
    String hudsonLoginEntryUrl = hostName + "loginEntry";
    GetMethod loginLink = new GetMethod(hudsonLoginEntryUrl);
    client.executeMethod(loginLink);/*w w w  .  j a  v  a2 s .co  m*/
    checkResult(loginLink.getStatusCode(), hudsonLoginEntryUrl);

    String location = hostName + "j_security_check";
    while (true) {
        PostMethod loginMethod = new PostMethod(location);
        loginMethod.addParameter("j_username", username);
        loginMethod.addParameter("j_password", password);
        loginMethod.addParameter("action", "login");
        client.executeMethod(loginMethod);
        if (loginMethod.getStatusCode() / 100 == 3) {
            // Commons HTTP client refuses to handle redirects for POST
            // so we have to do it manually.
            location = loginMethod.getResponseHeader("Location").getValue();
            continue;
        }
        checkResult(loginMethod.getStatusCode(), location);
        break;
    }
}

From source file:org.tinygroup.httpvisit.impl.HttpVisitorImpl.java

public String postUrl(String url, Map<String, ?> parameter) {
    PostMethod post = new PostMethod(url);
    if (parameter != null) {
        for (String key : parameter.keySet()) {
            Object value = parameter.get(key);
            if (value.getClass().isArray()) {
                Object[] arrayValue = (Object[]) value;
                for (Object o : arrayValue) {
                    post.addParameter(key, o.toString());
                }/*from w w w.  ja  v a  2  s .c o  m*/
            } else {
                post.setParameter(key, value.toString());
            }
        }
    }
    addHeader(post, headerMap);
    return execute(post);
}

From source file:org.wise.portal.domain.admin.DailyAdminJob.java

/**
 * POSTs WISE usage statistics to central hub
 *//* w  ww . j  a  va  2  s .c om*/
public void postStatistics(String wiseStatisticsString) {

    if (WISE_HUB_URL != null) {
        HttpClient client = new HttpClient();

        // Create a method instance.
        PostMethod method = new PostMethod(WISE_HUB_URL);

        // Provide custom retry handler is necessary
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));

        method.addParameter("name", wiseProperties.getProperty("wise.name"));
        method.addParameter("wiseBaseURL", wiseProperties.getProperty("wiseBaseURL"));
        method.addParameter("stats", wiseStatisticsString);

        try {

            // Execute the method.
            int statusCode = client.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + method.getStatusLine());
            }
            // Read the response body.
            //byte[] responseBody = null;
            //responseBody = method.getResponseBody();
            //String responseString = new String(responseBody);
            //System.out.println(responseString);

            // Deal with the response.
            // Use caution: ensure correct character encoding and is not binary data
        } catch (HttpException e) {
            System.err.println("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("Fatal transport error: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // Release the connection.
            method.releaseConnection();
        }
    }
}

From source file:org.wso2.carbon.esb.proxyservice.test.proxyservices.UrlEncordedFormPostViaProxyTestCase.java

@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
@Test(groups = {/*  w ww.  java2  s .  c o  m*/
        "wso2.esb" }, description = "Patch : ESBJAVA-1696 : Encoded Special characters in the URL is decoded at the Gateway and not re-encoded", enabled = true)
public void testEncodingSpecialCharacterViaHttpProxy() throws IOException {
    HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(0, false));
    client.getParams().setSoTimeout(5000);
    client.getParams().setConnectionManagerTimeout(5000);

    // Create POST method
    String url = getProxyServiceURLHttp("MyProxy");
    PostMethod method = new PostMethod(url);
    // Set parameters on POST
    String value1 = "Hello World";
    String value2 = "This is a Form Submission containing %";
    String value3 = URLEncoder.encode("This is an encoded value containing %");
    method.setParameter("test1", value1);
    method.addParameter("test2", value2);
    method.addParameter("test3", value3);

    // Execute and print response
    try {
        client.executeMethod(method);
    } catch (Exception e) {

    } finally {
        method.releaseConnection();
    }
    String response = wireMonitorServer.getCapturedMessage();
    String[] responseArray = response.split("test");

    if (responseArray.length < 3) {
        Assert.fail("All attributes are not sent");
    }
    for (String res : responseArray) {
        if (res.startsWith("1")) {
            Assert.assertTrue(res.startsWith("1=" + URLEncoder.encode(value1).replace("+", "%20")));
        } else if (res.startsWith("2")) {
            Assert.assertTrue(res.startsWith("2=" + URLEncoder.encode(value2).replace("+", "%20")));
        } else if (res.startsWith("3")) {
            Assert.assertTrue(res.startsWith("3=" + URLEncoder.encode(value3)));
        }
    }

}