Search in sources :

Example 1 with MimeContainer

use of org.springframework.oxm.mime.MimeContainer in project spring-framework by spring-projects.

the class Jaxb2MarshallerTests method marshalAttachments.

@Test
public void marshalAttachments() throws Exception {
    marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(BinaryObject.class);
    marshaller.setMtomEnabled(true);
    marshaller.afterPropertiesSet();
    MimeContainer mimeContainer = mock(MimeContainer.class);
    Resource logo = new ClassPathResource("spring-ws.png", getClass());
    DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
    given(mimeContainer.convertToXopPackage()).willReturn(true);
    byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
    BinaryObject object = new BinaryObject(bytes, dataHandler);
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, new StreamResult(writer), mimeContainer);
    assertTrue("No XML written", writer.toString().length() > 0);
    verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) ClassPathResource(org.springframework.core.io.ClassPathResource) MimeContainer(org.springframework.oxm.mime.MimeContainer) Test(org.junit.Test)

Example 2 with MimeContainer

use of org.springframework.oxm.mime.MimeContainer in project spring-framework by spring-projects.

the class Jaxb2UnmarshallerTests method marshalAttachments.

@Test
public void marshalAttachments() throws Exception {
    unmarshaller = new Jaxb2Marshaller();
    unmarshaller.setClassesToBeBound(BinaryObject.class);
    unmarshaller.setMtomEnabled(true);
    unmarshaller.afterPropertiesSet();
    MimeContainer mimeContainer = mock(MimeContainer.class);
    Resource logo = new ClassPathResource("spring-ws.png", getClass());
    DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
    given(mimeContainer.isXopPackage()).willReturn(true);
    given(mimeContainer.getAttachment("<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>")).willReturn(dataHandler);
    given(mimeContainer.getAttachment("<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>")).willReturn(dataHandler);
    given(mimeContainer.getAttachment("696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png")).willReturn(dataHandler);
    String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" + "<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" + "</bytes>" + "<dataHandler>" + "<xop:Include href='cid:99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" + "</dataHandler>" + "<swaDataHandler>696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png</swaDataHandler>" + "</binaryObject>";
    StringReader reader = new StringReader(content);
    Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer);
    assertTrue("Result is not a BinaryObject", result instanceof BinaryObject);
    BinaryObject object = (BinaryObject) result;
    assertNotNull("bytes property not set", object.getBytes());
    assertTrue("bytes property not set", object.getBytes().length > 0);
    assertNotNull("datahandler property not set", object.getSwaDataHandler());
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileDataSource(javax.activation.FileDataSource) StringReader(java.io.StringReader) DataHandler(javax.activation.DataHandler) ClassPathResource(org.springframework.core.io.ClassPathResource) MimeContainer(org.springframework.oxm.mime.MimeContainer) Test(org.junit.Test)

Aggregations

DataHandler (javax.activation.DataHandler)2 FileDataSource (javax.activation.FileDataSource)2 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)2 MimeContainer (org.springframework.oxm.mime.MimeContainer)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1