Example usage for javax.servlet FilterChain doFilter

List of usage examples for javax.servlet FilterChain doFilter

Introduction

In this page you can find the example usage for javax.servlet FilterChain doFilter.

Prototype

public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException;

Source Link

Document

Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked.

Usage

From source file:com.allanditzel.springframework.security.web.csrf.CsrfTokenResponseHeaderBindingFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        javax.servlet.FilterChain filterChain) throws ServletException, IOException {
    CsrfToken token = (CsrfToken) request.getAttribute(REQUEST_ATTRIBUTE_NAME);

    if (token != null) {
        response.setHeader(RESPONSE_HEADER_NAME, token.getHeaderName());
        response.setHeader(RESPONSE_PARAM_NAME, token.getParameterName());
        response.setHeader(RESPONSE_TOKEN_NAME, token.getToken());
    }/*from   ww w .j  a  va  2 s.  co m*/

    filterChain.doFilter(request, response);
}

From source file:com.jelastic.campitos.SimpleCORSFilter.java

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET,PUT, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Content-Type");
    chain.doFilter(req, res);
}

From source file:com.enonic.cms.web.filter.CharacterEncodingFilter.java

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding(encoding);
    }//from  w  w w .j  av  a  2s.  c  o  m

    final String forcedCharset = request.getParameter("_charset");
    if (!StringUtils.isBlank(forcedCharset)) {
        response.setCharacterEncoding(forcedCharset);
    } else {
        response.setCharacterEncoding(encoding);
    }

    filterChain.doFilter(request, response);
}

From source file:com.mirantis.cachemod.filter.CacheFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws ServletException, IOException {

    if (request.getAttribute(conf.getAlreadyFilteredKey()) != null || !isCacheable(request)) {

        /*/*from   w  ww.j  a v a  2  s .  com*/
         *  This request is not Cacheable
         */

        chain.doFilter(request, response);
    } else {
        request.setAttribute(conf.getAlreadyFilteredKey(), Boolean.TRUE);

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        boolean fragmentRequest = isFragment(httpRequest);

        String key = conf.getCacheKeyProvider().createKey(httpRequest);

        CacheEntry cacheEntry = conf.getCacheProvider().getEntry(key);
        if (cacheEntry != null) {

            if (!fragmentRequest) {

                /*
                 * -1 of no in header
                 */
                long clientLastModified = httpRequest.getDateHeader("If-Modified-Since");

                /*
                 * Reply with SC_NOT_MODIFIED for client that has newest page 
                 */
                if ((clientLastModified != -1) && (clientLastModified >= cacheEntry.getLastModified())) {
                    ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    return;
                }
            }

            writeCacheToResponse(cacheEntry, response, fragmentRequest);

        } else {
            cacheEntry = conf.getCacheProvider().instantiateEntry();

            CacheHttpServletResponse cacheResponse = new CacheHttpServletResponse(
                    (HttpServletResponse) response, cacheEntry, conf, fragmentRequest);
            chain.doFilter(request, cacheResponse);

            if (cacheResponse.getStatus() == HttpServletResponse.SC_OK) {
                cacheResponse.commit();
                if (conf.getUserDataProvider() != null) {
                    cacheEntry.setUserData(conf.getUserDataProvider().createUserData(httpRequest));
                }
                conf.getCacheProvider().putEntry(key, cacheEntry);
            }
        }
    }

}

From source file:net.digitalprimates.persistence.hibernate.utils.filters.HibernateSessionServletFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    Session session = null;//  w  w w.j a  v a 2  s . c o m
    try {
        log.debug("Starting a database transaction");
        session = HibernateUtil.getCurrentSession();
        HibernateUtil.beginTransaction();

        // Call the next filter (continue request processing)
        chain.doFilter(request, response);

        // Commit and cleanup
        log.debug("Committing the database transaction");
        HibernateUtil.commitTransaction();

    } catch (StaleObjectStateException staleEx) {
        log.error("This interceptor does not implement optimistic concurrency control!");
        log.error("Your application will not work until you add compensation actions!");

        // Rollback, close everything, possibly compensate for any permanent
        // changes
        // during the conversation, and finally restart business
        // conversation. Maybe
        // give the user of the application a chance to merge some of his
        // work with
        // fresh data... what you do here depends on your applications
        // design.
        throw staleEx;
    } catch (Throwable ex) {
        // Rollback only
        ex.printStackTrace();
        try {
            if (HibernateUtil.getTransaction().isActive()) {
                log.debug("Trying to rollback database transaction after exception");
                HibernateUtil.rollbackTransaction();
            }
        } catch (Throwable rbEx) {
            log.error("Could not rollback transaction after exception!", rbEx);
        }

        // Let others handle it... maybe another interceptor for exceptions?
        throw new ServletException(ex);
    } finally {
        //SessionStatistics statistics = session.getStatistics();
        HibernateUtil.closeSession();
    }
}

From source file:br.on.daed.services.filters.CacheControlFilter.java

@Override
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    HttpServletResponse resp = (HttpServletResponse) response;
    resp.setHeader("Expires", "Tue, 03 Jul 2001 06:00:00 GMT");
    resp.setDateHeader("Last-Modified", new Date().getTime());
    resp.setHeader("Cache-Control",
            "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
    resp.setHeader("Pragma", "no-cache");

    chain.doFilter(request, response);
}

From source file:proyectoFinal.FiltroCorsSimple.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow.Origin", "*");
    response.setHeader("Access-Control-Allow.Methods", "PST,GET,PUT " + "OPTTIONS,DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with-Content-Type");
    chain.doFilter(req, res);
}

From source file:jp.xet.uncommons.web.UsernameLogFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();

    boolean successfulRegistration = false;
    if (auth != null) {
        String username = auth.getName();
        successfulRegistration = registerUsername(username);
    }/*from  w  ww  .ja v  a  2s.co  m*/

    try {
        filterChain.doFilter(request, response);
    } finally {
        if (successfulRegistration) {
            MDC.remove(USER_KEY);
        }
    }
}

From source file:org.imsglobal.basiclti.provider.servlet.filter.BasicLTISecurityFilter.java

private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    HttpSession session = request.getSession(true);
    boolean isBasicLtiRequest = isBasicLtiRequest(request);
    if (!isBasicLtiRequest && isBasicLtiSet(session)) {
        chain.doFilter(request, response);
    } else {//  w  w  w  . j a  v  a2 s.c o m
        if (isBasicLtiRequest) {
            // handle
            handleBasicLtiRequest(request, response, chain);
        } else {
            (response).sendError(401, "Unauthorized - No BasicLTI session found.");
        }
    }

}

From source file:com.investdata.common.filter.CharacterEncodingFilter.java

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {

    if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
        request.setCharacterEncoding(this.encoding);//?  
        if (this.forceEncoding) {
            response.setCharacterEncoding(this.encoding);
        }/*from w w  w.j  ava  2  s . co m*/
    }
    filterChain.doFilter(request, response);//  

    /*
    if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
       request.setCharacterEncoding(this.encoding);
       if (this.forceEncoding && setCharacterEncodingAvailable) {
    response.setCharacterEncoding(this.encoding);
       }
    }
    filterChain.doFilter(request, response);
    */
}