working on SWS-207

This commit is contained in:
Arjen Poutsma
2008-02-08 04:11:44 +00:00
parent 915cd9462f
commit 8efae9f36c
2 changed files with 0 additions and 272 deletions

View File

@@ -1,175 +0,0 @@
/*
* Copyright 2006 the original author or authors.
*
* 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.springframework.ws.soap.security.xwss.callback;
import java.io.IOException;
import java.security.cert.X509Certificate;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
import com.sun.xml.wss.impl.callback.PasswordValidationCallback;
import com.sun.xml.wss.impl.callback.TimestampValidationCallback;
/**
* Represents a chain of <code>CallbackHandler</code>s. For each callback, each of the handlers is called in term. If a
* handler throws a <code>UnsupportedCallbackException</code>, the next handler is tried.
*
* @author Arjen Poutsma
* @since 1.0.0
*/
public class CallbackHandlerChain extends AbstractCallbackHandler {
private CallbackHandler[] callbackHandlers;
public CallbackHandlerChain(CallbackHandler[] callbackHandlers) {
this.callbackHandlers = callbackHandlers;
}
public void setCallbackHandlers(CallbackHandler[] callbackHandlers) {
this.callbackHandlers = callbackHandlers;
}
protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
if (callback instanceof CertificateValidationCallback) {
handleCertificateValidationCallback((CertificateValidationCallback) callback);
}
else if (callback instanceof PasswordValidationCallback) {
handlePasswordValidationCallback((PasswordValidationCallback) callback);
}
else if (callback instanceof TimestampValidationCallback) {
handleTimestampValidationCallback((TimestampValidationCallback) callback);
}
else {
boolean allUnsupported = true;
for (int i = 0; i < callbackHandlers.length; i++) {
CallbackHandler callbackHandler = callbackHandlers[i];
try {
callbackHandler.handle(new Callback[]{callback});
allUnsupported = false;
}
catch (UnsupportedCallbackException ex) {
// if an UnsupportedCallbackException occurs, go to the next handler
}
}
if (allUnsupported) {
throw new UnsupportedCallbackException(callback);
}
}
}
private void handleCertificateValidationCallback(CertificateValidationCallback callback) {
callback.setValidator(new CertificateValidatorChain(callback));
}
private void handlePasswordValidationCallback(PasswordValidationCallback callback) {
callback.setValidator(new PasswordValidatorChain(callback));
}
private void handleTimestampValidationCallback(TimestampValidationCallback callback) {
callback.setValidator(new TimestampValidatorChain(callback));
}
private class TimestampValidatorChain implements TimestampValidationCallback.TimestampValidator {
private TimestampValidationCallback callback;
private TimestampValidatorChain(TimestampValidationCallback callback) {
this.callback = callback;
}
public void validate(TimestampValidationCallback.Request request)
throws TimestampValidationCallback.TimestampValidationException {
for (int i = 0; i < callbackHandlers.length; i++) {
CallbackHandler callbackHandler = callbackHandlers[i];
try {
callbackHandler.handle(new Callback[]{callback});
callback.getResult();
}
catch (IOException e) {
throw new TimestampValidationCallback.TimestampValidationException(e);
}
catch (UnsupportedCallbackException e) {
// ignore
}
}
}
}
private class PasswordValidatorChain implements PasswordValidationCallback.PasswordValidator {
private PasswordValidationCallback callback;
private PasswordValidatorChain(PasswordValidationCallback callback) {
this.callback = callback;
}
public boolean validate(PasswordValidationCallback.Request request)
throws PasswordValidationCallback.PasswordValidationException {
boolean allUnsupported = true;
for (int i = 0; i < callbackHandlers.length; i++) {
CallbackHandler callbackHandler = callbackHandlers[i];
try {
callbackHandler.handle(new Callback[]{callback});
allUnsupported = false;
if (!callback.getResult()) {
return false;
}
}
catch (IOException e) {
throw new PasswordValidationCallback.PasswordValidationException(e);
}
catch (UnsupportedCallbackException e) {
// ignore
}
}
return !allUnsupported;
}
}
private class CertificateValidatorChain implements CertificateValidationCallback.CertificateValidator {
private CertificateValidationCallback callback;
private CertificateValidatorChain(CertificateValidationCallback callback) {
this.callback = callback;
}
public boolean validate(X509Certificate certificate)
throws CertificateValidationCallback.CertificateValidationException {
boolean allUnsupported = true;
for (int i = 0; i < callbackHandlers.length; i++) {
CallbackHandler callbackHandler = callbackHandlers[i];
try {
callbackHandler.handle(new Callback[]{callback});
allUnsupported = false;
if (!callback.getResult()) {
return false;
}
}
catch (IOException e) {
throw new CertificateValidationCallback.CertificateValidationException(e);
}
catch (UnsupportedCallbackException e) {
// ignore
}
}
return !allUnsupported;
}
}
}

View File

@@ -1,97 +0,0 @@
/*
* Copyright 2006 the original author or authors.
*
* 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.springframework.ws.soap.security.xwss;
import junit.framework.TestCase;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public abstract class XwssMessageInterceptorTestCase extends TestCase {
protected XwsSecurityInterceptor interceptor;
private MessageFactory messageFactory;
private Map namespaces;
protected final void setUp() throws Exception {
interceptor = new XwsSecurityInterceptor();
messageFactory = MessageFactory.newInstance();
namespaces = new HashMap();
namespaces.put("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
namespaces.put("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
namespaces.put("ds", "http://www.w3.org/2000/09/xmldsig#");
namespaces.put("xenc", "http://www.w3.org/2001/04/xmlenc#");
onSetup();
}
protected void assertXpathEvaluatesTo(String message,
String expectedValue,
String xpathExpression,
SOAPMessage soapMessage) {
XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
Document document = soapMessage.getSOAPPart();
String actualValue = expression.evaluateAsString(document);
assertEquals(message, expectedValue, actualValue);
}
protected void assertXpathExists(String message, String xpathExpression, SOAPMessage soapMessage) {
XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
Document document = soapMessage.getSOAPPart();
Node node = expression.evaluateAsNode(document);
assertNotNull(message, node);
}
protected void assertXpathNotExists(String message, String xpathExpression, SOAPMessage soapMessage) {
XPathExpression expression = XPathExpressionFactory.createXPathExpression(xpathExpression, namespaces);
Document document = soapMessage.getSOAPPart();
Node node = expression.evaluateAsNode(document);
assertNull(message, node);
}
protected SaajSoapMessage loadSaajMessage(String fileName) throws SOAPException, IOException {
MimeHeaders mimeHeaders = new MimeHeaders();
mimeHeaders.addHeader("Content-Type", "text/xml");
Resource resource = new ClassPathResource(fileName, getClass());
InputStream is = resource.getInputStream();
try {
assertTrue("Could not load SAAJ message [" + resource + "]", resource.exists());
is = resource.getInputStream();
return new SaajSoapMessage(messageFactory.createMessage(mimeHeaders, is));
}
finally {
is.close();
}
}
protected void onSetup() throws Exception {
}
}