Example usage for javax.servlet Filter Filter

List of usage examples for javax.servlet Filter Filter

Introduction

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

Prototype

Filter

Source Link

Usage

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

public static Filter myFilter() {
    Filter filter = new Filter() {

        private ServletContext context;

        @Override/*  w ww . j a v a2s  . com*/
        public void init(FilterConfig filterConfig) throws ServletException {
            log.debug("initiate general filter config");
            this.context = filterConfig.getServletContext();
            this.context.log("AuthenticationFilter initialized");
        }

        @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 = request.getParameter("name");
            String urlRequest = request.getRequestURI();
            log.debug("intercept url request : " + urlRequest);
            log.debug("intercept param : " + getParam);

            if ("aji".equals(getParam)) {
                log.debug("is aji");
                fc.doFilter(req, res);
            } else {
                log.debug("is not aji");
                response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE);
            }

        }

        @Override
        public void destroy() {

        }
    };

    return filter;
}

From source file:com.blacklocus.webapp.app.StaticResourceUTF8CharEncodingFilterHolder.java

public StaticResourceUTF8CharEncodingFilterHolder() {
    super(new Filter() {
        @Override/*from  ww  w.jav a  2  s .  co  m*/
        public void init(FilterConfig filterConfig) throws ServletException {
            // no-op
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            response.setCharacterEncoding(CharEncoding.UTF_8);
            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {
            // no-op
        }
    });
}

From source file:com.bennavetta.appsite.security.SecurityConfig.java

@Bean
public Filter log() {
    return new Filter() {

        @Override//from   w  ww .ja  va  2  s .c  om
        public void destroy() {
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter)
                throws IOException, ServletException {
            System.err.println("Intercepted: " + request);
            filter.doFilter(request, response);
        }

        @Override
        public void init(FilterConfig arg0) throws ServletException {
        }

    };
}

From source file:info.matsumana.config.AppConfig.java

@Bean
public FilterRegistrationBean filterRegistrationBean() {

    Filter filter = new Filter() {
        @Override//w w  w.j  av  a  2 s.  com
        public void init(FilterConfig filterConfig) throws ServletException {
        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                FilterChain filterChain) throws IOException, ServletException {
            try {
                counterService.increment("active-http-sessions");
                filterChain.doFilter(servletRequest, servletResponse);
            } finally {
                counterService.decrement("active-http-sessions");
            }
        }

        @Override
        public void destroy() {
        }
    };

    FilterRegistrationBean registrationBean = new FilterRegistrationBean();

    registrationBean.setFilter(filter);
    registrationBean.addUrlPatterns("/sample/api/*");

    return registrationBean;
}

From source file:org.jenkinsci.plugins.ncc.CertificateSecurityRealm.java

@Override
public Filter createFilter(FilterConfig filterConfig) {
    return new Filter() {
        public void init(FilterConfig filterConfig) throws ServletException {

        }/*from   w  w w  . j  av  a2  s  .co m*/

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            Authentication a;
            HttpServletRequest r = (HttpServletRequest) request;
            String sdn = r.getHeader(getSdnKey());
            String rule = getSdnStrip();

            if (sdn == null) {
                a = Hudson.ANONYMOUS;
            } else {

                Pattern p = Pattern.compile(rule);
                Matcher m = p.matcher(sdn);
                m.find();
                String user = m.group(1);
                user = user.toLowerCase();

                GrantedAuthority[] authorities = new GrantedAuthority[] {
                        SecurityRealm.AUTHENTICATED_AUTHORITY };
                a = new UsernamePasswordAuthenticationToken(user, "", authorities);
            }

            SecurityContextHolder.getContext().setAuthentication(a);
            chain.doFilter(request, response);
        }

        public void destroy() {
        }
    };
}

From source file:org.jenkinsci.plugins.certificate_auth.CertificateSecurityRealm.java

@Override
public Filter createFilter(FilterConfig filterConfig) {
    return new Filter() {
        public void init(FilterConfig filterConfig) throws ServletException {
        }/*from  w  w w. j  a  va  2  s  .  co m*/

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            HttpServletRequest r = (HttpServletRequest) request;
            final X509Certificate[] certChain = (X509Certificate[]) request
                    .getAttribute("javax.servlet.request.X509Certificate");

            Authentication a;
            if (certChain == null || certChain[0] == null) {
                a = Hudson.ANONYMOUS;
            } else {

                final String dn = certChain[0].getSubjectDN().getName();

                final String username = dn.split(getUserField() + "=")[1].split(",")[0];
                final String group = dn.split(getGroupField() + "=")[1].split(",")[0];

                GrantedAuthority[] authorities = new GrantedAuthority[] { SecurityRealm.AUTHENTICATED_AUTHORITY,
                        new GrantedAuthorityImpl(group) };

                a = new UsernamePasswordAuthenticationToken(username, "", authorities);
            }

            SecurityContextHolder.getContext().setAuthentication(a);

            chain.doFilter(request, response);
        }

        public void destroy() {
        }
    };
}

From source file:com.netflix.conductor.server.JerseyModule.java

@Provides
@Singleton//from w w w  .  ja  v a  2 s  .c o m
public Filter apiOriginFilter() {
    return new Filter() {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            HttpServletResponse res = (HttpServletResponse) response;
            if (!res.containsHeader("Access-Control-Allow-Origin")) {
                res.setHeader("Access-Control-Allow-Origin", "*");
            }
            res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
            res.addHeader("Access-Control-Allow-Headers", "Content-Type, api_key, Authorization");

            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {
        }

    };
}

From source file:com.github.camellabs.iot.cloudlet.document.driver.DriverDocumentCloudlet.java

@Bean
Filter corsFilter() {//from  ww  w. j  a v  a  2s. co  m
    return new Filter() {

        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, OPTIONS, DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "Authorization, x-requested-with");
            chain.doFilter(req, res);
        }

        public void init(FilterConfig filterConfig) {
        }

        public void destroy() {
        }

    };
}

From source file:com.jayway.restassured.examples.springmvc.controller.AutoSpringSecurityConfigurerITest.java

@Test
public void doesnt_add_spring_security_configurer_automatically_when_a_spring_security_configurer_has_been_manually_applied() {
    final AtomicBoolean filterUsed = new AtomicBoolean(false);

    given().webAppContextSetup(context, springSecurity(), springSecurity(new Filter() {
        public void init(FilterConfig filterConfig) throws ServletException {
        }/*  w w w . j  a va 2 s .c o m*/

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            filterUsed.set(true);
            chain.doFilter(request, response);
        }

        public void destroy() {
        }
    })).postProcessors(httpBasic("username", "password")).param("name", "Johan").when().get("/secured/greeting")
            .then().statusCode(200).body("content", equalTo("Hello, Johan!"))
            .expect(authenticated().withUsername("username"));

    assertThat(filterUsed.get(), is(true));
}

From source file:io.restassured.examples.springmvc.controller.AutoSpringSecurityConfigurerITest.java

@Test
public void doesnt_add_spring_security_configurer_automatically_when_a_spring_security_configurer_has_been_manually_applied() {
    final AtomicBoolean filterUsed = new AtomicBoolean(false);

    RestAssuredMockMvc.given().webAppContextSetup(context, springSecurity(), springSecurity(new Filter() {
        public void init(FilterConfig filterConfig) throws ServletException {
        }/*w  w  w. j ava2  s . c  om*/

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            filterUsed.set(true);
            chain.doFilter(request, response);
        }

        public void destroy() {
        }
    })).postProcessors(httpBasic("username", "password")).param("name", "Johan").when().get("/secured/greeting")
            .then().statusCode(200).body("content", equalTo("Hello, Johan!"))
            .expect(authenticated().withUsername("username"));

    assertThat(filterUsed.get(), is(true));
}