Search in sources :

Example 1 with JavalinJackson

use of io.javalin.plugin.json.JavalinJackson in project cwms-radar-api by USACE.

the class CatalogControllerTest method catalog_returns_only_original_ids_by_default.

@Test
public void catalog_returns_only_original_ids_by_default() throws Exception {
    CatalogController controller = new CatalogController(new MetricRegistry());
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = new TestHttpServletResponse();
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(ContextUtil.maxRequestSizeKey, Integer.MAX_VALUE);
    attributes.put(JsonMapperKt.JSON_MAPPER_KEY, new JavalinJackson());
    attributes.put("PolicyFactory", this.sanitizer);
    when(request.getInputStream()).thenReturn(new TestServletInputStream(""));
    when(request.getAttribute("database")).thenReturn(this.conn);
    when(request.getRequestURI()).thenReturn("/catalog/TIMESERIES");
    when(request.getHeader(Header.ACCEPT)).thenReturn("application/json;version=2");
    Context context = ContextUtil.init(request, response, "*", new HashMap<String, String>(), HandlerType.GET, attributes);
    context.attribute("database", this.conn);
    controller.getOne(context, CatalogableEndpoint.TIMESERIES.getValue());
    assertEquals(HttpCode.OK.getStatus(), response.getStatus(), "200 OK was not returned");
    assertNotNull(response.getOutputStream(), "Output stream wasn't created");
}
Also used : Context(io.javalin.http.Context) HashMap(java.util.HashMap) MetricRegistry(com.codahale.metrics.MetricRegistry) TestHttpServletResponse(fixtures.TestHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) TestHttpServletResponse(fixtures.TestHttpServletResponse) TestServletInputStream(fixtures.TestServletInputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) JavalinJackson(io.javalin.plugin.json.JavalinJackson) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with JavalinJackson

use of io.javalin.plugin.json.JavalinJackson in project javalin by tipsy.

the class ExampleController method main.

public static void main(String[] args) {
    ObjectMapper objectMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
    OpenApiOptions openApiOptions = new OpenApiOptions(new Info().version("1.0").description("My Application")).activateAnnotationScanningFor("io.javalin.examples").path("/swagger-docs").swagger(new SwaggerOptions("/swagger").title("My Swagger Documentation")).reDoc(new ReDocOptions("/redoc", new RedocOptionsObject.Builder().setHideDownloadButton(true).setTheme(new RedocOptionsTheme.Builder().setSpacingUnit(10).setTypographyOptimizeSpeed(true).build()).build()).title("My ReDoc Documentation"));
    Javalin app = Javalin.create(config -> {
        config.registerPlugin(new OpenApiPlugin(openApiOptions));
        config.jsonMapper(new JavalinJackson(objectMapper));
    }).start(7070);
    app.post("/users", ExampleController::create);
}
Also used : Javalin(io.javalin.Javalin) HttpMethod(io.javalin.plugin.openapi.annotations.HttpMethod) OpenApiPlugin(io.javalin.plugin.openapi.OpenApiPlugin) ReDocOptions(io.javalin.plugin.openapi.ui.ReDocOptions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RedocOptionsObject(io.javalin.plugin.openapi.ui.RedocOptionsObject) Info(io.swagger.v3.oas.models.info.Info) OpenApiContent(io.javalin.plugin.openapi.annotations.OpenApiContent) Javalin(io.javalin.Javalin) OpenApiOptions(io.javalin.plugin.openapi.OpenApiOptions) JavalinJackson(io.javalin.plugin.json.JavalinJackson) RedocOptionsTheme(io.javalin.plugin.openapi.ui.RedocOptionsTheme) OpenApiParam(io.javalin.plugin.openapi.annotations.OpenApiParam) Context(io.javalin.http.Context) OpenApiResponse(io.javalin.plugin.openapi.annotations.OpenApiResponse) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) SwaggerOptions(io.javalin.plugin.openapi.ui.SwaggerOptions) OpenApi(io.javalin.plugin.openapi.annotations.OpenApi) OpenApiOptions(io.javalin.plugin.openapi.OpenApiOptions) SwaggerOptions(io.javalin.plugin.openapi.ui.SwaggerOptions) JavalinJackson(io.javalin.plugin.json.JavalinJackson) ReDocOptions(io.javalin.plugin.openapi.ui.ReDocOptions) Info(io.swagger.v3.oas.models.info.Info) OpenApiPlugin(io.javalin.plugin.openapi.OpenApiPlugin) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with JavalinJackson

use of io.javalin.plugin.json.JavalinJackson in project javalin by tipsy.

the class JavalinConfig method applyUserConfig.

public static void applyUserConfig(Javalin app, JavalinConfig config, Consumer<JavalinConfig> userConfig) {
    // apply user config to the default config
    userConfig.accept(config);
    AtomicBoolean anyHandlerAdded = new AtomicBoolean(false);
    app.events(listener -> {
        listener.handlerAdded(x -> anyHandlerAdded.set(true));
        listener.wsHandlerAdded(x -> anyHandlerAdded.set(true));
    });
    config.getPluginsExtending(PluginLifecycleInit.class).forEach(plugin -> {
        plugin.init(app);
        if (anyHandlerAdded.get()) {
            // check if any "init" added a handler
            throw new PluginInitLifecycleViolationException(((Plugin) plugin).getClass());
        }
    });
    config.inner.plugins.values().forEach(plugin -> plugin.apply(app));
    if (config.enforceSsl) {
        app.before(SecurityUtil::sslRedirect);
    }
    config.inner.appAttributes.putIfAbsent(JSON_MAPPER_KEY, new JavalinJackson());
    app.attribute(maxRequestSizeKey, config.maxRequestSize);
    config.inner.appAttributes.putIfAbsent(CONTEXT_RESOLVER_KEY, new ContextResolver());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PluginInitLifecycleViolationException(io.javalin.core.plugin.PluginInitLifecycleViolationException) JavalinJackson(io.javalin.plugin.json.JavalinJackson) PluginLifecycleInit(io.javalin.core.plugin.PluginLifecycleInit) SecurityUtil(io.javalin.core.security.SecurityUtil) ContextResolver(io.javalin.http.ContextResolver)

Example 4 with JavalinJackson

use of io.javalin.plugin.json.JavalinJackson in project emfcloud-modelserver by eclipse-emfcloud.

the class ProviderDefaults method provideJavalin.

public static Javalin provideJavalin() {
    return Javalin.create(config -> {
        config.enableCorsForAllOrigins();
        config.requestLogger((ctx, ms) -> {
            String requestPath = ctx.path() + (ctx.queryString() == null ? "" : "?" + ctx.queryString());
            LOG.info(ctx.method() + " " + requestPath + " -> Status: " + ctx.status() + " (took " + ms + " ms)");
        });
        config.asyncRequestTimeout = 5000L;
        config.jsonMapper(new JavalinJackson(ProviderDefaults.provideObjectMapper()));
        config.wsLogger(ws -> {
            ws.onConnect(ctx -> LOG.info("WS Connected: " + ctx.getSessionId()));
            ws.onMessage(ctx -> LOG.info("WS Received: " + ctx.message() + " by " + ctx.getSessionId()));
            ws.onClose(ctx -> LOG.info("WS Closed: " + ctx.getSessionId()));
            ws.onError(ctx -> LOG.info("WS Errored: " + ctx.getSessionId()));
        });
        if (ProviderDefaults.isDevLoggingEnabled) {
            config.enableDevLogging();
        }
    });
}
Also used : JavalinJackson(io.javalin.plugin.json.JavalinJackson)

Example 5 with JavalinJackson

use of io.javalin.plugin.json.JavalinJackson in project okaeri-platform by OkaeriPoland.

the class JavalinSetupTask method execute.

@Override
public void execute(OkaeriWebApplication platform) {
    // basic setup
    JavalinConfig.applyUserConfig(platform.getJavalin(), platform.getJavalin()._conf, config -> {
        // simple properties
        config.showJavalinBanner = false;
        // json mapper
        JsonMapper jsonMapper = new JsonMapper();
        jsonMapper.addMixIn(Document.class, DocumentMixIn.class);
        jsonMapper.addMixIn(OkaeriConfig.class, OkaeriConfigMixIn.class);
        config.jsonMapper(new JavalinJackson(jsonMapper));
        // access manager
        config.accessManager(platform.getInjector().getExact("accessManager", AccessManager.class).orElse(new FallbackAccessManager()));
    });
    // custom setup routine
    platform.getInjector().getExact("javalinConfigurer", Consumer.class).ifPresent(configurer -> {
        @SuppressWarnings("unchecked") Consumer<JavalinConfig> javalinConfigurer = (Consumer<JavalinConfig>) configurer;
        JavalinConfig.applyUserConfig(platform.getJavalin(), platform.getJavalin()._conf, javalinConfigurer);
    });
}
Also used : JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) FallbackAccessManager(eu.okaeri.platform.web.meta.role.FallbackAccessManager) Consumer(java.util.function.Consumer) JavalinJackson(io.javalin.plugin.json.JavalinJackson) JavalinConfig(io.javalin.core.JavalinConfig)

Aggregations

JavalinJackson (io.javalin.plugin.json.JavalinJackson)8 Context (io.javalin.http.Context)5 MetricRegistry (com.codahale.metrics.MetricRegistry)4 TestServletInputStream (fixtures.TestServletInputStream)4 HashMap (java.util.HashMap)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 Test (org.junit.jupiter.api.Test)4 TestHttpServletResponse (fixtures.TestHttpServletResponse)2 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonMapper (com.fasterxml.jackson.databind.json.JsonMapper)1 ClobController (cwms.radar.api.ClobController)1 RatingController (cwms.radar.api.RatingController)1 DaoTest (cwms.radar.data.dao.DaoTest)1 JsonRatingUtilsTest (cwms.radar.data.dao.JsonRatingUtilsTest)1 FallbackAccessManager (eu.okaeri.platform.web.meta.role.FallbackAccessManager)1 Javalin (io.javalin.Javalin)1 JavalinConfig (io.javalin.core.JavalinConfig)1 PluginInitLifecycleViolationException (io.javalin.core.plugin.PluginInitLifecycleViolationException)1