Search in sources :

Example 1 with ServletModule

use of io.crnk.servlet.internal.ServletModule in project crnk-framework by crnk-project.

the class ServletModuleTest method testName.

@Test
public void testName() {
    HttpRequestContextProvider provider = new HttpRequestContextProvider();
    ServletModule module = new ServletModule(provider);
    Assert.assertEquals("servlet", module.getModuleName());
}
Also used : ServletModule(io.crnk.servlet.internal.ServletModule) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider) Test(org.junit.Test)

Example 2 with ServletModule

use of io.crnk.servlet.internal.ServletModule in project crnk-framework by crnk-project.

the class CrnkConfigV3 method crnkBoot.

@Bean
@ConditionalOnMissingBean(CrnkBoot.class)
public CrnkBoot crnkBoot(ServiceDiscovery serviceDiscovery) {
    CrnkBoot boot = new CrnkBoot();
    boot.setObjectMapper(objectMapper);
    if (properties.getDomainName() != null && properties.getPathPrefix() != null) {
        String baseUrl = properties.getDomainName() + properties.getPathPrefix();
        boot.setServiceUrlProvider(new ConstantServiceUrlProvider(baseUrl));
    }
    boot.setServiceDiscovery(serviceDiscovery);
    boot.setDefaultPageLimit(properties.getDefaultPageLimit());
    boot.setMaxPageLimit(properties.getMaxPageLimit());
    boot.setPropertiesProvider(new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (CrnkProperties.RESOURCE_SEARCH_PACKAGE.equals(key)) {
                return properties.getResourcePackage();
            }
            if (CrnkProperties.RESOURCE_DEFAULT_DOMAIN.equals(key)) {
                return properties.getDomainName();
            }
            if (CrnkProperties.WEB_PATH_PREFIX.equals(key)) {
                return properties.getPathPrefix();
            }
            if (CrnkProperties.ALLOW_UNKNOWN_ATTRIBUTES.equals(key)) {
                return String.valueOf(properties.getAllowUnknownAttributes());
            }
            if (CrnkProperties.ALLOW_UNKNOWN_PARAMETERS.equals(key)) {
                return String.valueOf(properties.getAllowUnknownParameters());
            }
            if (CrnkProperties.RETURN_404_ON_NULL.equals(key)) {
                return String.valueOf(properties.getReturn404OnNull());
            }
            return applicationContext.getEnvironment().getProperty(key);
        }
    });
    boot.setAllowUnknownAttributes();
    boot.addModule(new ServletModule(boot.getModuleRegistry().getHttpRequestContextProvider()));
    boot.boot();
    return boot;
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) CrnkBoot(io.crnk.core.boot.CrnkBoot) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) ServletModule(io.crnk.servlet.internal.ServletModule) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with ServletModule

use of io.crnk.servlet.internal.ServletModule in project crnk-framework by crnk-project.

the class CrnkFilter method init.

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    boot = new CrnkBoot();
    boot.setPropertiesProvider(new FilterPropertiesProvider(filterConfig));
    HttpRequestContextProvider provider = boot.getModuleRegistry().getHttpRequestContextProvider();
    boot.addModule(new ServletModule(provider));
    initCrnk(boot);
    boot.boot();
}
Also used : FilterPropertiesProvider(io.crnk.servlet.internal.FilterPropertiesProvider) CrnkBoot(io.crnk.core.boot.CrnkBoot) ServletModule(io.crnk.servlet.internal.ServletModule) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider)

Example 4 with ServletModule

use of io.crnk.servlet.internal.ServletModule in project crnk-framework by crnk-project.

the class CrnkServlet method init.

@Override
public void init() throws ServletException {
    boot.setPropertiesProvider(new ServletPropertiesProvider(getServletConfig()));
    acceptPlainJson = !Boolean.parseBoolean(boot.getPropertiesProvider().getProperty(CrnkProperties.REJECT_PLAIN_JSON));
    HttpRequestContextProvider provider = boot.getModuleRegistry().getHttpRequestContextProvider();
    boot.addModule(new ServletModule(provider));
    initCrnk(boot);
    boot.boot();
}
Also used : ServletPropertiesProvider(io.crnk.servlet.internal.ServletPropertiesProvider) ServletModule(io.crnk.servlet.internal.ServletModule) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider)

Example 5 with ServletModule

use of io.crnk.servlet.internal.ServletModule in project crnk-framework by crnk-project.

the class ServletModuleTest method testSecurityProviderInstalled.

@Test
public void testSecurityProviderInstalled() {
    HttpRequestContextProvider provider = new HttpRequestContextProvider();
    ServletModule module = new ServletModule(provider);
    CrnkBoot boot = new CrnkBoot();
    boot.addModule(module);
    boot.boot();
    SecurityProvider securityProvider = boot.getModuleRegistry().getSecurityProvider();
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.addUserRole("guest");
    request.addUserRole("admin");
    provider.onRequestStarted(new HttpRequestContextBaseAdapter(new ServletRequestContext(servletContext, request, response, "api", HttpHeaders.DEFAULT_CHARSET)));
    Assert.assertFalse(securityProvider.isUserInRole("doesNotExist"));
    Assert.assertTrue(securityProvider.isUserInRole("guest"));
    Assert.assertTrue(securityProvider.isUserInRole("admin"));
}
Also used : HttpRequestContextBaseAdapter(io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter) CrnkBoot(io.crnk.core.boot.CrnkBoot) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SecurityProvider(io.crnk.core.engine.security.SecurityProvider) ServletContext(javax.servlet.ServletContext) ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) ServletModule(io.crnk.servlet.internal.ServletModule) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

ServletModule (io.crnk.servlet.internal.ServletModule)5 HttpRequestContextProvider (io.crnk.core.engine.http.HttpRequestContextProvider)4 CrnkBoot (io.crnk.core.boot.CrnkBoot)3 Test (org.junit.Test)2 HttpRequestContextBaseAdapter (io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter)1 PropertiesProvider (io.crnk.core.engine.properties.PropertiesProvider)1 SecurityProvider (io.crnk.core.engine.security.SecurityProvider)1 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)1 FilterPropertiesProvider (io.crnk.servlet.internal.FilterPropertiesProvider)1 ServletPropertiesProvider (io.crnk.servlet.internal.ServletPropertiesProvider)1 ServletRequestContext (io.crnk.servlet.internal.ServletRequestContext)1 ServletContext (javax.servlet.ServletContext)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1