Search in sources :

Example 1 with ExceptionListener

use of org.apache.hc.core5.http.ExceptionListener in project JAuswertung by dennisfabri.

the class HttpServerPlugin method startUp.

boolean startUp() {
    try {
        state = true;
        final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(15, TimeUnit.SECONDS).setTcpNoDelay(true).build();
        httpServer = ServerBootstrap.bootstrap().setListenerPort(port).setSocketConfig(socketConfig).setExceptionListener(new ExceptionListener() {

            @Override
            public void onError(final Exception ex) {
                ex.printStackTrace();
            }

            @Override
            public void onError(final HttpConnection conn, final Exception ex) {
                if (ex instanceof SocketTimeoutException) {
                    System.err.println("Connection timed out");
                } else if (ex instanceof ConnectionClosedException) {
                    System.err.println(ex.getMessage());
                } else {
                    ex.printStackTrace();
                }
            }
        }).register("*", new DPRequestHandler(getDataProvider())).create();
        httpServer.start();
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                httpServer.close(CloseMode.GRACEFUL);
            }
        });
        httpServer.start();
        httpOptions.setEnabled(false);
        filter.setEnabled(source.getExportMode() == ExportMode.Filtered);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        DialogUtils.wichtigeMeldung(null, I18n.get("HttpServerNotStartet"));
        shutDown();
        httpServer = null;
        state = false;
        httpOptions.setEnabled(true);
        filter.setEnabled(false);
        return false;
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) SocketConfig(org.apache.hc.core5.http.io.SocketConfig) HttpConnection(org.apache.hc.core5.http.HttpConnection) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) ExceptionListener(org.apache.hc.core5.http.ExceptionListener) SocketTimeoutException(java.net.SocketTimeoutException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException)

Example 2 with ExceptionListener

use of org.apache.hc.core5.http.ExceptionListener in project httpcomponents-core by apache.

the class ClassicFileServerExample method main.

public static void main(final String[] args) throws Exception {
    if (args.length < 1) {
        System.err.println("Please specify document root directory");
        System.exit(1);
    }
    // Document root directory
    final String docRoot = args[0];
    int port = 8080;
    if (args.length >= 2) {
        port = Integer.parseInt(args[1]);
    }
    SSLContext sslContext = null;
    if (port == 8443) {
        // Initialize SSL context
        final URL url = ClassicFileServerExample.class.getResource("/my.keystore");
        if (url == null) {
            System.out.println("Keystore not found");
            System.exit(1);
        }
        sslContext = SSLContexts.custom().loadKeyMaterial(url, "secret".toCharArray(), "secret".toCharArray()).build();
    }
    final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(15, TimeUnit.SECONDS).setTcpNoDelay(true).build();
    final HttpServer server = ServerBootstrap.bootstrap().setListenerPort(port).setSocketConfig(socketConfig).setSslContext(sslContext).setExceptionListener(new ExceptionListener() {

        @Override
        public void onError(final Exception ex) {
            ex.printStackTrace();
        }

        @Override
        public void onError(final HttpConnection conn, final Exception ex) {
            if (ex instanceof SocketTimeoutException) {
                System.err.println("Connection timed out");
            } else if (ex instanceof ConnectionClosedException) {
                System.err.println(ex.getMessage());
            } else {
                ex.printStackTrace();
            }
        }
    }).register("*", new HttpFileHandler(docRoot)).create();
    server.start();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> server.close(CloseMode.GRACEFUL)));
    System.out.println("Listening on port " + port);
    server.awaitTermination(TimeValue.MAX_VALUE);
}
Also used : SocketConfig(org.apache.hc.core5.http.io.SocketConfig) HttpConnection(org.apache.hc.core5.http.HttpConnection) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) SSLContext(javax.net.ssl.SSLContext) URL(java.net.URL) MethodNotSupportedException(org.apache.hc.core5.http.MethodNotSupportedException) SocketTimeoutException(java.net.SocketTimeoutException) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) SocketTimeoutException(java.net.SocketTimeoutException) HttpServer(org.apache.hc.core5.http.impl.bootstrap.HttpServer) ExceptionListener(org.apache.hc.core5.http.ExceptionListener)

Example 3 with ExceptionListener

use of org.apache.hc.core5.http.ExceptionListener in project httpcomponents-core by apache.

the class ClassicReverseProxyExample method main.

public static void main(final String[] args) throws Exception {
    if (args.length < 1) {
        System.out.println("Usage: <hostname[:port]> [listener port]");
        System.exit(1);
    }
    final HttpHost targetHost = HttpHost.create(args[0]);
    int port = 8080;
    if (args.length > 1) {
        port = Integer.parseInt(args[1]);
    }
    System.out.println("Reverse proxy to " + targetHost);
    final HttpRequester requester = RequesterBootstrap.bootstrap().setStreamListener(new Http1StreamListener() {

        @Override
        public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
            System.out.println("[proxy->origin] " + Thread.currentThread() + " " + request.getMethod() + " " + request.getRequestUri());
        }

        @Override
        public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
            System.out.println("[proxy<-origin] " + Thread.currentThread() + " status " + response.getCode());
        }

        @Override
        public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
            System.out.println("[proxy<-origin] " + Thread.currentThread() + " exchange completed; " + "connection " + (keepAlive ? "kept alive" : "cannot be kept alive"));
        }
    }).setConnPoolListener(new ConnPoolListener<HttpHost>() {

        @Override
        public void onLease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
            final StringBuilder buf = new StringBuilder();
            buf.append("[proxy->origin] ").append(Thread.currentThread()).append(" connection leased ").append(route);
            System.out.println(buf);
        }

        @Override
        public void onRelease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
            final StringBuilder buf = new StringBuilder();
            buf.append("[proxy->origin] ").append(Thread.currentThread()).append(" connection released ").append(route);
            final PoolStats totals = connPoolStats.getTotalStats();
            buf.append("; total kept alive: ").append(totals.getAvailable()).append("; ");
            buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable());
            buf.append(" of ").append(totals.getMax());
            System.out.println(buf);
        }
    }).create();
    final HttpServer server = ServerBootstrap.bootstrap().setListenerPort(port).setStreamListener(new Http1StreamListener() {

        @Override
        public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
            System.out.println("[client->proxy] " + Thread.currentThread() + " " + request.getMethod() + " " + request.getRequestUri());
        }

        @Override
        public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
            System.out.println("[client<-proxy] " + Thread.currentThread() + " status " + response.getCode());
        }

        @Override
        public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
            System.out.println("[client<-proxy] " + Thread.currentThread() + " exchange completed; " + "connection " + (keepAlive ? "kept alive" : "cannot be kept alive"));
        }
    }).setExceptionListener(new ExceptionListener() {

        @Override
        public void onError(final Exception ex) {
            if (ex instanceof SocketException) {
                System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
            } else {
                System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
                ex.printStackTrace(System.out);
            }
        }

        @Override
        public void onError(final HttpConnection connection, final Exception ex) {
            if (ex instanceof SocketTimeoutException) {
                System.out.println("[client->proxy] " + Thread.currentThread() + " time out");
            } else if (ex instanceof SocketException || ex instanceof ConnectionClosedException) {
                System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
            } else {
                System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
                ex.printStackTrace(System.out);
            }
        }
    }).register("*", new ProxyHandler(targetHost, requester)).create();
    server.start();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        server.close(CloseMode.GRACEFUL);
        requester.close(CloseMode.GRACEFUL);
    }));
    System.out.println("Listening on port " + port);
    server.awaitTermination(TimeValue.MAX_VALUE);
}
Also used : BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) SocketException(java.net.SocketException) HttpConnection(org.apache.hc.core5.http.HttpConnection) Http1StreamListener(org.apache.hc.core5.http.impl.Http1StreamListener) ConnPoolStats(org.apache.hc.core5.pool.ConnPoolStats) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) PoolStats(org.apache.hc.core5.pool.PoolStats) ConnPoolStats(org.apache.hc.core5.pool.ConnPoolStats) SocketTimeoutException(java.net.SocketTimeoutException) HttpHost(org.apache.hc.core5.http.HttpHost) ConnPoolListener(org.apache.hc.core5.pool.ConnPoolListener) HttpServer(org.apache.hc.core5.http.impl.bootstrap.HttpServer) ExceptionListener(org.apache.hc.core5.http.ExceptionListener) HttpRequester(org.apache.hc.core5.http.impl.bootstrap.HttpRequester)

Aggregations

SocketTimeoutException (java.net.SocketTimeoutException)3 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)3 ExceptionListener (org.apache.hc.core5.http.ExceptionListener)3 HttpConnection (org.apache.hc.core5.http.HttpConnection)3 IOException (java.io.IOException)2 HttpException (org.apache.hc.core5.http.HttpException)2 HttpServer (org.apache.hc.core5.http.impl.bootstrap.HttpServer)2 SocketConfig (org.apache.hc.core5.http.io.SocketConfig)2 SocketException (java.net.SocketException)1 URL (java.net.URL)1 SSLContext (javax.net.ssl.SSLContext)1 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)1 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)1 HttpHost (org.apache.hc.core5.http.HttpHost)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 MethodNotSupportedException (org.apache.hc.core5.http.MethodNotSupportedException)1 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)1 HttpRequester (org.apache.hc.core5.http.impl.bootstrap.HttpRequester)1 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)1