|
1 /* |
|
2 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package sun.net.httpserver; |
|
27 |
|
28 import com.sun.net.httpserver.*; |
|
29 import java.io.*; |
|
30 import java.nio.*; |
|
31 import java.nio.channels.*; |
|
32 import java.util.*; |
|
33 import javax.security.auth.*; |
|
34 import javax.security.auth.callback.*; |
|
35 import javax.security.auth.login.*; |
|
36 |
|
37 public class AuthFilter extends Filter { |
|
38 |
|
39 private Authenticator authenticator; |
|
40 |
|
41 public AuthFilter (Authenticator authenticator) { |
|
42 this.authenticator = authenticator; |
|
43 } |
|
44 |
|
45 public String description () { |
|
46 return "Authentication filter"; |
|
47 } |
|
48 |
|
49 public void setAuthenticator (Authenticator a) { |
|
50 authenticator = a; |
|
51 } |
|
52 |
|
53 public void consumeInput (HttpExchange t) throws IOException { |
|
54 InputStream i = t.getRequestBody(); |
|
55 byte[] b = new byte [4096]; |
|
56 while (i.read (b) != -1); |
|
57 i.close (); |
|
58 } |
|
59 |
|
60 /** |
|
61 * The filter's implementation, which is invoked by the server |
|
62 */ |
|
63 public void doFilter (HttpExchange t, Filter.Chain chain) throws IOException |
|
64 { |
|
65 if (authenticator != null) { |
|
66 Authenticator.Result r = authenticator.authenticate (t); |
|
67 if (r instanceof Authenticator.Success) { |
|
68 Authenticator.Success s = (Authenticator.Success)r; |
|
69 ExchangeImpl e = ExchangeImpl.get (t); |
|
70 e.setPrincipal (s.getPrincipal()); |
|
71 chain.doFilter (t); |
|
72 } else if (r instanceof Authenticator.Retry) { |
|
73 Authenticator.Retry ry = (Authenticator.Retry)r; |
|
74 consumeInput (t); |
|
75 t.sendResponseHeaders (ry.getResponseCode(), -1); |
|
76 } else if (r instanceof Authenticator.Failure) { |
|
77 Authenticator.Failure f = (Authenticator.Failure)r; |
|
78 consumeInput (t); |
|
79 t.sendResponseHeaders (f.getResponseCode(), -1); |
|
80 } |
|
81 } else { |
|
82 chain.doFilter (t); |
|
83 } |
|
84 } |
|
85 } |