Search in sources :

Example 51 with StreamSource

use of javax.xml.transform.stream.StreamSource in project spring-framework by spring-projects.

the class FormHttpMessageConverterTests method writeMultipart.

@Test
public void writeMultipart() throws Exception {
    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
    parts.add("name 1", "value 1");
    parts.add("name 2", "value 2+1");
    parts.add("name 2", "value 2+2");
    parts.add("name 3", null);
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    parts.add("logo", logo);
    // SPR-12108
    Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {

        @Override
        public String getFilename() {
            return "Hallöle.jpg";
        }
    };
    parts.add("utf8", utf8);
    Source xml = new StreamSource(new StringReader("<root><child/></root>"));
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.setContentType(MediaType.TEXT_XML);
    HttpEntity<Source> entity = new HttpEntity<>(xml, entityHeaders);
    parts.add("xml", entity);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.converter.setMultipartCharset(StandardCharsets.UTF_8);
    this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);
    final MediaType contentType = outputMessage.getHeaders().getContentType();
    assertNotNull("No boundary found", contentType.getParameter("boundary"));
    // see if Commons FileUpload can read what we wrote
    FileItemFactory fileItemFactory = new DiskFileItemFactory();
    FileUpload fileUpload = new FileUpload(fileItemFactory);
    RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
    List<FileItem> items = fileUpload.parseRequest(requestContext);
    assertEquals(6, items.size());
    FileItem item = items.get(0);
    assertTrue(item.isFormField());
    assertEquals("name 1", item.getFieldName());
    assertEquals("value 1", item.getString());
    item = items.get(1);
    assertTrue(item.isFormField());
    assertEquals("name 2", item.getFieldName());
    assertEquals("value 2+1", item.getString());
    item = items.get(2);
    assertTrue(item.isFormField());
    assertEquals("name 2", item.getFieldName());
    assertEquals("value 2+2", item.getString());
    item = items.get(3);
    assertFalse(item.isFormField());
    assertEquals("logo", item.getFieldName());
    assertEquals("logo.jpg", item.getName());
    assertEquals("image/jpeg", item.getContentType());
    assertEquals(logo.getFile().length(), item.getSize());
    item = items.get(4);
    assertFalse(item.isFormField());
    assertEquals("utf8", item.getFieldName());
    assertEquals("Hallöle.jpg", item.getName());
    assertEquals("image/jpeg", item.getContentType());
    assertEquals(logo.getFile().length(), item.getSize());
    item = items.get(5);
    assertEquals("xml", item.getFieldName());
    assertEquals("text/xml", item.getContentType());
    verify(outputMessage.getBody(), never()).close();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) StreamSource(javax.xml.transform.stream.StreamSource) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) StringReader(java.io.StringReader) MediaType(org.springframework.http.MediaType) RequestContext(org.apache.commons.fileupload.RequestContext) FileUpload(org.apache.commons.fileupload.FileUpload) Test(org.junit.Test)

Example 52 with StreamSource

use of javax.xml.transform.stream.StreamSource in project spring-framework by spring-projects.

the class SourceHttpMessageConverterTests method writeStreamSource.

@Test
public void writeStreamSource() throws Exception {
    String xml = "<root>Hello World</root>";
    StreamSource streamSource = new StreamSource(new StringReader(xml));
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(streamSource, null, outputMessage);
    assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<root>Hello World</root>"));
    assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) StringReader(java.io.StringReader) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 53 with StreamSource

use of javax.xml.transform.stream.StreamSource in project spring-framework by spring-projects.

the class SourceHttpMessageConverterTests method readStreamSource.

@Test
public void readStreamSource() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
    StreamSource result = (StreamSource) converter.read(StreamSource.class, inputMessage);
    String s = FileCopyUtils.copyToString(new InputStreamReader(result.getInputStream()));
    assertThat("Invalid result", s, isSimilarTo(BODY));
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) InputStreamReader(java.io.InputStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 54 with StreamSource

use of javax.xml.transform.stream.StreamSource in project spring-framework by spring-projects.

the class XsltViewTests method testContentTypeCarriedFromTemplate.

@Test
public void testContentTypeCarriedFromTemplate() throws Exception {
    XsltView view = getXsltView(HTML_OUTPUT);
    Source source = new StreamSource(getProductDataResource().getInputStream());
    view.render(singletonMap("someKey", source), this.request, this.response);
    assertTrue(this.response.getContentType().startsWith("text/html"));
    assertEquals("UTF-8", this.response.getCharacterEncoding());
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 55 with StreamSource

use of javax.xml.transform.stream.StreamSource in project camel by apache.

the class XAdESSignaturePropertiesTest method validateAgainstSchema.

private void validateAgainstSchema(Document doc) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schema1 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/XAdES.xsd"));
    Source schema2 = new StreamSource(new File("target/test-classes/org/apache/camel/component/xmlsecurity/xades/xmldsig-core-schema.xsd"));
    Schema schema = factory.newSchema(new Source[] { schema2, schema1 });
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(doc));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) File(java.io.File) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator)

Aggregations

StreamSource (javax.xml.transform.stream.StreamSource)338 Source (javax.xml.transform.Source)115 StringReader (java.io.StringReader)101 StreamResult (javax.xml.transform.stream.StreamResult)85 Transformer (javax.xml.transform.Transformer)74 Test (org.junit.Test)73 InputStream (java.io.InputStream)68 TransformerFactory (javax.xml.transform.TransformerFactory)58 ByteArrayInputStream (java.io.ByteArrayInputStream)56 IOException (java.io.IOException)52 DOMSource (javax.xml.transform.dom.DOMSource)49 TransformerException (javax.xml.transform.TransformerException)48 StringWriter (java.io.StringWriter)45 SchemaFactory (javax.xml.validation.SchemaFactory)44 InputSource (org.xml.sax.InputSource)44 Schema (javax.xml.validation.Schema)43 SAXException (org.xml.sax.SAXException)42 SAXSource (javax.xml.transform.sax.SAXSource)33 Validator (javax.xml.validation.Validator)31 File (java.io.File)27