View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.security.jaspi;
15  
16  import java.util.Collection;
17  import java.util.HashMap;
18  import java.util.Map;
19  import java.util.Set;
20  
21  import javax.security.auth.message.MessageInfo;
22  import javax.servlet.ServletRequest;
23  import javax.servlet.ServletResponse;
24  
25  
26  /**
27   * Almost an implementation of jaspi MessageInfo.
28   *
29   * @version $Rev: 4660 $ $Date: 2009-02-25 17:29:53 +0100 (Wed, 25 Feb 2009) $
30   */
31  public class JaspiMessageInfo implements MessageInfo
32  {
33      public static final String MANDATORY_KEY = "javax.security.auth.message.MessagePolicy.isMandatory";
34      public static final String AUTH_METHOD_KEY = "javax.servlet.http.authType";
35      private ServletRequest request;
36      private ServletResponse response;
37      private final MIMap map;
38  
39      public JaspiMessageInfo(ServletRequest request, ServletResponse response, boolean isAuthMandatory)
40      {
41          this.request = request;
42          this.response = response;
43          //JASPI 3.8.1
44          map = new MIMap(isAuthMandatory);
45      }
46  
47      public Map getMap()
48      {
49          return map;
50      }
51  
52      public Object getRequestMessage()
53      {
54          return request;
55      }
56  
57      public Object getResponseMessage()
58      {
59          return response;
60      }
61  
62      public void setRequestMessage(Object request)
63      {
64          this.request = (ServletRequest)request;
65      }
66  
67      public void setResponseMessage(Object response)
68      {
69          this.response = (ServletResponse)response;
70      }
71  
72      public String getAuthMethod()
73      {
74          return map.getAuthMethod();
75      }
76  
77      public boolean isAuthMandatory() {
78          return map.isAuthMandatory();
79      }
80  
81  
82      //TODO this has bugs in the view implementations.  Changing them will not affect the hardcoded values.
83      private static class MIMap implements Map
84      {
85          private final boolean isMandatory;
86          private String authMethod;
87          private Map delegate;
88  
89          private MIMap(boolean mandatory)
90          {
91              isMandatory = mandatory;
92          }
93  
94          public int size()
95          {
96              return (isMandatory? 1:0) +
97                      (authMethod == null? 0: 1) +
98                      (delegate == null? 0: delegate.size());
99          }
100 
101         public boolean isEmpty()
102         {
103             return !isMandatory && authMethod == null && (delegate == null || delegate.isEmpty());
104         }
105 
106         public boolean containsKey(Object key)
107         {
108             if (MANDATORY_KEY.equals(key)) return isMandatory;
109             if (AUTH_METHOD_KEY.equals(key)) return authMethod != null;
110             return delegate != null && delegate.containsKey(key);
111         }
112 
113         public boolean containsValue(Object value)
114         {
115             if (isMandatory && "true".equals(value)) return true;
116             if (authMethod == value || (authMethod != null && authMethod.equals(value))) return true;
117             return delegate != null && delegate.containsValue(value);
118         }
119 
120         public Object get(Object key)
121         {
122             if (MANDATORY_KEY.equals(key)) return isMandatory? "true": null;
123             if (AUTH_METHOD_KEY.equals(key)) return authMethod;
124             if (delegate == null) return null;
125             return delegate.get(key);
126         }
127 
128         public Object put(Object key, Object value)
129         {
130             if (MANDATORY_KEY.equals(key))
131             {
132                 throw new IllegalArgumentException("Mandatory not mutable");
133             }
134             if (AUTH_METHOD_KEY.equals(key))
135             {
136                 String authMethod = this.authMethod;
137                 this.authMethod = (String) value;
138                 if (delegate != null) delegate.put(AUTH_METHOD_KEY, value);
139                 return authMethod;
140             }
141 
142             return getDelegate(true).put(key, value);
143         }
144 
145         public Object remove(Object key)
146         {
147             if (MANDATORY_KEY.equals(key))
148             {
149                 throw new IllegalArgumentException("Mandatory not mutable");
150             }
151             if (AUTH_METHOD_KEY.equals(key))
152             {
153                 String authMethod = this.authMethod;
154                 this.authMethod = null;
155                 if (delegate != null) delegate.remove(AUTH_METHOD_KEY);
156                 return authMethod;
157             }
158             if (delegate == null) return null;
159             return delegate.remove(key);
160         }
161 
162         public void putAll(Map map)
163         {
164             if (map != null)
165             {
166                 for (Object o: map.entrySet())
167                 {
168                     Map.Entry entry = (Entry) o;
169                     put(entry.getKey(), entry.getValue());
170                 }
171             }
172         }
173 
174         public void clear()
175         {
176             authMethod = null;
177             delegate = null;
178         }
179 
180         public Set keySet()
181         {
182             return getDelegate(true).keySet();
183         }
184 
185         public Collection values()
186         {
187             return getDelegate(true).values();
188         }
189 
190         public Set entrySet()
191         {
192             return getDelegate(true).entrySet();
193         }
194 
195         private Map getDelegate(boolean create)
196         {
197             if (!create || delegate != null) return delegate;
198             if (create)
199             {
200                 delegate = new HashMap();
201                 if (isMandatory) delegate.put(MANDATORY_KEY, "true");
202                 if (authMethod != null) delegate.put(AUTH_METHOD_KEY, authMethod);
203             }
204             return delegate;
205         }
206 
207         boolean isAuthMandatory()
208         {
209             return isMandatory;
210         }
211 
212         String getAuthMethod()
213         {
214             return authMethod;
215         }
216     }
217 }