Java Code Examples for io.vertx.ext.web.handler.BodyHandler#create()

The following examples show how to use io.vertx.ext.web.handler.BodyHandler#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestRouterTest.java    From rest.vertx with Apache License 2.0 6 votes vote down vote up
@BeforeAll
static void start() {

    before();

    TestRest testRest = new TestRest();

    BodyHandler bodyHandler = BodyHandler.create("my_upload_folder");
    RestRouter.setBodyHandler(bodyHandler);

    Router router = RestRouter.register(vertx,
                                  testRest,
                                  TestRestWithNonRestMethod.class);

    vertx.createHttpServer()
        .requestHandler(router)
        .listen(PORT, vertxTestContext.succeeding());
}
 
Example 2
Source File: RestBuilderTest.java    From rest.vertx with Apache License 2.0 6 votes vote down vote up
@Test
void buildInterfaceTest() {

    BodyHandler handler = BodyHandler.create("my_upload_folder");

    new RestBuilder(vertx)
        .bodyHandler(handler)
        .register(TestRest.class, TestRegExRest.class, TestPostRest.class)
        .reader(Dummy.class, DummyBodyReader.class)
        .writer(MediaType.APPLICATION_JSON, TestCustomWriter.class)
        .errorHandler(IllegalArgumentExceptionHandler.class)
        .errorHandler(MyExceptionHandler.class)
        .provide(request -> new Dummy("test", "name"))
        .addProvider(TokenProvider.class)
        .build();
}
 
Example 3
Source File: DelegatingCredentialsManagementHttpEndpoint.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void addRoutes(final Router router) {

    final String pathWithTenantAndDeviceId = String.format("/%s/:%s/:%s",
            getName(), PARAM_TENANT_ID, PARAM_DEVICE_ID);


    // Add CORS handler
    router.route(pathWithTenantAndDeviceId).handler(createCorsHandler(config.getCorsAllowedOrigin(), EnumSet.of(HttpMethod.GET, HttpMethod.PUT)));

    final BodyHandler bodyHandler = BodyHandler.create();
    bodyHandler.setBodyLimit(config.getMaxPayloadSize());

    // get all credentials for a given device
    router.get(pathWithTenantAndDeviceId).handler(this::getCredentialsForDevice);

    // set credentials for a given device
    router.put(pathWithTenantAndDeviceId).handler(bodyHandler);
    router.put(pathWithTenantAndDeviceId).handler(this::extractRequiredJsonArrayPayload);
    router.put(pathWithTenantAndDeviceId).handler(this::extractIfMatchVersionParam);
    router.put(pathWithTenantAndDeviceId).handler(this::updateCredentials);
}
 
Example 4
Source File: BaseRouterFactory.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
public BaseRouterFactory(Vertx vertx, Specification spec, RouterFactoryOptions options) {
  this.vertx = vertx;
  this.spec = spec;
  this.options = options;

  this.validationFailureHandler = DEFAULT_VALIDATION_FAILURE_HANDLER;
  this.notImplementedFailureHandler = DEFAULT_NOT_IMPLEMENTED_HANDLER;
  this.bodyHandler = BodyHandler.create();
  this.globalHandlers = new ArrayList<>();
  this.extraOperationContextPayloadMapper = DEFAULT_EXTRA_OPERATION_CONTEXT_PAYLOAD_MAPPER;
}
 
Example 5
Source File: VertxConfiguration.java    From prebid-server-java with Apache License 2.0 4 votes vote down vote up
@Bean
BodyHandler bodyHandler(@Value("${vertx.uploads-dir}") String uploadsDir) {
    return BodyHandler.create(uploadsDir);
}
 
Example 6
Source File: VertxHttpRecorder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public Handler<RoutingContext> createBodyHandler(HttpConfiguration httpConfiguration) {
    BodyHandler bodyHandler = BodyHandler.create();
    Optional<MemorySize> maxBodySize = httpConfiguration.limits.maxBodySize;
    if (maxBodySize.isPresent()) {
        bodyHandler.setBodyLimit(maxBodySize.get().asLongValue());
    }
    final BodyConfig bodyConfig = httpConfiguration.body;
    bodyHandler.setHandleFileUploads(bodyConfig.handleFileUploads);
    bodyHandler.setUploadsDirectory(bodyConfig.uploadsDirectory);
    bodyHandler.setDeleteUploadedFilesOnEnd(bodyConfig.deleteUploadedFilesOnEnd);
    bodyHandler.setMergeFormAttributes(bodyConfig.mergeFormAttributes);
    bodyHandler.setPreallocateBodyBuffer(bodyConfig.preallocateBodyBuffer);
    return new Handler<RoutingContext>() {
        @Override
        public void handle(RoutingContext event) {
            if (!Context.isOnEventLoopThread()) {
                ((ConnectionBase) event.request().connection()).channel().eventLoop().execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //this can happen if blocking authentication is involved for get requests
                            if (!event.request().isEnded()) {
                                event.request().resume();
                                if (CAN_HAVE_BODY.contains(event.request().method())) {
                                    bodyHandler.handle(event);
                                } else {
                                    event.next();
                                }
                            } else {
                                event.next();
                            }
                        } catch (Throwable t) {
                            event.fail(t);
                        }
                    }
                });
            } else {
                event.request().resume();
                if (CAN_HAVE_BODY.contains(event.request().method())) {
                    bodyHandler.handle(event);
                } else {
                    event.next();
                }
            }
        }
    };
}
 
Example 7
Source File: DelegatingTenantManagementHttpEndpoint.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void addRoutes(final Router router) {

    final String path = String.format("/%s", getName());
    final String pathWithTenant = String.format("/%s/:%s", getName(), PARAM_TENANT_ID);

    // Add CORS handler
    router.route(path).handler(createCorsHandler(config.getCorsAllowedOrigin(), EnumSet.of(HttpMethod.POST)));
    router.route(pathWithTenant).handler(createDefaultCorsHandler(config.getCorsAllowedOrigin()));

    final BodyHandler bodyHandler = BodyHandler.create();
    bodyHandler.setBodyLimit(config.getMaxPayloadSize());

    // ADD tenant with auto-generated ID
    router.post(path)
            .handler(bodyHandler)
            .handler(this::extractOptionalJsonPayload)
            .handler(this::createTenant);

    // ADD tenant
    router.post(pathWithTenant)
            .handler(bodyHandler)
            .handler(this::extractOptionalJsonPayload)
            .handler(this::createTenant);

    // GET tenant
    router.get(pathWithTenant)
            .handler(this::getTenant);

    // UPDATE tenant
    router.put(pathWithTenant)
            .handler(bodyHandler)
            .handler(this::extractRequiredJsonPayload)
            .handler(this::extractIfMatchVersionParam)
            .handler(this::updateTenant);

    // REMOVE tenant
    router.delete(pathWithTenant)
            .handler(this::extractIfMatchVersionParam)
            .handler(this::deleteTenant);
}
 
Example 8
Source File: SessionRouterConfig.java    From vxms with Apache License 2.0 4 votes vote down vote up
public BodyHandler bodyHandler() {
  return BodyHandler.create();
}
 
Example 9
Source File: StaticContentRouterConfig.java    From vxms with Apache License 2.0 4 votes vote down vote up
public BodyHandler bodyHandler() {
  return BodyHandler.create();
}
 
Example 10
Source File: RestrictedCorsRouterConfig.java    From vxms with Apache License 2.0 4 votes vote down vote up
public BodyHandler bodyHandler() {
  return BodyHandler.create();
}
 
Example 11
Source File: RestrictedCorsRouterConfig2.java    From vxms with Apache License 2.0 4 votes vote down vote up
public BodyHandler bodyHandler() {
  return BodyHandler.create();
}
 
Example 12
Source File: RestrictedCorsRouterConfig3.java    From vxms with Apache License 2.0 4 votes vote down vote up
public BodyHandler bodyHandler() {
  return BodyHandler.create();
}
 
Example 13
Source File: OpenAPI3RouterFactoryImpl.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
public OpenAPI3RouterFactoryImpl(Vertx vertx, OpenAPIHolderImpl spec, OpenAPILoaderOptions options) {
  this.vertx = vertx;
  this.openapi = spec;
  this.options = new RouterFactoryOptions();
  this.bodyHandler = BodyHandler.create();
  this.globalHandlers = new ArrayList<>();
  this.schemaRouter = SchemaRouter.create(vertx, options.toSchemaRouterOptions());
  this.schemaParser = OpenAPI3SchemaParser.create(schemaRouter);
  this.validationHandlerGenerator = new OpenAPI3ValidationHandlerGenerator(spec, schemaParser);

  spec.getAbsolutePaths().forEach((u, jo) -> schemaRouter.addJson(u, jo));

  // Load default generators
  this.validationHandlerGenerator
    .addParameterProcessorGenerator(new DeepObjectParameterProcessorGenerator())
    .addParameterProcessorGenerator(new ExplodedArrayParameterProcessorGenerator())
    .addParameterProcessorGenerator(new ExplodedMatrixArrayParameterProcessorGenerator())
    .addParameterProcessorGenerator(new ExplodedObjectParameterProcessorGenerator())
    .addParameterProcessorGenerator(new ExplodedSimpleObjectParameterProcessorGenerator())
    .addParameterProcessorGenerator(new JsonParameterProcessorGenerator())
    .addParameterProcessorGenerator(new DefaultParameterProcessorGenerator());

  this.validationHandlerGenerator
    .addBodyProcessorGenerator(new JsonBodyProcessorGenerator())
    .addBodyProcessorGenerator(new UrlEncodedFormBodyProcessorGenerator())
    .addBodyProcessorGenerator(new MultipartFormBodyProcessorGenerator());

  this.operations = new LinkedHashMap<>();
  this.securityHandlers = new AuthenticationHandlersStore();

  /* --- Initialization of operations --- */
  spec.solveIfNeeded(spec.getOpenAPI().getJsonObject("paths")).forEach(pathEntry -> {
    if (pathEntry.getKey().startsWith("x-")) return;
    JsonObject pathModel = spec.solveIfNeeded((JsonObject) pathEntry.getValue());
    Stream.of(
      "get", "put", "post", "delete", "options", "head", "patch", "trace"
    )
      .filter(pathModel::containsKey)
      .forEach(verb -> {
        JsonObject operationModel = spec.solveIfNeeded(pathModel.getJsonObject(verb));
        this.operations.put(
          operationModel.getString("operationId"),
          new OperationImpl(
            operationModel.getString("operationId"),
            HttpMethod.valueOf(verb.toUpperCase()),
            pathEntry.getKey(),
            operationModel,
            pathModel,
            spec.getInitialScope(),
            openapi
          )
        );
      });
  });
}