1 /*
2 * $Id: SecurityWrappingFilter.java 637430 2008-03-15 15:38:35Z apetrelli $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.tiles.test.filter;
22
23 import java.io.IOException;
24 import javax.servlet.Filter;
25 import javax.servlet.FilterChain;
26 import javax.servlet.FilterConfig;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletRequestWrapper;
32
33 /**
34 * Filter that wraps an HttpServletRequest to override "isUserInRole".
35 *
36 * @version $Rev: 637430 $ $Date: 2008-03-16 02:38:35 +1100 (Sun, 16 Mar 2008) $
37 */
38 public class SecurityWrappingFilter implements Filter {
39
40 /**
41 * The role that the current user is supposed to use.
42 */
43 public static final String GOOD_ROLE = "goodrole";
44
45 /** {@inheritDoc} */
46 public void init(FilterConfig filterConfig) throws ServletException {
47 // No operation
48 }
49
50 /** {@inheritDoc} */
51 public void doFilter(ServletRequest servletRequest,
52 ServletResponse servletResponse, FilterChain filterChain)
53 throws IOException, ServletException {
54 HttpServletRequest wrappedRequest = new SecurityWrapperHttpServletRequest(
55 (HttpServletRequest) servletRequest);
56 filterChain.doFilter(wrappedRequest, servletResponse);
57 }
58
59 /** {@inheritDoc} */
60 public void destroy() {
61 // No operation
62 }
63
64 /**
65 * Request wrapper that overrides "isUserInRole" method.
66 *
67 * @version $Rev: 637430 $ $Date: 2008-03-16 02:38:35 +1100 (Sun, 16 Mar 2008) $
68 */
69 private static class SecurityWrapperHttpServletRequest extends
70 HttpServletRequestWrapper {
71 /**
72 * Constructor.
73 *
74 * @param request The HTTP servlet request.
75 */
76 public SecurityWrapperHttpServletRequest(HttpServletRequest request) {
77 super(request);
78 }
79
80 /** {@inheritDoc} */
81 @Override
82 public boolean isUserInRole(String role) {
83 return GOOD_ROLE.equals(role);
84 }
85 }
86 }