View Javadoc

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