Search in sources :

Example 41 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project camel by apache.

the class NettyHttpBridgeEncodedPathTest method testEncodedPath.

@Test
public void testEncodedPath() throws Exception {
    String path = URLEncoder.encode(" :/?#[]@!$", "UTF-8") + "/" + URLEncoder.encode("&'()+,;=", "UTF-8");
    MockEndpoint mock = getMockEndpoint("mock:encodedPath");
    mock.message(0).header(Exchange.HTTP_PATH).isEqualTo("/" + path);
    mock.message(0).header(Exchange.HTTP_QUERY).isNull();
    mock.message(0).header(Exchange.HTTP_RAW_QUERY).isNull();
    // cannot use template as it automatically decodes some chars in the path
    HttpClient httpClient = new HttpClient();
    GetMethod httpGet = new GetMethod("http://localhost:" + port4 + "/nettyTestRouteC/" + path);
    int status = httpClient.executeMethod(httpGet);
    assertEquals("Get a wrong response status", 200, status);
    assertMockEndpointsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 42 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project camel by apache.

the class WeatherConsumer method poll.

@Override
protected int poll() throws Exception {
    LOG.debug("Going to execute the Weather query {}", query);
    HttpClient httpClient = ((WeatherComponent) getEndpoint().getComponent()).getHttpClient();
    GetMethod getMethod = new GetMethod(query);
    try {
        int status = httpClient.executeMethod(getMethod);
        if (status != HttpStatus.SC_OK) {
            LOG.warn("HTTP call for weather returned error status code {} - {} as a result with query: {}", status, getMethod.getStatusLine(), query);
            return 0;
        }
        String weather = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(String.class, getMethod.getResponseBodyAsStream());
        LOG.debug("Got back the Weather information {}", weather);
        if (ObjectHelper.isEmpty(weather)) {
            // empty response
            return 0;
        }
        Exchange exchange = getEndpoint().createExchange();
        String header = getEndpoint().getConfiguration().getHeaderName();
        if (header != null) {
            exchange.getIn().setHeader(header, weather);
        } else {
            exchange.getIn().setBody(weather);
        }
        exchange.getIn().setHeader(WeatherConstants.WEATHER_QUERY, query);
        getProcessor().process(exchange);
        return 1;
    } finally {
        getMethod.releaseConnection();
    }
}
Also used : Exchange(org.apache.camel.Exchange) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 43 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project h2o-2 by h2oai.

the class WebAPI method listJobs.

/**
   * Lists jobs currently running.
   */
static void listJobs() throws Exception {
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(URL + "/Jobs.json");
    int status = client.executeMethod(get);
    if (status != 200)
        throw new Exception(get.getStatusText());
    Gson gson = new Gson();
    JobsRes res = gson.fromJson(new InputStreamReader(get.getResponseBodyAsStream()), JobsRes.class);
    System.out.println("Running jobs:");
    for (Job job : res.jobs) System.out.println(job.description + " " + job.destination_key);
    get.releaseConnection();
}
Also used : InputStreamReader(java.io.InputStreamReader) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Gson(dontweave.gson.Gson)

Example 44 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project h2o-2 by h2oai.

the class WebAPI method exportModel.

/**
   * Exports a model to a JSON file.
   */
static void exportModel() throws Exception {
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(URL + "/2/ExportModel.json?model=MyInitialNeuralNet");
    int status = client.executeMethod(get);
    if (status != 200)
        throw new Exception(get.getStatusText());
    JsonObject response = (JsonObject) new JsonParser().parse(new InputStreamReader(get.getResponseBodyAsStream()));
    JsonElement model = response.get("model");
    JsonWriter writer = new JsonWriter(new FileWriter(JSON_FILE));
    writer.setLenient(true);
    writer.setIndent("  ");
    Streams.write(model, writer);
    writer.close();
    get.releaseConnection();
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonElement(dontweave.gson.JsonElement) HttpClient(org.apache.commons.httpclient.HttpClient) FileWriter(java.io.FileWriter) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JsonObject(dontweave.gson.JsonObject) JsonWriter(dontweave.gson.stream.JsonWriter) JsonParser(dontweave.gson.JsonParser)

Example 45 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project h2o-2 by h2oai.

the class CloudConnect method launch.

/**
   * Upload jars to an existing cloud and launches a custom job. Uses the Web API.
   */
public static void launch(String job, String host, File... jars) throws Exception {
    HttpClient client = new HttpClient();
    String args = "job_class=" + job + "&jars=";
    for (File f : jars) {
        PostMethod post = new PostMethod("http://" + host + "/Upload.json?key=" + f.getName());
        Part[] parts = { new FilePart(f.getName(), f) };
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        if (200 != client.executeMethod(post))
            throw new RuntimeException("Request failed: " + post.getStatusLine());
        args += f.getName() + ",";
        post.releaseConnection();
    }
    String href = new LaunchJar().href();
    GetMethod get = new GetMethod("http://" + host + href + ".json?" + args);
    if (200 != client.executeMethod(get))
        throw new RuntimeException("Request failed: " + get.getStatusLine());
    get.releaseConnection();
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) LaunchJar(water.deploy.LaunchJar)

Aggregations

GetMethod (org.apache.commons.httpclient.methods.GetMethod)357 HttpClient (org.apache.commons.httpclient.HttpClient)216 HttpMethod (org.apache.commons.httpclient.HttpMethod)93 IOException (java.io.IOException)82 Test (org.junit.Test)70 InputStream (java.io.InputStream)65 HttpException (org.apache.commons.httpclient.HttpException)41 Map (java.util.Map)39 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)36 JSONParser (org.json.simple.parser.JSONParser)34 JSONObject (org.json.simple.JSONObject)31 Header (org.apache.commons.httpclient.Header)22 Element (org.w3c.dom.Element)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 TypeToken (com.google.gson.reflect.TypeToken)20 List (java.util.List)19 HttpState (org.apache.commons.httpclient.HttpState)18 URI (java.net.URI)17 JSONArray (org.json.simple.JSONArray)17