View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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;
20  
21  import javax.servlet.ServletException;
22  import javax.security.auth.Subject;
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: 431706 $ $Date: 2006-08-15 14:19:27 -0700 (Tue, 15 Aug 2006) $
32   */
33  public class DefaultSubjectValve extends ValveBase {
34  
35      private final Subject defaultSubject;
36  
37      public DefaultSubjectValve(Subject defaultSubject) {
38          this.defaultSubject = defaultSubject;
39      }
40  
41      public void invoke(Request request, Response response) throws IOException, ServletException {
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  }