vertx/src/main/java/eu/internetofus/common/vertx/AbstractAPIVerticle.java
/**
   * Create the handler that allow any CORS connection.
   *
   * @return the handler that manage the CORS over the API.
   */
Create the handler that allow any CORS connection.
@return the handler that manage the CORS over the API.
Create the handler that allow any CORS connection . @return the handler that manage the CORS over the API .
protected Handler<RoutingContext> createCORSHandler() {
    final var corsConf = this.config().getJsonObject("api", new JsonObject()).getJsonObject("cors", new JsonObject());
    var handler = CorsHandler.create(corsConf.getString("origin", "*"));
    final var headers = corsConf.getJsonArray("headers",
        new JsonArray().add(AbstractServicesVerticle.WENET_COMPONENT_APIKEY_HEADER).add(HttpHeaders.CONTENT_TYPE)
            .add(HttpHeaders.CONTENT_LENGTH).add(HttpHeaders.CONTENT_RANGE).add(HttpHeaders.ACCEPT)
            .add(HttpHeaders.ACCEPT_RANGES).add(HttpHeaders.ORIGIN));
    for (var i = 0; i < headers.size(); i++) {
      final var headerName = headers.getString(i);
      if (headerName != null && headerName.length() > 0) {
        handler = handler.allowedHeader(headerName);
      }
    }
    final var methods = corsConf.getJsonArray("methods",
        new JsonArray().add(HttpMethod.GET.name()).add(HttpMethod.POST.name()).add(HttpMethod.PUT.name())
            .add(HttpMethod.PATCH.name()).add(HttpMethod.DELETE.name()).add(HttpMethod.OPTIONS.name())
            .add(HttpMethod.HEAD.name()));
    for (var i = 0; i < methods.size(); i++) {
      var methodName = methods.getString(i);
      if (methodName != null && methodName.length() > 0) {
        try {
          methodName = methodName.trim().toUpperCase();
          final var method = HttpMethod.valueOf(methodName);
          handler = handler.allowedMethod(method);
        } catch (final Throwable throwable) {
          Logger.error(throwable, "Cannot add {} as allowed method for CORS", methodName);
        }
      }
    }
    handler.allowCredentials(corsConf.getBoolean("credentials", true));
    handler.maxAgeSeconds(corsConf.getInteger("maxAgeSeconds", 1209600));
    return handler;
  }
protected Handler < RoutingContext > createCORSHandler ( ) { final var corsConf = this . config ( ) . getJsonObject ( "api" , new JsonObject ( ) ) . getJsonObject ( "cors" , new JsonObject ( ) ) ; var handler = CorsHandler . create ( corsConf . getString ( "origin" , "*" ) ) ; final var headers = corsConf . getJsonArray ( "headers" , new JsonArray ( ) . add ( AbstractServicesVerticle . WENET_COMPONENT_APIKEY_HEADER ) . add ( HttpHeaders . CONTENT_TYPE ) . add ( HttpHeaders . CONTENT_LENGTH ) . add ( HttpHeaders . CONTENT_RANGE ) . add ( HttpHeaders . ACCEPT ) . add ( HttpHeaders . ACCEPT_RANGES ) . add ( HttpHeaders . ORIGIN ) ) ; for ( var i = 0 ; i < headers . size ( ) ; i ++ ) { final var headerName = headers . getString ( i ) ; if ( headerName != null && headerName . length ( ) > 0 ) { handler = handler . allowedHeader ( headerName ) ; } } final var methods = corsConf . getJsonArray ( "methods" , new JsonArray ( ) . add ( HttpMethod . GET . name ( ) ) . add ( HttpMethod . POST . name ( ) ) . add ( HttpMethod . PUT . name ( ) ) . add ( HttpMethod . PATCH . name ( ) ) . add ( HttpMethod . DELETE . name ( ) ) . add ( HttpMethod . OPTIONS . name ( ) ) . add ( HttpMethod . HEAD . name ( ) ) ) ; for ( var i = 0 ; i < methods . size ( ) ; i ++ ) { var methodName = methods . getString ( i ) ; if ( methodName != null && methodName . length ( ) > 0 ) { try { methodName = methodName . trim ( ) . toUpperCase ( ) ; final var method = HttpMethod . valueOf ( methodName ) ; handler = handler . allowedMethod ( method ) ; } catch ( final Throwable throwable ) { Logger . error ( throwable , "Cannot add {} as allowed method for CORS" , methodName ) ; } } } handler . allowCredentials ( corsConf . getBoolean ( "credentials" , true ) ) ; handler . maxAgeSeconds ( corsConf . getInteger ( "maxAgeSeconds" , 1209600 ) ) ; return handler ; }
Create the handler that allow any CORS connection.
Create the handler that allow any CORS connection .