KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Authentication05


1 /*
2  * Copyright 1999, 2000, 2001 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import java.security.Principal JavaDoc;
22 import javax.servlet.*;
23 import javax.servlet.http.*;
24
25 /**
26  * Ensure that a resource protected a a security constratint that allows all
27  * roles will permit access to an authenticated user.
28  *
29  * @author Craig R. McClanahan
30  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:56 $
31  */

32
33 public class Authentication05 extends HttpServlet {
34
35     public void doGet(HttpServletRequest request, HttpServletResponse response)
36         throws IOException, ServletException {
37
38         response.setContentType("text/plain");
39         PrintWriter writer = response.getWriter();
40         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
41
42         String JavaDoc remoteUser = request.getRemoteUser();
43         if (remoteUser == null)
44             sb.append(" No remote user returned/");
45         else if (!"tomcat".equals(remoteUser)) {
46             sb.append(" Remote user is '");
47             sb.append(remoteUser);
48             sb.append("'/");
49         }
50
51         Principal JavaDoc userPrincipal = request.getUserPrincipal();
52         if (userPrincipal == null)
53             sb.append(" No user principal returned/");
54         else if (!"tomcat".equals(userPrincipal.getName())) {
55             sb.append(" User principal is '");
56             sb.append(userPrincipal);
57             sb.append("'/");
58         }
59
60         if (!request.isUserInRole("tomcat"))
61             sb.append(" Not in role 'tomcat'/");
62
63         if (sb.length() < 1)
64             writer.println("Authentication05 PASSED");
65         else {
66             writer.print("Authentication05 FAILED -");
67             writer.println(sb.toString());
68         }
69
70         while (true) {
71             String JavaDoc message = StaticLogger.read();
72             if (message == null)
73                 break;
74             writer.println(message);
75         }
76         StaticLogger.reset();
77
78     }
79
80
81 }
82
Popular Tags