package org.springframework.http.codec.multipart;
import java.nio.file.Path;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Assert;
abstract class DefaultParts {
public static FormFieldPart (HttpHeaders headers, String value) {
Assert.notNull(headers, "Headers must not be null");
Assert.notNull(value, "Value must not be null");
return new DefaultFormFieldPart(headers, value);
}
public static Part part(HttpHeaders headers, Flux<DataBuffer> content) {
Assert.notNull(headers, "Headers must not be null");
Assert.notNull(content, "Content must not be null");
String filename = headers.getContentDisposition().getFilename();
if (filename != null) {
return new DefaultFilePart(headers, content);
}
else {
return new DefaultPart(headers, content);
}
}
private static abstract class AbstractPart implements Part {
private final HttpHeaders ;
protected (HttpHeaders headers) {
Assert.notNull(headers, "HttpHeaders is required");
this.headers = headers;
}
@Override
public String name() {
String name = headers().getContentDisposition().getName();
Assert.state(name != null, "No name available");
return name;
}
@Override
public HttpHeaders () {
return this.headers;
}
}
private static class DefaultFormFieldPart extends AbstractPart implements FormFieldPart {
private final String value;
public (HttpHeaders headers, String value) {
super(headers);
this.value = value;
}
@Override
public Flux<DataBuffer> content() {
return Flux.defer(() -> {
byte[] bytes = this.value.getBytes(MultipartUtils.charset(headers()));
return Flux.just(DefaultDataBufferFactory.sharedInstance.wrap(bytes));
});
}
@Override
public String value() {
return this.value;
}
@Override
public String toString() {
String name = headers().getContentDisposition().getName();
if (name != null) {
return "DefaultFormFieldPart{" + name() + "}";
}
else {
return "DefaultFormFieldPart";
}
}
}
private static class DefaultPart extends AbstractPart {
private final Flux<DataBuffer> content;
public DefaultPart(HttpHeaders headers, Flux<DataBuffer> content) {
super(headers);
this.content = content;
}
@Override
public Flux<DataBuffer> content() {
return this.content;
}
@Override
public String toString() {
String name = headers().getContentDisposition().getName();
if (name != null) {
return "DefaultPart{" + name + "}";
}
else {
return "DefaultPart";
}
}
}
private static class DefaultFilePart extends DefaultPart implements FilePart {
public DefaultFilePart(HttpHeaders headers, Flux<DataBuffer> content) {
super(headers, content);
}
@Override
public String filename() {
String filename = this.headers().getContentDisposition().getFilename();
Assert.state(filename != null, "No filename found");
return filename;
}
@Override
public Mono<Void> transferTo(Path dest) {
return DataBufferUtils.write(content(), dest);
}
@Override
public String toString() {
ContentDisposition contentDisposition = headers().getContentDisposition();
String name = contentDisposition.getName();
String filename = contentDisposition.getFilename();
if (name != null) {
return "DefaultFilePart{" + name() + " (" + filename + ")}";
}
else {
return "DefaultFilePart{(" + filename + ")}";
}
}
}
}