1

Basically my dataHandler content should be a string referencing the id of the soap attachment but my Stub puts the base64string based on the DataHandler object passed to the Stub instead.

How can I "hack" into the body of the message before sending?

env.getBody().toString().replaceAll(pattern, "<dataHandler>cid:" + cid + "</dataHandler") does exactly what I want but env.getBody().setText doesn't set the text of the body**

// create a message context
_messageContext = new org.apache.axis2.context.MessageContext();

// Modified
// Attachment
String cid = _messageContext.addAttachment(dispatchDocumentRequest8.getDataDescription().getDataHandler());

// create SOAP envelope with that payload
org.apache.axiom.soap.SOAPEnvelope env = null;


env = toEnvelope(getFactory(_operationClient.getOptions().getSoapVersionURI()),
    dispatchDocumentRequest8,
    optimizeContent(new javax.xml.namespace.QName("com.test",
    "dispatchDocument")));

// Modified
String pattern = "(?s)<dataHandler[^>]*>.*?</dataHandler>";

env.getBody().setText(env.getBody().toString().replaceAll(pattern, " 
<dataHandler>cid:" + cid + "</dataHandler"));
CodeMatrix
  • 2,124
  • 1
  • 18
  • 30
  • Is there an `env.setBody` ? EDIT: actually I think it's `env.addBody` – Matt Dec 13 '18 at 13:16
  • Also depending on what versions etc you are using, possibly `env.getBody().setTextContent` – Matt Dec 13 '18 at 13:20
  • @Matt there is no setTextContent nor addBody. Axiom 1.2.12 – user10753685 Dec 13 '18 at 13:23
  • SOAPEnvelope is an interface, what is the concrete type you are getting back from the 'toEnvelope' method? You could try get your string, do your replace, and create a new envelope – Matt Dec 13 '18 at 13:29

1 Answers1

0

Never ever parse XML / HTML / YML / any-other-ML using regular expressions!

Use appropriate parsers. There are three different parsers for XML: DOM, SAX, StAX. But in your case you already have XML via getBody()!

final Document doc = env.getBody().extractContentAsDocument();
final NodeList nodes = doc.getElementsByTagName("dataHandler")

// Do stuff like
IntStream.range(0, nodes.getLength()).mapToObj(nodes::item).forEach(item->{
    item.setNodeValue(…);
});
madhead
  • 31,729
  • 16
  • 153
  • 201