001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: JPolicyContextHandler.java 4620 2004-04-19 09:10:25Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.security.jacc;
026:
027: import javax.security.jacc.PolicyContextException;
028: import javax.security.jacc.PolicyContextHandler;
029:
030: import org.objectweb.jonas.common.Log;
031:
032: import org.objectweb.util.monolog.api.BasicLevel;
033: import org.objectweb.util.monolog.api.Logger;
034:
035: /**
036: * JOnAS class which contains all Context Handler to register inside the
037: * PolicyContext
038: * @author Florent Benoit
039: * @see jacc Section 4.6.1
040: */
041: public class JPolicyContextHandler implements PolicyContextHandler {
042:
043: /**
044: * Container Subject Policy Context Handler
045: * @see jacc section 4.6.1.1
046: */
047: private static final String CONTAINER_SUBJECT = "javax.security.auth.Subject.container";
048:
049: /**
050: * SOAPMessage Policy Context Handler
051: * @see jacc 4.6.1.2
052: */
053: private static final String SOAP_MESSAGE = "javax.xml.soap.SOAPMessage";
054:
055: /**
056: * HttpServletRequest Policy Context Handler
057: * @see jacc 4.6.1.3
058: */
059: private static final String HTTP_SERVLET_REQUEST = "javax.servlet.http.HttpServletRequest";
060:
061: /**
062: * EnterpriseBean Policy Context Handler
063: * @see jacc 4.6.1.4
064: */
065: private static final String ENTERPRISE_BEAN = "javax.ejb.EnterpriseBean";
066:
067: /**
068: * EJB Arguments Policy Context Handler
069: * @see jacc 4.6.1.5
070: */
071: private static final String EJB_ARGUMENTS = "javax.ejb.arguments";
072:
073: /**
074: * List of supported Context Handler
075: */
076: private static final String[] SUPPORTED_KEYS = new String[] {
077: CONTAINER_SUBJECT, SOAP_MESSAGE, HTTP_SERVLET_REQUEST,
078: ENTERPRISE_BEAN, EJB_ARGUMENTS };
079:
080: /**
081: * Logger
082: */
083: private static Logger logger = Log
084: .getLogger(Log.JONAS_JACC_SECURITY_PREFIX);
085:
086: /**
087: * Default constructor
088: */
089: public JPolicyContextHandler() {
090: super ();
091:
092: }
093:
094: /**
095: * @param key the supported key
096: * @return true if the key is supported, else false.
097: * @throws PolicyContextException (never used here)
098: * @see javax.security.jacc.PolicyContextHandler#supports(java.lang.String)
099: */
100: public boolean supports(String key) throws PolicyContextException {
101: for (int k = 0; k < SUPPORTED_KEYS.length; k++) {
102: if (key.equals(SUPPORTED_KEYS[k])) {
103: return true;
104: }
105: }
106: return false;
107: }
108:
109: /**
110: * Supported keys by this JOnAS Context Handler
111: * @see javax.security.jacc.PolicyContextHandler#getKeys()
112: */
113: public String[] getKeys() throws PolicyContextException {
114: return SUPPORTED_KEYS;
115: }
116:
117: /**
118: * @param key the key for the object that we want to retrieve
119: * @param data the handler data given by PolicyContext class
120: * @return the object for the specified key and with the given data object
121: * @throws PolicyContextException (never used)
122: * @see javax.security.jacc.PolicyContextHandler#getContext(java.lang.String,
123: * java.lang.Object)
124: */
125: public Object getContext(String key, Object data)
126: throws PolicyContextException {
127:
128: if (logger.isLoggable(BasicLevel.DEBUG)) {
129: logger.log(BasicLevel.DEBUG, "Asking key" + key);
130: }
131:
132: // Handle only JOnAS HandleData
133: if (data == null
134: || !(data instanceof JPolicyContextHandlerData)) {
135: logger
136: .log(BasicLevel.WARN,
137: "No data object or Data object not instance of JPolicyContextHandlerData");
138: return null;
139: }
140:
141: Object contextObject = null;
142: JPolicyContextHandlerData jPolicyContextHandlerData = (JPolicyContextHandlerData) data;
143:
144: if (key.equals(HTTP_SERVLET_REQUEST)) {
145: if (logger.isLoggable(BasicLevel.DEBUG)) {
146: logger.log(BasicLevel.DEBUG, "Key == '"
147: + HTTP_SERVLET_REQUEST + "'");
148: }
149: contextObject = jPolicyContextHandlerData
150: .getHttpServletRequest();
151: } else if (key.equals(EJB_ARGUMENTS)) {
152: if (logger.isLoggable(BasicLevel.DEBUG)) {
153: logger.log(BasicLevel.DEBUG, "Key == '" + EJB_ARGUMENTS
154: + "'");
155: }
156: contextObject = jPolicyContextHandlerData.getEjbArguments();
157: } else if (key.equals(CONTAINER_SUBJECT)) {
158: if (logger.isLoggable(BasicLevel.DEBUG)) {
159: logger.log(BasicLevel.DEBUG, "Key == '"
160: + CONTAINER_SUBJECT + "'");
161: }
162: contextObject = jPolicyContextHandlerData
163: .getContainerSubject();
164:
165: }
166:
167: if (logger.isLoggable(BasicLevel.DEBUG)) {
168: logger.log(BasicLevel.DEBUG, "Returning object '"
169: + contextObject + "' for key '" + key + "'");
170: }
171: return contextObject;
172:
173: }
174:
175: }
|