@Test public void testSerialize_WithContentEncoding_FromMessage() throws Exception { String myName = testName.getMethodName(); Notification n = new Notification(myName, myName, 1, myName); n.setUserData(new Object()); XmlNotificationSerializer serializer = new XmlNotificationSerializer(); AdaptrisMessageFactory factory = new DefaultMessageFactory(); factory.setDefaultCharEncoding("ISO-8859-1"); AdaptrisMessage msg = serializer.serialize(n, factory.newMessage()); XmlUtils utils = XmlHelper.createXmlUtils(msg, null, null); assertNotNull(msg.getObjectHeaders().get(NotificationSerializer.OBJ_METADATA_USERDATA)); assertEquals(myName, utils.getSingleTextItem("/Notification/Message")); assertEquals(myName, utils.getSingleTextItem("/Notification/Type")); assertEquals(myName, utils.getSingleTextItem("/Notification/Source")); assertEquals("1", utils.getSingleTextItem("/Notification/SequenceNumber")); assertNotNull(utils.getSingleTextItem("/Notification/Timestamp")); assertEquals("ISO-8859-1", msg.getContentEncoding()); }
@Override public AdaptrisMessage serialize(Notification n, AdaptrisMessage msg) throws CoreException, IOException { try { XmlUtils xmlBuilder = new XmlUtils(); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = doc.createElement(XML_ROOT_ELEMENT); doc.appendChild(root); xmlBuilder.setSource(doc); for (NotificationElement e : NotificationElement.values()) { xmlBuilder.setNodeValue(e.xpathToNode(), e.getValue(n)); } try (OutputStream out = msg.getOutputStream()) { String encoding = XmlHelper.getXmlEncoding(msg, getOutputMessageEncoding()); xmlBuilder.writeDocument(out, encoding); msg.setContentEncoding(encoding); } if (n.getUserData() != null) { msg.getObjectHeaders().put(OBJ_METADATA_USERDATA, n.getUserData()); } } catch (Exception e) { throw ExceptionHelper.wrapCoreException(e); } return msg; }
XmlNotificationSerializer implements NotificationSerializer { @Override public AdaptrisMessage serialize(Notification n, AdaptrisMessage msg) throws CoreException, IOException { try { XmlUtils xmlBuilder = new XmlUtils(); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = doc.createElement(XML_ROOT_ELEMENT); doc.appendChild(root); xmlBuilder.setSource(doc); for (NotificationElement e : NotificationElement.values()) { xmlBuilder.setNodeValue(e.xpathToNode(), e.getValue(n)); } try (OutputStream out = msg.getOutputStream()) { String encoding = XmlHelper.getXmlEncoding(msg, getOutputMessageEncoding()); xmlBuilder.writeDocument(out, encoding); msg.setContentEncoding(encoding); } if (n.getUserData() != null) { msg.getObjectHeaders().put(OBJ_METADATA_USERDATA, n.getUserData()); } } catch (Exception e) { throw ExceptionHelper.wrapCoreException(e); } return msg; } }
XmlNotificationSerializer implements NotificationSerializer { @Override public AdaptrisMessage serialize(Notification n, AdaptrisMessage msg) throws CoreException, IOException { try { XmlUtils xmlBuilder = new XmlUtils(); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = doc.createElement(XML_ROOT_ELEMENT); doc.appendChild(root); xmlBuilder.setSource(doc); for (NotificationElement e : NotificationElement.values()) { xmlBuilder.setNodeValue(e.xpathToNode(), e.getValue(n)); } try (OutputStream out = msg.getOutputStream()) { String encoding = XmlHelper.getXmlEncoding(msg, getOutputMessageEncoding()); xmlBuilder.writeDocument(out, encoding); msg.setContentEncoding(encoding); } if (n.getUserData() != null) { msg.getObjectHeaders().put(OBJ_METADATA_USERDATA, n.getUserData()); } } catch (Exception e) { throw ExceptionHelper.wrapCoreException(e); } return msg; } XmlNotificationSerializer(); }
XmlNotificationSerializer implements NotificationSerializer { @Override public AdaptrisMessage serialize(Notification n, AdaptrisMessage msg) throws CoreException, IOException { try { XmlUtils xmlBuilder = new XmlUtils(); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = doc.createElement(XML_ROOT_ELEMENT); doc.appendChild(root); xmlBuilder.setSource(doc); for (NotificationElement e : NotificationElement.values()) { xmlBuilder.setNodeValue(e.xpathToNode(), e.getValue(n)); } try (OutputStream out = msg.getOutputStream()) { String encoding = XmlHelper.getXmlEncoding(msg, getOutputMessageEncoding()); xmlBuilder.writeDocument(out, encoding); msg.setContentEncoding(encoding); } if (n.getUserData() != null) { msg.getObjectHeaders().put(OBJ_METADATA_USERDATA, n.getUserData()); } } catch (Exception e) { throw ExceptionHelper.wrapCoreException(e); } return msg; } XmlNotificationSerializer(); @Override AdaptrisMessage serialize(Notification n, AdaptrisMessage msg); String getOutputMessageEncoding(); void setOutputMessageEncoding(String encoding); }
XmlNotificationSerializer implements NotificationSerializer { @Override public AdaptrisMessage serialize(Notification n, AdaptrisMessage msg) throws CoreException, IOException { try { XmlUtils xmlBuilder = new XmlUtils(); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = doc.createElement(XML_ROOT_ELEMENT); doc.appendChild(root); xmlBuilder.setSource(doc); for (NotificationElement e : NotificationElement.values()) { xmlBuilder.setNodeValue(e.xpathToNode(), e.getValue(n)); } try (OutputStream out = msg.getOutputStream()) { String encoding = XmlHelper.getXmlEncoding(msg, getOutputMessageEncoding()); xmlBuilder.writeDocument(out, encoding); msg.setContentEncoding(encoding); } if (n.getUserData() != null) { msg.getObjectHeaders().put(OBJ_METADATA_USERDATA, n.getUserData()); } } catch (Exception e) { throw ExceptionHelper.wrapCoreException(e); } return msg; } XmlNotificationSerializer(); @Override AdaptrisMessage serialize(Notification n, AdaptrisMessage msg); String getOutputMessageEncoding(); void setOutputMessageEncoding(String encoding);  }