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:ch.astina.hesperid.web.services.springsecurity.internal.SpringSecurityExceptionTranslationFilter.java

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    try {//  w  w w . ja v  a 2s. c  om
        chain.doFilter(request, response);

        if (logger.isDebugEnabled()) {
            logger.debug("Chain processed normally");
        }
    } catch (IOException ex) {
        throw ex;
    } catch (Exception ex) {
        // Try to extract a SpringSecurityException from the stacktrace
        Throwable[] causeChain = throwableAnalyzer.determineCauseChain(ex);
        RuntimeException ase = (AuthenticationException) throwableAnalyzer
                .getFirstThrowableOfType(AuthenticationException.class, causeChain);

        if (ase == null) {
            ase = (AccessDeniedException) throwableAnalyzer.getFirstThrowableOfType(AccessDeniedException.class,
                    causeChain);
        }

        if (ase != null) {
            handleException(request, response, chain, ase);
        } else {
            // Rethrow ServletExceptions and RuntimeExceptions as-is
            if (ex instanceof ServletException) {
                throw (ServletException) ex;
            } else if (ex instanceof RuntimeException) {
                throw (RuntimeException) ex;
            }

            // Wrap other Exceptions. These are not expected to happen
            throw new RuntimeException(ex);
        }
    }
}

From source file:com.mycompany.projetsportmanager.filter.XHttpMethodOverrideFilter.java

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

    String headerValue = request.getHeader(this.headerParam);
    if ("POST".equals(request.getMethod()) && StringUtils.hasLength(headerValue)) {
        String method = headerValue.toUpperCase(Locale.ENGLISH);
        HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
        filterChain.doFilter(wrapper, response);
    } else {/*from   w  w  w . jav  a 2  s  .c o  m*/
        filterChain.doFilter(request, response);
    }
}

From source file:net.big_oh.resourcestats.web.filter.ResourceRequestStatsTrackerFilter.java

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

    // capture time at which request was received
    Date requestReceivedOn = new Date();

    // Time the operation at large
    Duration duration = new Duration();

    // Carry on with the requested operation.
    chain.doFilter(request, response);

    // record resource access stats
    try {/*w  w w  .j  a  v  a  2s.c o  m*/
        trackResourceRequestStats(request, requestReceivedOn, duration.stop());
    } catch (RuntimeException re) {
        logger.error(re.getMessage(), re);
    }

}

From source file:grails.plugin.springsecurity.web.filter.IpAddressFilter.java

public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    if (!isAllowed(request)) {
        deny(request, response);//from  ww  w  .  j  a v  a  2  s  .  c om
        return;
    }

    chain.doFilter(request, response);
}

From source file:csns.web.filter.RegistrationFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    String contextPath = request.getContextPath();
    String path = request.getRequestURI().substring(contextPath.length());

    if (SecurityUtils.isAuthenticated() && SecurityUtils.getUser().isTemporary() && !isPassThrough(path)) {
        response.sendRedirect(contextPath + "/register");
        return;/*ww w.j  a v  a2 s.c o m*/
    }

    filterChain.doFilter(request, response);
}

From source file:ar.com.zauber.commons.facebook.utils.filters.FacebookOnlyFilter.java

/** @see OncePerRequestFilter#doFilterInternal(HttpServletRequest, 
 * HttpServletResponse, FilterChain) */
@Override//from  w w  w.ja v  a 2  s .  c  om
protected final void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
        final FilterChain chain) throws ServletException, IOException {

    if (validator.validate(request)) {
        if (needsLogin && request.getParameter("fb_sig_user") == null) {
            response.getOutputStream().print("<fb:redirect url='"
                    + "http://www.facebook.com/apps/application.php?api_key=" + apiKey + "' />");
        } else {
            chain.doFilter(request, response);
        }
    } else {
        response.sendError(403, "requests doesn't seem to come from a facebook server");
    }
}

From source file:com.alliander.osgp.shared.security.MellonTokenProcessingFilter.java

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

    if (!this.useMellonForUserIdentity) {
        LOGGER.debug("Skipping Mellon token processing, since Mellon is not configured to be used.");
        chain.doFilter(request, response);
        return;//from w  ww  .jav  a 2 s  .  com
    }

    if (!(request instanceof HttpServletRequest)) {
        throw new IOException("Expecting a HTTP request");
    }

    final HttpServletRequest httpRequest = (HttpServletRequest) request;

    /*
     * If this point in code is reached, the Apache module auth_mellon
     * should be configured, and has allowed the application to be reached.
     * The username from the HTTP header (configured as
     * httpHeaderForUsername) belongs with a user that has authenticated
     * with the 3rd party Identity Provider (IdP) Keycloak.
     *
     * Checks to be performed are if a user with the provided username
     * exists as an active user for this application, and if so, the
     * organisation, domains and authorities with the user need to be
     * retrieved without further checking of any password.
     */

    final String username = httpRequest.getHeader(this.httpHeaderForUsername);

    if (this.noValidUsernamePresent(username)) {
        chain.doFilter(request, response);
        return;
    }

    if (this.isLogoutRequest(httpRequest)) {
        this.logoutMellon((HttpServletResponse) response, username);
        return;
    }

    LOGGER.info("Validating login for user {} based on input via Mellon", username);

    final Authentication authenticationFromSecurityContext = SecurityContextHolder.getContext()
            .getAuthentication();

    if (this.authenticationExistsForUsername(authenticationFromSecurityContext, username)) {
        this.includeFrontendHeader(response, authenticationFromSecurityContext);
        chain.doFilter(request, response);
        return;
    }

    final Authentication authentication = this.authenticateUser(authenticationFromSecurityContext, username);
    if (authentication != null) {
        SecurityContextHolder.getContext().setAuthentication(authentication);
        this.includeFrontendHeader(response, authentication);
    }

    chain.doFilter(request, response);
}

From source file:org.juiser.spring.security.web.authentication.HeaderAuthenticationFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    if (!requiresAuthentication(request, response)) {
        chain.doFilter(request, response);
        return;/*from  w w w.j  a va 2s.c  o m*/
    }

    log.debug("Request will be authenticated.");

    boolean continueChain = true;
    Authentication authResult = null;

    try {
        authResult = attemptAuthentication(request, response);
        Assert.notNull(authResult, "authResult cannot be null.");
    } catch (InternalAuthenticationServiceException failed) {
        log.error(
                "An internal error occurred while trying to authenticate the request header. Request will be anonymous.",
                failed);
        continueChain = unsuccessfulAuthentication(request, response, chain, failed);
    } catch (AuthenticationException failed) {
        // Authentication failed
        continueChain = unsuccessfulAuthentication(request, response, chain, failed);
    }

    if (authResult != null) {
        continueChain = successfulAuthentication(request, response, chain, authResult);
    }

    if (continueChain) {
        chain.doFilter(request, response);
    }
}

From source file:com.proofpoint.http.server.GZipRequestFilter.java

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    String contentEncoding = request.getHeader("content-encoding");
    if (equalsIgnoreCase(contentEncoding, "gzip")) { // TODO: gzip; q=0.0 should mean "don't want gzip"
        filterChain.doFilter(new GZipRequestWrapper(request), servletResponse);
    } else {/*w w w  .j  a va 2s  .  c om*/
        filterChain.doFilter(request, response);
    }
}

From source file:com.ewcms.web.filter.PreviewFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse rep, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    String uri = request.getRequestURI();
    if (!StringUtils.endsWith(uri, PREVIEW_PATH)) {
        logger.debug("Uri is not preview address");
        chain.doFilter(req, rep);
        return;/* w  ww.j  av a  2 s .  c  o  m*/
    }

    HttpServletResponse response = (HttpServletResponse) rep;
    try {
        Integer channelId = getChannelId(request);
        Integer templateId = getTemplateId(request);
        Long articleId = getArticleId(request);
        if (templateId == null && (channelId == null || articleId == null)) {

            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }

        String redirectUri = getRedirectUri(request, articleId, templateId);
        if (redirectUri == null) {
            logger.debug("Redirect's address is null");
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        } else {
            logger.debug("Redirect's address is {}", redirectUri);
            response.sendRedirect(redirectUri);
        }
    } catch (Exception e) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.toString());
    }
}