use of java.net.HttpURLConnection in project jetty.project by eclipse.
the class FileInitializer method download.
protected void download(URI uri, Path destination) throws IOException {
if (FS.ensureDirectoryExists(destination.getParent()))
StartLog.log("MKDIR", _basehome.toShortForm(destination.getParent()));
StartLog.log("DOWNLD", "%s to %s", uri, _basehome.toShortForm(destination));
HttpURLConnection http = (HttpURLConnection) uri.toURL().openConnection();
http.setInstanceFollowRedirects(true);
http.setAllowUserInteraction(false);
int status = http.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
throw new IOException("URL GET Failure [" + status + "/" + http.getResponseMessage() + "] on " + uri);
}
byte[] buf = new byte[8192];
try (InputStream in = http.getInputStream();
OutputStream out = Files.newOutputStream(destination, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
while (true) {
int len = in.read(buf);
if (len > 0) {
out.write(buf, 0, len);
}
if (len < 0) {
break;
}
}
}
}
use of java.net.HttpURLConnection in project jetty.project by eclipse.
the class ServerConnectorTest method getResponse.
private String getResponse(URI uri) throws IOException {
HttpURLConnection http = (HttpURLConnection) uri.toURL().openConnection();
assertThat("Valid Response Code", http.getResponseCode(), anyOf(is(200), is(404)));
try (InputStream in = http.getInputStream()) {
return IO.toString(in, StandardCharsets.UTF_8);
}
}
use of java.net.HttpURLConnection in project jetty.project by eclipse.
the class RequestLogHandlerTest method testMultipleLogHandlers.
@Test(timeout = 4000)
public void testMultipleLogHandlers() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
List<CaptureLog> captureLogs = new ArrayList<>();
List<Handler> handlerList = new ArrayList<>();
handlerList.add(testHandler);
for (int i = 0; i < 4; ++i) {
CaptureLog captureLog = new CaptureLog();
captureLogs.add(captureLog);
RequestLogHandler requestLog = new RequestLogHandler();
requestLog.setRequestLog(captureLog);
handlerList.add(requestLog);
}
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(handlerList.toArray(new Handler[0]));
server.setHandler(handlers);
try {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, requestPath, null, null);
// Make call to test handler
HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
try {
connection.setAllowUserInteraction(false);
// log response status code
int statusCode = connection.getResponseCode();
LOG.debug("Response Status Code: {}", statusCode);
if (statusCode == 200) {
// collect response message and log it
String content = getResponseContent(connection);
LOG.debug("Response Content: {}", content);
}
} finally {
connection.disconnect();
}
for (CaptureLog captureLog : captureLogs) assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of java.net.HttpURLConnection in project jetty.project by eclipse.
the class RequestLogHandlerTest method testLogHandlerCollection_DispatchErrorHandler.
/**
* Test a RequestLogHandler at the end of a HandlerCollection and also with the ErrorHandler in place.
* @throws Exception if test failure
*/
@Test(timeout = 4000)
public void testLogHandlerCollection_DispatchErrorHandler() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
DispatchErrorHandler errorDispatcher = new DispatchErrorHandler();
server.addBean(errorDispatcher);
ContextHandlerCollection contexts = new ContextHandlerCollection();
ContextHandler errorContext = new ContextHandler("/errorok");
errorContext.setHandler(new OKErrorHandler());
ContextHandler testContext = new ContextHandler("/test");
testContext.setHandler(testHandler);
contexts.addHandler(errorContext);
contexts.addHandler(testContext);
RequestLogHandler requestLog = new RequestLogHandler();
CaptureLog captureLog = new CaptureLog();
requestLog.setRequestLog(captureLog);
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] { contexts, requestLog });
server.setHandler(handlers);
try {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, requestPath, null, null);
// Make call to test handler
HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
try {
connection.setAllowUserInteraction(false);
// log response status code
int statusCode = connection.getResponseCode();
LOG.debug("Response Status Code: {}", statusCode);
if (statusCode == 200) {
// collect response message and log it
String content = getResponseContent(connection);
LOG.debug("Response Content: {}", content);
}
} finally {
connection.disconnect();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of java.net.HttpURLConnection in project jetty.project by eclipse.
the class RequestLogHandlerTest method testLogHandlerCollection_AltErrorHandler.
/**
* Test a RequestLogHandler at the end of a HandlerCollection and also with the ErrorHandler in place.
* @throws Exception if test failure
*/
@Test(timeout = 4000)
public void testLogHandlerCollection_AltErrorHandler() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
AltErrorHandler errorDispatcher = new AltErrorHandler();
server.addBean(errorDispatcher);
ContextHandlerCollection contexts = new ContextHandlerCollection();
ContextHandler errorContext = new ContextHandler("/errorpage");
errorContext.setHandler(new AltErrorHandler());
ContextHandler testContext = new ContextHandler("/test");
testContext.setHandler(testHandler);
contexts.addHandler(errorContext);
contexts.addHandler(testContext);
RequestLogHandler requestLog = new RequestLogHandler();
CaptureLog captureLog = new CaptureLog();
requestLog.setRequestLog(captureLog);
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] { contexts, requestLog });
server.setHandler(handlers);
try {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, requestPath, null, null);
// Make call to test handler
HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
try {
connection.setAllowUserInteraction(false);
// log response status code
int statusCode = connection.getResponseCode();
LOG.debug("Response Status Code: {}", statusCode);
if (statusCode == 200) {
// collect response message and log it
String content = getResponseContent(connection);
LOG.debug("Response Content: {}", content);
}
} finally {
connection.disconnect();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
Aggregations