/** * Marshalls an object to an XML, returned as a String instance. * * @param marshaller to perform the object -> XML marshalling * @param o the object to marshall to XML * @return the XML in a String * @throws java.io.IOException in the event of an error writing out the XML to the StringWriter. */ private String marshal(Marshaller marshaller, Object o) throws IOException { Writer writer = new StringWriter(); marshaller.marshal(o, new StreamResult(writer)); String xml = writer.toString(); LOGGER.debug("\n" + xml); return xml; }
@Override public String exportData(String typeId, String dataId, Result result) { final IDataExporter<Object> portalDataExporter = this.getPortalDataExporter(typeId); final Object data = portalDataExporter.exportData(dataId); if (data == null) { return null; } final Marshaller marshaller = portalDataExporter.getMarshaller(); try { marshaller.marshal(data, result); return portalDataExporter.getFileName(data); } catch (XmlMappingException e) { throw new RuntimeException("Failed to map provided portal data to XML", e); } catch (IOException e) { throw new RuntimeException("Failed to write the provided XML data", e); } }
@Test public void testMarshallSettings() throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); marshaller.marshal(settings, new StreamResult(bos)); String result = bos.toString("UTF-8"); System.out.println(result); assertThat(result, StringContains.containsString("<?xml")); assertThat(result, StringContains.containsString("<message>Hello OXM</message>")); }
@Override public byte[] serialize(Object t) throws SerializationException { if (t == null) { return SerializationUtils.EMPTY_ARRAY; } ByteArrayOutputStream stream = new ByteArrayOutputStream(); StreamResult result = new StreamResult(stream); try { marshaller.marshal(t, result); } catch (Exception ex) { throw new SerializationException("Cannot serialize object", ex); } return stream.toByteArray(); }
@ServiceActivator public Message<String> sayHelloInternal(Message<String> request) { try { Message<HelloRequest> helloRequest = MessageBuilder.withPayload( (HelloRequest) helloUnmarshaller.unmarshal(new StringSource(request.getPayload()))) .copyHeaders(request.getHeaders()) .build(); StringResult result = new StringResult(); helloMarshaller.marshal(sayHello(helloRequest).getPayload(), result); return MessageBuilder.withPayload(result.toString()) .copyHeaders(request.getHeaders()) .build(); } catch (XmlMappingException e) { throw new CitrusRuntimeException("Failed to marshal/unmarshal XML", e); } catch (IOException e) { throw new CitrusRuntimeException("Failed due to IO error", e); } }