No problem. This is the code.
Code:
package de.cas.springws.webservice;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPMessage;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.sun.xml.wss.ProcessingContext;
import com.sun.xml.wss.XWSSProcessor;
import com.sun.xml.wss.XWSSProcessorFactory;
import com.sun.xml.wss.XWSSecurityException;
import com.sun.xml.wss.impl.callback.PasswordCallback;
import com.sun.xml.wss.impl.callback.UsernameCallback;
public class SecureEchoWSTest extends TestCase {
static {
PropertyConfigurator.configure(SecureEchoWSTest.class.getResource("/resources/test/log4j.properties"));
}
/** Logger for SecureEchoWSTest. */
private static final Logger LOG = Logger.getLogger(SecureEchoWSTest.class);
public static final String NAMESPACE_URI = "http://springws.cas.de";
public static final String PREFIX = "tns";
private SOAPConnectionFactory connectionFactory;
private MessageFactory messageFactory;
private URL url;
private XWSSProcessorFactory processorFactory;
protected void setUp() throws Exception {
super.setUp();
connectionFactory = SOAPConnectionFactory.newInstance();
messageFactory = MessageFactory.newInstance();
processorFactory = XWSSProcessorFactory.newInstance();
this.url = new URL("http://localhost:8080/springws/services");
}
public void testSecureEcho() throws SOAPException, IOException, XWSSecurityException {
String username = "Britta Glatt";
String password = "b";
SOAPMessage request = createSecureEchoRequest();
request = secureMessage(request, username, password);
SOAPConnection connection = connectionFactory.createConnection();
SOAPMessage response = connection.call(request, url);
if (!response.getSOAPBody().hasFault()) {
SOAPBodyElement echo = (SOAPBodyElement) response.getSOAPBody().getChildElements().next();
LOG.info("Echo is " + echo.getValue());
} else {
SOAPFault fault = response.getSOAPBody().getFault();
LOG.error("Received SOAP Fault");
LOG.error("SOAP Fault Code :" + fault.getFaultCode());
LOG.error("SOAP Fault String :" + fault.getFaultString());
}
}
private SOAPMessage createSecureEchoRequest() throws SOAPException {
SOAPMessage message = messageFactory.createMessage();
// I'm using payload mapping, not soap action.
// message.getMimeHeaders().addHeader("SOAPAction", "\"http://springws.cas.de/springws/services/secureEcho\"");
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name secureEchoRequestName = envelope.createName("secureEchoRequest", PREFIX, NAMESPACE_URI);
SOAPBodyElement requestElement = message.getSOAPBody().addBodyElement(secureEchoRequestName);
requestElement.addTextNode("helloIngo");
return message;
}
private SOAPMessage secureMessage(SOAPMessage message, final String username, final String password)
throws IOException, XWSSecurityException {
CallbackHandler callbackHandler = new CallbackHandler() {
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
LOG.debug("Callback handler for securing message got callback!");
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof UsernameCallback) {
UsernameCallback callback = (UsernameCallback) callbacks[i];
callback.setUsername(username);
} else if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback callback = (PasswordCallback) callbacks[i];
callback.setPassword(password);
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
}
};
InputStream policyStream = null;
XWSSProcessor processor = null;
try {
policyStream = getClass().getResourceAsStream("/deployment/web/WEB-INF/securityPolicy.xml");
processor = processorFactory.createProcessorForSecurityConfiguration(policyStream, callbackHandler);
} finally {
if (policyStream != null) {
policyStream.close();
}
}
ProcessingContext context = processor.createProcessingContext(message);
SOAPMessage secureMessage = processor.secureOutboundMessage(context);
// verify the secured message.
// ProcessingContext verifyContext = new ProcessingContext();
// verifyContext.setSOAPMessage(secureMessage);
//
// SOAPMessage verifiedMsg = null;
// try {
// verifiedMsg = processor.verifyInboundMessage(verifyContext);
// System.out.println("\nRequester Subject " + SubjectAccessor.getRequesterSubject(context));
// } catch (Exception ex){
// System.err.println("verify error:");
// ex.printStackTrace();
// System.err.println(verifyContext.getSOAPMessage().toString());
// }
return secureMessage;
}
}
Ingo