protected Object unmarshallData( final XMLEventReader bufferedXmlEventReader, final IDataImporter<Object> dataImporterExporter) { final Unmarshaller unmarshaller = dataImporterExporter.getUnmarshaller(); try { final StAXSource source = new StAXSource(bufferedXmlEventReader); return unmarshaller.unmarshal(source); } catch (XmlMappingException e) { throw new RuntimeException("Failed to map provided XML to portal data", e); } catch (IOException e) { throw new RuntimeException("Failed to read the provided XML data", e); } catch (XMLStreamException e) { throw new RuntimeException("Failed to create StAX Source to read XML data", e); } }
@Override public Object deserialize(byte[] bytes) throws SerializationException { if (SerializationUtils.isEmpty(bytes)) { return null; } try { return unmarshaller.unmarshal(new StreamSource(new ByteArrayInputStream(bytes))); } catch (Exception ex) { throw new SerializationException("Cannot deserialize bytes", ex); } }
@Test public void testUnmarshallSettings() throws Exception { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<settings>" + "<fooEnabled>true</fooEnabled>" + "<message>Hello OXM</message>" + "<message1>Hello OXM</message1>" + "</settings>"; Settings result = (Settings) unmarshaller.unmarshal(new StreamSource(new StringReader(xml))); assertThat(result.getMessage(), is("Hello OXM")); assertThat(result.isFooEnabled(), is(true)); }
@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); } }
/** * Loads up the workflow description from the given file. * * @param descriptionFile The file to load up a workflow description. * @return An IridaWorkflowDescription object. * @throws IOException If there was an issue reading the passed file. * @throws IridaWorkflowLoadException If there was an issue loading up the workflow description. */ public IridaWorkflowDescription loadWorkflowDescription(Path descriptionFile) throws IOException, IridaWorkflowLoadException { checkNotNull(descriptionFile, "descriptionFile is null"); if (!Files.exists(descriptionFile)) { throw new FileNotFoundException(descriptionFile.toAbsolutePath().toString()); } Source source = new StreamSource(Files.newInputStream(descriptionFile)); IridaWorkflowDescription workflowDescription = (IridaWorkflowDescription) workflowDescriptionUnmarshaller.unmarshal(source); if (workflowDescription.getId() == null) { throw new IridaWorkflowLoadException( "No id for workflow description from file " + descriptionFile); } else if (workflowDescription.getAnalysisType() == null) { throw new IridaWorkflowLoadException( "Invalid analysisType for workflow description from file " + descriptionFile); } else { if (workflowDescription.acceptsParameters()) { for (IridaWorkflowParameter workflowParameter : workflowDescription.getParameters()) { try { workflowParameter.getDefaultValue(); } catch (NullPointerException e) { throw new IridaWorkflowLoadException( "Workflow parameter" + workflowParameter + " has no default value set: " + descriptionFile, e); } } } return workflowDescription; } }