001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.tomcat.interceptor; 018 019 import javax.security.jacc.PolicyContext; 020 import javax.security.auth.Subject; 021 import javax.servlet.ServletRequest; 022 import javax.servlet.ServletResponse; 023 024 import org.apache.geronimo.security.Callers; 025 import org.apache.geronimo.security.ContextManager; 026 027 public class PolicyContextBeforeAfter implements BeforeAfter{ 028 029 public static final String DEFAULT_SUBJECT = "~DEFAULT_SUBJECT"; 030 031 private final BeforeAfter next; 032 private final String policyContextID; 033 private final int policyContextIDIndex; 034 private final int callersIndex; 035 private final int defaultSubjectIndex; 036 private final Subject defaultSubject; 037 038 public PolicyContextBeforeAfter(BeforeAfter next, int policyContextIDIndex, int callersIndex, int defaultSubjectIndex, String policyContextID, Subject defaultSubject) { 039 this.next = next; 040 this.policyContextIDIndex = policyContextIDIndex; 041 this.callersIndex = callersIndex; 042 this.defaultSubjectIndex = defaultSubjectIndex; 043 this.policyContextID = policyContextID; 044 this.defaultSubject = defaultSubject; 045 } 046 047 public void before(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse, int dispatch) { 048 049 //Save the old 050 051 context[policyContextIDIndex] = PolicyContext.getContextID(); 052 context[callersIndex] = ContextManager.getCallers(); 053 054 //Set the new 055 PolicyContext.setContextID(policyContextID); 056 PolicyContext.setHandlerData(httpRequest); 057 if (httpRequest != null){ 058 context[defaultSubjectIndex] = httpRequest.getAttribute(DEFAULT_SUBJECT); 059 httpRequest.setAttribute(DEFAULT_SUBJECT, defaultSubject); 060 } 061 062 063 if (next != null) { 064 next.before(context, httpRequest, httpResponse, dispatch); 065 } 066 } 067 068 public void after(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse, int dispatch) { 069 if (next != null) { 070 next.after(context, httpRequest, httpResponse, dispatch); 071 } 072 073 //Replace the old 074 PolicyContext.setContextID((String)context[policyContextIDIndex]); 075 ContextManager.popCallers((Callers) context[callersIndex]); 076 if (httpRequest != null) 077 httpRequest.setAttribute(DEFAULT_SUBJECT, context[defaultSubjectIndex]); 078 079 } 080 081 } 082