use of org.apache.camel.component.cm.exceptions.XMLConstructionException in project camel by apache.
the class CMSenderOneMessageImpl method createXml.
private String createXml(final CMMessage message) {
try {
final ByteArrayOutputStream xml = new ByteArrayOutputStream();
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
// Get the DocumentBuilder
final DocumentBuilder docBuilder = factory.newDocumentBuilder();
// Create blank DOM Document
final DOMImplementation impl = docBuilder.getDOMImplementation();
final Document doc = impl.createDocument(null, "MESSAGES", null);
// ROOT Element es MESSAGES
final Element root = doc.getDocumentElement();
// AUTHENTICATION element
final Element authenticationElement = doc.createElement("AUTHENTICATION");
final Element productTokenElement = doc.createElement("PRODUCTTOKEN");
authenticationElement.appendChild(productTokenElement);
final Text productTokenValue = doc.createTextNode("" + productToken);
productTokenElement.appendChild(productTokenValue);
root.appendChild(authenticationElement);
// MSG Element
final Element msgElement = doc.createElement("MSG");
root.appendChild(msgElement);
// <FROM>VALUE</FROM>
final Element fromElement = doc.createElement("FROM");
fromElement.appendChild(doc.createTextNode(message.getSender()));
msgElement.appendChild(fromElement);
// <BODY>VALUE</BODY>
final Element bodyElement = doc.createElement("BODY");
bodyElement.appendChild(doc.createTextNode(message.getMessage()));
msgElement.appendChild(bodyElement);
// <TO>VALUE</TO>
final Element toElement = doc.createElement("TO");
toElement.appendChild(doc.createTextNode(message.getPhoneNumber()));
msgElement.appendChild(toElement);
// false
if (message.isUnicode()) {
final Element dcsElement = doc.createElement("DCS");
dcsElement.appendChild(doc.createTextNode("8"));
msgElement.appendChild(dcsElement);
}
// <REFERENCE>VALUE</REFERENCE> -Alfanum
final String id = message.getIdAsString();
if (id != null && !id.isEmpty()) {
final Element refElement = doc.createElement("REFERENCE");
refElement.appendChild(doc.createTextNode("" + message.getIdAsString()));
msgElement.appendChild(refElement);
}
// <MAXIMUMNUMBEROFMESSAGEPARTS>8</MAXIMUMNUMBEROFMESSAGEPARTS>
if (message.isMultipart()) {
final Element minMessagePartsElement = doc.createElement("MINIMUMNUMBEROFMESSAGEPARTS");
minMessagePartsElement.appendChild(doc.createTextNode("1"));
msgElement.appendChild(minMessagePartsElement);
final Element maxMessagePartsElement = doc.createElement("MAXIMUMNUMBEROFMESSAGEPARTS");
maxMessagePartsElement.appendChild(doc.createTextNode(Integer.toString(message.getMultiparts())));
msgElement.appendChild(maxMessagePartsElement);
}
// Creatate XML as String
final Transformer aTransformer = TransformerFactory.newInstance().newTransformer();
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
final Source src = new DOMSource(doc);
final Result dest = new StreamResult(xml);
aTransformer.transform(src, dest);
return xml.toString();
} catch (final TransformerException e) {
throw new XMLConstructionException(String.format("Cant serialize CMMessage %s", message), e);
} catch (final ParserConfigurationException e) {
throw new XMLConstructionException(String.format("Cant serialize CMMessage %s", message), e);
}
}
Aggregations