001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.xml.xwss;
021:
022: import java.io.InputStream;
023: import javax.security.auth.callback.CallbackHandler;
024:
025: import com.sun.xml.wss.SecurityEnvironment;
026: import com.sun.xml.wss.XWSSecurityException;
027: import com.sun.xml.wss.impl.XWSSecurityRuntimeException;
028: import com.sun.xml.wss.impl.config.SecurityConfigurationXmlReader;
029: import com.sun.xml.wss.impl.config.ApplicationSecurityConfiguration;
030: import com.sun.xml.wss.impl.misc.DefaultSecurityEnvironmentImpl;
031: import java.io.IOException;
032: import java.net.URL;
033:
034: /**
035: * Digester for XWS-Security configuration.
036: * @since JAXWS 2.0
037: */
038:
039: public class SecurityConfiguration implements XWSSecurityConfiguration {
040:
041: //public static final String MESSAGE_SECURITY_CONFIGURATION =
042: // "com.sun.xml.ws.security.configuration";
043:
044: private ApplicationSecurityConfiguration configuration = null;
045: private CallbackHandler callbackhandler = null;
046: private SecurityEnvironment securityEnvironment = null;
047: private boolean configEmpty = false;
048:
049: public SecurityConfiguration(URL configUrl)
050: throws XWSSecurityException {
051:
052: if (configUrl == null) {
053: configEmpty = true;
054: return;
055: }
056:
057: InputStream config = null;
058: try {
059: config = configUrl.openStream();
060:
061: if (config == null) {
062: configEmpty = true;
063: return;
064: }
065:
066: configuration = SecurityConfigurationXmlReader
067: .createApplicationSecurityConfiguration(config);
068: callbackhandler = (CallbackHandler) Class.forName(
069: configuration.getSecurityEnvironmentHandler(),
070: true,
071: Thread.currentThread().getContextClassLoader())
072: .newInstance();
073: securityEnvironment = new DefaultSecurityEnvironmentImpl(
074: callbackhandler);
075:
076: } catch (IOException e) {
077: throw new XWSSecurityException(e);
078: } catch (Exception e) {
079: throw new XWSSecurityException(e);
080: } finally {
081: try {
082: if (config != null) {
083: config.close();
084: }
085: } catch (IOException e) {
086: //do nothing
087: }
088: }
089: }
090:
091: /**
092: *
093: * @param config XWSS Security Configuration.
094: * @throws com.sun.xml.wss.XWSSecurityException is XWS-Security configuration file is not wellformed.
095: */
096: public SecurityConfiguration(InputStream config)
097: throws XWSSecurityException {
098:
099: if (config == null) {
100: configEmpty = true;
101: return;
102: }
103:
104: try {
105: configuration = SecurityConfigurationXmlReader
106: .createApplicationSecurityConfiguration(config);
107: callbackhandler = (CallbackHandler) Class.forName(
108: configuration.getSecurityEnvironmentHandler(),
109: true,
110: Thread.currentThread().getContextClassLoader())
111: .newInstance();
112: securityEnvironment = new DefaultSecurityEnvironmentImpl(
113: callbackhandler);
114: } catch (Exception e) {
115: throw new XWSSecurityException(e);
116: }
117: }
118:
119: /**
120: *
121: * @return digested form XWS-Security configuration.
122: */
123: public ApplicationSecurityConfiguration getSecurityConfiguration() {
124: return configuration;
125: }
126:
127: /**
128: *
129: * @return instance of SecurityEnvironment configured in the XWS-Security Configuration
130: * file.
131: */
132: public SecurityEnvironment getSecurityEnvironment() {
133: return securityEnvironment;
134: }
135:
136: /**
137: *
138: * @return
139: */
140: public boolean isEmpty() {
141: return configEmpty;
142: }
143: }
|