View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009-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  
15  package org.eclipse.jetty.security.authentication;
16  
17  import java.io.IOException;
18  import java.io.PrintWriter;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Locale;
22  
23  import javax.servlet.ServletOutputStream;
24  import javax.servlet.ServletRequest;
25  import javax.servlet.ServletResponse;
26  import javax.servlet.http.Cookie;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.eclipse.jetty.security.Authenticator;
30  import org.eclipse.jetty.security.IdentityService;
31  import org.eclipse.jetty.security.ServerAuthException;
32  import org.eclipse.jetty.server.Authentication;
33  import org.eclipse.jetty.util.IO;
34  import org.eclipse.jetty.util.log.Log;
35  
36  public class DeferredAuthentication implements Authentication.Deferred
37  {
38      protected final Authenticator _authenticator;
39  
40      private IdentityService _identityService;
41      private Object _previousAssociation;
42  
43      public DeferredAuthentication(Authenticator authenticator)
44      {
45          if (authenticator == null)
46              throw new NullPointerException("No Authenticator");
47          this._authenticator = authenticator;
48      }
49      
50      /* ------------------------------------------------------------ */
51      /** Get the identityService.
52       * @return the identityService
53       */
54      public IdentityService getIdentityService()
55      {
56          return _identityService;
57      }
58  
59      /* ------------------------------------------------------------ */
60      /** Set the identityService.
61       * @param identityService the identityService to set
62       */
63      public void setIdentityService(IdentityService identityService)
64      {
65          _identityService = identityService;
66      }
67  
68      /* ------------------------------------------------------------ */
69      /**
70       * @see org.eclipse.jetty.server.Authentication.Deferred#authenticate()
71       */
72      public Authentication authenticate(ServletRequest request)
73      {
74          try
75          {
76              Authentication authentication = _authenticator.validateRequest(request,__deferredResponse,true);
77              
78              if (authentication!=null && (authentication instanceof Authentication.User) && !(authentication instanceof Authentication.ResponseSent))
79              {
80                  if (_identityService!=null)
81                      _previousAssociation=_identityService.associate(((Authentication.User)authentication).getUserIdentity());
82                  return authentication;
83              }
84          }
85          catch (ServerAuthException e)
86          {
87              Log.debug(e);
88          }
89          return Authentication.UNAUTHENTICATED;
90      }
91      
92      /* ------------------------------------------------------------ */
93      /**
94       * @see org.eclipse.jetty.server.Authentication.Deferred#authenticate(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
95       */
96      public Authentication authenticate(ServletRequest request, ServletResponse response)
97      {
98          try
99          {
100             Authentication authentication = _authenticator.validateRequest(request,response,true);
101             if (authentication instanceof Authentication.User && _identityService!=null)
102                 _previousAssociation=_identityService.associate(((Authentication.User)authentication).getUserIdentity());
103             return authentication;
104         }
105         catch (ServerAuthException e)
106         {
107             Log.debug(e);
108         }
109         return Authentication.UNAUTHENTICATED;
110     }
111 
112     /* ------------------------------------------------------------ */
113     /**
114      * @see org.eclipse.jetty.server.Authentication.Deferred#login(java.lang.String, java.lang.String)
115      */
116     public Authentication login(String username, String password)
117     {
118         return null; // TODO implement
119     }
120 
121     /* ------------------------------------------------------------ */
122     public Object getPreviousAssociation()
123     {
124         return _previousAssociation;
125     }
126 
127     /* ------------------------------------------------------------ */
128     /**
129      * @param response
130      * @return true if this response is from a deferred call to {@link #authenticate(ServletRequest)}
131      */
132     public boolean isDeferred(HttpServletResponse response)
133     {
134         return response==__deferredResponse;
135     }
136     
137     /* ------------------------------------------------------------ */
138     /* ------------------------------------------------------------ */
139     /* ------------------------------------------------------------ */
140     static HttpServletResponse __deferredResponse = new HttpServletResponse()
141     {
142         public void addCookie(Cookie cookie)
143         {
144         }
145 
146         public void addDateHeader(String name, long date)
147         {
148         }
149 
150         public void addHeader(String name, String value)
151         {
152         }
153 
154         public void addIntHeader(String name, int value)
155         {
156         }
157 
158         public boolean containsHeader(String name)
159         {
160             return false;
161         }
162 
163         public String encodeRedirectURL(String url)
164         {
165             return null;
166         }
167 
168         public String encodeRedirectUrl(String url)
169         {
170             return null;
171         }
172 
173         public String encodeURL(String url)
174         {
175             return null;
176         }
177 
178         public String encodeUrl(String url)
179         {
180             return null;
181         }
182 
183         public void sendError(int sc) throws IOException
184         {
185         }
186 
187         public void sendError(int sc, String msg) throws IOException
188         {
189         }
190 
191         public void sendRedirect(String location) throws IOException
192         {
193         }
194 
195         public void setDateHeader(String name, long date)
196         {
197         }
198 
199         public void setHeader(String name, String value)
200         {
201         }
202 
203         public void setIntHeader(String name, int value)
204         {
205         }
206 
207         public void setStatus(int sc)
208         {
209         }
210 
211         public void setStatus(int sc, String sm)
212         {
213         }
214 
215         public void flushBuffer() throws IOException
216         {
217         }
218 
219         public int getBufferSize()
220         {
221             return 1024;
222         }
223 
224         public String getCharacterEncoding()
225         {
226             return null;
227         }
228 
229         public String getContentType()
230         {
231             return null;
232         }
233 
234         public Locale getLocale()
235         {
236             return null;
237         }
238 
239         public ServletOutputStream getOutputStream() throws IOException
240         {
241             return __nullOut;
242         }
243 
244         public PrintWriter getWriter() throws IOException
245         {
246             return IO.getNullPrintWriter();
247         }
248 
249         public boolean isCommitted()
250         {
251             return true;
252         }
253 
254         public void reset()
255         {
256         }
257 
258         public void resetBuffer()
259         {
260         }
261 
262         public void setBufferSize(int size)
263         {
264         }
265 
266         public void setCharacterEncoding(String charset)
267         {
268         }
269 
270         public void setContentLength(int len)
271         {
272         }
273 
274         public void setContentType(String type)
275         {
276         }
277 
278         public void setLocale(Locale loc)
279         {
280         }
281 
282 	public Collection<String> getHeaderNames()
283 	{
284 	    return Collections.emptyList();
285 	}
286 
287 	@Override
288 	public String getHeader(String arg0)
289 	{
290 	    return null;
291 	}
292 
293 	@Override
294 	public Collection<String> getHeaders(String arg0)
295 	{
296             return Collections.emptyList();
297 	}
298 
299 	@Override
300 	public int getStatus()
301 	{
302 	    return 0;
303 	}
304 
305     };
306 
307     /* ------------------------------------------------------------ */
308     /* ------------------------------------------------------------ */
309     /* ------------------------------------------------------------ */
310     private static ServletOutputStream __nullOut = new ServletOutputStream()
311     {
312         public void write(int b) throws IOException
313         {
314         }
315 
316         public void print(String s) throws IOException
317         {
318         }
319 
320         public void println(String s) throws IOException
321         {
322         }
323     };
324 
325     
326 }