KickJava   Java API By Example, From Geeks To Geeks.

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


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 we get the correct results from <code>isUserInRole()</code>
27  * for an actual role, a role aliased with a
28  * <code>&lt;security-role-ref&gt;</code> element, and for a role that is
29  * not assigned to the specified user.
30  *
31  * @author Craig R. McClanahan
32  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:56 $
33  */

34
35 public class Authentication03 extends HttpServlet {
36
37     public void doGet(HttpServletRequest request, HttpServletResponse response)
38         throws IOException, ServletException {
39
40         // Prepare to create this response
41
response.setContentType("text/plain");
42         PrintWriter writer = response.getWriter();
43         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
44
45         // Validate that we have been authenticated correctly
46
String JavaDoc remoteUser = request.getRemoteUser();
47         if (remoteUser == null) {
48             results.append(" Not Authenticated/");
49         } else if (!"tomcat".equals(remoteUser)) {
50             results.append(" Authenticated as '");
51             results.append(remoteUser);
52             results.append("'/");
53         }
54
55         // Validate that this user is part of the "tomcat" role
56
if (!request.isUserInRole("tomcat")) {
57             results.append(" Not in role 'tomcat'/");
58         }
59
60         // Validate that this user is part of the "alias" role
61
// (mapped to "tomcat" in a <security-role-ref> element
62
if (!request.isUserInRole("alias")) {
63             results.append(" Not in role 'alias'/");
64         }
65
66         // Validate that this user is NOT part of the "unknown" role
67
if (request.isUserInRole("unknown")) {
68             results.append(" In role 'unknown'/");
69         }
70
71         // Generate our response
72
if (results.length() < 1) {
73             writer.println("Authentication03 PASSED");
74         } else {
75             writer.print("Authentication03 FAILED -");
76             writer.println(results.toString());
77         }
78
79         // Add wrapper messages as required
80
while (true) {
81             String JavaDoc message = StaticLogger.read();
82             if (message == null)
83                 break;
84             writer.println(message);
85         }
86         StaticLogger.reset();
87
88
89     }
90
91 }
92
Popular Tags