Drop "get" prefix from Part accessor methods

This commit is contained in:
Rossen Stoyanchev 2017-05-04 09:27:59 -04:00
parent 4525c6a537
commit ac1db169a4
13 changed files with 73 additions and 73 deletions

View File

@ -29,10 +29,9 @@ import reactor.core.publisher.Mono;
public interface FilePart extends Part { public interface FilePart extends Part {
/** /**
* Return the name of the file selected by the user in a browser form. * Return the original filename in the client's filesystem.
*/ */
String getFilename(); String filename();
/** /**
* Transfer the file in this part to the given file destination. * Transfer the file in this part to the given file destination.

View File

@ -27,6 +27,6 @@ public interface FormFieldPart extends Part {
/** /**
* Return the form field value. * Return the form field value.
*/ */
String getValue(); String value();
} }

View File

@ -85,7 +85,7 @@ public class MultipartHttpMessageReader implements HttpMessageReader<MultiValueM
ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) { ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
return this.partReader.read(elementType, inputMessage, hints) return this.partReader.read(elementType, inputMessage, hints)
.collectMultimap(Part::getName).map(this::toMultiValueMap); .collectMultimap(Part::name).map(this::toMultiValueMap);
} }
private LinkedMultiValueMap<String, Part> toMultiValueMap(Map<String, Collection<Part>> map) { private LinkedMultiValueMap<String, Part> toMultiValueMap(Map<String, Collection<Part>> map) {

View File

@ -43,16 +43,18 @@ public interface Part {
* Return the name of the part in the multipart form. * Return the name of the part in the multipart form.
* @return the name of the part, never {@code null} or empty * @return the name of the part, never {@code null} or empty
*/ */
String getName(); String name();
/** /**
* Return the headers associated with the part. * Return the headers associated with the part.
*/ */
HttpHeaders getHeaders(); HttpHeaders headers();
/** /**
* Return the part raw content as a stream of DataBuffer's. * Return the content for this part.
* <p>Note that for a {@link FormFieldPart} the content may be accessed
* more easily via {@link FormFieldPart#value()}.
*/ */
Flux<DataBuffer> getContent(); Flux<DataBuffer> content();
} }

View File

@ -185,10 +185,14 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
public void onPartFinished(StreamStorage storage, Map<String, List<String>> headers) { public void onPartFinished(StreamStorage storage, Map<String, List<String>> headers) {
HttpHeaders httpHeaders = new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.putAll(headers); httpHeaders.putAll(headers);
Part part = MultipartUtils.getFileName(httpHeaders) != null ? this.sink.next(createPart(httpHeaders, storage));
new SynchronossFilePart(httpHeaders, storage, this.bufferFactory) : }
private Part createPart(HttpHeaders httpHeaders, StreamStorage storage) {
String fileName = MultipartUtils.getFileName(httpHeaders);
return fileName != null ?
new SynchronossFilePart(httpHeaders, storage, fileName, this.bufferFactory) :
new DefaultSynchronossPart(httpHeaders, storage, this.bufferFactory); new DefaultSynchronossPart(httpHeaders, storage, this.bufferFactory);
this.sink.next(part);
} }
@Override @Override
@ -238,12 +242,12 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
@Override @Override
public String getName() { public String name() {
return MultipartUtils.getFieldName(this.headers); return MultipartUtils.getFieldName(this.headers);
} }
@Override @Override
public HttpHeaders getHeaders() { public HttpHeaders headers() {
return this.headers; return this.headers;
} }
@ -265,7 +269,7 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
@Override @Override
public Flux<DataBuffer> getContent() { public Flux<DataBuffer> content() {
InputStream inputStream = this.storage.getInputStream(); InputStream inputStream = this.storage.getInputStream();
return DataBufferUtils.read(inputStream, getBufferFactory(), 4096); return DataBufferUtils.read(inputStream, getBufferFactory(), 4096);
} }
@ -278,14 +282,14 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
private static class SynchronossFilePart extends DefaultSynchronossPart implements FilePart { private static class SynchronossFilePart extends DefaultSynchronossPart implements FilePart {
public SynchronossFilePart(HttpHeaders headers, StreamStorage storage, DataBufferFactory factory) { public SynchronossFilePart(HttpHeaders headers, StreamStorage storage, String fileName, DataBufferFactory factory) {
super(headers, storage, factory); super(headers, storage, factory);
} }
@Override @Override
public String getFilename() { public String filename() {
return MultipartUtils.getFileName(getHeaders()); return MultipartUtils.getFileName(headers());
} }
@Override @Override
@ -341,12 +345,12 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
@Override @Override
public String getValue() { public String value() {
return this.content; return this.content;
} }
@Override @Override
public Flux<DataBuffer> getContent() { public Flux<DataBuffer> content() {
byte[] bytes = this.content.getBytes(getCharset()); byte[] bytes = this.content.getBytes(getCharset());
DataBuffer buffer = getBufferFactory().allocateBuffer(bytes.length); DataBuffer buffer = getBufferFactory().allocateBuffer(bytes.length);
buffer.write(bytes); buffer.write(bytes);
@ -354,7 +358,7 @@ public class SynchronossPartHttpMessageReader implements HttpMessageReader<Part>
} }
private Charset getCharset() { private Charset getCharset() {
return Optional.ofNullable(MultipartUtils.getCharEncoding(getHeaders())) return Optional.ofNullable(MultipartUtils.getCharEncoding(headers()))
.map(Charset::forName).orElse(StandardCharsets.UTF_8); .map(Charset::forName).orElse(StandardCharsets.UTF_8);
} }
} }

View File

@ -108,7 +108,7 @@ public class WebExchangeDataBinder extends WebDataBinder {
private static void addBindValue(Map<String, Object> params, String key, List<?> values) { private static void addBindValue(Map<String, Object> params, String key, List<?> values) {
if (!CollectionUtils.isEmpty(values)) { if (!CollectionUtils.isEmpty(values)) {
values = values.stream() values = values.stream()
.map(value -> value instanceof FormFieldPart ? ((FormFieldPart) value).getValue() : value) .map(value -> value instanceof FormFieldPart ? ((FormFieldPart) value).value() : value)
.collect(Collectors.toList()); .collect(Collectors.toList());
params.put(key, values.size() == 1 ? values.get(0) : values); params.put(key, values.size() == 1 ? values.get(0) : values);
} }

View File

@ -118,38 +118,38 @@ public class MultipartHttpMessageWriterTests {
Part part = requestParts.getFirst("name 1"); Part part = requestParts.getFirst("name 1");
assertTrue(part instanceof FormFieldPart); assertTrue(part instanceof FormFieldPart);
assertEquals("name 1", part.getName()); assertEquals("name 1", part.name());
assertEquals("value 1", ((FormFieldPart) part).getValue()); assertEquals("value 1", ((FormFieldPart) part).value());
List<Part> parts2 = requestParts.get("name 2"); List<Part> parts2 = requestParts.get("name 2");
assertEquals(2, parts2.size()); assertEquals(2, parts2.size());
part = parts2.get(0); part = parts2.get(0);
assertTrue(part instanceof FormFieldPart); assertTrue(part instanceof FormFieldPart);
assertEquals("name 2", part.getName()); assertEquals("name 2", part.name());
assertEquals("value 2+1", ((FormFieldPart) part).getValue()); assertEquals("value 2+1", ((FormFieldPart) part).value());
part = parts2.get(1); part = parts2.get(1);
assertTrue(part instanceof FormFieldPart); assertTrue(part instanceof FormFieldPart);
assertEquals("name 2", part.getName()); assertEquals("name 2", part.name());
assertEquals("value 2+2", ((FormFieldPart) part).getValue()); assertEquals("value 2+2", ((FormFieldPart) part).value());
part = requestParts.getFirst("logo"); part = requestParts.getFirst("logo");
assertTrue(part instanceof FilePart); assertTrue(part instanceof FilePart);
assertEquals("logo", part.getName()); assertEquals("logo", part.name());
assertEquals("logo.jpg", ((FilePart) part).getFilename()); assertEquals("logo.jpg", ((FilePart) part).filename());
assertEquals(MediaType.IMAGE_JPEG, part.getHeaders().getContentType()); assertEquals(MediaType.IMAGE_JPEG, part.headers().getContentType());
assertEquals(logo.getFile().length(), part.getHeaders().getContentLength()); assertEquals(logo.getFile().length(), part.headers().getContentLength());
part = requestParts.getFirst("utf8"); part = requestParts.getFirst("utf8");
assertTrue(part instanceof FilePart); assertTrue(part instanceof FilePart);
assertEquals("utf8", part.getName()); assertEquals("utf8", part.name());
assertEquals("Hall\u00F6le.jpg", ((FilePart) part).getFilename()); assertEquals("Hall\u00F6le.jpg", ((FilePart) part).filename());
assertEquals(MediaType.IMAGE_JPEG, part.getHeaders().getContentType()); assertEquals(MediaType.IMAGE_JPEG, part.headers().getContentType());
assertEquals(utf8.getFile().length(), part.getHeaders().getContentLength()); assertEquals(utf8.getFile().length(), part.headers().getContentLength());
part = requestParts.getFirst("json"); part = requestParts.getFirst("json");
assertEquals("json", part.getName()); assertEquals("json", part.name());
assertEquals(MediaType.APPLICATION_JSON_UTF8, part.getHeaders().getContentType()); assertEquals(MediaType.APPLICATION_JSON_UTF8, part.headers().getContentType());
assertEquals("{\"bar\":\"bar\"}", ((FormFieldPart) part).getValue()); assertEquals("{\"bar\":\"bar\"}", ((FormFieldPart) part).value());
} }

View File

@ -88,9 +88,9 @@ public class SynchronossPartHttpMessageReaderTests {
assertTrue(parts.containsKey("fooPart")); assertTrue(parts.containsKey("fooPart"));
Part part = parts.getFirst("fooPart"); Part part = parts.getFirst("fooPart");
assertTrue(part instanceof FilePart); assertTrue(part instanceof FilePart);
assertEquals("fooPart", part.getName()); assertEquals("fooPart", part.name());
assertEquals("foo.txt", ((FilePart) part).getFilename()); assertEquals("foo.txt", ((FilePart) part).filename());
DataBuffer buffer = part.getContent().reduce(DataBuffer::write).block(); DataBuffer buffer = part.content().reduce(DataBuffer::write).block();
assertEquals(12, buffer.readableByteCount()); assertEquals(12, buffer.readableByteCount());
byte[] byteContent = new byte[12]; byte[] byteContent = new byte[12];
buffer.read(byteContent); buffer.read(byteContent);
@ -99,8 +99,8 @@ public class SynchronossPartHttpMessageReaderTests {
assertTrue(parts.containsKey("barPart")); assertTrue(parts.containsKey("barPart"));
part = parts.getFirst("barPart"); part = parts.getFirst("barPart");
assertTrue(part instanceof FormFieldPart); assertTrue(part instanceof FormFieldPart);
assertEquals("barPart", part.getName()); assertEquals("barPart", part.name());
assertEquals("bar", ((FormFieldPart) part).getValue()); assertEquals("bar", ((FormFieldPart) part).value());
} }
@Test @Test

View File

@ -17,7 +17,6 @@
package org.springframework.http.server.reactive; package org.springframework.http.server.reactive;
import java.net.URI; import java.net.URI;
import java.util.Optional;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -100,11 +99,11 @@ public class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTes
} }
private void assertFooPart(Part part) { private void assertFooPart(Part part) {
assertEquals("fooPart", part.getName()); assertEquals("fooPart", part.name());
assertTrue(part instanceof FilePart); assertTrue(part instanceof FilePart);
assertEquals("foo.txt", ((FilePart) part).getFilename()); assertEquals("foo.txt", ((FilePart) part).filename());
DataBuffer buffer = part DataBuffer buffer = part
.getContent() .content()
.reduce(DataBuffer::write) .reduce(DataBuffer::write)
.block(); .block();
assertEquals(12, buffer.readableByteCount()); assertEquals(12, buffer.readableByteCount());
@ -114,9 +113,9 @@ public class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTes
} }
private void assertBarPart(Part part) { private void assertBarPart(Part part) {
assertEquals("barPart", part.getName()); assertEquals("barPart", part.name());
assertTrue(part instanceof FormFieldPart); assertTrue(part instanceof FormFieldPart);
assertEquals("bar", ((FormFieldPart) part).getValue()); assertEquals("bar", ((FormFieldPart) part).value());
} }
} }

View File

@ -207,10 +207,10 @@ public class WebExchangeDataBinderTests {
assertEquals("bar", bean.getName()); assertEquals("bar", bean.getName());
assertEquals(Arrays.asList("123", "abc"), bean.getSomeList()); assertEquals(Arrays.asList("123", "abc"), bean.getSomeList());
assertArrayEquals(new String[] {"dec", "456"}, bean.getSomeArray()); assertArrayEquals(new String[] {"dec", "456"}, bean.getSomeArray());
assertEquals("foo.txt", bean.getPart().getFilename()); assertEquals("foo.txt", bean.getPart().filename());
assertEquals(2, bean.getSomePartList().size()); assertEquals(2, bean.getSomePartList().size());
assertEquals("foo.txt", bean.getSomePartList().get(0).getFilename()); assertEquals("foo.txt", bean.getSomePartList().get(0).filename());
assertEquals("spring.png", bean.getSomePartList().get(1).getFilename()); assertEquals("spring.png", bean.getSomePartList().get(1).filename());
} }

View File

@ -36,8 +36,6 @@ import reactor.test.StepVerifier;
import org.springframework.core.codec.ByteBufferDecoder; import org.springframework.core.codec.ByteBufferDecoder;
import org.springframework.core.codec.StringDecoder; import org.springframework.core.codec.StringDecoder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBuffer; import org.springframework.core.io.buffer.DefaultDataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.core.io.buffer.DefaultDataBufferFactory;
@ -56,7 +54,6 @@ import org.springframework.http.codec.xml.Jaxb2XmlDecoder;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -297,24 +294,24 @@ public class BodyExtractorsTests {
StepVerifier.create(result) StepVerifier.create(result)
.consumeNextWith(part -> { .consumeNextWith(part -> {
assertEquals("text", part.getName()); assertEquals("text", part.name());
assertTrue(part instanceof FormFieldPart); assertTrue(part instanceof FormFieldPart);
FormFieldPart formFieldPart = (FormFieldPart) part; FormFieldPart formFieldPart = (FormFieldPart) part;
assertEquals("text default", formFieldPart.getValue()); assertEquals("text default", formFieldPart.value());
}) })
.consumeNextWith(part -> { .consumeNextWith(part -> {
assertEquals("file1", part.getName()); assertEquals("file1", part.name());
assertTrue(part instanceof FilePart); assertTrue(part instanceof FilePart);
FilePart filePart = (FilePart) part; FilePart filePart = (FilePart) part;
assertEquals("a.txt", filePart.getFilename()); assertEquals("a.txt", filePart.filename());
assertEquals(MediaType.TEXT_PLAIN, filePart.getHeaders().getContentType()); assertEquals(MediaType.TEXT_PLAIN, filePart.headers().getContentType());
}) })
.consumeNextWith(part -> { .consumeNextWith(part -> {
assertEquals("file2", part.getName()); assertEquals("file2", part.name());
assertTrue(part instanceof FilePart); assertTrue(part instanceof FilePart);
FilePart filePart = (FilePart) part; FilePart filePart = (FilePart) part;
assertEquals("a.html", filePart.getFilename()); assertEquals("a.html", filePart.filename());
assertEquals(MediaType.TEXT_HTML, filePart.getHeaders().getContentType()); assertEquals(MediaType.TEXT_HTML, filePart.headers().getContentType());
}) })
.expectComplete() .expectComplete()
.verify(); .verify();

View File

@ -16,7 +16,6 @@
package org.springframework.web.reactive.function; package org.springframework.web.reactive.function;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Test; import org.junit.Test;
@ -106,8 +105,8 @@ public class MultipartIntegrationTests extends AbstractRouterFunctionIntegration
Map<String, Part> parts = map.toSingleValueMap(); Map<String, Part> parts = map.toSingleValueMap();
try { try {
assertEquals(2, parts.size()); assertEquals(2, parts.size());
assertEquals("foo.txt", ((FilePart) parts.get("fooPart")).getFilename()); assertEquals("foo.txt", ((FilePart) parts.get("fooPart")).filename());
assertEquals("bar", ((FormFieldPart) parts.get("barPart")).getValue()); assertEquals("bar", ((FormFieldPart) parts.get("barPart")).value());
} }
catch(Exception e) { catch(Exception e) {
return Mono.error(e); return Mono.error(e);
@ -121,8 +120,8 @@ public class MultipartIntegrationTests extends AbstractRouterFunctionIntegration
.flatMap(parts -> { .flatMap(parts -> {
try { try {
assertEquals(2, parts.size()); assertEquals(2, parts.size());
assertEquals("foo.txt", ((FilePart) parts.get(0)).getFilename()); assertEquals("foo.txt", ((FilePart) parts.get(0)).filename());
assertEquals("bar", ((FormFieldPart) parts.get(1)).getValue()); assertEquals("bar", ((FormFieldPart) parts.get(1)).value());
} }
catch(Exception e) { catch(Exception e) {
return Mono.error(e); return Mono.error(e);

View File

@ -155,8 +155,8 @@ public class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTes
@PostMapping("/requestPart") @PostMapping("/requestPart")
void requestPart(@RequestPart FormFieldPart barPart, @RequestPart Mono<FilePart> fooPart) { void requestPart(@RequestPart FormFieldPart barPart, @RequestPart Mono<FilePart> fooPart) {
assertEquals("bar", barPart.getValue()); assertEquals("bar", barPart.value());
assertEquals("foo.txt", fooPart.block(Duration.ZERO).getFilename()); assertEquals("foo.txt", fooPart.block(Duration.ZERO).filename());
} }
@PostMapping("/requestBodyMap") @PostMapping("/requestBodyMap")
@ -167,7 +167,7 @@ public class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTes
@PostMapping("/requestBodyFlux") @PostMapping("/requestBodyFlux")
Mono<String> requestBodyFlux(@RequestBody Flux<Part> parts) { Mono<String> requestBodyFlux(@RequestBody Flux<Part> parts) {
return parts.map(Part::getName).collectList() return parts.map(Part::name).collectList()
.map(names -> names.stream().sorted().collect(Collectors.joining(",", "Flux[", "]"))); .map(names -> names.stream().sorted().collect(Collectors.joining(",", "Flux[", "]")));
} }
@ -202,7 +202,7 @@ public class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTes
@Override @Override
public String toString() { public String toString() {
return "TestBean[barPart=" + getBarPart() + ",fooPart=" + getFooPart().getFilename() + "]"; return "TestBean[barPart=" + getBarPart() + ",fooPart=" + getFooPart().filename() + "]";
} }
} }