Author: samisa Date: Wed Dec 22 10:56:01 2010 New Revision: 1051827 URL: http://svn.apache.org/viewvc?rev=1051827&view=rev Log: Fixed the issue of RampartEngine throwing ClassCastException retrieving SOAPHeaderBlocks - RAMPART-202
Modified: axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rampart/MessageBuilderTestBase.java axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rampart/RampartEngineTest.java Modified: axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rampart/MessageBuilderTestBase.java URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rampart/MessageBuilderTestBase.java?rev=1051827&r1=1051826&r2=1051827&view=diff ============================================================================== --- axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rampart/MessageBuilderTestBase.java (original) +++ axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rampart/MessageBuilderTestBase.java Wed Dec 22 10:56:01 2010 @@ -19,6 +19,8 @@ package org.apache.rampart; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axiom.soap.SOAPEnvelope; +import org.apache.axiom.soap.SOAP11Constants; +import org.apache.axiom.soap.SOAP12Constants; import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder; import org.apache.axis2.AxisFault; import org.apache.axis2.client.Options; @@ -63,8 +65,28 @@ public class MessageBuilderTestBase exte * @throws AxisFault */ protected MessageContext getMsgCtx() throws Exception { + return initMsgCtxFromMessage("test-resources/policy/soapmessage.xml"); + } + + /** + * Return a message context initialized with a SOAP 1.2 message. + * + * @throws XMLStreamException + * @throws FactoryConfigurationError + * @throws AxisFault + */ + protected MessageContext getMsgCtx12() throws Exception { + return initMsgCtxFromMessage("test-resources/policy/soapmessage.xml"); + } + + /** + * @throws XMLStreamException + * @throws FactoryConfigurationError + * @throws AxisFault + */ + private MessageContext initMsgCtxFromMessage(String messageResource) throws Exception { MessageContext ctx = new MessageContext(); - + AxisConfiguration axisConfiguration = new AxisConfiguration(); AxisService axisService = new AxisService("TestService"); axisConfiguration.addService(axisService); @@ -88,7 +110,7 @@ public class MessageBuilderTestBase exte XMLStreamReader reader = XMLInputFactory.newInstance(). - createXMLStreamReader(new FileInputStream("test-resources/policy/soapmessage.xml")); + createXMLStreamReader(new FileInputStream(messageResource)); ctx.setEnvelope(new StAXSOAPModelBuilder(reader, null).getSOAPEnvelope()); return ctx; } @@ -121,4 +143,15 @@ public class MessageBuilderTestBase exte } } + public String getContentTypeForEnvelope(SOAPEnvelope env) { + String contentType = SOAP11Constants.SOAP_11_CONTENT_TYPE; //default + if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(env.getNamespace().getNamespaceURI())) { + contentType = SOAP11Constants.SOAP_11_CONTENT_TYPE; + } + else if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(env.getNamespace().getNamespaceURI())) { + contentType = SOAP12Constants.SOAP_12_CONTENT_TYPE; + } + return contentType; + } + } Modified: axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rampart/RampartEngineTest.java URL: http://svn.apache.org/viewvc/axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rampart/RampartEngineTest.java?rev=1051827&r1=1051826&r2=1051827&view=diff ============================================================================== --- axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rampart/RampartEngineTest.java (original) +++ axis/axis2/java/rampart/trunk/modules/rampart-tests/src/test/java/org/apache/rampart/RampartEngineTest.java Wed Dec 22 10:56:01 2010 @@ -18,14 +18,23 @@ package org.apache.rampart; import java.io.ByteArrayInputStream; import java.util.Vector; +import java.util.ArrayList; +import java.security.cert.X509Certificate; import org.apache.axiom.om.OMElement; import org.apache.axiom.soap.SOAPEnvelope; import org.apache.axis2.builder.SOAPBuilder; import org.apache.axis2.context.MessageContext; import org.apache.axis2.engine.AxisEngine; +import org.apache.axis2.namespace.Constants; import org.apache.neethi.Policy; import org.apache.rampart.util.Axis2Util; +import org.apache.ws.security.WSConstants; +import org.apache.ws.security.WSSecurityEngineResult; +import org.apache.ws.security.handler.WSHandlerResult; +import org.apache.ws.security.handler.WSHandlerConstants; + +import javax.xml.namespace.QName; public class RampartEngineTest extends MessageBuilderTestBase { @@ -45,9 +54,10 @@ public class RampartEngineTest extends M RampartEngine engine = new RampartEngine(); engine.process(ctx); - } catch (RampartException e) { + } + catch (RampartException e) { assertEquals("Expected rampart to complain about missing security header", - "Missing wsse:Security header in request", e.getMessage()); + "Missing wsse:Security header in request", e.getMessage()); } } @@ -64,14 +74,74 @@ public class RampartEngineTest extends M builder.build(ctx); // Building the SOAP envelope from the OMElement + buildSOAPEnvelope(ctx); + + RampartEngine engine = new RampartEngine(); + Vector results = engine.process(ctx); + + /* + The principle purpose of the test case is to verify that the above processes + without throwing an exception. However, perform a minimal amount of validation on the + results. + */ + assertNotNull("RampartEngine returned null result", results); + //verify cert was stored + X509Certificate usedCert = null; + for (int i = 0; i < results.size(); i++) { + WSSecurityEngineResult wser = (WSSecurityEngineResult) results.get(i); + Integer action = (Integer) wser.get(WSSecurityEngineResult.TAG_ACTION); + if (action.intValue() == WSConstants.SIGN) { + //the result is for the signature, which contains the used certificate + usedCert = (X509Certificate) wser.get(WSSecurityEngineResult.TAG_X509_CERTIFICATE); + break; + } + } + assertNotNull("Result of processing did not include a certificate", usedCert); + } + + public void testValidSOAP12Message() throws Exception { + + MessageContext ctx = getMsgCtx12(); + + String policyXml = "test-resources/policy/rampart-asymm-binding-6-3des-r15.xml"; + Policy policy = loadPolicy(policyXml); + + ctx.setProperty(RampartMessageData.KEY_RAMPART_POLICY, policy); + + MessageBuilder builder = new MessageBuilder(); + builder.build(ctx); + + // Building the SOAP envelope from the OMElement + buildSOAPEnvelope(ctx); + + RampartEngine engine = new RampartEngine(); + Vector results = engine.process(ctx); + + /* + The principle purpose of the test case is to verify that the above processes + without throwing an exception. However, perform a minimal amount of validation on the + results. + */ + assertNotNull("RampartEngine returned null result", results); + //verify cert was stored + X509Certificate usedCert = null; + for (int i = 0; i < results.size(); i++) { + WSSecurityEngineResult wser = (WSSecurityEngineResult) results.get(i); + Integer action = (Integer) wser.get(WSSecurityEngineResult.TAG_ACTION); + if (action.intValue() == WSConstants.SIGN) { + //the result is for the signature, which contains the used certificate + usedCert = (X509Certificate) wser.get(WSSecurityEngineResult.TAG_X509_CERTIFICATE); + break; + } + } + assertNotNull("Result of processing did not include a certificate", usedCert); + } + + private void buildSOAPEnvelope(MessageContext ctx) throws Exception { SOAPBuilder soapBuilder = new SOAPBuilder(); SOAPEnvelope env = ctx.getEnvelope(); ByteArrayInputStream inStream = new ByteArrayInputStream(env.toString().getBytes()); - env = (SOAPEnvelope) soapBuilder.processDocument(inStream, "text/xml", ctx); + env = (SOAPEnvelope) soapBuilder.processDocument(inStream, getContentTypeForEnvelope(env), ctx); ctx.setEnvelope(env); - - RampartEngine engine = new RampartEngine(); - engine.process(ctx); - } }