Search in sources :

Example 1 with DefaultServletConfig

use of io.undertow.servlet.api.DefaultServletConfig in project undertow by undertow-io.

the class ContentTypeFilesTestCase method setup.

@BeforeClass
public static void setup() throws ServletException {
    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();
    DeploymentInfo builder = new DeploymentInfo().setClassIntrospecter(TestClassIntrospector.INSTANCE).setClassLoader(ContentTypeFilesTestCase.class.getClassLoader()).setContextPath("/app").setDeploymentName("servletContext.war").setResourceManager(new TestResourceLoader(ContentTypeServlet.class)).setDefaultServletConfig(new DefaultServletConfig(true)).addMimeMapping(new MimeMapping("jnlp", "application/x-java-jnlp-file"));
    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();
    root.addPrefixPath(builder.getContextPath(), manager.start());
    DefaultServer.setRootHandler(root);
}
Also used : TestResourceLoader(io.undertow.servlet.test.util.TestResourceLoader) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ServletContainer(io.undertow.servlet.api.ServletContainer) PathHandler(io.undertow.server.handlers.PathHandler) DefaultServletConfig(io.undertow.servlet.api.DefaultServletConfig) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) MimeMapping(io.undertow.servlet.api.MimeMapping) BeforeClass(org.junit.BeforeClass)

Example 2 with DefaultServletConfig

use of io.undertow.servlet.api.DefaultServletConfig in project undertow by undertow-io.

the class DefaultServlet method init.

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    ServletContextImpl sc = (ServletContextImpl) config.getServletContext();
    this.deployment = sc.getDeployment();
    DefaultServletConfig defaultServletConfig = deployment.getDeploymentInfo().getDefaultServletConfig();
    if (defaultServletConfig != null) {
        defaultAllowed = defaultServletConfig.isDefaultAllowed();
        allowed = new HashSet<>();
        if (defaultServletConfig.getAllowed() != null) {
            allowed.addAll(defaultServletConfig.getAllowed());
        }
        disallowed = new HashSet<>();
        if (defaultServletConfig.getDisallowed() != null) {
            disallowed.addAll(defaultServletConfig.getDisallowed());
        }
    }
    if (config.getInitParameter(DEFAULT_ALLOWED) != null) {
        defaultAllowed = Boolean.parseBoolean(config.getInitParameter(DEFAULT_ALLOWED));
    }
    if (config.getInitParameter(ALLOWED_EXTENSIONS) != null) {
        String extensions = config.getInitParameter(ALLOWED_EXTENSIONS);
        allowed = new HashSet<>(Arrays.asList(extensions.split(",")));
    }
    if (config.getInitParameter(DISALLOWED_EXTENSIONS) != null) {
        String extensions = config.getInitParameter(DISALLOWED_EXTENSIONS);
        disallowed = new HashSet<>(Arrays.asList(extensions.split(",")));
    }
    if (config.getInitParameter(RESOLVE_AGAINST_CONTEXT_ROOT) != null) {
        resolveAgainstContextRoot = Boolean.parseBoolean(config.getInitParameter(RESOLVE_AGAINST_CONTEXT_ROOT));
    }
    if (config.getInitParameter(ALLOW_POST) != null) {
        allowPost = Boolean.parseBoolean(config.getInitParameter(ALLOW_POST));
    }
    if (deployment.getDeploymentInfo().getPreCompressedResources().isEmpty()) {
        this.resourceSupplier = new DefaultResourceSupplier(deployment.getDeploymentInfo().getResourceManager());
    } else {
        PreCompressedResourceSupplier preCompressedResourceSupplier = new PreCompressedResourceSupplier(deployment.getDeploymentInfo().getResourceManager());
        for (Map.Entry<String, String> entry : deployment.getDeploymentInfo().getPreCompressedResources().entrySet()) {
            preCompressedResourceSupplier.addEncoding(entry.getKey(), entry.getValue());
        }
        this.resourceSupplier = preCompressedResourceSupplier;
    }
    String listings = config.getInitParameter(DIRECTORY_LISTING);
    if (Boolean.valueOf(listings)) {
        this.directoryListingEnabled = true;
    }
}
Also used : ServletContextImpl(io.undertow.servlet.spec.ServletContextImpl) DefaultServletConfig(io.undertow.servlet.api.DefaultServletConfig) DefaultResourceSupplier(io.undertow.server.handlers.resource.DefaultResourceSupplier) PreCompressedResourceSupplier(io.undertow.server.handlers.resource.PreCompressedResourceSupplier) Map(java.util.Map)

Example 3 with DefaultServletConfig

use of io.undertow.servlet.api.DefaultServletConfig in project keycloak by keycloak.

the class KeycloakOnUndertow method createAuthServerDeploymentInfo.

private DeploymentInfo createAuthServerDeploymentInfo() {
    ResteasyDeployment deployment = new ResteasyDeployment();
    deployment.setApplicationClass(KeycloakApplication.class.getName());
    // RESTEASY-2034
    deployment.setProperty(ResteasyContextParameters.RESTEASY_DISABLE_HTML_SANITIZER, true);
    // Prevent double gzip encoding of resources
    deployment.getDisabledProviderClasses().add("org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor");
    DeploymentInfo di = undertow.undertowDeployment(deployment);
    di.setClassLoader(getClass().getClassLoader());
    di.setContextPath("/auth");
    di.setDeploymentName("Keycloak");
    if (configuration.getKeycloakConfigPropertyOverridesMap() != null) {
        try {
            di.addInitParameter(JsonConfigProviderFactory.SERVER_CONTEXT_CONFIG_PROPERTY_OVERRIDES, JsonSerialization.writeValueAsString(configuration.getKeycloakConfigPropertyOverridesMap()));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
    di.setDefaultServletConfig(new DefaultServletConfig(true));
    di.addWelcomePage("theme/keycloak/welcome/resources/index.html");
    // This is needed as in case of clustered undertow, several undertow instances share the same JVM, hence the default
    // way accessing the factory in the UndertowRequestFilter via static reference to KeycloakApplication does not work:
    // There are several KeycloakApplication instances in the JVM with no classloader separation as in a full-blown server.
    InstanceHandle<Filter> filterInstance = new InstanceHandle<Filter>() {

        @Override
        public Filter getInstance() {
            return new UndertowRequestFilter(sessionFactory);
        }

        @Override
        public void release() {
        }
    };
    FilterInfo filter = Servlets.filter("SessionFilter", UndertowRequestFilter.class, () -> filterInstance);
    di.addFilter(filter);
    di.addFilterUrlMapping("SessionFilter", "/*", DispatcherType.REQUEST);
    filter.setAsyncSupported(true);
    return di;
}
Also used : ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) UndertowRequestFilter(org.keycloak.testsuite.UndertowRequestFilter) KeycloakApplication(org.keycloak.services.resources.KeycloakApplication) Filter(javax.servlet.Filter) UndertowRequestFilter(org.keycloak.testsuite.UndertowRequestFilter) IOException(java.io.IOException) DefaultServletConfig(io.undertow.servlet.api.DefaultServletConfig) InstanceHandle(io.undertow.servlet.api.InstanceHandle) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) FilterInfo(io.undertow.servlet.api.FilterInfo)

Example 4 with DefaultServletConfig

use of io.undertow.servlet.api.DefaultServletConfig in project keycloak by keycloak.

the class KeycloakServer method start.

public void start() throws Throwable {
    long start = System.currentTimeMillis();
    ResteasyDeployment deployment = new ResteasyDeployment();
    deployment.setApplicationClass(KeycloakApplication.class.getName());
    Builder builder = Undertow.builder().addHttpListener(config.getPort(), config.getHost()).setWorkerThreads(config.getWorkerThreads()).setIoThreads(config.getWorkerThreads() / 8);
    if (config.getPortHttps() != -1) {
        builder = builder.addHttpsListener(config.getPortHttps(), config.getHost(), createSSLContext()).setSocketOption(Options.SSL_CLIENT_AUTH_MODE, SslClientAuthMode.REQUESTED);
    }
    server = new UndertowJaxrsServer();
    try {
        server.start(builder);
        DeploymentInfo di = server.undertowDeployment(deployment, "");
        di.setClassLoader(getClass().getClassLoader());
        di.setContextPath("/auth");
        di.setDeploymentName("Keycloak");
        di.setDefaultEncoding("UTF-8");
        di.setDefaultServletConfig(new DefaultServletConfig(true));
        // Note that the ResteasyServlet is configured via server.undertowDeployment(...);
        // KEYCLOAK-14178
        deployment.setProperty(ResteasyContextParameters.RESTEASY_DISABLE_HTML_SANITIZER, true);
        InstanceHandle<Filter> filterInstance = new InstanceHandle<Filter>() {

            @Override
            public Filter getInstance() {
                return new UndertowRequestFilter(sessionFactory);
            }

            @Override
            public void release() {
            }
        };
        FilterInfo filter = Servlets.filter("SessionFilter", UndertowRequestFilter.class, () -> filterInstance);
        filter.setAsyncSupported(true);
        di.addFilter(filter);
        di.addFilterUrlMapping("SessionFilter", "/*", DispatcherType.REQUEST);
        server.deploy(di);
        sessionFactory = KeycloakApplication.getSessionFactory();
        setupDevConfig();
        if (config.getResourcesHome() != null) {
            info("Loading resources from " + config.getResourcesHome());
        }
        info("Started Keycloak (http://" + config.getHost() + ":" + config.getPort() + "/auth" + (config.getPortHttps() > 0 ? ", https://" + config.getHost() + ":" + config.getPortHttps() + "/auth" : "") + ") in " + (System.currentTimeMillis() - start) + " ms\n");
    } catch (RuntimeException e) {
        server.stop();
        throw e;
    }
}
Also used : KeycloakApplication(org.keycloak.services.resources.KeycloakApplication) Builder(io.undertow.Undertow.Builder) ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) Filter(javax.servlet.Filter) DefaultServletConfig(io.undertow.servlet.api.DefaultServletConfig) InstanceHandle(io.undertow.servlet.api.InstanceHandle) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) FilterInfo(io.undertow.servlet.api.FilterInfo) UndertowJaxrsServer(org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer)

Aggregations

DefaultServletConfig (io.undertow.servlet.api.DefaultServletConfig)4 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)3 FilterInfo (io.undertow.servlet.api.FilterInfo)2 InstanceHandle (io.undertow.servlet.api.InstanceHandle)2 Filter (javax.servlet.Filter)2 ResteasyDeployment (org.jboss.resteasy.spi.ResteasyDeployment)2 KeycloakApplication (org.keycloak.services.resources.KeycloakApplication)2 Builder (io.undertow.Undertow.Builder)1 PathHandler (io.undertow.server.handlers.PathHandler)1 DefaultResourceSupplier (io.undertow.server.handlers.resource.DefaultResourceSupplier)1 PreCompressedResourceSupplier (io.undertow.server.handlers.resource.PreCompressedResourceSupplier)1 DeploymentManager (io.undertow.servlet.api.DeploymentManager)1 MimeMapping (io.undertow.servlet.api.MimeMapping)1 ServletContainer (io.undertow.servlet.api.ServletContainer)1 ServletContextImpl (io.undertow.servlet.spec.ServletContextImpl)1 TestResourceLoader (io.undertow.servlet.test.util.TestResourceLoader)1 IOException (java.io.IOException)1 Map (java.util.Map)1 UndertowJaxrsServer (org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer)1 BeforeClass (org.junit.BeforeClass)1