All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.iprogrammerr.gentle.request.multipart.HttpMultipartForm Maven / Gradle / Ivy

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 HttpMultipartForm implements MultipartForm {

	private static final String TWO_HYPHENS = "--";
	private final Initialization boundary;
	private byte[] source;
	private final Initialization> parts;

	private HttpMultipartForm(Initialization boundary, byte[] source, Initialization> parts) {
		this.boundary = boundary;
		this.source = source;
		this.parts = parts;
	}

	public HttpMultipartForm(String boundary, byte[] source) {
		this(new StickyInitialization<>(() -> boundary), source, new StickyInitialization<>(() -> {
			List rawParts = new HttpBoundaryBinaryParts(TWO_HYPHENS + boundary).parts(source);
			List parts = new ArrayList<>(rawParts.size());
			for (byte[] rp : rawParts) {
				parts.add(new HttpFormPart(rp));
			}
			return parts;
		}));
	}

	public HttpMultipartForm(List parts) {
		this(new StickyInitialization<>(new HttpBoundary()), new byte[0], () -> parts);
	}

	public HttpMultipartForm(FormPart part, FormPart... parts) {
		this(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.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