View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21  
22  package org.miloss.fgsms.presentation;
23  
24  import java.security.Principal;
25  import java.util.List;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletRequestWrapper;
29  
30  /**
31   * An extension for the HTTPServletRequest that overrides the getUserPrincipal() and isUserInRole().
32   *  We supply these implementations here, where they are not normally populated unless we are going through
33   *  the facility provided by the container.
34   * <p>If he user or roles are null on this wrapper, the parent request is consulted to try to fetch what ever the container has set for us.
35   * This is intended to be created and used by the UserRoleFilter.
36   * @author thein
37   *
38   */
39  public class UserRequestWrapper extends HttpServletRequestWrapper {
40  
41      String user;
42      List<String> roles = null;
43      HttpServletRequest realRequest;
44  
45      public UserRequestWrapper(String user, List<String> roles, HttpServletRequest request) {
46          super(request);
47          this.user = user;
48          this.roles = roles;
49          this.realRequest = request;
50  
51      }
52  
53      @Override
54      public String getAuthType() {
55          return "BASIC";
56      }
57  
58      @Override
59      public boolean isUserInRole(String role) {
60          if (roles == null) {
61              return this.realRequest.isUserInRole(role);
62          }
63          return roles.contains(role);
64      }
65      @Override
66      public String getRemoteUser()
67      {
68          return user;
69      }
70  
71      @Override
72      public Principal getUserPrincipal() {
73          if (this.user == null) {
74              return realRequest.getUserPrincipal();
75          }
76  
77          // make an anonymous implementation to just return our user
78          return new Principal() {
79  
80              @Override
81              public String getName() {
82                  return user;
83              }
84          };
85      }
86  }