/**
 *
 * Copyright 2003-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.geronimo.tomcat.interceptor;

import javax.security.jacc.PolicyContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class PolicyContextBeforeAfter implements BeforeAfter{
    
    private final BeforeAfter next;
    private final String policyContextID;
    private final int policyContextIDIndex;

    public PolicyContextBeforeAfter(BeforeAfter next, int policyContextIDIndex, String policyContextID) {
        this.next = next;
        this.policyContextIDIndex = policyContextIDIndex;
        this.policyContextID = policyContextID;
    }

    public void before(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse) {
        
        //Save the old
        context[policyContextIDIndex] = PolicyContext.getContextID();
        
        //Set the new
        PolicyContext.setContextID(policyContextID);
        PolicyContext.setHandlerData(httpRequest);
        
        if (next != null) {
            next.before(context, httpRequest, httpResponse);
        }
    }

    public void after(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse) {
        if (next != null) {
            next.after(context, httpRequest, httpResponse);
        }
        
        //Replace the old
        PolicyContext.setContextID((String)context[policyContextIDIndex]);
    }

}

@@ -673,16 +673,52 @@
 eptor;%0A%0A
+import javax.security.auth.Subject;%0A
 import j
@@ -751,16 +751,16 @@
 ontext;%0A
-
 import j
@@ -827,16 +827,69 @@
 ponse;%0A%0A
+import org.apache.geronimo.security.ContextManager;%0A%0A
 public c
@@ -1456,25 +1456,99 @@
         
-c
+PolicyHolder policyHolder = new PolicyHolder();%0A        policyHolder.setC
 ontext
-%5Bp
+Id(P
 olicyCon
@@ -1555,47 +1555,155 @@
 text
-IDIndex%5D = PolicyContext.getContextID()
+.getContextID());%0A        policyHolder.setSubject(ContextManager.getCurrentCaller());%0A        %0A        context%5BpolicyContextIDIndex%5D = policyHolder
 ;%0A  
@@ -2201,68 +2201,681 @@
 licy
-Context.setContextID((String)context%5BpolicyContextIDIndex%5D);
+Holder policyHolder = (PolicyHolder)context%5BpolicyContextIDIndex%5D;%0A        PolicyContext.setContextID(policyHolder.getContextId());%0A        ContextManager.setCurrentCaller(policyHolder.getSubject());%0A    %7D%0A    %0A    class PolicyHolder%7B%0A        %0A        private Subject subject;%0A        private String contextId;%0A        %0A        public String getContextId() %7B%0A            return contextId;%0A        %7D%0A        public void setContextId(String contextId) %7B%0A            this.contextId = contextId;%0A        %7D%0A        public Subject getSubject() %7B%0A            return subject;%0A        %7D%0A        public void setSubject(Subject subject) %7B%0A            this.subject = subject;%0A        %7D
 %0A   

Reset the current caller in the before afters