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();
}
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());
}
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));
}
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());
}
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));
}
Aggregations