Codota Logo
CorsHandler
Code IndexAdd Codota to your IDE (free)

How to use
CorsHandler
in
io.vertx.ext.web.handler

Best Java code snippets using io.vertx.ext.web.handler.CorsHandler (Showing top 20 results out of 315)

  • Common ways to obtain CorsHandler
private void myMethod () {
CorsHandler c =
  • Codota IconCorsHandler corsHandler;corsHandler.maxAgeSeconds(maxAgeSeconds)
  • Smart code suggestions by Codota
}
origin: vert-x3/vertx-web

@Test
public void testRealRequestAllowCredentials() throws Exception {
 Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));
 router.route().handler(CorsHandler.create("vertx\\.io").allowedMethods(allowedMethods).allowCredentials(true));
 router.route().handler(context -> context.response().end());
 testRequest(HttpMethod.GET, "/", req -> req.headers().add("origin", "vertx.io"), resp -> checkHeaders(resp, "vertx.io", null, null, null, "true", null), 200, "OK", null);
}
origin: vert-x3/vertx-web

@Test
public void testChaining() throws Exception {
 CorsHandler cors = CorsHandler.create("*");
 assertNotNull(cors);
 assertSame(cors, cors.allowedMethod(HttpMethod.POST));
 assertSame(cors, cors.allowedMethod(HttpMethod.DELETE));
 assertSame(cors, cors.allowedMethods(new HashSet<>()));
 assertSame(cors, cors.allowedHeader("X-foo"));
 assertSame(cors, cors.allowedHeader("X-bar"));
 assertSame(cors, cors.allowedHeaders(new HashSet<>()));
 assertSame(cors, cors.exposedHeader("X-wibble"));
 assertSame(cors, cors.exposedHeader("X-blah"));
 assertSame(cors, cors.exposedHeaders(new HashSet<>()));
}
origin: apache/servicecomb-java-chassis

/**
 * Support CORS
 */
void mountCorsHandler(Router mainRouter) {
 if (!TransportConfig.isCorsEnabled()) {
  return;
 }
 CorsHandler corsHandler = getCorsHandler(TransportConfig.getCorsAllowedOrigin());
 // Access-Control-Allow-Credentials
 corsHandler.allowCredentials(TransportConfig.isCorsAllowCredentials());
 // Access-Control-Allow-Headers
 corsHandler.allowedHeaders(TransportConfig.getCorsAllowedHeaders());
 // Access-Control-Allow-Methods
 Set<String> allowedMethods = TransportConfig.getCorsAllowedMethods();
 for (String method : allowedMethods) {
  corsHandler.allowedMethod(HttpMethod.valueOf(method));
 }
 // Access-Control-Expose-Headers
 corsHandler.exposedHeaders(TransportConfig.getCorsExposedHeaders());
 // Access-Control-Max-Age
 int maxAge = TransportConfig.getCorsMaxAge();
 if (maxAge >= 0) {
  corsHandler.maxAgeSeconds(maxAge);
 }
 LOGGER.info("mount CorsHandler");
 mainRouter.route().handler(corsHandler);
}
origin: vert-x3/vertx-examples

allowedMethods.add(HttpMethod.PUT);
router.route().handler(CorsHandler.create("*").allowedHeaders(allowedHeaders).allowedMethods(allowedMethods));
origin: vert-x3/vertx-web

@Test
public void testUnsecureCorsShouldNotBeAllowed() throws Exception {
 try {
  CorsHandler.create("*").allowCredentials(true);
  fail("Should not be allowed!");
 } catch (IllegalStateException e) {
  // OK
 }
}
origin: georocket/georocket

String allowedOrigin = config().getString(
 ConfigConstants.HTTP_CORS_ALLOW_ORIGIN, "$."); // match nothing by default
CorsHandler corsHandler = CorsHandler.create(allowedOrigin);
 corsHandler.allowCredentials(true);
 corsHandler.allowedHeader((String)allowHeaders);
} else if (allowHeaders instanceof JsonArray) {
 corsHandler.allowedHeaders(Seq.seq((JsonArray)allowHeaders)
  .cast(String.class).toSet());
} else if (allowHeaders != null) {
 corsHandler.allowedMethod(HttpMethod.valueOf((String)allowMethods));
} else if (allowMethods instanceof JsonArray) {
 corsHandler.allowedMethods(Seq.seq((JsonArray)allowMethods)
  .cast(String.class).map(HttpMethod::valueOf).toSet());
} else if (allowMethods != null) {
 corsHandler.exposedHeader((String)exposeHeaders);
} else if (exposeHeaders instanceof JsonArray) {
 corsHandler.exposedHeaders(Seq.seq((JsonArray)exposeHeaders)
  .cast(String.class).toSet());
} else if (exposeHeaders != null) {
corsHandler.maxAgeSeconds(maxAge);
origin: apache/servicecomb-java-chassis

private CorsHandler getCorsHandler(String corsAllowedOrigin) {
 return CorsHandler.create(corsAllowedOrigin);
}
origin: zandero/rest.vertx

/**
 * Enables CORS
 *
 * @param allowedOriginPattern allowed origin
 * @param allowCredentials     allow credentials (true/false)
 * @param maxAge               in seconds
 * @param allowedHeaders       set of allowed headers
 * @param methods              list of methods ... if empty all methods are allowed  @return self
 * @return self
 */
public RestBuilder enableCors(String allowedOriginPattern,
               boolean allowCredentials,
               int maxAge,
               Set<String> allowedHeaders,
               HttpMethod... methods) {
  corsHandler = CorsHandler.create(allowedOriginPattern)
               .allowCredentials(allowCredentials)
               .maxAgeSeconds(maxAge);
  if (methods == null || methods.length == 0) { // if not given than all
    methods = HttpMethod.values();
  }
  for (HttpMethod method : methods) {
    corsHandler.allowedMethod(method);
  }
  if (allowedHeaders.size() > 0) {
    corsHandler.allowedHeaders(allowedHeaders);
  }
  return this;
}
origin: silentbalanceyh/vertx-zero

.handler(CorsHandler.create("*")
    .allowCredentials(false)
    .allowedHeaders(new HashSet<String>() {
    .allowedMethods(new HashSet<HttpMethod>() {
origin: folio-org/okapi

logger.debug("Setting up routes");
router.route().handler(CorsHandler.create("*")
    .allowedMethod(HttpMethod.PUT)
    .allowedMethod(HttpMethod.DELETE)
    .allowedMethod(HttpMethod.GET)
    .allowedMethod(HttpMethod.POST)
    .allowedHeader(HttpHeaders.CONTENT_TYPE.toString())
    .allowedHeader(XOkapiHeaders.TENANT)
    .allowedHeader(XOkapiHeaders.TOKEN)
    .allowedHeader(XOkapiHeaders.AUTHORIZATION)
 .allowedHeader(XOkapiHeaders.REQUEST_ID)            //expose response headers
    .exposedHeader(HttpHeaders.LOCATION.toString())
    .exposedHeader(XOkapiHeaders.TRACE)
    .exposedHeader(XOkapiHeaders.TOKEN)
    .exposedHeader(XOkapiHeaders.AUTHORIZATION)
 .exposedHeader(XOkapiHeaders.REQUEST_ID)
);
origin: io.gravitee.am.gateway.handlers/gravitee-am-gateway-handler

private io.vertx.ext.web.handler.CorsHandler corsHandler() {
  return io.vertx.ext.web.handler.CorsHandler
      .create(environment.getProperty("http.cors.allow-origin", String.class, "*"))
      .allowedHeaders(getStringPropertiesAsList("http.cors.allow-headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With, If-Match, x-xsrf-token"))
      .allowedMethods(getHttpMethodPropertiesAsList("http.cors.allow-methods", "GET, POST"))
      .maxAgeSeconds(environment.getProperty("http.cors.max-age", Integer.class, 86400));
}
origin: org.eclipse.hono/hono-adapter-http-vertx

private void addTelemetryApiRoutes(final Router router, final Handler<RoutingContext> authHandler) {
  router.routeWithRegex("\\/telemetry\\/[^\\/]+\\/.*").handler(CorsHandler.create(getConfig().getCorsAllowedOrigin())
      .allowedMethod(HttpMethod.PUT)
      .allowedHeader(Constants.HEADER_QOS_LEVEL)
      .allowedHeader(Constants.HEADER_TIME_TIL_DISCONNECT)
      .allowedHeader(HttpHeaders.AUTHORIZATION.toString())
      .allowedHeader(HttpHeaders.CONTENT_TYPE.toString()));
    router.route("/telemetry").handler(CorsHandler.create(getConfig().getCorsAllowedOrigin())
        .allowedMethod(HttpMethod.POST)
        .allowedHeader(Constants.HEADER_QOS_LEVEL)
        .allowedHeader(Constants.HEADER_TIME_TIL_DISCONNECT)
        .allowedHeader(HttpHeaders.AUTHORIZATION.toString())
        .allowedHeader(HttpHeaders.CONTENT_TYPE.toString()));
origin: vert-x3/vertx-web

@Test
public void testPreflightMaxAge() throws Exception {
 Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));
 int maxAge = 131233;
 router.route().handler(CorsHandler.create("vertx\\.io").allowedMethods(allowedMethods).maxAgeSeconds(maxAge));
 router.route().handler(context -> context.response().end());
 testRequest(HttpMethod.OPTIONS, "/", req -> {
  req.headers().add("origin", "vertx.io");
  req.headers().add("access-control-request-method", "PUT,DELETE");
 }, resp -> checkHeaders(resp, "vertx.io", "PUT,DELETE", null, null, null, String.valueOf(maxAge)), 200, "OK", null);
}
origin: vert-x3/vertx-web

@Test
public void testPreflightNoExposeHeaders() throws Exception {
 Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));
 Set<String> exposeHeaders = new LinkedHashSet<>(Arrays.asList("X-floob", "X-blurp"));
 router.route().handler(CorsHandler.create("vertx\\.io").allowedMethods(allowedMethods).exposedHeaders(exposeHeaders));
 router.route().handler(context -> context.response().end());
 testRequest(HttpMethod.OPTIONS, "/", req -> {
  req.headers().add("origin", "vertx.io");
  req.headers().add("access-control-request-method", "PUT,DELETE");
 }, resp -> {
  // Note expose headers header is never provided in response of pre-flight request
  checkHeaders(resp, "vertx.io", "PUT,DELETE", null, null);
 }, 200, "OK", null);
}
origin: vert-x3/vertx-web

@Test
public void testPreflightSimple() throws Exception {
 Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));
 router.route().handler(CorsHandler.create("vertx\\.io").allowedMethods(allowedMethods));
 router.route().handler(context -> context.response().end());
 testRequest(HttpMethod.OPTIONS, "/", req -> {
  req.headers().add("origin", "vertx.io");
  req.headers().add("access-control-request-method", "PUT,DELETE");
 }, resp -> checkHeaders(resp, "vertx.io", "PUT,DELETE", null, null), 200, "OK", null);
}
origin: io.vertx/vertx-rx-java

/**
 * Add a set of allowed headers
 * @param headerNames the allowed header names
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.ext.web.handler.CorsHandler allowedHeaders(Set<String> headerNames) { 
 delegate.allowedHeaders(headerNames);
 return this;
}
origin: vert-x3/vertx-rx

/**
 * Add an allowed method
 * @param method the method to add
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.ext.web.handler.CorsHandler allowedMethod(HttpMethod method) { 
 delegate.allowedMethod(method);
 return this;
}
origin: io.vertx/vertx-rx-java

/**
 * Set whether credentials are allowed. Note that user agents will block
 * requests that use a wildcard as origin and include credentials.
 *
 * From the MDN documentation you can read:
 *
 * <blockquote>
 * Important note: when responding to a credentialed request,
 * server must specify a domain, and cannot use wild carding.
 * </blockquote>
 * @param allow true if allowed
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.ext.web.handler.CorsHandler allowCredentials(boolean allow) { 
 delegate.allowCredentials(allow);
 return this;
}
origin: io.vertx/vertx-rx-java

/**
 * Set how long the browser should cache the information
 * @param maxAgeSeconds max age in seconds
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.ext.web.handler.CorsHandler maxAgeSeconds(int maxAgeSeconds) { 
 delegate.maxAgeSeconds(maxAgeSeconds);
 return this;
}
origin: vert-x3/vertx-rx

/**
 * Add an exposed header
 * @param headerName the exposed header name
 * @return a reference to this, so the API can be used fluently
 */
public io.vertx.rxjava.ext.web.handler.CorsHandler exposedHeader(String headerName) { 
 delegate.exposedHeader(headerName);
 return this;
}
io.vertx.ext.web.handlerCorsHandler

Javadoc

A handler which implements server side http://www.w3.org/TR/cors/[CORS] support for Vert.x-Web.

Most used methods

  • create
    Create a CORS handler
  • allowedHeaders
    Add a set of allowed headers
  • allowCredentials
    Set whether credentials are allowed. Note that user agents will block requests that use a wildcard a
  • allowedMethod
    Add an allowed method
  • allowedMethods
    Add a set of allowed methods
  • maxAgeSeconds
    Set how long the browser should cache the information
  • allowedHeader
    Add an allowed header
  • exposedHeader
    Add an exposed header
  • exposedHeaders
    Add a set of exposed headers
  • handle

Popular in Java

  • Finding current android device location
  • getApplicationContext (Context)
  • findViewById (Activity)
  • scheduleAtFixedRate (ScheduledExecutorService)
    Creates and executes a periodic action that becomes enabled first after the given initial delay, and
  • FileNotFoundException (java.io)
    Thrown when a file specified by a program cannot be found.
  • InputStream (java.io)
    A readable source of bytes.Most clients will use input streams that read data from the file system (
  • BigInteger (java.math)
    Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in
  • KeyStore (java.security)
    This class represents an in-memory collection of keys and certificates. It manages two types of entr
  • Stream (java.util.stream)
    A sequence of elements supporting sequential and parallel aggregate operations. The following exampl
  • JFileChooser (javax.swing)
Codota Logo
  • Products

    Search for Java codeSearch for JavaScript codeEnterprise
  • IDE Plugins

    IntelliJ IDEAWebStormAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogCodota Academy Plugin user guide Terms of usePrivacy policyJava Code IndexJavascript Code Index
Get Codota for your IDE now