01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.geronimo.tomcat.interceptor;
17:
18: import javax.security.jacc.PolicyContext;
19: import javax.security.auth.Subject;
20: import javax.servlet.ServletRequest;
21: import javax.servlet.ServletResponse;
22:
23: import org.apache.geronimo.security.Callers;
24: import org.apache.geronimo.security.ContextManager;
25:
26: public class PolicyContextBeforeAfter implements BeforeAfter {
27:
28: public static final String DEFAULT_SUBJECT = "~DEFAULT_SUBJECT";
29:
30: private final BeforeAfter next;
31: private final String policyContextID;
32: private final int policyContextIDIndex;
33: private final int callersIndex;
34: private final int defaultSubjectIndex;
35: private final Subject defaultSubject;
36:
37: public PolicyContextBeforeAfter(BeforeAfter next,
38: int policyContextIDIndex, int callersIndex,
39: int defaultSubjectIndex, String policyContextID,
40: Subject defaultSubject) {
41: this .next = next;
42: this .policyContextIDIndex = policyContextIDIndex;
43: this .callersIndex = callersIndex;
44: this .defaultSubjectIndex = defaultSubjectIndex;
45: this .policyContextID = policyContextID;
46: this .defaultSubject = defaultSubject;
47: }
48:
49: public void before(Object[] context, ServletRequest httpRequest,
50: ServletResponse httpResponse, int dispatch) {
51:
52: //Save the old
53:
54: context[policyContextIDIndex] = PolicyContext.getContextID();
55: context[callersIndex] = ContextManager.getCallers();
56:
57: //Set the new
58: PolicyContext.setContextID(policyContextID);
59: PolicyContext.setHandlerData(httpRequest);
60: if (httpRequest != null) {
61: context[defaultSubjectIndex] = httpRequest
62: .getAttribute(DEFAULT_SUBJECT);
63: httpRequest.setAttribute(DEFAULT_SUBJECT, defaultSubject);
64: }
65:
66: if (next != null) {
67: next.before(context, httpRequest, httpResponse, dispatch);
68: }
69: }
70:
71: public void after(Object[] context, ServletRequest httpRequest,
72: ServletResponse httpResponse, int dispatch) {
73: if (next != null) {
74: next.after(context, httpRequest, httpResponse, dispatch);
75: }
76:
77: //Replace the old
78: PolicyContext
79: .setContextID((String) context[policyContextIDIndex]);
80: ContextManager.popCallers((Callers) context[callersIndex]);
81: if (httpRequest != null)
82: httpRequest.setAttribute(DEFAULT_SUBJECT,
83: context[defaultSubjectIndex]);
84:
85: }
86:
87: }
|