Search in sources :

Example 6 with HttpURLConnection

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;
            }
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 7 with HttpURLConnection

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);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream)

Example 8 with HttpURLConnection

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();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) ArrayList(java.util.ArrayList) Handler(org.eclipse.jetty.server.Handler) URI(java.net.URI) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 9 with HttpURLConnection

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();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) URI(java.net.URI) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 10 with HttpURLConnection

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();
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpURLConnection(java.net.HttpURLConnection) Server(org.eclipse.jetty.server.Server) URI(java.net.URI) Test(org.junit.Test)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3831 URL (java.net.URL)2447 IOException (java.io.IOException)1634 InputStream (java.io.InputStream)1082 InputStreamReader (java.io.InputStreamReader)692 Test (org.junit.Test)650 BufferedReader (java.io.BufferedReader)573 OutputStream (java.io.OutputStream)466 MalformedURLException (java.net.MalformedURLException)372 URLConnection (java.net.URLConnection)248 HashMap (java.util.HashMap)216 OutputStreamWriter (java.io.OutputStreamWriter)208 Map (java.util.Map)199 Gson (com.google.gson.Gson)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 ArrayList (java.util.ArrayList)168 ExecutionException (java.util.concurrent.ExecutionException)161 File (java.io.File)159 AsyncTask (android.os.AsyncTask)158 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)157