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 testQuery() throws IllegalArgumentException, HttpException, IOException {
    final String query = "SELECT ?p WHERE { ?s ?p ?o . }LIMIT 4";

    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\":[{\"p\":{\"type\""
                + ":\"uri\",\"value\":\"http://opengraphprotocol.org/sch"
                + "ema/firstName\"}},{\"p\":{\"type\":\"uri\",\"value\":"
                + "\"http://opengraphprotocol.org/schema/type\"}},{\"p\":"
                + "{\"type\":\"uri\",\"value\":\"http://opengraphprotocol."
                + "org/schema/like\"}},{\"p\":{\"type\":\"uri\",\"value\":"
                + "\"http://opengraphprotocol.org/schema/firstName\"}}"
                + "]},\"head\":{\"link\":[],\"vars\":\"p\"}," + "\"status\":\"SUCCESS\",\"message\":\"\"}";

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

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

@Test
public void testQueryOrdered() throws IllegalArgumentException, HttpException, IOException {
    final String query = "SELECT ?s ?p WHERE { ?s ?p ?o . }ORDER BY ?s ?p";

    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\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/a\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/like\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/a\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/type\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/b\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/firstName\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/b\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/firstName\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/b\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/think_at\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/b\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/type\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/c\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/firstName\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/c\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/test\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/c\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/type\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/d\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/fail\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/e\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/type\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/f\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/do\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/f\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/type\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/g\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/test\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/g\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/type\"}},"
                + "{\"s\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/g\"},\"p\":{\"type\":\"uri\",\"value\":\"http://opengraphprotocol.org/schema/type\"}}]},"
                + "\"head\":{\"link\":[],\"vars\":[\"s\",\"p\"]}," + "\"status\":\"SUCCESS\",\"message\":\"\"}";
        assertEquals(ref, json.toString());
    } else {//from w  w w.j  a  v a2s .  co  m
        fail("code=" + code);
    }
}

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

@Test
public void testQueryPagination() throws IllegalArgumentException, HttpException, IOException {
    final String query = "SELECT ?p WHERE { ?s ?p ?o . } LIMIT 2 OFFSET 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\":[{\"p\":"
                + "{\"type\":\"uri\",\"value\":\"http://opengraphprotocol."
                + "org/schema/like\"}},{\"p\":{\"type\":\"uri\",\"value\":"
                + "\"http://opengraphprotocol.org/schema/firstName\"}}"
                + "]},\"head\":{\"link\":[],\"vars\":\"p\"}," + "\"status\":\"SUCCESS\",\"message\":\"\"}";

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

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

@Test
public void testQuerySelectStar() throws IllegalArgumentException, HttpException, IOException {
    final String query = "SELECT * WHERE { ?s ?p ?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\\\"\"}}},"
                + "\"head\":{\"link\":[],\"vars\":[\"s\",\"p\",\"o\"]},"
                + "\"status\":\"SUCCESS\",\"message\":\"\"}";

        assertEquals(ref, json.toString());
    } else {/*from   w  w w . j  a  v  a2s.c  o  m*/
        fail("code=" + code);
    }
}

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

@Test
public void testQueryFrom() throws IllegalArgumentException, HttpException, IOException {
    final String query = "SELECT ?p FROM <http://rottentomatoes.com> WHERE { ?s ?p ?o . } LIMIT 4";

    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\":[]},\"head\":{\"link\":[],\"vars\":\"p\"},"
                + "\"status\":\"SUCCESS\",\"message\":\"\"}";

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

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

@Test
public void testQueryFromAndPagination() throws IllegalArgumentException, HttpException, IOException {
    final String query = "SELECT ?s ?p FROM <http://rottentomatoes.com> WHERE { ?s ?p ?o . } LIMIT 2 OFFSET 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\":[]},\"head\":{\"link\":[],\"vars\":[\"s\",\"p\"]},"
                + "\"status\":\"SUCCESS\",\"message\":\"\"}";

        assertEquals(ref, json.toString());
    } else {//from  w ww .  j a v a 2s.com
        fail("code=" + code);
    }
}

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

@Test
public void testQueryEmptySelectStar() throws IllegalArgumentException, HttpException, IOException {
    final String query = "SELECT * WHERE { ?s <http://purl.org/dc/terms/title> ?o }LIMIT 10";

    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\":[]},\"head\":{\"link\":[],\"vars\":[\"s\",\"o\"]},"
                + "\"status\":\"SUCCESS\",\"message\":\"\"}";

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

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

@Test
public void testQueryInvalidSelect() throws IllegalArgumentException, HttpException, IOException {
    final String query = "SELECT ?p WHERE { ?s <http://purl.org/dc/terms/title> ?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 = "{\"results\":{\"distinct\":\"false\","
                + "\"ordered\":\"true\",\"bindings\":[]},\"head\":{\"link\":[],\"vars\":\"p\"},"
                + "\"status\":\"SUCCESS\",\"message\":\"\"}";

        assertEquals(ref, json.toString());
    } else {/*from   w w w.j a v  a 2s  . c  o m*/
        fail("code=" + code);
    }
}

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

@Test
public void testQueryInvalid() throws IllegalArgumentException, HttpException, IOException {
    final String query = "SELECT ?p WHERE { ?s ?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 = "{\"status\":\"ERROR\","
                + "\"message\":\"org.openrdf.query.parser.sparql.ast.ParseException: "
                + "Encountered \\\" \\\"}\\\" \\\"} \\\"\\\" "
                + "at line 1, column 25.\\nWas expecting one of:\\n    \\\"("
                + "\\\" ...\\n    \\\"[\\\" ...\\n    <NIL> ...\\n    <ANON> ..."
                + "\\n    \\\"true\\\" ...\\n    \\\"false\\\" ...\\n    "
                + "<Q_IRI_REF> ...\\n    <PNAME_NS> ...\\n    <PNAME_LN> ..."
                + "\\n    <BLANK_NODE_LABEL> ...\\n    <VAR1> ...\\n    "
                + "<VAR2> ...\\n    <INTEGER> ...\\n    <INTEGER_POSITIVE> "
                + "...\\n    <INTEGER_NEGATIVE> ...\\n    <DECIMAL> ...\\n"
                + "    <DECIMAL_POSITIVE> ...\\n    <DECIMAL_NEGATIVE> ..."
                + "\\n    <DOUBLE> ...\\n    <DOUBLE_POSITIVE> ...\\n    "
                + "<DOUBLE_NEGATIVE> ...\\n    <STRING_LITERAL1> ...\\n    "
                + "<STRING_LITERAL2> ...\\n    <STRING_LITERAL_LONG1> ...\\n"
                + "    <STRING_LITERAL_LONG2> ...\\n    \"}";

        assertEquals(ref, json.toString());
    } else {/*from  ww  w .  ja  va  2s  .c  o  m*/
        fail("code=" + code);
    }
}

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

@Test
public void testSubmitQueryInvalid() throws IllegalArgumentException, HttpException, IOException {
    final String query = "SELECT ?p WHERE { ?s <INVALID> ?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 = "{\"status\":\"ERROR\"," + "\"message\":\"org.openrdf.query.MalformedQueryException: "
                + "Not a valid (absolute) URI: INVALID\"}";

        assertEquals(ref, json.toString());
    } else {// w  ww. ja  va  2 s  . com
        fail("code=" + code);
    }
}