use of org.apache.axiom.soap.SOAPHeader in project webservices-axiom by apache.
the class SOAPTestCase method createSOAPHeaderBlock.
protected SOAPHeaderBlock createSOAPHeaderBlock() {
OMNamespace namespace = soapFactory.createOMNamespace("http://www.example.org", "test");
;
SOAPEnvelope soapEnvelope = soapFactory.createSOAPEnvelope();
SOAPHeader soapHeader = soapFactory.createSOAPHeader(soapEnvelope);
return soapFactory.createSOAPHeaderBlock("testHeaderBlock", namespace, soapHeader);
}
use of org.apache.axiom.soap.SOAPHeader in project carbon-business-process by wso2.
the class HTCoordinationContextHandler method invoke.
@Override
public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {
if (serverConfig == null || !serverConfig.isTaskRegistrationEnabled()) {
return InvocationResponse.CONTINUE;
}
SOAPHeader soapHeader;
try {
soapHeader = messageContext.getEnvelope().getHeader();
} catch (OMException ex) {
throw new AxisFault("Error while extracting SOAP header", ex);
}
if (soapHeader == null) {
if (log.isDebugEnabled()) {
log.debug("No SOAP Header received. Continuing as an uncoordinated HumanTask.");
}
return InvocationResponse.CONTINUE;
}
Iterator headers = soapHeader.getChildElements();
SOAPHeaderBlock coordinationHeaderBlock = null;
// Searching for WS-Coor Coordination Context
while (headers.hasNext()) {
SOAPHeaderBlock hb = (SOAPHeaderBlock) headers.next();
if (hb.getLocalName().equals(Constants.WS_COOR_COORDINATION_CONTEXT) && hb.getNamespace().getNamespaceURI().equals(Constants.WS_COOR_NAMESPACE)) {
coordinationHeaderBlock = hb;
break;
}
}
if (coordinationHeaderBlock == null) {
if (log.isDebugEnabled()) {
log.debug("No coordination context received. Processing as an uncoordinated HumanTask.");
}
return InvocationResponse.CONTINUE;
}
// We have received a ws-coordination context. Now validate it for HT coordination type
String coordinationType = SOAPUtils.getCoordinationType(coordinationHeaderBlock);
if (!Constants.WS_HT_COORDINATION_TYPE.equals(coordinationType)) {
// Found wrong coordination Type. We Only support http://docs.oasis-open.org/ns/bpel4people/ws-humantask/protocol/200803.
// So we cannot allow message to go forward.
String errorMsg = "Message aborted ! Invalid coordination type" + coordinationType + " . Support only " + Constants.WS_HT_COORDINATION_TYPE;
log.error(errorMsg);
return InvocationResponse.ABORT;
}
if (log.isDebugEnabled()) {
log.debug("HT coordination context received.");
}
String identifier = SOAPUtils.getCoordinationIdentifier(coordinationHeaderBlock);
String registrationService = SOAPUtils.getRegistrationService(coordinationHeaderBlock);
// validating values. These values cannot be empty
if (identifier == null || identifier.isEmpty() || registrationService == null || registrationService.isEmpty()) {
String errorMsg = "Message aborted ! Invalid coordination context parameters.";
log.error(errorMsg);
return InvocationResponse.ABORT;
}
// Service URL of the HumanTask Coordination Protocol Handler AdminService
String humanTaskProtocolHandlerServiceURL;
try {
humanTaskProtocolHandlerServiceURL = ServiceUtils.getTaskProtocolHandlerURL(messageContext.getConfigurationContext());
} catch (HumanTaskCoordinationException e) {
String errorMsg = "Error while generating HumanTask engine's protocol Handler Service URL.";
log.error(errorMsg);
throw new AxisFault(e.getLocalizedMessage(), e);
}
// We are OK to invokeRegistrationService Registration service
try {
OMElement response = invokeRegistrationServiceUsingServiceClient(identifier, humanTaskProtocolHandlerServiceURL, registrationService);
// But we are validating it for successful completion.
if (!SOAPUtils.validateResponse(response, identifier)) {
String errorMsg = "Message aborted ! registration response validation failed.";
log.error(errorMsg);
return InvocationResponse.ABORT;
}
// successful coordination
if (log.isDebugEnabled()) {
log.debug("RegistrationResponse received. Task is successfully coordinated with Task parent.");
}
} catch (AxisFault e) {
String errorMsg = "Error while invoking registration service";
log.error(errorMsg);
throw new AxisFault(e.getLocalizedMessage(), e);
}
return InvocationResponse.CONTINUE;
}
use of org.apache.axiom.soap.SOAPHeader in project carbon-apimgt by wso2.
the class UtilsTestCase method testSetSOAPFault.
@Test
public void testSetSOAPFault() {
SOAPEnvelope soapEnvelope = Mockito.mock(SOAPEnvelope.class);
SOAPHeader soapHeader = Mockito.mock(SOAPHeader.class);
Iterator iterator = Mockito.mock(Iterator.class);
OMNode soapHeaderBlock = Mockito.mock(OMNode.class);
Mockito.when(messageContext.getMessageID()).thenReturn("123");
Mockito.when(messageContext.getEnvelope()).thenReturn(soapEnvelope);
Mockito.when(soapEnvelope.getHeader()).thenReturn(soapHeader);
Mockito.when(soapHeader.examineAllHeaderBlocks()).thenReturn(iterator);
Mockito.when(iterator.hasNext()).thenReturn(true, false);
// Mockito.when(iterator.next()).thenReturn(omelement);
Mockito.when(iterator.next()).thenReturn(soapHeaderBlock);
Utils.setSOAPFault(messageContext, "", "code", "detail");
// No error has occurred. hence test passes.
Assert.assertTrue(true);
// when messageContext.isSOAP11() is true
Mockito.when(messageContext.isSOAP11()).thenReturn(true);
Utils.setSOAPFault(messageContext, "", "code", "detail");
// No error has occurred. hence test passes.
Assert.assertTrue(true);
// when messageContext.getFaultTo() != null
EndpointReference endpointReference = Mockito.mock(EndpointReference.class);
Mockito.when(messageContext.getFaultTo()).thenReturn(endpointReference);
Utils.setSOAPFault(messageContext, "", "code", "detail");
// No error has occurred. hence test passes.
Assert.assertTrue(true);
// when messageContext.getFaultTo() != null
Mockito.when(messageContext.getFaultTo()).thenReturn(endpointReference);
Utils.setSOAPFault(messageContext, "", "code", "detail");
// No error has occurred. hence test passes.
Assert.assertTrue(true);
}
use of org.apache.axiom.soap.SOAPHeader in project webservices-axiom by apache.
the class TestBuilder method runTest.
@Override
protected void runTest() throws Throwable {
String soap11Message = "<?xml version='1.0' ?>" + "<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" + " <env:Header>\n" + " <test:echoOk xmlns:test=\"http://example.org/ts-tests\"\n" + " env:actor=\"http://schemas.xmlsoap.org/soap/actor/next\"\n" + " env:mustUnderstand=\"1\"" + " >\n" + " foo\n" + " </test:echoOk>\n" + " </env:Header>\n" + " <env:Body>\n" + " <env:Fault>\n" + " <faultcode>\n" + " env:Sender\n" + " </faultcode>\n" + " <faultstring>\n" + " Sender Timeout\n" + " </faultstring>\n" + " <faultactor>\n" + " http://schemas.xmlsoap.org/soap/envelope/actor/ultimateReceiver\n" + " </faultactor>\n" + " <detail xmlns:m=\"http:www.sample.org\">\n" + " Details of error\n" + " <m:MaxTime m:detail=\"This is only a test\">\n" + " P5M\n" + " </m:MaxTime>\n" + " <m:AveTime>\n" + " <m:Time>\n" + " P3M\n" + " </m:Time>\n" + " </m:AveTime>\n" + " </detail>\n" + " <n:Test xmlns:n=\"http:www.Test.org\">\n" + " <n:TestElement>\n" + " This is only a test\n" + " </n:TestElement>\n" + " </n:Test>\n" + " </env:Fault>\n" + " </env:Body>\n" + "</env:Envelope>";
OMXMLParserWrapper soap11Builder = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, new StringReader(soap11Message));
SOAPEnvelope soap11Envelope = (SOAPEnvelope) soap11Builder.getDocumentElement();
// soap11Envelope.build();
// writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
// soap11Envelope.internalSerializeAndConsume(writer);
// writer.flush();
assertTrue("SOAP 1.1 :- envelope local name mismatch", soap11Envelope.getLocalName().equals(SOAPConstants.SOAPENVELOPE_LOCAL_NAME));
assertTrue("SOAP 1.1 :- envelope namespace uri mismatch", soap11Envelope.getNamespace().getNamespaceURI().equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI));
SOAPHeader header = soap11Envelope.getHeader();
assertTrue("SOAP 1.1 :- Header local name mismatch", header.getLocalName().equals(SOAPConstants.HEADER_LOCAL_NAME));
assertTrue("SOAP 1.1 :- Header namespace uri mismatch", header.getNamespace().getNamespaceURI().equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI));
SOAPHeaderBlock headerBlock = (SOAPHeaderBlock) header.getFirstElement();
assertTrue("SOAP 1.1 :- Header block name mismatch", headerBlock.getLocalName().equals("echoOk"));
assertTrue("SOAP 1.1 :- Header block name space uri mismatch", headerBlock.getNamespace().getNamespaceURI().equals("http://example.org/ts-tests"));
assertTrue("SOAP 1.1 :- Headaer block text mismatch", headerBlock.getText().trim().equals("foo"));
// Attribute iteration is not in any guaranteed order.
// Use QNames to get the OMAttributes.
QName actorQName = new QName(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI, SOAP11Constants.ATTR_ACTOR);
QName mustUnderstandQName = new QName(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI, SOAP11Constants.ATTR_MUSTUNDERSTAND);
OMAttribute actorAttribute = headerBlock.getAttribute(actorQName);
OMAttribute mustUnderstandAttribute = headerBlock.getAttribute(mustUnderstandQName);
assertTrue("SOAP 1.1 :- Mustunderstand attribute not found", mustUnderstandAttribute != null);
assertTrue("SOAP 1.1 :- Mustunderstand value mismatch", mustUnderstandAttribute.getAttributeValue().equals(SOAPConstants.ATTR_MUSTUNDERSTAND_1));
assertTrue("SOAP 1.1 :- Mustunderstand attribute namespace uri mismatch", mustUnderstandAttribute.getNamespace().getNamespaceURI().equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI));
assertTrue("SOAP 1.1 :- Actor attribute name not found", actorAttribute != null);
assertTrue("SOAP 1.1 :- Actor value mismatch", actorAttribute.getAttributeValue().trim().equals("http://schemas.xmlsoap.org/soap/" + SOAP11Constants.ATTR_ACTOR + "/" + "next"));
assertTrue("SOAP 1.1 :- Actor attribute namespace uri mismatch", actorAttribute.getNamespace().getNamespaceURI().equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI));
SOAPBody body = soap11Envelope.getBody();
assertTrue("SOAP 1.1 :- Body local name mismatch", body.getLocalName().equals(SOAPConstants.BODY_LOCAL_NAME));
assertTrue("SOAP 1.1 :- Body namespace uri mismatch", body.getNamespace().getNamespaceURI().equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI));
SOAPFault fault = body.getFault();
assertTrue("SOAP 1.1 :- Fault namespace uri mismatch", fault.getNamespace().getNamespaceURI().equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI));
Iterator<OMNode> iteratorInFault = fault.getChildren();
iteratorInFault.next();
SOAPFaultCode code = (SOAPFaultCode) iteratorInFault.next();
assertEquals("SOAP Fault code local name mismatch", code.getLocalName(), (SOAP11Constants.SOAP_FAULT_CODE_LOCAL_NAME));
assertEquals("SOAP 1.1 :- Fault code value mismatch", code.getText().trim(), "env:Sender");
iteratorInFault.next();
SOAPFaultReason reason = (SOAPFaultReason) iteratorInFault.next();
assertTrue("SOAP 1.1 :- Fault string local name mismatch", reason.getLocalName().equals(SOAP11Constants.SOAP_FAULT_STRING_LOCAL_NAME));
assertTrue("SOAP 1.1 :- Fault string value mismatch", reason.getText().trim().equals("Sender Timeout"));
iteratorInFault.next();
SOAPFaultRole role = (SOAPFaultRole) iteratorInFault.next();
assertTrue("SOAP 1.1 :- Fault actor local name mismatch", role.getLocalName().equals(SOAP11Constants.SOAP_FAULT_ACTOR_LOCAL_NAME));
assertTrue("SOAP 1.1 :- Actor value mismatch", role.getText().trim().equals("http://schemas.xmlsoap.org/soap/envelope/actor/ultimateReceiver"));
iteratorInFault.next();
SOAPFaultDetail detail = (SOAPFaultDetail) iteratorInFault.next();
assertTrue("SOAP 1.1 :- Fault detail local name mismatch", detail.getLocalName().equals(SOAP11Constants.SOAP_FAULT_DETAIL_LOCAL_NAME));
assertTrue("SOAP 1.2 :- Text in detail mismatch", detail.getText().trim().equals("Details of error"));
Iterator<OMNode> iteratorInDetail = detail.getChildren();
iteratorInDetail.next();
OMElement element1 = (OMElement) iteratorInDetail.next();
assertTrue("SOAP 1.1 :- MaxTime element mismatch", element1.getLocalName().equals("MaxTime"));
assertTrue("SOAP 1.1 :- MaxTime element namespace mismatch", element1.getNamespace().getNamespaceURI().equals("http:www.sample.org"));
assertTrue("SOAP 1.1 :- Text value in MaxTime element mismatch", element1.getText().trim().equals("P5M"));
Iterator<OMAttribute> attributeIterator = element1.getAllAttributes();
OMAttribute attributeInMaxTime = attributeIterator.next();
assertTrue("SOAP 1.1 :- Attribute local name mismatch", attributeInMaxTime.getLocalName().equals("detail"));
assertTrue("SOAP 1.1 :- Attribute namespace mismatch", attributeInMaxTime.getNamespace().getNamespaceURI().equals("http:www.sample.org"));
assertTrue("SOAP 1.1 :- Attribute value mismatch", attributeInMaxTime.getAttributeValue().equals("This is only a test"));
iteratorInDetail.next();
OMElement element2 = (OMElement) iteratorInDetail.next();
assertTrue("SOAP 1.1 :- AveTime element mismatch", element2.getLocalName().equals("AveTime"));
assertTrue("SOAP 1.1 :- AveTime element namespace mismatch", element2.getNamespace().getNamespaceURI().equals("http:www.sample.org"));
Iterator<OMNode> iteratorInAveTimeElement = element2.getChildren();
iteratorInAveTimeElement.next();
OMElement element21 = (OMElement) iteratorInAveTimeElement.next();
assertTrue("SOAP 1.1 :- Time element mismatch", element21.getLocalName().equals("Time"));
assertTrue("SOAP 1.1 :- Time element namespace mismatch", element21.getNamespace().getNamespaceURI().equals("http:www.sample.org"));
assertTrue("SOAP 1.1 :- Text value in Time element mismatch", element21.getText().trim().equals("P3M"));
iteratorInFault.next();
OMElement testElement = (OMElement) iteratorInFault.next();
assertTrue("SOAP 1.1 :- Test element mismatch", testElement.getLocalName().equals("Test"));
assertTrue("SOAP 1.1 :- Test element namespace mismatch", testElement.getNamespace().getNamespaceURI().equals("http:www.Test.org"));
OMElement childOfTestElement = testElement.getFirstElement();
assertTrue("SOAP 1.1 :- Test element child local name mismatch", childOfTestElement.getLocalName().equals("TestElement"));
assertTrue("SOAP 1.1 :- Test element child namespace mismatch", childOfTestElement.getNamespace().getNamespaceURI().equals("http:www.Test.org"));
assertTrue("SOAP 1.1 :- Test element child value mismatch", childOfTestElement.getText().trim().equals("This is only a test"));
soap11Builder.close();
}
use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.
the class HeaderMediator method mediate.
/**
* Sets/Removes a SOAP header on the current message
*
* @param synCtx the current message which is altered as necessary
* @return true always
*/
public boolean mediate(MessageContext synCtx) {
if (synCtx.getEnvironment().isDebuggerEnabled()) {
if (super.divertMediationRoute(synCtx)) {
return true;
}
}
SynapseLog synLog = getLog(synCtx);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Start : Header mediator");
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Message : " + synCtx.getEnvelope());
}
}
String value = (getExpression() == null ? getValue() : expression.stringValueOf(synCtx));
if (scope == null || XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
if (action == ACTION_SET) {
if (value == null) {
value = EMPTY_STRING;
log.warn("Setting SOAP header : " + qName + " to empty as evaluated value is null");
}
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Set SOAP header : " + qName + " to : " + value);
}
if (!isImplicit() && (qName.getNamespaceURI() == null || "".equals(qName.getNamespaceURI()))) {
// is this a "well known" Synapse header?
if (SynapseConstants.HEADER_TO.equals(qName.getLocalPart())) {
synCtx.setTo(new EndpointReference(value));
} else if (SynapseConstants.HEADER_FROM.equals(qName.getLocalPart())) {
synCtx.setFrom(new EndpointReference(value));
} else if (SynapseConstants.HEADER_ACTION.equals(qName.getLocalPart())) {
synCtx.setWSAAction(value);
} else if (SynapseConstants.HEADER_FAULT.equals(qName.getLocalPart())) {
synCtx.setFaultTo(new EndpointReference(value));
} else if (SynapseConstants.HEADER_REPLY_TO.equals(qName.getLocalPart())) {
synCtx.setReplyTo(new EndpointReference(value));
} else if (SynapseConstants.HEADER_RELATES_TO.equals(qName.getLocalPart())) {
synCtx.setRelatesTo(new RelatesTo[] { new RelatesTo(value) });
} else {
addCustomHeader(synCtx, value);
}
} else {
addCustomHeader(synCtx, value);
}
} else {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Removing SOAP Header : " + qName);
}
if (qName.getNamespaceURI() == null || "".equals(qName.getNamespaceURI())) {
// is this a "well known" Synapse header?
if (SynapseConstants.HEADER_TO.equals(qName.getLocalPart())) {
synCtx.setTo(null);
} else if (SynapseConstants.HEADER_FROM.equals(qName.getLocalPart())) {
synCtx.setFrom(null);
} else if (SynapseConstants.HEADER_ACTION.equals(qName.getLocalPart())) {
synCtx.setWSAAction(null);
} else if (SynapseConstants.HEADER_FAULT.equals(qName.getLocalPart())) {
synCtx.setFaultTo(null);
} else if (SynapseConstants.HEADER_REPLY_TO.equals(qName.getLocalPart())) {
synCtx.setReplyTo(null);
} else if (SynapseConstants.HEADER_RELATES_TO.equals(qName.getLocalPart())) {
synCtx.setRelatesTo(null);
} else {
SOAPEnvelope envelope = synCtx.getEnvelope();
if (envelope != null) {
SOAPHeader header = envelope.getHeader();
if (header != null) {
removeFromHeaderList(header.getHeaderBlocksWithNSURI(""));
}
}
}
} else {
SOAPEnvelope envelope = synCtx.getEnvelope();
if (envelope != null) {
SOAPHeader header = envelope.getHeader();
if (header != null) {
removeFromHeaderList(header.getHeaderBlocksWithNSURI(qName.getNamespaceURI()));
}
}
}
}
} else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope)) {
String headerName = qName.getLocalPart();
if (action == ACTION_SET) {
if (value == null) {
value = EMPTY_STRING;
log.warn("Setting HTTP header : " + headerName + " to empty as evaluated value is null");
}
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Set HTTP header : " + headerName + " to : " + value);
}
// Setting Transport Headers
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
if (headers != null && headers instanceof Map) {
Map headersMap = (Map) headers;
headersMap.put(headerName, value);
}
if (headers == null) {
Map<String, Object> headersMap = new TreeMap<>(new Comparator<String>() {
public int compare(String o1, String o2) {
return o1.compareToIgnoreCase(o2);
}
});
headersMap.put(headerName, value);
axis2MessageCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, headersMap);
}
} else {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Removing HTTP Header : " + qName);
}
// Removing transport headers
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
if (headers != null && headers instanceof Map) {
Map headersMap = (Map) headers;
headersMap.remove(headerName);
} else {
synLog.traceOrDebug("No transport headers found for the message");
}
}
}
// NOTE: We dont' use an else here because the HTTPMediatorFactory should capture cases where scope is not default or transport.
synLog.traceOrDebug("End : Header mediator");
return true;
}
Aggregations