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:alpine.filters.FqdnForwardFilter.java

/**
 * Forward requests...../*from ww w.j a v  a 2  s . c o m*/
 *
 * @param request The request object.
 * @param response The response object.
 * @param chain Refers to the {@code FilterChain} object to pass control to the next {@code Filter}.
 * @throws IOException a IOException
 * @throws ServletException a ServletException
 */
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {

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

    if (req.getServerName().equals(host)) {
        chain.doFilter(request, response);
        return;
    }

    res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);

    StringBuilder sb = new StringBuilder();
    sb.append("http");
    if (req.isSecure()) {
        sb.append("s");
    }
    sb.append("://").append(host);
    if (StringUtils.isNotBlank(req.getPathInfo())) {
        sb.append(req.getPathInfo());
    }
    if (StringUtils.isNotBlank(req.getQueryString())) {
        sb.append("?").append(req.getQueryString());
    }
    res.setHeader("Location", sb.toString());
}

From source file:com.googlesource.gerrit.plugins.github.oauth.OAuthFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;

    if (!config.enabled || isStaticResource(httpRequest) || isRpcCall(httpRequest)
            || isAuthenticatedRestCall(httpRequest)) {
        chain.doFilter(request, response);
        return;//from ww w. j a v a2 s.c om
    }

    String requestUrl = httpRequest.getRequestURI();
    if (GIT_HTTP_REQUEST_PATTERN.matcher(requestUrl).matches()) {
        gitFilter.doFilter(request, response, chain);
    } else {
        webFilter.doFilter(request, response, chain);
    }
}

From source file:com.belajar_filter.config.GeneralFilterConfig.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc)
        throws IOException, ServletException {
    log.debug("execute do filter ... ");
    HttpServletResponse response = (HttpServletResponse) res;
    HttpServletRequest request = (HttpServletRequest) req;

    String getParam = req.getParameter("name");
    log.debug("intercept param : " + getParam);

    fc.doFilter(req, res);

}

From source file:de.metas.ui.web.config.ServletLoggingFilter.java

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    try {//from   www .j a  v  a 2  s  .co m
        updateMDC(request);

        chain.doFilter(request, response);
    } finally {
        //
        // log the request
        if (logger.isInfoEnabled()) {
            final String requestInfo = extractRequestInfo(request);
            logger.info("Executed in {}: {}", stopwatch.stop(), requestInfo);
        }

        //
        // Cleanup MDC (keep it last)
        cleanupMDC();
    }
}

From source file:info.magnolia.cms.security.SecurityFilter.java

/**
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse,
 * javax.servlet.FilterChain)//  w w w .jav a 2 s.c o m
 */
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {

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

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

From source file:CacheFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    for (int n = 0; n < replyHeaders.length; n++) {
        String name = replyHeaders[n][0];
        String value = replyHeaders[n][1];
        httpResponse.addHeader(name, value);
    }//from  www .  j a  v a2  s .c  o  m
    chain.doFilter(request, response);
}

From source file:grails.plugin.springsecurity.web.SecurityRequestHolderFilter.java

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

    HttpServletRequest request = wrapRequest((HttpServletRequest) req);

    SecurityRequestHolder.set(request, (HttpServletResponse) response);

    try {//from w  ww.j  av  a 2  s.  co m
        chain.doFilter(request, response);
    } finally {
        SecurityRequestHolder.reset();
    }
}

From source file:be.dnsbelgium.rdap.servlet.LoggerFilter.java

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    StatusExposingServletResponse response = new StatusExposingServletResponse(
            (HttpServletResponse) servletResponse);
    try {//from w w w  .jav  a 2s.  c o m
        chain.doFilter(servletRequest, response);
    } finally {
        String currentUser = null;
        try {
            currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
        } catch (NullPointerException npe) {
            LOGGER.debug("No SecurityContext", npe);
        }
        LOGGER.info("{} {} {} {}", request.getRemoteAddr(), currentUser, request.getServletPath(),
                response.getStatus());
    }
}

From source file:com.centurylink.cloud.sdk.sample.port.adapter.web.filters.InterceptCredentialsFilter.java

private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    String username = request.getHeader("X-Clc-Username");
    String password = request.getHeader("X-Clc-Password");

    if (username != null && password != null) {
        sdkRegistry.setCredentials(new SdkCredentials(username, password));
    }/*  ww  w  .  j  a  va  2s. c o m*/

    chain.doFilter(request, response);
}

From source file:com.adobe.acs.livereload.impl.JavaScriptInjectionFilter.java

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
    if (!(servletRequest instanceof SlingHttpServletRequest)
            || !(servletResponse instanceof SlingHttpServletResponse)) {
        filterChain.doFilter(servletRequest, servletResponse);
        return;// w ww.  ja  v  a  2s .c o  m
    }

    final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) servletRequest;
    final SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) servletResponse;

    if (!this.accepts(slingRequest)) {
        filterChain.doFilter(slingRequest, slingResponse);
        return;
    }

    final BufferingResponse capturedResponse = new BufferingResponse(slingResponse);

    filterChain.doFilter(slingRequest, capturedResponse);

    // Get contents
    final String contents = capturedResponse.getContents();

    if (contents != null) {
        if (StringUtils.contains(slingResponse.getContentType(), "html")) {

            final int bodyIndex = contents.indexOf("</body>");
            if (bodyIndex != -1) {

                final PrintWriter printWriter = slingResponse.getWriter();

                printWriter.write(contents.substring(0, bodyIndex));
                printWriter.write(String.format(
                        "<script type=\"text/javascript\" src=\"http://%s:%s/livereload.js\"></script>",
                        slingRequest.getServerName(), port));
                printWriter.write(contents.substring(bodyIndex));
                return;
            }
        }
    }

    if (contents != null) {
        slingResponse.getWriter().write(contents);
    }

}