View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr;
17  
18  import java.io.IOException;
19  
20  import javax.servlet.Filter;
21  import javax.servlet.FilterChain;
22  import javax.servlet.FilterConfig;
23  import javax.servlet.ServletException;
24  import javax.servlet.ServletRequest;
25  import javax.servlet.ServletResponse;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletRequestWrapper;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.kuali.rice.krad.web.filter.AutoLoginFilter;
31  
32  /**
33   * Automatically logs in with the user specified via filter init parameter {@link AutoLoginFilter#USER_PARAM_NAME}.
34   * <p>
35   * There are no guarantees made that the user specified is a valid user in the system.
36   * </p>
37   * <p>
38   * In rice this Filter can be used via config like that following assuming the bootstrap filter is used: <br />
39   * {@code <param name="filter.login.class">org.kuali.kra.test.infrastructure.AutoLoginFilter</param>} <br />
40   * {@code <param name="filtermapping.login.1">/*</param>} <br />
41   * {@code <param name="filter.login.autouser">admin</param>} <br />
42   * </p>
43   */
44  public class TestAutoLoginFilter implements Filter {
45      public static final String USER_PARAM_NAME = "autouser";
46      public static String OVERRIDE_ID = "";
47  
48      private FilterConfig filterConfig;
49  
50      /** {@inheritDoc} */
51      public void init(FilterConfig config) throws ServletException {
52          this.filterConfig = config;
53      }
54  
55      /** {@inheritDoc} */
56      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
57          String username = StringUtils.isBlank(OVERRIDE_ID) ? filterConfig.getInitParameter(USER_PARAM_NAME) : OVERRIDE_ID;
58          if (username == null) {
59              throw new IllegalStateException("the " + USER_PARAM_NAME + " param is not set");
60          }
61  
62          chain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) request) {
63              @Override
64              public String getRemoteUser() {
65                  String username = TestAutoLoginFilter.OVERRIDE_ID;
66                  if (StringUtils.isBlank(username)) {
67                      username = TestAutoLoginFilter.this.filterConfig.getInitParameter(USER_PARAM_NAME);
68                  }
69                  return username;
70              }
71          }, response);
72      }
73  
74      /** {@inheritDoc} */
75      public void destroy() {
76          this.filterConfig = null;
77      }
78  }