Example usage for org.apache.commons.httpclient HttpClient HttpClient

List of usage examples for org.apache.commons.httpclient HttpClient HttpClient

Introduction

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

Prototype

public HttpClient() 

Source Link

Usage

From source file:atg.taglib.json.Helper.java

/**
 * Get a response from the server for a test
 *
 * @param pTestUrl The test url, relative to the tests context root
 * @return the <code>ResponseData</code> object for this test
 * @throws IOException /* ww  w .j av a 2  s .c  o  m*/
 * @throws HttpException 
 * @throws JSONException 
 */
public static ResponseData getData(String pType, int pTestNum)
        throws HttpException, IOException, JSONException {
    // Setup HTTP client
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(getTestUrl(pType, pTestNum));

    // Execute the GET request, capturing response status
    int statusCode = client.executeMethod(method);
    String responseBody = method.getResponseBodyAsString();
    method.releaseConnection();

    // Create response data object
    ResponseData data = new ResponseData();
    data.body = responseBody.trim();
    data.statusCode = statusCode;
    if (statusCode == HttpStatus.SC_OK) {
        // Parse the JSON response
        data.json = parseJsonTextToObject(responseBody);
    }

    // Get the expected result
    method = new GetMethod(getStatus200ResultUrl(pType, pTestNum));
    data.expectedStatusCode = HttpStatus.SC_OK;

    // Execute the GET request, capturing response status
    statusCode = client.executeMethod(method);

    if (statusCode == HttpStatus.SC_NOT_FOUND) {
        // Test result wasn't found - we must be expecting an error for this test
        method.releaseConnection();
        method = new GetMethod(getStatus500ResultUrl(pType, pTestNum));
        statusCode = client.executeMethod(method);
        data.expectedStatusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    }

    responseBody = method.getResponseBodyAsString().trim();
    method.releaseConnection();

    // Parse the expected data   
    if (data.expectedStatusCode == HttpStatus.SC_OK) {
        // Parse body into JSON object
        data.expectedJson = parseJsonTextToObject(responseBody);
    } else {
        // Exception is expected, set the expected key
        data.expectedMsgKey = responseBody.trim();
    }

    return data;
}

From source file:com.knowledgebooks.rdf.SparqlClient.java

public SparqlClient(String endpoint_URL, String sparql) throws Exception {
    //System.out.println("SparqlClient("+endpoint_URL+", "+sparql+")");
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);

    String req = URLEncoder.encode(sparql, "utf-8");
    HttpMethod method = new GetMethod(endpoint_URL + "?query=" + req);
    method.setFollowRedirects(false);//from ww  w . j  ava  2s. c  om
    try {
        client.executeMethod(method);
        //System.out.println(method.getResponseBodyAsString());
        //if (true) return;
        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.err.println("Http error connecting to '" + endpoint_URL + "'");
    } catch (IOException ioe) {
        System.err.println("Unable to connect to '" + endpoint_URL + "'");
    }
    method.releaseConnection();
}

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

public GrantGroupRight(String baseUri) {
    this.baseUri = baseUri;
    this.httpClient = new HttpClient();
}

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

public RemoveUserFromGroup(String baseUri) {
    this.baseUri = baseUri;
    this.httpClient = new HttpClient();
}

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 . j a v a2  s  . c o m
        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:com.atlantbh.jmeter.plugins.oauth.OAuthSamplerEmul.java

@Override
protected HttpClient setupConnection(java.net.URL u, HttpMethodBase httpMethod) throws IOException {
    return new HttpClient();
}

From source file:it.iit.genomics.cru.simsearch.bundle.utils.PantherBridge.java

public static Collection<String[]> getEnrichment(String organism, String fileName, double threshold) {
    ArrayList<String[]> results = new ArrayList<>();

    ArrayListMultimap<String, String> genes = ArrayListMultimap.create();
    ArrayListMultimap<Double, String> pvalues = ArrayListMultimap.create();

    HashSet<String> uniqueGenes = new HashSet<>();

    try {//from  ww  w.  j a v a  2 s .  c  o m
        String[] enrichmentTypes = { "process", "pathway" };

        for (String enrichmentType : enrichmentTypes) {

            HttpClient client = new HttpClient();
            MultipartPostMethod method = new MultipartPostMethod(
                    "http://pantherdb.org/webservices/garuda/tools/enrichment/VER_2/enrichment.jsp?");

            // Define name-value pairs to set into the QueryString
            method.addParameter("organism", organism);
            method.addParameter("type", "enrichment");
            method.addParameter("enrichmentType", enrichmentType); // "function",
            // "process",
            // "cellular_location",
            // "protein_class",
            // "pathway"
            File inputFile = new File(fileName);
            method.addPart(new FilePart("geneList", inputFile, "text/plain", "ISO-8859-1"));

            // PANTHER does not use the ID type
            // method.addParameter("IdType", "UniProt");

            // Execute and print response
            client.executeMethod(method);
            String response = method.getResponseBodyAsString();

            for (String line : response.split("\n")) {
                if (false == "".equals(line.trim())) {

                    String[] row = line.split("\t");
                    // Id Name GeneId P-value
                    if ("Id".equals(row[0])) {
                        // header
                        continue;
                    }
                    // if (row.length > 1) {
                    String name = row[1];

                    String gene = row[2];
                    Double pvalue = Double.valueOf(row[3]);

                    uniqueGenes.add(gene);

                    if (pvalue < threshold) {
                        if (false == genes.containsKey(name)) {
                            pvalues.put(pvalue, name);
                        }
                        genes.put(name, gene);
                    }
                    // } else {
                    //    System.out.println("oups: " + row[0]);
                    // }
                }
            }

            method.releaseConnection();
        }
        ArrayList<Double> pvalueList = new ArrayList<>();
        Collections.sort(pvalueList);

        pvalueList.addAll(pvalues.keySet());
        Collections.sort(pvalueList);

        int numGenes = uniqueGenes.size();

        for (Double pvalue : pvalueList) {
            for (String name : pvalues.get(pvalue)) {
                String geneList = String.join(",", genes.get(name));
                String result[] = { name, "" + pvalue, genes.get(name).size() + "/" + numGenes, geneList };
                results.add(result);
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    return results;
}

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

public DeleteGroup(String baseUri) {
    this.baseUri = baseUri;
    this.httpClient = new HttpClient();
}

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

public RemoveGroupRight(String baseUri) {
    this.baseUri = baseUri;
    this.httpClient = new HttpClient();
}

From source file:net.jadler.JadlerTimeoutIntegrationTest.java

@Before
public void setUp() {
    initJadler();
    this.client = new HttpClient();
}