title: How to get Roles from UserPrincipal in Java?
body: <p>I created a class(Named as <strong>CustomRequestWrapper</strong>) which is implementing <strong>HttpServletRequestWrapper</strong> .In <strong>CustomRequestWrapper</strong> class i am setting user principal.Now in my code i want to get list of roles from the user principal.I tried to use GenericPrincipal Class from <strong>tomcat-catalina</strong> jar but i am getting casting exception <strong>CustomRequestWrapper</strong> cannot be cast to <strong>GenericPrincipal</strong>. Could any one have idea how to get roles from user principal?</p>
  
  <p>Note: I am using Apache Tomcat Server</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public class CustomRequestWrapper extends javax.servlet.http.HttpServletRequestWrapper {
  
  public CustomRequestWrapper(String User,List&lt;String&gt; roles,HttpServletRequest request) {
      super(request);
      this.user=User;
      this.roles=roles;
      this.realRequest=request;
      headerMap = new HashMap();
  }
  String user;  
  List&lt;String&gt; roles = null; 
  HttpServletRequest realRequest;  
  private Map headerMap;
  
  public void addHeader(String name, String value) {
      headerMap.put(name, new String(value));
  }
  
  public Enumeration getHeaderNames() {
      HttpServletRequest request = (HttpServletRequest) getRequest();
      List list = new ArrayList();
      for (Enumeration e = request.getHeaderNames(); e.hasMoreElements();) {
          list.add(e.nextElement().toString());
      }
  
      for (Iterator i = headerMap.keySet().iterator(); i.hasNext();) {
          list.add(i.next());
      }
      return Collections.enumeration(list);
  }
  
  public String getHeader(String name) {
      Object value;
      if ((value = headerMap.get("" + name)) != null)
          return value.toString();
      else
          return ((HttpServletRequest) getRequest()).getHeader(name);
  }
       @override
  public boolean isUserInRole(String role) {  
      if (roles == null) {  
          return this.realRequest.isUserInRole(role);  
      }  
      return roles.contains(role);  
  }  
  
  @override
  public Principal getUserPrincipal() {  
      if (this.user == null) {  
          return realRequest.getUserPrincipal();  
      }  
  
      // make an anonymous implementation to just return our user  
      return new Principal() {  
  
          public String getName() {       
              return user;  
          }  
      };  
  }  
  </code></pre>
  
  <p>}</p>