use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.
the class MessageHelper method cloneSOAPEnvelope.
/**
* This method will clone the provided SOAPEnvelope and returns the cloned envelope
* as an exact copy of the provided envelope
*
* @param envelope - this will be cloned to get the new envelope
* @return cloned SOAPEnvelope from the provided one
*/
public static SOAPEnvelope cloneSOAPEnvelope(SOAPEnvelope envelope) {
SOAPFactory fac;
if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(envelope.getBody().getNamespace().getNamespaceURI())) {
fac = OMAbstractFactory.getSOAP11Factory();
} else {
fac = OMAbstractFactory.getSOAP12Factory();
}
SOAPEnvelope newEnvelope = fac.getDefaultEnvelope();
Iterator childIterator;
if (envelope.getHeader() != null) {
SOAPHeader body = envelope.getHeader();
childIterator = body.getChildren();
while (childIterator.hasNext()) {
Object bodyNs = childIterator.next();
if (bodyNs instanceof SOAPHeaderBlock) {
try {
newEnvelope.getHeader().addChild(ElementHelper.toSOAPHeaderBlock(((OMElement) bodyNs).cloneOMElement(), fac));
} catch (Exception e) {
handleException(e);
}
} else if (bodyNs instanceof OMElement) {
newEnvelope.getHeader().addChild(((OMElement) bodyNs).cloneOMElement());
}
}
}
if (envelope.getBody() != null) {
// fault would lead to class cast exceptions if accessed through the getFault method
if (envelope.getBody().getFirstElement() instanceof SOAPFault && envelope.getBody().hasFault()) {
SOAPFault fault = envelope.getBody().getFault();
newEnvelope.getBody().addFault(cloneSOAPFault(fault));
} else {
OMElement body = envelope.getBody().cloneOMElement();
Iterator ns = body.getAllDeclaredNamespaces();
OMNamespace bodyNs = body.getNamespace();
String nsUri = bodyNs.getNamespaceURI();
String nsPrefix = bodyNs.getPrefix();
while (ns.hasNext()) {
OMNamespace namespace = ((OMNamespace) ns.next());
if (nsUri != null && !nsUri.equals(namespace.getNamespaceURI()) && nsPrefix != null && !nsPrefix.equals(namespace.getPrefix())) {
newEnvelope.getBody().declareNamespace(namespace);
}
ns.remove();
}
Iterator attributes = body.getAllAttributes();
while (attributes.hasNext()) {
OMAttribute attrb = (OMAttribute) attributes.next();
newEnvelope.getBody().addAttribute(attrb);
attributes.remove();
}
Iterator itr = body.getChildren();
while (itr.hasNext()) {
OMNode node = (OMNode) itr.next();
itr.remove();
newEnvelope.getBody().addChild(node);
}
}
}
return newEnvelope;
}
use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.
the class MessageConverter method getSoapEnvelope.
private static SOAPEnvelope getSoapEnvelope(String soapEnvelpe) {
OMElement response;
try {
response = AXIOMUtil.stringToOM(soapEnvelpe);
} catch (Exception e) {
logger.error("Cannot create SOAP Envelop. Error:" + e.getLocalizedMessage(), e);
return null;
}
SOAPFactory soapFactory;
String soapNamespace = response.getNamespace().getNamespaceURI();
if (soapNamespace.equals(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
soapFactory = OMAbstractFactory.getSOAP12Factory();
} else {
soapFactory = OMAbstractFactory.getSOAP11Factory();
}
SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope();
// Set the headers of the message
if (response.getFirstElement().getLocalName().contains(HEADER_STRING)) {
SOAPHeader header = soapEnvelope.getHeader();
SOAPFactory fac = (SOAPFactory) soapEnvelope.getOMFactory();
Iterator headers = response.getFirstElement().getChildElements();
while (headers.hasNext()) {
OMElement soapHeader = (OMElement) headers.next();
SOAPHeaderBlock hb = header.addHeaderBlock(soapHeader.getLocalName(), fac.createOMNamespace(soapHeader.getNamespace().getNamespaceURI(), soapHeader.getNamespace().getPrefix()));
hb.setText(soapHeader.getText());
// Add the child elements of the header (if any)
Iterator elements = soapHeader.getChildElements();
while (elements.hasNext()) {
hb.addChild((OMNode) elements.next());
}
}
response.getFirstElement().detach();
}
if (response.getFirstElement().getFirstElement() != null) {
soapEnvelope.getBody().addChild(response.getFirstElement().getFirstElement());
}
return soapEnvelope;
}
use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.
the class RelaySecuirtyMessageBuilderDispatchandler method invoke.
@Override
public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {
InvocationResponse invocationResponse = super.invoke(messageContext);
EndpointReference toEPR = messageContext.getTo();
Pipe pipe = (Pipe) messageContext.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
if (pipe != null) {
if (toEPR != null) {
ConfigurationContext configurationContext = messageContext.getConfigurationContext();
AxisConfiguration registry = configurationContext.getAxisConfiguration();
String filePart = toEPR.getAddress();
if (filePart != null) {
String serviceOpPart = Utils.getServiceAndOperationPart(filePart, messageContext.getConfigurationContext().getServiceContextPath());
AxisService axisService = null;
// only service context path onwards values will be taken
if (messageContext.getConfigurationContext().getServiceContextPath() != null && serviceOpPart != null) {
axisService = registry.getService(serviceOpPart);
if (axisService != null) {
Parameter parameter = axisService.getParameter(SERVICE_TYPE);
if (parameter != null) {
if (!parameter.getValue().equals(PROXY)) {
build(messageContext);
}
} else {
build(messageContext);
}
}
}
}
}
if (messageContext.isEngaged(PassThroughConstants.SECURITY_MODULE_NAME)) {
SOAPHeader header = null;
if (messageContext.getEnvelope().getHeader() != null) {
header = messageContext.getEnvelope().getHeader();
}
build(messageContext);
this.handlePOXRequests(messageContext, header);
}
}
return invocationResponse;
}
use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.
the class NashornJavaScriptMessageContext method addHeader.
/**
* Add a new SOAP header to the message.
*
* @param mustUnderstand the value for the <code>soapenv:mustUnderstand</code> attribute
* @param content the XML for the new header
* @throws ScriptException if an error occurs when converting the XML to OM
*/
public void addHeader(boolean mustUnderstand, Object content) throws ScriptException {
SOAPEnvelope envelope = mc.getEnvelope();
SOAPFactory factory = (SOAPFactory) envelope.getOMFactory();
SOAPHeader header = envelope.getHeader();
if (header == null) {
header = factory.createSOAPHeader(envelope);
}
OMElement element = xmlHelper.toOMElement(content);
// We can't add the element directly to the SOAPHeader. Instead, we need to copy the
// information over to a SOAPHeaderBlock.
SOAPHeaderBlock headerBlock = header.addHeaderBlock(element.getLocalName(), element.getNamespace());
for (Iterator it = element.getAllAttributes(); it.hasNext(); ) {
headerBlock.addAttribute((OMAttribute) it.next());
}
headerBlock.setMustUnderstand(mustUnderstand);
OMNode child = element.getFirstOMChild();
while (child != null) {
// Get the next child before addChild will detach the node from its original place.
OMNode next = child.getNextOMSibling();
headerBlock.addChild(child);
child = next;
}
}
use of org.apache.axiom.soap.SOAPHeader in project wso2-synapse by wso2.
the class StockQuoteSampleClient method buildSoapEnvelope.
private SOAPEnvelope buildSoapEnvelope(String clientID, String value) {
SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory();
SOAPEnvelope envelope = soapFactory.createSOAPEnvelope();
SOAPHeader header = soapFactory.createSOAPHeader();
envelope.addChild(header);
OMNamespace synNamespace = soapFactory.createOMNamespace("http://ws.apache.org/ns/synapse", "syn");
OMElement clientIDElement = soapFactory.createOMElement("ClientID", synNamespace);
clientIDElement.setText(clientID);
header.addChild(clientIDElement);
SOAPBody body = soapFactory.createSOAPBody();
envelope.addChild(body);
OMElement valueElement = soapFactory.createOMElement("Value", null);
valueElement.setText(value);
body.addChild(valueElement);
return envelope;
}
Aggregations