Example usage for io.vertx.core.http HttpServerOptions setCompressionSupported

List of usage examples for io.vertx.core.http HttpServerOptions setCompressionSupported

Introduction

In this page you can find the example usage for io.vertx.core.http HttpServerOptions setCompressionSupported.

Prototype

public HttpServerOptions setCompressionSupported(boolean compressionSupported) 

Source Link

Document

Set whether the server should support gzip/deflate compression (serving compressed responses to clients advertising support for them with Accept-Encoding header)

Usage

From source file:com.company.vertxstarter.MainVerticle.java

@Override
public void start(Future<Void> fut) {
    // Create a router object.
    Router router = Router.router(vertx);
    //CORS handler
    router.route().handler(CorsHandler.create("*").allowedMethod(HttpMethod.GET).allowedMethod(HttpMethod.POST)
            .allowedMethod(HttpMethod.OPTIONS).allowedHeader("Content-Type").allowedHeader("Accept"));

    //default headers
    router.route().handler(ctx -> {/*w w w .  j  av  a 2  s .  c om*/
        ctx.response().putHeader("Cache-Control", "no-store, no-cache").putHeader("Content-Type",
                "application/json");

        if (StringUtils.isEmpty(ctx.request().getHeader("Accept"))) {
            ctx.fail(Failure.NO_MEDIA_TYPE);
            return;
        } else if (!"application/json".equalsIgnoreCase(ctx.request().getHeader("Accept"))) {
            ctx.fail(Failure.UNSUPPORTED_MEDIA_TYPE);
            return;
        }
        ctx.next();
    });

    //error handling
    router.route().failureHandler(ctx -> {
        HttpServerResponse response = ctx.response();
        final JsonObject error = new JsonObject();
        Failure ex;

        if (ctx.failure() instanceof Failure) { //specific error
            ex = (Failure) ctx.failure();
        } else { //general error
            ctx.failure().printStackTrace();
            ex = Failure.INTERNAL_ERROR;
        }
        error.put("message", ex.getMessage());
        response.setStatusCode(ex.getCode()).end(error.encode());
    });
    //default 404 handling
    router.route().last().handler(ctx -> {
        HttpServerResponse response = ctx.response();
        final JsonObject error = new JsonObject();
        error.put("message", Failure.NOT_FOUND.getMessage());
        response.setStatusCode(404).end(error.encode());
    });

    //routes
    Injector injector = Guice.createInjector(new AppInjector());
    router.route(HttpMethod.GET, "/people").handler(injector.getInstance(PersonResource.class)::get);

    // Create the HTTP server and pass the "accept" method to the request handler.
    HttpServerOptions serverOptions = new HttpServerOptions();
    serverOptions.setCompressionSupported(true);
    vertx.createHttpServer(serverOptions).requestHandler(router::accept)
            .listen(config().getInteger("http.port", 8080), result -> {
                if (result.succeeded()) {
                    fut.complete();
                } else {
                    fut.fail(result.cause());
                }
            });
}

From source file:io.gravitee.am.gateway.vertx.VertxHttpServerFactory.java

License:Apache License

@Override
public HttpServer getObject() throws Exception {
    HttpServerOptions options = new HttpServerOptions();

    // Binding port
    options.setPort(httpServerConfiguration.getPort());
    options.setHost(httpServerConfiguration.getHost());

    // Netty pool buffers must be enabled by default
    options.setUsePooledBuffers(true);//from w  w w .j a va  2 s .  com

    if (httpServerConfiguration.isSecured()) {
        options.setSsl(httpServerConfiguration.isSecured());
        options.setUseAlpn(httpServerConfiguration.isAlpn());

        if (httpServerConfiguration.isClientAuth()) {
            options.setClientAuth(ClientAuth.REQUIRED);
        }

        if (httpServerConfiguration.getTrustStorePath() != null) {
            options.setTrustStoreOptions(new JksOptions().setPath(httpServerConfiguration.getTrustStorePath())
                    .setPassword(httpServerConfiguration.getTrustStorePassword()));
        }

        if (httpServerConfiguration.getKeyStorePath() != null) {
            options.setKeyStoreOptions(new JksOptions().setPath(httpServerConfiguration.getKeyStorePath())
                    .setPassword(httpServerConfiguration.getKeyStorePassword()));
        }
    }

    // Customizable configuration
    options.setCompressionSupported(httpServerConfiguration.isCompressionSupported());
    options.setIdleTimeout(httpServerConfiguration.getIdleTimeout());
    options.setTcpKeepAlive(httpServerConfiguration.isTcpKeepAlive());

    return vertx.createHttpServer(options);
}

From source file:io.gravitee.gateway.standalone.vertx.VertxHttpServerFactory.java

License:Apache License

@Override
public HttpServer getObject() throws Exception {
    HttpServerOptions options = new HttpServerOptions();

    // Binding port
    options.setPort(httpServerConfiguration.getPort());

    // Netty pool buffers must be enabled by default
    options.setUsePooledBuffers(true);/*  ww  w  .  j av a 2 s . co m*/

    if (httpServerConfiguration.isSecured()) {
        options.setSsl(httpServerConfiguration.isSecured());

        if (httpServerConfiguration.isClientAuth()) {
            options.setClientAuth(ClientAuth.REQUIRED);
        }

        options.setTrustStoreOptions(new JksOptions().setPath(httpServerConfiguration.getKeyStorePath())
                .setPassword(httpServerConfiguration.getKeyStorePassword()));
        options.setKeyStoreOptions(new JksOptions().setPath(httpServerConfiguration.getTrustStorePath())
                .setPassword(httpServerConfiguration.getKeyStorePassword()));
    }

    // Customizable configuration
    options.setCompressionSupported(httpServerConfiguration.isCompressionSupported());
    options.setIdleTimeout(httpServerConfiguration.getIdleTimeout());
    options.setTcpKeepAlive(httpServerConfiguration.isTcpKeepAlive());

    return vertx.createHttpServer(options);
}

From source file:org.apache.servicecomb.transport.rest.vertx.RestServerVerticle.java

License:Apache License

private HttpServerOptions createDefaultHttpServerOptions() {
    HttpServerOptions serverOptions = new HttpServerOptions();
    serverOptions.setUsePooledBuffers(true);
    serverOptions.setIdleTimeout(TransportConfig.getConnectionIdleTimeoutInSeconds());
    serverOptions.setCompressionSupported(TransportConfig.getCompressed());
    serverOptions.setMaxHeaderSize(TransportConfig.getMaxHeaderSize());
    serverOptions.setMaxInitialLineLength(TransportConfig.getMaxInitialLineLength());
    if (endpointObject.isHttp2Enabled()) {
        serverOptions.setUseAlpn(TransportConfig.getUseAlpn()).setInitialSettings(
                new Http2Settings().setMaxConcurrentStreams(TransportConfig.getMaxConcurrentStreams()));
    }//from   w  w w. j a v  a 2  s.c o m
    if (endpointObject.isSslEnabled()) {
        SSLOptionFactory factory = SSLOptionFactory.createSSLOptionFactory(SSL_KEY, null);
        SSLOption sslOption;
        if (factory == null) {
            sslOption = SSLOption.buildFromYaml(SSL_KEY);
        } else {
            sslOption = factory.createSSLOption();
        }
        SSLCustom sslCustom = SSLCustom.createSSLCustom(sslOption.getSslCustomClass());
        VertxTLSBuilder.buildNetServerOptions(sslOption, sslCustom, serverOptions);
    }

    return serverOptions;
}

From source file:org.wisdom.framework.vertx.Server.java

License:Apache License

private void bind(int p, Handler<AsyncResult<Void>> completion) {
    // Get port number.
    final int thePort = pickAPort(port);
    HttpServerOptions options = new HttpServerOptions();
    if (ssl) {/* w ww .j a  va 2s.com*/
        options.setSsl(true);
        options.setTrustStoreOptions(SSLServerContext.getTrustStoreOption(accessor));
        options.setKeyStoreOptions(SSLServerContext.getKeyStoreOption(accessor));
        if (authentication) {
            options.setClientAuth(ClientAuth.REQUIRED);
        }
    }

    if (hasCompressionEnabled()) {
        options.setCompressionSupported(true);
    }

    if (configuration.getIntegerWithDefault("vertx.acceptBacklog", -1) != -1) {
        options.setAcceptBacklog(configuration.getInteger("vertx.acceptBacklog"));
    }
    if (configuration.getIntegerWithDefault("vertx.maxWebSocketFrameSize", -1) != -1) {
        options.setMaxWebsocketFrameSize(configuration.getInteger("vertx.maxWebSocketFrameSize"));
    }
    if (configuration.getStringArray("wisdom.websocket.subprotocols").length > 0) {
        options.setWebsocketSubProtocols(configuration.get("wisdom.websocket.subprotocols"));
    }
    if (configuration.getStringArray("vertx.websocket-subprotocols").length > 0) {
        options.setWebsocketSubProtocols(configuration.get("vertx.websocket-subprotocols"));
    }
    if (configuration.getIntegerWithDefault("vertx.receiveBufferSize", -1) != -1) {
        options.setReceiveBufferSize(configuration.getInteger("vertx.receiveBufferSize"));
    }
    if (configuration.getIntegerWithDefault("vertx.sendBufferSize", -1) != -1) {
        options.setSendBufferSize(configuration.getInteger("vertx.sendBufferSize"));
    }

    http = vertx.createHttpServer(options).requestHandler(new HttpHandler(vertx, accessor, this))
            .websocketHandler(new WebSocketHandler(accessor, this));

    http.listen(thePort, host, event -> {
        if (event.succeeded()) {
            logger.info("Wisdom is going to serve HTTP requests on port {}.", thePort);
            port = thePort;
            completion.handle(Future.succeededFuture());
        } else if (port == 0) {
            logger.debug("Cannot bind on port {} (port already used probably)", thePort, event.cause());
            bind(0, completion);
        } else {
            logger.error("Cannot bind on port {} (port already used probably)", thePort, event.cause());
            completion.handle(Future.failedFuture("Cannot bind on port " + thePort));
        }
    });
}

From source file:suonos.httpserver.VertxHttpServer.java

License:Apache License

@Override
public void run() {
    HttpServerOptions opts = new HttpServerOptions();
    opts.setHost(host);/* w  ww . ja  v  a2  s . c o  m*/
    opts.setPort(port);
    opts.setCompressionSupported(true);

    staticFileHandler.setContextPath("/res");
    server = vertx.createHttpServer(opts);

    webApp.routes().addRouteHandler("/res/:path*", staticFileHandler);
    server.requestHandler(new VertxRequestHandler(vertx, webApp));
    server.listen();
}