From fce88c72b4a549d60010b7b3ccab6a058a617ccc Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 7 Dec 2012 21:35:34 +0100 Subject: o Better Jenkins importer. --- .../io/trygvis/esper/testing/util/HttpClient.java | 37 +++++++++++++++------- .../trygvis/esper/testing/util/XmlHttpClient.java | 2 +- 2 files changed, 26 insertions(+), 13 deletions(-) (limited to 'src/main/java/io/trygvis/esper/testing/util') diff --git a/src/main/java/io/trygvis/esper/testing/util/HttpClient.java b/src/main/java/io/trygvis/esper/testing/util/HttpClient.java index ef6f2a9..420a949 100755 --- a/src/main/java/io/trygvis/esper/testing/util/HttpClient.java +++ b/src/main/java/io/trygvis/esper/testing/util/HttpClient.java @@ -3,8 +3,6 @@ package io.trygvis.esper.testing.util; import fj.*; import fj.data.*; import io.trygvis.esper.testing.*; -import static java.lang.System.*; - import org.apache.http.conn.scheme.*; import org.apache.http.impl.client.*; import org.apache.http.impl.conn.tsccm.*; @@ -12,44 +10,59 @@ import org.apache.http.params.*; import org.codehaus.httpcache4j.*; import org.codehaus.httpcache4j.cache.*; import org.codehaus.httpcache4j.resolver.*; -import org.jdom2.*; import java.io.*; import java.net.*; -import javax.xml.stream.*; + +import static java.lang.System.*; public class HttpClient { private final HTTPCache http; - private final F> f; + private final F> f; - public HttpClient(HTTPCache http, F> f) { + public HttpClient(HTTPCache http, F> f) { this.http = http; this.f = f; } + public static HttpClient httpClient(HTTPCache http, final F> f) { + return new HttpClient<>(http, new F>() { + @Override + public Option f(HTTPResponse response) { + return f.f(response.getPayload().getInputStream()); + } + }); + } + public Option fetch(URI uri) throws IOException { HTTPResponse response = null; try { + System.out.println("Fetching " + uri); + long start = currentTimeMillis(); response = http.execute(new HTTPRequest(uri)); + long end = currentTimeMillis(); + int code = response.getStatus().getCode(); + System.out.println("Fetched in " + (end - start) + "ms. Status: " + code); - if (response.getStatus().getCode() != 200) { - throw new IOException("Did not get 200 back, got " + response.getStatus().getCode()); + if (code != 200) { + throw new IOException("Did not get 200 back, got " + code); } -// return getDocument(response.getPayload().getInputStream()); - return f.f(response.getPayload().getInputStream()); + return f.f(response); } catch (HTTPException e) { throw new IOException(e); } finally { if (response != null) { - response.consume(); + try { + response.consume(); + } catch (Exception ignore) { + } } } } - public static HTTPCache createHttpCache(Config config) { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); diff --git a/src/main/java/io/trygvis/esper/testing/util/XmlHttpClient.java b/src/main/java/io/trygvis/esper/testing/util/XmlHttpClient.java index 2c2f922..dc3b46f 100755 --- a/src/main/java/io/trygvis/esper/testing/util/XmlHttpClient.java +++ b/src/main/java/io/trygvis/esper/testing/util/XmlHttpClient.java @@ -14,7 +14,7 @@ public class XmlHttpClient { public XmlHttpClient(HTTPCache http) { final XmlParser parser = new XmlParser(); - httpClient = new HttpClient<>(http, new F>() { + httpClient = HttpClient.httpClient(http, new F>() { public Option f(InputStream inputStream) { return parser.parseDocument(inputStream); } -- cgit v1.2.3