use of org.apache.axiom.soap.SOAPFactory in project carbon-apimgt by wso2.
the class Utils method setSOAPFault.
public static void setSOAPFault(MessageContext messageContext, String code, String reason, String detail) {
SOAPFactory factory = (messageContext.isSOAP11() ? OMAbstractFactory.getSOAP11Factory() : OMAbstractFactory.getSOAP12Factory());
OMDocument soapFaultDocument = factory.createOMDocument();
SOAPEnvelope faultEnvelope = factory.getDefaultFaultEnvelope();
soapFaultDocument.addChild(faultEnvelope);
SOAPFault fault = faultEnvelope.getBody().getFault();
if (fault == null) {
fault = factory.createSOAPFault();
}
SOAPFaultCode faultCode = factory.createSOAPFaultCode();
if (messageContext.isSOAP11()) {
faultCode.setText(new QName(fault.getNamespace().getNamespaceURI(), code));
} else {
SOAPFaultValue value = factory.createSOAPFaultValue(faultCode);
value.setText(new QName(fault.getNamespace().getNamespaceURI(), code));
}
fault.setCode(faultCode);
SOAPFaultReason faultReason = factory.createSOAPFaultReason();
if (messageContext.isSOAP11()) {
faultReason.setText(reason);
} else {
SOAPFaultText text = factory.createSOAPFaultText();
text.setText(reason);
text.setLang("en");
faultReason.addSOAPText(text);
}
fault.setReason(faultReason);
SOAPFaultDetail soapFaultDetail = factory.createSOAPFaultDetail();
soapFaultDetail.setText(detail);
fault.setDetail(soapFaultDetail);
// set the all headers of original SOAP Envelope to the Fault Envelope
if (messageContext.getEnvelope() != null) {
SOAPHeader soapHeader = messageContext.getEnvelope().getHeader();
if (soapHeader != null) {
for (Iterator iterator = soapHeader.examineAllHeaderBlocks(); iterator.hasNext(); ) {
Object o = iterator.next();
if (o instanceof SOAPHeaderBlock) {
SOAPHeaderBlock header = (SOAPHeaderBlock) o;
faultEnvelope.getHeader().addChild(header);
} else if (o instanceof OMElement) {
faultEnvelope.getHeader().addChild((OMElement) o);
}
}
}
}
try {
messageContext.setEnvelope(faultEnvelope);
} catch (AxisFault af) {
log.error("Error while setting SOAP fault as payload", af);
return;
}
if (messageContext.getFaultTo() != null) {
messageContext.setTo(messageContext.getFaultTo());
} else if (messageContext.getReplyTo() != null) {
messageContext.setTo(messageContext.getReplyTo());
} else {
messageContext.setTo(null);
}
// set original messageID as relatesTo
if (messageContext.getMessageID() != null) {
RelatesTo relatesTo = new RelatesTo(messageContext.getMessageID());
messageContext.setRelatesTo(new RelatesTo[] { relatesTo });
}
}
use of org.apache.axiom.soap.SOAPFactory in project wso2-axis2-transports by wso2.
the class TCPEchoRawXMLTest method testEchoXMLSyncMC.
public void testEchoXMLSyncMC() throws Exception {
AxisOperation opdesc = new OutInAxisOperation(new QName("echoOMElement"));
Options options = new Options();
options.setTo(targetEPR);
options.setAction(operationName.getLocalPart());
options.setTransportInProtocol(Constants.TRANSPORT_TCP);
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://localhost/my", "my");
OMElement method = fac.createOMElement("echoOMElement", omNs);
OMElement value = fac.createOMElement("myValue", omNs);
value.setText("Isaac Asimov, The Foundation Trilogy");
method.addChild(value);
SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope envelope = factory.getDefaultEnvelope();
envelope.getBody().addChild(method);
MessageContext requestContext = new MessageContext();
requestContext.setConfigurationContext(configContext);
requestContext.setAxisService(clientService);
requestContext.setAxisOperation(opdesc);
requestContext.setEnvelope(envelope);
ServiceClient sender = new ServiceClient(configContext, clientService);
sender.setOptions(options);
OperationClient opClient = sender.createClient(new QName("echoOMElement"));
opClient.addMessageContext(requestContext);
opClient.setOptions(options);
opClient.execute(true);
MessageContext response = opClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
SOAPEnvelope env = response.getEnvelope();
assertNotNull(env);
env.getBody().serialize(StAXUtils.createXMLStreamWriter(System.out));
sender.cleanup();
}
use of org.apache.axiom.soap.SOAPFactory in project wso2-axis2-transports by wso2.
the class XMPPPacketListener method createSOAPEnvelopeForRawMessage.
/**
* Creates a SOAP envelope using details found in chat message.
* @param msgCtx
* @param chatMessage
* @return
*/
private SOAPEnvelope createSOAPEnvelopeForRawMessage(MessageContext msgCtx, String chatMessage) throws AxisFault {
// TODO : need to add error handling logic
String callRemoved = chatMessage.replaceFirst("call", "");
// extract Service name
String serviceName = callRemoved.trim().substring(0, callRemoved.indexOf(":") - 1);
String operationName = callRemoved.trim().substring(callRemoved.indexOf(":"), callRemoved.indexOf("(") - 1);
// Extract parameters from IM message
String parameterList = callRemoved.trim().substring(callRemoved.indexOf("("), callRemoved.trim().length() - 1);
StringTokenizer st = new StringTokenizer(parameterList, ",");
MultipleEntryHashMap parameterMap = new MultipleEntryHashMap();
while (st.hasMoreTokens()) {
String token = st.nextToken();
String name = token.substring(0, token.indexOf("="));
String value = token.substring(token.indexOf("=") + 1);
parameterMap.put(name, value);
}
SOAPEnvelope envelope = null;
try {
msgCtx.setProperty(XMPPConstants.CONTAINS_SOAP_ENVELOPE, new Boolean(true));
if (serviceName != null && serviceName.trim().length() > 0) {
AxisService axisService = msgCtx.getConfigurationContext().getAxisConfiguration().getService(serviceName);
msgCtx.setAxisService(axisService);
AxisOperation axisOperation = axisService.getOperationBySOAPAction("urn:" + operationName);
if (axisOperation != null) {
msgCtx.setAxisOperation(axisOperation);
}
}
if (operationName != null && operationName.trim().length() > 0) {
msgCtx.setSoapAction("urn:" + operationName);
}
XMPPOutTransportInfo xmppOutTransportInfo = (XMPPOutTransportInfo) msgCtx.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO);
// This should be only set for messages received via chat.
// TODO : need to read from a constant
xmppOutTransportInfo.setContentType("xmpp/text");
msgCtx.setServerSide(true);
// TODO : need to support SOAP12 as well
SOAPFactory soapFactory = new SOAP11Factory();
envelope = BuilderUtil.buildsoapMessage(msgCtx, parameterMap, soapFactory);
// TODO : improve error handling & messages
} catch (AxisFault e) {
throw new AxisFault(e.getMessage());
} catch (OMException e) {
throw new AxisFault(e.getMessage());
} catch (FactoryConfigurationError e) {
throw new AxisFault(e.getMessage());
}
return envelope;
}
use of org.apache.axiom.soap.SOAPFactory in project wso2-axis2-transports by wso2.
the class SimpleInOutMessageReceiver method invokeBusinessLogic.
public void invokeBusinessLogic(MessageContext inMessageContext, MessageContext outMessageContext) throws AxisFault {
log.debug("Got The message to the MessageReceiver");
String soapNamespace = inMessageContext.getEnvelope().getNamespace().getNamespaceURI();
// creating a soap factory according the the soap namespce uri
SOAPFactory soapFactory = null;
if (soapNamespace.equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
soapFactory = OMAbstractFactory.getSOAP11Factory();
} else if (soapNamespace.equals(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
soapFactory = OMAbstractFactory.getSOAP12Factory();
} else {
System.out.println("Unknow soap message");
}
SOAPEnvelope responseEnvelope = soapFactory.getDefaultEnvelope();
// creating a body element
OMFactory omFactory = OMAbstractFactory.getOMFactory();
OMNamespace omNamespace = omFactory.createOMNamespace("http://sms.test", "ns1");
OMElement omElement = omFactory.createOMElement("Response", omNamespace);
omElement.setText("Sucess");
responseEnvelope.getBody().addChild(omElement);
outMessageContext.setEnvelope(responseEnvelope);
}
use of org.apache.axiom.soap.SOAPFactory in project webservices-axiom by apache.
the class TestCloneWithSourcedElement2 method runTest.
@Override
protected void runTest() throws Throwable {
SOAPEnvelope sourceEnv = soapFactory.getDefaultEnvelope();
SOAPBody body = sourceEnv.getBody();
SOAPHeader header = sourceEnv.getHeader();
// Create a header OMSE
OMDataSource dsHdr = new StringOMDataSource("<hdr:myheader xmlns:hdr=\"urn://test\">Hello World</hdr:myheader>");
OMNamespace hdrNS = header.getOMFactory().createOMNamespace("urn://test", "hdr");
SOAPFactory sf = (SOAPFactory) header.getOMFactory();
SOAPHeaderBlock shb = sf.createSOAPHeaderBlock("myheader", hdrNS, dsHdr);
// test setting processing flag
shb.setProcessed();
header.addChild(shb);
// Create a payload
OMDataSource ds = new StringOMDataSource("<tns:payload xmlns:tns=\"urn://test\">Hello World</tns:payload>");
OMNamespace ns = body.getOMFactory().createOMNamespace("urn://test", "tns");
OMSourcedElement omse = body.getOMFactory().createOMElement(ds, "payload", ns);
body.addChild(omse);
copyAndCheck(sourceEnv);
// The source SOAPHeaderBlock should not be expanded in the process
assertFalse(shb.isExpanded());
}
Aggregations