Example usage for javax.servlet DispatcherType INCLUDE

List of usage examples for javax.servlet DispatcherType INCLUDE

Introduction

In this page you can find the example usage for javax.servlet DispatcherType INCLUDE.

Prototype

DispatcherType INCLUDE

To view the source code for javax.servlet DispatcherType INCLUDE.

Click Source Link

Usage

From source file:http2.DispatcherServletInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    servletContext.addFilter("pcf", "org.eclipse.jetty.servlets.PushCacheFilter").addMappingForUrlPatterns(
            EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE), true, "/*");
    super.onStartup(servletContext);
}

From source file:io.undertow.servlet.test.dispatcher.DispatcherIncludeTestCase.java

@BeforeClass
public static void setup() throws ServletException {

    final PathHandler root = new PathHandler();
    final ServletContainer container = ServletContainer.Factory.newInstance();

    DeploymentInfo builder = new DeploymentInfo().setClassLoader(SimpleServletTestCase.class.getClassLoader())
            .setContextPath("/servletContext").setClassIntrospecter(TestClassIntrospector.INSTANCE)
            .setDeploymentName("servletContext.war")
            .setResourceManager(new TestResourceLoader(DispatcherIncludeTestCase.class))
            .addServlet(new ServletInfo("include", MessageServlet.class)
                    .addInitParam(MessageServlet.MESSAGE, "included").addMapping("/include"))
            .addServlet(new ServletInfo("dispatcher", IncludeServlet.class).addMapping("/dispatch"))
            .addServlet(new ServletInfo("pathTest", PathTestServlet.class).addMapping("/path"))
            .addFilter(new FilterInfo("notIncluded", MessageFilter.class).addInitParam(MessageFilter.MESSAGE,
                    "Not Included"))
            .addFilter(new FilterInfo("inc", MessageFilter.class).addInitParam(MessageFilter.MESSAGE, "Path!"))
            .addFilter(new FilterInfo("nameFilter", MessageFilter.class).addInitParam(MessageFilter.MESSAGE,
                    "Name!"))
            .addFilterUrlMapping("notIncluded", "/include", DispatcherType.REQUEST)
            .addFilterUrlMapping("inc", "/include", DispatcherType.INCLUDE)
            .addFilterServletNameMapping("nameFilter", "include", DispatcherType.INCLUDE);

    DeploymentManager manager = container.addDeployment(builder);
    manager.deploy();/*from   w ww  . j  av  a  2 s.co  m*/
    root.addPrefixPath(builder.getContextPath(), manager.start());

    DefaultServer.setRootHandler(root);
}

From source file:com.almende.eve.transport.http.embed.JettyLauncher.java

@Override
public void addFilter(final String filterpath, final String path) {
    LOG.info("Adding filter:" + filterpath + " / " + path);
    context.addFilter(filterpath, path, EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));
}

From source file:org.ireland.jnetty.dispatch.filter.FilterMapping.java

/**
 * True if the dispatcher is for INCLUDE.
 *//*from   w  w w  . j  a v  a 2s.c  om*/
public boolean isInclude() {
    return _dispatcherTypes != null && _dispatcherTypes.contains(DispatcherType.INCLUDE);
}

From source file:com.jsmartframework.web.manager.ContextControl.java

@Override
@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent event) {
    try {// w w w. j a va2  s  .  c  o  m
        ServletContext servletContext = event.getServletContext();

        CONFIG.init(servletContext);
        if (CONFIG.getContent() == null) {
            throw new RuntimeException("Configuration file " + Constants.WEB_CONFIG_XML
                    + " was not found in WEB-INF resources folder!");
        }

        String contextConfigLocation = "com.jsmartframework.web.manager";
        if (CONFIG.getContent().getPackageScan() != null) {
            contextConfigLocation += "," + CONFIG.getContent().getPackageScan();
        }

        // Configure necessary parameters in the ServletContext to set Spring configuration without needing an XML file
        AnnotationConfigWebApplicationContext configWebAppContext = new AnnotationConfigWebApplicationContext();
        configWebAppContext.setConfigLocation(contextConfigLocation);

        CONTEXT_LOADER = new ContextLoader(configWebAppContext);
        CONTEXT_LOADER.initWebApplicationContext(servletContext);

        TagEncrypter.init();
        TEXTS.init();
        IMAGES.init(servletContext);
        HANDLER.init(servletContext);

        // ServletControl -> @MultipartConfig @WebServlet(name = "ServletControl", displayName = "ServletControl", loadOnStartup = 1)
        Servlet servletControl = servletContext.createServlet(
                (Class<? extends Servlet>) Class.forName("com.jsmartframework.web.manager.ServletControl"));
        ServletRegistration.Dynamic servletControlReg = (ServletRegistration.Dynamic) servletContext
                .addServlet("ServletControl", servletControl);
        servletControlReg.setAsyncSupported(true);
        servletControlReg.setLoadOnStartup(1);

        // ServletControl Initial Parameters
        InitParam[] initParams = CONFIG.getContent().getInitParams();
        if (initParams != null) {
            for (InitParam initParam : initParams) {
                servletControlReg.setInitParameter(initParam.getName(), initParam.getValue());
            }
        }

        // MultiPart to allow file upload on ServletControl
        MultipartConfigElement multipartElement = getServletMultipartElement();
        if (multipartElement != null) {
            servletControlReg.setMultipartConfig(multipartElement);
        }

        // Security constraint to ServletControl
        ServletSecurityElement servletSecurityElement = getServletSecurityElement(servletContext);
        if (servletSecurityElement != null) {
            servletControlReg.setServletSecurity(servletSecurityElement);
        }

        // TODO: Fix problem related to authentication by container to use SSL dynamically (Maybe create more than one servlet for secure and non-secure patterns)
        // Check also the use of request.login(user, pswd)
        // Check the HttpServletRequest.BASIC_AUTH, CLIENT_CERT_AUTH, FORM_AUTH, DIGEST_AUTH
        // servletReg.setRunAsRole("admin");
        // servletContext.declareRoles("admin");

        // ServletControl URL mapping
        String[] servletMapping = getServletMapping();
        servletControlReg.addMapping(servletMapping);

        // ErrorFilter -> @WebFilter(urlPatterns = {"/*"})
        Filter errorFilter = servletContext.createFilter(
                (Class<? extends Filter>) Class.forName("com.jsmartframework.web.filter.ErrorFilter"));
        FilterRegistration.Dynamic errorFilterReg = (FilterRegistration.Dynamic) servletContext
                .addFilter("ErrorFilter", errorFilter);

        errorFilterReg.setAsyncSupported(true);
        errorFilterReg.addMappingForUrlPatterns(
                EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ERROR), true, "/*");

        // EncodeFilter -> @WebFilter(urlPatterns = {"/*"})
        Filter encodeFilter = servletContext.createFilter(
                (Class<? extends Filter>) Class.forName("com.jsmartframework.web.filter.EncodeFilter"));
        FilterRegistration.Dynamic encodeFilterReg = (FilterRegistration.Dynamic) servletContext
                .addFilter("EncodeFilter", encodeFilter);

        encodeFilterReg.setAsyncSupported(true);
        encodeFilterReg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR), true,
                "/*");

        // CacheFilter -> @WebFilter(urlPatterns = {"/*"})
        Filter cacheFilter = servletContext.createFilter(
                (Class<? extends Filter>) Class.forName("com.jsmartframework.web.filter.CacheFilter"));
        FilterRegistration.Dynamic cacheFilterReg = (FilterRegistration.Dynamic) servletContext
                .addFilter("CacheFilter", cacheFilter);

        cacheFilterReg.setAsyncSupported(true);
        cacheFilterReg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR), true,
                "/*");

        // Add custom filters defined by client
        for (String filterName : sortCustomFilters()) {
            Filter customFilter = servletContext
                    .createFilter((Class<? extends Filter>) HANDLER.webFilters.get(filterName));
            HANDLER.executeInjection(customFilter);

            WebFilter webFilter = customFilter.getClass().getAnnotation(WebFilter.class);
            FilterRegistration.Dynamic customFilterReg = (FilterRegistration.Dynamic) servletContext
                    .addFilter(filterName, customFilter);

            if (webFilter.initParams() != null) {
                for (WebInitParam initParam : webFilter.initParams()) {
                    customFilterReg.setInitParameter(initParam.name(), initParam.value());
                }
            }
            customFilterReg.setAsyncSupported(webFilter.asyncSupported());
            customFilterReg.addMappingForUrlPatterns(EnumSet.copyOf(Arrays.asList(webFilter.dispatcherTypes())),
                    true, webFilter.urlPatterns());
        }

        // FilterControl -> @WebFilter(servletNames = {"ServletControl"})
        Filter filterControl = servletContext.createFilter(
                (Class<? extends Filter>) Class.forName("com.jsmartframework.web.manager.FilterControl"));
        FilterRegistration.Dynamic filterControlReg = (FilterRegistration.Dynamic) servletContext
                .addFilter("FilterControl", filterControl);

        filterControlReg.setAsyncSupported(true);
        filterControlReg.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD,
                DispatcherType.ERROR, DispatcherType.INCLUDE), true, "ServletControl");

        // OutputFilter -> @WebFilter(servletNames = {"ServletControl"})
        Filter outputFilter = servletContext.createFilter(
                (Class<? extends Filter>) Class.forName("com.jsmartframework.web.manager.OutputFilter"));
        FilterRegistration.Dynamic outputFilterReg = (FilterRegistration.Dynamic) servletContext
                .addFilter("OutputFilter", outputFilter);

        outputFilterReg.setAsyncSupported(true);
        outputFilterReg.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD,
                DispatcherType.ERROR, DispatcherType.INCLUDE), true, "ServletControl");

        // AsyncFilter -> @WebFilter(servletNames = {"ServletControl"})
        // Filter used case AsyncContext is dispatched internally by AsyncBean implementation
        Filter asyncFilter = servletContext.createFilter(
                (Class<? extends Filter>) Class.forName("com.jsmartframework.web.manager.AsyncFilter"));
        FilterRegistration.Dynamic asyncFilterReg = (FilterRegistration.Dynamic) servletContext
                .addFilter("AsyncFilter", asyncFilter);

        asyncFilterReg.setAsyncSupported(true);
        asyncFilterReg.addMappingForServletNames(EnumSet.of(DispatcherType.ASYNC), true, "ServletControl");

        // SessionControl -> @WebListener
        EventListener sessionListener = servletContext.createListener((Class<? extends EventListener>) Class
                .forName("com.jsmartframework.web.manager.SessionControl"));
        servletContext.addListener(sessionListener);

        // RequestControl -> @WebListener
        EventListener requestListener = servletContext.createListener((Class<? extends EventListener>) Class
                .forName("com.jsmartframework.web.manager.RequestControl"));
        servletContext.addListener(requestListener);

        // Custom WebServlet -> Custom Servlets created by application
        for (String servletName : HANDLER.webServlets.keySet()) {
            Servlet customServlet = servletContext
                    .createServlet((Class<? extends Servlet>) HANDLER.webServlets.get(servletName));
            HANDLER.executeInjection(customServlet);

            WebServlet webServlet = customServlet.getClass().getAnnotation(WebServlet.class);
            ServletRegistration.Dynamic customReg = (ServletRegistration.Dynamic) servletContext
                    .addServlet(servletName, customServlet);

            customReg.setLoadOnStartup(webServlet.loadOnStartup());
            customReg.setAsyncSupported(webServlet.asyncSupported());

            WebInitParam[] customInitParams = webServlet.initParams();
            if (customInitParams != null) {
                for (WebInitParam customInitParam : customInitParams) {
                    customReg.setInitParameter(customInitParam.name(), customInitParam.value());
                }
            }

            // Add mapping url for custom servlet
            customReg.addMapping(webServlet.urlPatterns());

            if (customServlet.getClass().isAnnotationPresent(MultipartConfig.class)) {
                customReg.setMultipartConfig(new MultipartConfigElement(
                        customServlet.getClass().getAnnotation(MultipartConfig.class)));
            }
        }

        // Controller Dispatcher for Spring MVC
        Set<String> requestPaths = HANDLER.requestPaths.keySet();
        if (!requestPaths.isEmpty()) {
            ServletRegistration.Dynamic mvcDispatcherReg = servletContext.addServlet("DispatcherServlet",
                    new DispatcherServlet(configWebAppContext));
            mvcDispatcherReg.setLoadOnStartup(1);
            mvcDispatcherReg.addMapping(requestPaths.toArray(new String[requestPaths.size()]));

            // RequestPathFilter -> @WebFilter(servletNames = {"DispatcherServlet"})
            Filter requestPathFilter = servletContext.createFilter((Class<? extends Filter>) Class
                    .forName("com.jsmartframework.web.manager.RequestPathFilter"));
            FilterRegistration.Dynamic reqPathFilterReg = (FilterRegistration.Dynamic) servletContext
                    .addFilter("RequestPathFilter", requestPathFilter);

            reqPathFilterReg.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST,
                    DispatcherType.FORWARD, DispatcherType.ERROR, DispatcherType.INCLUDE, DispatcherType.ASYNC),
                    true, "DispatcherServlet");
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.ireland.jnetty.webapp.WebApp.java

private void initConstructor() {

    _beanFactory = new BeanFactory(getClassLoader());

    _servletManager = new ServletManager(this);
    _servletMapper = new ServletMapper(this, this, _servletManager);

    _filterManager = new FilterManager(this, this);

    _dispatchFilterMapper = new FilterMapper(this, _filterManager, DispatcherType.REQUEST);

    _includeFilterMapper = new FilterMapper(this, _filterManager, DispatcherType.INCLUDE);

    _forwardFilterMapper = new FilterMapper(this, _filterManager, DispatcherType.FORWARD);

    _errorFilterMapper = new FilterMapper(this, _filterManager, DispatcherType.ERROR);

    // _errorPageManager = new ErrorPageManager(_server, this);

    // Use JVM temp dir as ServletContext temp dir.
    _tempDir = System.getProperty(TEMPDIR);

    _sessionManager = new SessionManager(this);

}

From source file:org.dd4t.core.util.HttpUtils.java

public static String getCurrentURL(final HttpServletRequest request) {
    String url;//  ww  w  . j a  va2 s .  c o m

    DispatcherType dispatcherType = request.getDispatcherType();
    if (dispatcherType == DispatcherType.ERROR) {
        url = request.getRequestURI();
    } else if (dispatcherType == DispatcherType.INCLUDE) {
        url = (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
    } else {
        url = getOriginalUri(request);
    }

    return url;
}

From source file:org.debux.webmotion.server.WebMotionServer.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest httpServletRequest = ((HttpServletRequest) request);
    HttpServletResponse httpServletResponse = ((HttpServletResponse) response);
    ServerContext serverContext = getServerContext(httpServletRequest);

    String uri = null;/* www.  ja v a  2 s.  c  om*/
    DispatcherType dispatcherType = request.getDispatcherType();
    if (dispatcherType == DispatcherType.INCLUDE) {
        uri = (String) httpServletRequest.getAttribute(HttpContext.ATTRIBUTE_INCLUDE_REQUEST_URI);
    }
    if (uri == null) {
        uri = httpServletRequest.getRequestURI();
    }

    String contextPath = httpServletRequest.getContextPath();
    String url = StringUtils.substringAfter(uri, contextPath);
    log.debug("Pass in filter = " + url);

    // Search if url is exclude path
    for (String path : serverContext.getExcludePaths()) {
        if (url.startsWith(path)) {
            url = PATH_SERVLET + url;
            break;
        }
    }

    if (url.startsWith(PATH_DEPLOY) || url.startsWith(PATH_ERROR) || url.equals("/")) {
        log.debug("Is deploy");
        doAction(httpServletRequest, httpServletResponse);

    } else if (url.startsWith(PATH_STATIC)) {
        log.debug("Is static");
        doResource(httpServletRequest, httpServletResponse);

    } else if (url.startsWith(PATH_SERVLET)) {
        log.debug("Is servlet");
        chain.doFilter(request, response);

    } else {
        String webappPath = serverContext.getWebappPath();
        File file = new File(webappPath, url);
        if (file.exists()) {
            // css js html png jpg jpeg xml ...
            log.debug("Is file");
            chain.doFilter(request, response);
        } else {
            log.debug("Is default");
            doAction(httpServletRequest, httpServletResponse);
        }
    }
}

From source file:org.debux.webmotion.server.WebMotionServer.java

/**
 * Static resources management//from  w ww  .  j a v  a 2  s  .c  om
 */
protected void doResource(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    // Suppress the static in path
    HttpServletRequest requestWrapper = new HttpServletRequestWrapper(request) {
        @Override
        public String getServletPath() {
            String servletPath = super.getServletPath();
            if (servletPath != null) {
                return servletPath.replaceFirst(PATH_STATIC, "");
            }
            return null;
        }

        @Override
        public String getPathInfo() {
            String pathInfo = super.getPathInfo();
            if (pathInfo != null) {
                return pathInfo.replaceFirst(PATH_STATIC, "");
            }
            return null;
        }

        @Override
        public String getRequestURI() {
            String requestURI = super.getRequestURI();
            if (requestURI != null) {
                return requestURI.replaceFirst(PATH_STATIC, "");
            }
            return null;
        }
    };

    // Dispatch on default servlet
    ServletContext servletContext = request.getServletContext();
    RequestDispatcher dispatcher = servletContext.getNamedDispatcher("default");

    DispatcherType dispatcherType = request.getDispatcherType();
    if (dispatcherType == DispatcherType.INCLUDE) {
        dispatcher.include(requestWrapper, response);
    } else {
        dispatcher.forward(requestWrapper, response);
    }
}