Search in sources :

Example 6 with SocketConfig

use of org.apache.hc.core5.http.io.SocketConfig 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 7 with SocketConfig

use of org.apache.hc.core5.http.io.SocketConfig in project sonarlint-language-server by SonarSource.

the class SecurityHotspotsHandlerServer method initialize.

public void initialize(String ideName, String clientVersion, @Nullable String workspaceName) {
    final var socketConfig = SocketConfig.custom().setSoTimeout(15, TimeUnit.SECONDS).setTcpNoDelay(true).build();
    port = INVALID_PORT;
    var triedPort = STARTING_PORT;
    HttpServer startedServer = null;
    while (port < 0 && triedPort <= ENDING_PORT) {
        try {
            startedServer = ServerBootstrap.bootstrap().setLocalAddress(InetAddress.getLoopbackAddress()).setListenerPort(triedPort).setSocketConfig(socketConfig).addFilterFirst("CORS", new CorsFilter()).register("/sonarlint/api/status", new StatusRequestHandler(ideName, clientVersion, workspaceName)).register("/sonarlint/api/hotspots/show", new ShowHotspotRequestHandler(output, bindingManager, client, telemetry)).create();
            startedServer.start();
            port = triedPort;
        } catch (Exception t) {
            output.debug("Error while starting port: " + t.getMessage());
            triedPort++;
        }
    }
    if (port > 0) {
        output.info("Started security hotspot handler on port " + port);
        server = startedServer;
    } else {
        output.error("Unable to start security hotspot handler");
        server = null;
    }
}
Also used : HttpServer(org.apache.hc.core5.http.impl.bootstrap.HttpServer) URISyntaxException(java.net.URISyntaxException) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException)

Example 8 with SocketConfig

use of org.apache.hc.core5.http.io.SocketConfig in project eomcs-java by eomcs.

the class Exam0110 method main.

public static void main(final String[] args) throws Exception {
    final SocketConfig socketConfig = // 
    SocketConfig.custom().setSoTimeout(15, // 
    TimeUnit.SECONDS).setTcpNoDelay(// 
    true).build();
    final HttpServer server = // 
    ServerBootstrap.bootstrap().setListenerPort(// 웹서버 포트 번호 설정
    9999).setSocketConfig(// 기본 소켓 동작 설정
    socketConfig).setSslContext(// SSL 설정
    null).setExceptionListener(// 예외 처리자 설정
    new MyExceptionListener()).register("*", // 요청 처리자 설정
    new HttpFileHandler()).create();
    // 웹서버를 시작시킨다.
    server.start();
    // 웹서버를 종료시키는 스레드를 등록한다.
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            server.close(CloseMode.GRACEFUL);
        }
    });
    System.out.println("서버 시작(9999)!");
    server.awaitTermination(TimeValue.MAX_VALUE);
}
Also used : SocketConfig(org.apache.hc.core5.http.io.SocketConfig) HttpServer(org.apache.hc.core5.http.impl.bootstrap.HttpServer)

Example 9 with SocketConfig

use of org.apache.hc.core5.http.io.SocketConfig in project eomcs-java by eomcs.

the class Exam0210 method main.

public static void main(final String[] args) throws Exception {
    final SocketConfig socketConfig = // 
    SocketConfig.custom().setSoTimeout(15, // 
    TimeUnit.SECONDS).setTcpNoDelay(// 
    true).build();
    final HttpServer server = // 
    ServerBootstrap.bootstrap().setListenerPort(// 웹서버 포트 번호 설정
    9999).setSocketConfig(// 기본 소켓 동작 설정
    socketConfig).setSslContext(// SSL 설정
    null).setExceptionListener(// 예외 처리자 설정
    new MyExceptionListener()).register("*", // 요청 처리자 설정
    new MyRequestHandler()).create();
    // 웹서버를 시작시킨다.
    server.start();
    // 웹서버를 종료시키는 스레드를 등록한다.
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            server.close(CloseMode.GRACEFUL);
        }
    });
    System.out.println("서버 시작(9999)!");
    server.awaitTermination(TimeValue.MAX_VALUE);
}
Also used : SocketConfig(org.apache.hc.core5.http.io.SocketConfig) HttpServer(org.apache.hc.core5.http.impl.bootstrap.HttpServer)

Example 10 with SocketConfig

use of org.apache.hc.core5.http.io.SocketConfig 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)

Aggregations

SocketConfig (org.apache.hc.core5.http.io.SocketConfig)8 HttpServer (org.apache.hc.core5.http.impl.bootstrap.HttpServer)6 IOException (java.io.IOException)4 HttpException (org.apache.hc.core5.http.HttpException)3 HttpHost (org.apache.hc.core5.http.HttpHost)3 SocketTimeoutException (java.net.SocketTimeoutException)2 SSLContext (javax.net.ssl.SSLContext)2 SSLConnectionSocketFactory (org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory)2 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)2 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)2 ExceptionListener (org.apache.hc.core5.http.ExceptionListener)2 HttpConnection (org.apache.hc.core5.http.HttpConnection)2 ServerBootstrap (org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap)2 InetAddress (java.net.InetAddress)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 TimeUnit (java.util.concurrent.TimeUnit)1 DnsResolver (org.apache.hc.client5.http.DnsResolver)1 HttpRoute (org.apache.hc.client5.http.HttpRoute)1 SystemDefaultDnsResolver (org.apache.hc.client5.http.SystemDefaultDnsResolver)1