KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > tomcat > valve > DefaultSubjectValve


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

17 package org.apache.geronimo.tomcat.valve;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletException JavaDoc;
22 import javax.security.auth.Subject JavaDoc;
23
24 import org.apache.catalina.valves.ValveBase;
25 import org.apache.catalina.connector.Request;
26 import org.apache.catalina.connector.Response;
27 import org.apache.geronimo.security.ContextManager;
28 import org.apache.geronimo.security.Callers;
29
30 /**
31  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
32  */

33 public class DefaultSubjectValve extends ValveBase {
34
35     private final Subject JavaDoc defaultSubject;
36
37     public DefaultSubjectValve(Subject JavaDoc defaultSubject) {
38         this.defaultSubject = defaultSubject;
39     }
40
41     public void invoke(Request request, Response response) throws IOException JavaDoc, ServletException JavaDoc {
42         Callers oldCallers = null;
43         boolean setSubject = false;
44         if (defaultSubject != null) {
45             oldCallers = ContextManager.getCallers();
46             setSubject = oldCallers == null || oldCallers.getCurrentCaller() == null;
47         }
48         if (setSubject) {
49             ContextManager.setCallers(defaultSubject, defaultSubject);
50             try {
51                 getNext().invoke(request, response);
52             } finally {
53                 ContextManager.popCallers(oldCallers);
54             }
55         } else {
56             getNext().invoke(request, response);
57         }
58
59     }
60 }
61
Popular Tags