com.iprogrammerr.gentle.request.multipart.HttpMultipart Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gentle-request Show documentation
Show all versions of gentle-request Show documentation
Compact library for creating and reading http requests.
The newest version!
package com.iprogrammerr.gentle.request.multipart;
import java.util.ArrayList;
import java.util.List;
import com.iprogrammerr.gentle.request.Header;
import com.iprogrammerr.gentle.request.binary.HttpBoundaryBinaryParts;
import com.iprogrammerr.gentle.request.initialization.ArraysToList;
import com.iprogrammerr.gentle.request.initialization.HttpBoundary;
import com.iprogrammerr.gentle.request.initialization.Initialization;
import com.iprogrammerr.gentle.request.initialization.StickyInitialization;
import com.iprogrammerr.gentle.request.template.MultipartContentTypeHeader;
public final class HttpMultipart implements Multipart {
private static final String TWO_HYPHENS = "--";
private final String type;
private final Initialization boundary;
private byte[] source;
private final Initialization> parts;
private HttpMultipart(String type, Initialization boundary, byte[] source,
Initialization> parts) {
this.type = type;
this.boundary = boundary;
this.source = source;
this.parts = parts;
}
public HttpMultipart(String type, String boundary, byte[] source) {
this(type, new StickyInitialization<>(() -> boundary), source, new StickyInitialization<>(() -> {
List rawParts = new HttpBoundaryBinaryParts(TWO_HYPHENS + boundary).parts(source);
List parts = new ArrayList<>(rawParts.size());
for (byte[] p : rawParts) {
parts.add(new HttpPart(p));
}
return parts;
}));
}
public HttpMultipart(String type, List parts) {
this(type, new StickyInitialization<>(new HttpBoundary()), new byte[0], () -> parts);
}
public HttpMultipart(String type, Part part, Part... parts) {
this(type, new StickyInitialization<>(new HttpBoundary()), new byte[0],
new StickyInitialization<>(new ArraysToList<>(parts, part)));
}
@Override
public List parts() {
return this.parts.value();
}
@Override
public Header header() {
return new MultipartContentTypeHeader(this.type, this.boundary.value());
}
@Override
public byte[] body() throws Exception {
if (this.source.length < 1) {
this.source = new HttpMultipartBody(this.boundary.value(), this.parts.value()).content();
}
return this.source;
}
@Override
public String boundary() {
return this.boundary.value();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy