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:org.usergrid.rest.SwaggerServlet.java

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

    if (handleJsonOutput(request, response)) {
        return;//ww  w . j a va 2  s . co  m
    }

    chain.doFilter(request, response);
}

From source file:org.frontcache.hystrix.FC_BypassCache.java

private void forwardToOrigin() throws IOException, ServletException {
    HttpServletRequest request = context.getRequest();

    if (context.isFilterMode()) {
        HttpServletResponse response = context.getResponse();
        FilterChain chain = context.getFilterChain();
        chain.doFilter(request, response);
    } else {/*from   w  w w .j  a  v  a2  s.co m*/

        // stand alone mode

        Map<String, List<String>> headers = FCUtils.buildRequestHeaders(request);
        headers.put(FCHeaders.X_FRONTCACHE_CLIENT_IP,
                Arrays.asList(new String[] { FCUtils.getClientIP(context.getRequest()) }));

        String verb = request.getMethod();
        InputStream requestEntity = getRequestBody(request);
        String uri = context.getRequestURI();

        try {
            HttpResponse response = forward(client, verb, uri, request, headers, requestEntity);

            // response 2 context
            setResponse(response);

        } catch (Exception ex) {
            ex.printStackTrace();
            context.set("error.status_code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            context.set("error.exception", ex);

            context.setHystrixFallback();
            logger.error("FC - ORIGIN ERROR - " + uri);

        }
    }

    return;
}

From source file:com.knowbout.hibernate.OpenTransactionInViewFilter.java

/**
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
 *///from   w  w  w .  java  2  s  . co m
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    TransactionManager.beginTransaction();
    boolean commit = false;
    try {
        chain.doFilter(request, response);
        commit = !TransactionManager.isRollbackOnly();
    } catch (ServletException se) {
        String message = se.toString();
        if (se.getRootCause() != null) {
            message = se.getRootCause().toString();
        }
        if (printFullExceptions) {
            if (se.getRootCause() != null) {
                log.error(message, se.getRootCause());
            } else {
                log.error(message, se);
            }
        } else {
            log.error(message);
        }
        commit = false;
        throw se;
    } catch (Throwable t) {
        log.error("Error with transaction, rolling back instead.", t);
        commit = false;
    } finally {
        //dispose of the open transaction by commit or rollback
        //transactionHolder.remove(transaction);
        if (commit) {
            try {
                TransactionManager.commitTransaction();
            } catch (Throwable e) {
                log.error("Cannot commit transaction, rolling back instead.", e);
                TransactionManager.rollbackTransaction();
                throw new ServletException(e);
            }
        } else {
            try {
                TransactionManager.rollbackTransaction();
            } catch (Throwable e) {
                log.error("Cannot rollback transaction.", e);
                throw new ServletException(e);
            }
        }
    }
}

From source file:com.cloudbees.jenkins.plugins.bitbucket.hooks.BitbucketSCMSourcePushHookReceiver.java

@Override
public boolean process(HttpServletRequest req, HttpServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    String pathInfo = req.getPathInfo();
    if (pathInfo != null && pathInfo.startsWith("/" + FULL_PATH)) {
        chain.doFilter(req, resp);
        return true;
    }/*from   w ww . ja v  a2s  .  c o m*/
    return false;
}

From source file:cn.vlabs.duckling.vwb.CPSFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    int advSiteId = Integer.parseInt(StringUtils.defaultIfEmpty(request.getParameter("as"), "-1"));

    if (advSiteId <= 0) {
        chain.doFilter(request, response);
        return;/*from  w  ww  .ja va 2 s . co  m*/
    }

    HttpServletResponse rep = (HttpServletResponse) response;
    HttpServletRequest req = (HttpServletRequest) request;
    request.setAttribute("cps", advSiteId);

    Cookie[] cookies = req.getCookies();

    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (StringUtils.equals(CPS_ADV, cookie.getName())) {
                cookie.setPath(req.getContextPath());
                cookie.setMaxAge(0);
            }
        }
    }

    Cookie myCookie = new Cookie(CPS_ADV, advSiteId + "");
    myCookie.setMaxAge(60 * 60 * 24);//
    myCookie.setPath(req.getContextPath());
    rep.addCookie(myCookie);
    chain.doFilter(request, response);
}

From source file:com.stormpath.spring.boot.examples.filter.ReCaptchaFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    if (!(req instanceof HttpServletRequest)
            || !("POST".equalsIgnoreCase(((HttpServletRequest) req).getMethod()))) {
        chain.doFilter(req, res);
        return;//from www.j  a  va 2  s. c  o  m
    }

    PostMethod method = new PostMethod(RECAPTCHA_URL);
    method.addParameter("secret", RECAPTCHA_SECRET);
    method.addParameter("response", req.getParameter(RECAPTCHA_RESPONSE_PARAM));
    method.addParameter("remoteip", req.getRemoteAddr());

    HttpClient client = new HttpClient();
    client.executeMethod(method);
    BufferedReader br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
    String readLine;
    StringBuffer response = new StringBuffer();
    while (((readLine = br.readLine()) != null)) {
        response.append(readLine);
    }

    JSONObject jsonObject = new JSONObject(response.toString());
    boolean success = jsonObject.getBoolean("success");

    if (success) {
        chain.doFilter(req, res);
    } else {
        ((HttpServletResponse) res).sendError(HttpStatus.BAD_REQUEST.value(), "Bad ReCaptcha");
    }
}

From source file:com.anyun.sample.db.doma.DomaTransactionFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    DomaTransactionManager transactionManager = BeanLoader.getBean("domaTransactionManager");
    LocalTransaction tx = transactionManager.getLocalTransaction();

    try {/* w  ww .  ja  va 2  s .c  o m*/
        tx.begin();
        filterChain.doFilter(request, response);
        tx.commit();
    } finally {
        tx.rollback();
    }
}

From source file:net.sourceforge.subsonic.filter.RESTFilter.java

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    try {//from w  w  w. j av a2  s  .  co  m
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        chain.doFilter(req, res);
    } catch (Throwable x) {
        handleException(x, (HttpServletRequest) req, (HttpServletResponse) res);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.filters.CharsetEncodingFilter.java

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) servletRequest;

    setCharacterEncodingOnRequest(req);//from   w  ww .j a v a 2  s  .  co  m
    setContentTypeOnResponse(req, servletResponse);

    filterChain.doFilter(servletRequest, servletResponse);
}

From source file:com.pocketgorilla.stripesem.TransactionFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (log.isDebugEnabled())
        log.debug("filtering " + ((HttpServletRequest) request).getRequestURI());

    try {/*from   w  w  w . jav a  2  s. co m*/
        chain.doFilter(request, response);
    } catch (Exception ex) {
        try {
            EntityManager em = provider.getEntityManager(false);
            if (em != null) {
                EntityTransaction tx = em.getTransaction();
                if (tx.isActive()) {
                    tx.setRollbackOnly();
                }
            }
        } finally {
            throw new ServletException(ex);
        }
    } finally {
        doAfter();
    }
}