/** * Set the {@code Content-Disposition} header when creating a * {@code "multipart/form-data"} request. * <p>Applications typically would not set this header directly but * rather prepare a {@code MultiValueMap<String, Object>}, containing an * Object or a {@link org.springframework.core.io.Resource} for each part, * and then pass that to the {@code RestTemplate} or {@code WebClient}. * @param name the control name * @param filename the filename (may be {@code null}) * @see #getContentDisposition() */ public void setContentDispositionFormData(String name, @Nullable String filename) { Assert.notNull(name, "Name must not be null"); ContentDisposition.Builder disposition = ContentDisposition.builder("form-data").name(name); if (filename != null) { disposition.filename(filename); } setContentDisposition(disposition.build()); }
/** * Set the {@code Content-Disposition} header when creating a * {@code "multipart/form-data"} request. * <p>Applications typically would not set this header directly but * rather prepare a {@code MultiValueMap<String, Object>}, containing an * Object or a {@link org.springframework.core.io.Resource} for each part, * and then pass that to the {@code RestTemplate} or {@code WebClient}. * @param name the control name * @param filename the filename (may be {@code null}) * @see #getContentDisposition() */ public void setContentDispositionFormData(String name, @Nullable String filename) { Assert.notNull(name, "Name must not be null"); ContentDisposition.Builder disposition = ContentDisposition.builder("form-data").name(name); if (filename != null) { disposition.filename(filename); } setContentDisposition(disposition.build()); }
@Test public void parseDates() { ContentDisposition disposition = ContentDisposition .parse("attachment; creation-date=\"Mon, 12 Feb 2007 10:15:30 -0500\"; " + "modification-date=\"Tue, 13 Feb 2007 10:15:30 -0500\"; " + "read-date=\"Wed, 14 Feb 2007 10:15:30 -0500\""); DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; assertEquals(ContentDisposition.builder("attachment") .creationDate(ZonedDateTime.parse("Mon, 12 Feb 2007 10:15:30 -0500", formatter)) .modificationDate(ZonedDateTime.parse("Tue, 13 Feb 2007 10:15:30 -0500", formatter)) .readDate(ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter)).build(), disposition); }
@Test public void shouldReadImageResource() throws IOException { byte[] body = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("logo.jpg")); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); inputMessage.getHeaders().setContentDisposition( ContentDisposition.builder("attachment").filename("yourlogo.jpg").build()); Resource actualResource = converter.read(Resource.class, inputMessage); assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream()), is(body)); assertEquals("yourlogo.jpg", actualResource.getFilename()); }
@Test public void parseInvalidDates() { ContentDisposition disposition = ContentDisposition .parse("attachment; creation-date=\"-1\"; modification-date=\"-1\"; " + "read-date=\"Wed, 14 Feb 2007 10:15:30 -0500\""); DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; assertEquals(ContentDisposition.builder("attachment") .readDate(ZonedDateTime.parse("Wed, 14 Feb 2007 10:15:30 -0500", formatter)).build(), disposition); }
@Test // SPR-13443 public void shouldReadInputStreamResource() throws IOException { try (InputStream body = getClass().getResourceAsStream("logo.jpg") ) { MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); inputMessage.getHeaders().setContentDisposition( ContentDisposition.builder("attachment").filename("yourlogo.jpg").build()); Resource actualResource = converter.read(InputStreamResource.class, inputMessage); assertThat(actualResource, instanceOf(InputStreamResource.class)); assertThat(actualResource.getInputStream(), is(body)); assertEquals("yourlogo.jpg", actualResource.getFilename()); } }
@Test public void parseType() { ContentDisposition disposition = ContentDisposition.parse("form-data"); assertEquals(ContentDisposition.builder("form-data").build(), disposition); }
@Test public void parseUnquotedFilename() { ContentDisposition disposition = ContentDisposition .parse("form-data; filename=unquoted"); assertEquals(ContentDisposition.builder("form-data").filename("unquoted").build(), disposition); }
@Test // SPR-16091 public void parseFilenameWithSemicolon() { ContentDisposition disposition = ContentDisposition .parse("attachment; filename=\"filename with ; semicolon.txt\""); assertEquals(ContentDisposition.builder("attachment") .filename("filename with ; semicolon.txt").build(), disposition); }
@Test public void headerValueWithEncodedFilename() { ContentDisposition disposition = ContentDisposition.builder("form-data") .name("name").filename("中文.txt", StandardCharsets.UTF_8).build(); assertEquals("form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt", disposition.toString()); }
@Test public void parseEncodedFilename() { ContentDisposition disposition = ContentDisposition .parse("form-data; name=\"name\"; filename*=UTF-8''%E4%B8%AD%E6%96%87.txt"); assertEquals(ContentDisposition.builder("form-data").name("name") .filename("中文.txt", StandardCharsets.UTF_8).build(), disposition); }
@Test public void headerValue() { ContentDisposition disposition = ContentDisposition.builder("form-data") .name("foo").filename("foo.txt").size(123L).build(); assertEquals("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123", disposition.toString()); }
@Test public void parse() { ContentDisposition disposition = ContentDisposition .parse("form-data; name=\"foo\"; filename=\"foo.txt\"; size=123"); assertEquals(ContentDisposition.builder("form-data") .name("foo").filename("foo.txt").size(123L).build(), disposition); }
@Test public void parseAndIgnoreEmptyParts() { ContentDisposition disposition = ContentDisposition .parse("form-data; name=\"foo\";; ; filename=\"foo.txt\"; size=123"); assertEquals(ContentDisposition.builder("form-data") .name("foo").filename("foo.txt").size(123L).build(), disposition); }
@Test public void contentDisposition() { ContentDisposition disposition = headers.getContentDisposition(); assertNotNull(disposition); assertEquals("Invalid Content-Disposition header", ContentDisposition.empty(), headers.getContentDisposition()); disposition = ContentDisposition.builder("attachment").name("foo").filename("foo.txt").size(123L).build(); headers.setContentDisposition(disposition); assertEquals("Invalid Content-Disposition header", disposition, headers.getContentDisposition()); }
/** * Set the {@code Content-Disposition} header when creating a * {@code "multipart/form-data"} request. * <p>Applications typically would not set this header directly but * rather prepare a {@code MultiValueMap<String, Object>}, containing an * Object or a {@link org.springframework.core.io.Resource} for each part, * and then pass that to the {@code RestTemplate} or {@code WebClient}. * @param name the control name * @param filename the filename (may be {@code null}) * @see #getContentDisposition() */ public void setContentDispositionFormData(String name, @Nullable String filename) { Assert.notNull(name, "Name must not be null"); ContentDisposition.Builder disposition = ContentDisposition.builder("form-data").name(name); if (filename != null) { disposition.filename(filename); } setContentDisposition(disposition.build()); }
/** * Set the {@code Content-Disposition} header when creating a * {@code "multipart/form-data"} request. * <p>Applications typically would not set this header directly but * rather prepare a {@code MultiValueMap<String, Object>}, containing an * Object or a {@link org.springframework.core.io.Resource} for each part, * and then pass that to the {@code RestTemplate} or {@code WebClient}. * @param name the control name * @param filename the filename (may be {@code null}) * @see #getContentDisposition() */ public void setContentDispositionFormData(String name, @Nullable String filename) { Assert.notNull(name, "'name' must not be null"); ContentDisposition.Builder disposition = ContentDisposition.builder("form-data").name(name); if (filename != null) { disposition.filename(filename); } setContentDisposition(disposition.build()); }