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

de.mklinger.qetcher.client.httpclient.BodyHandlers Maven / Gradle / Ivy

There is a newer version: 2.0.42.rc
Show newest version
package de.mklinger.commons.httpclient;

import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import de.mklinger.commons.httpclient.HttpResponse.BodyHandler;
import de.mklinger.commons.httpclient.internal.ByteArrayCompleteListener;
import de.mklinger.commons.httpclient.internal.DiscardBodyCompleteListener;
import de.mklinger.commons.httpclient.internal.FileCompleteListener;
import de.mklinger.commons.httpclient.internal.StringBodyHandler;

/**
 * @author Marc Klinger - mklinger[at]mklinger[dot]de
 */
public class BodyHandlers {
	private BodyHandlers() {
	}

	public static BodyHandler asFile(final Path file, final OpenOption... openOptions) {
		return (statusCode, responseHeaders) -> new FileCompleteListener(file, openOptions);
	}

	public static BodyHandler asFile(final Path file) {
		return asFile(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
	}

	public static BodyHandler asByteArray() {
		return (statusCode, responseHeaders) -> new ByteArrayCompleteListener();
	}

	public static BodyHandler asString() {
		return new StringBodyHandler();
	}

	public static  BodyHandler discard(final U value) {
		return (statusCode, responseHeaders) -> new DiscardBodyCompleteListener<>(value);
	}

	public static BodyHandler discard() {
		return discard(null);
	}
}