package jdk.incubator.http;
import java.net.URI;
import jdk.incubator.http.HttpRequest.BodyProcessor;
import java.time.Duration;
import java.util.Optional;
import static java.util.Objects.requireNonNull;
import jdk.incubator.http.internal.common.HttpHeadersImpl;
import static jdk.incubator.http.internal.common.Utils.isValidName;
import static jdk.incubator.http.internal.common.Utils.isValidValue;
class HttpRequestBuilderImpl extends HttpRequest.Builder {
private HttpHeadersImpl ;
private URI uri;
private String method;
private boolean expectContinue;
private HttpRequest.BodyProcessor body;
private volatile Optional<HttpClient.Version> version;
private Duration duration;
public HttpRequestBuilderImpl(URI uri) {
checkURI(uri);
this.uri = uri;
this.userHeaders = new HttpHeadersImpl();
this.method = "GET";
this.version = Optional.empty();
}
public HttpRequestBuilderImpl() {
this.userHeaders = new HttpHeadersImpl();
this.version = Optional.empty();
}
@Override
public HttpRequestBuilderImpl uri(URI uri) {
requireNonNull(uri);
checkURI(uri);
this.uri = uri;
return this;
}
private static void checkURI(URI uri) {
String scheme = uri.getScheme().toLowerCase();
if (!scheme.equals("https") && !scheme.equals("http")) {
throw new IllegalArgumentException("invalid URI scheme");
}
}
@Override
public HttpRequestBuilderImpl (String name, String value) {
checkNameAndValue(name, value);
userHeaders.addHeader(name, value);
return this;
}
@Override
public HttpRequestBuilderImpl (String... params) {
requireNonNull(params);
if (params.length % 2 != 0) {
throw new IllegalArgumentException("wrong number of parameters");
}
for (int i = 0; i < params.length; i += 2) {
String name = params[i];
String value = params[i + 1];
header(name, value);
}
return this;
}
@Override
public HttpRequestBuilderImpl copy() {
HttpRequestBuilderImpl b = new HttpRequestBuilderImpl(this.uri);
b.userHeaders = this.userHeaders.deepCopy();
b.method = this.method;
b.expectContinue = this.expectContinue;
b.body = body;
b.uri = uri;
return b;
}
@Override
public HttpRequestBuilderImpl (String name, String value) {
checkNameAndValue(name, value);
userHeaders.setHeader(name, value);
return this;
}
private void checkNameAndValue(String name, String value) {
requireNonNull(name, "name");
requireNonNull(value, "value");
if (!isValidName(name)) {
throw new IllegalArgumentException("invalid header name");
}
if (!isValidValue(value)) {
throw new IllegalArgumentException("invalid header value");
}
}
@Override
public HttpRequestBuilderImpl expectContinue(boolean enable) {
expectContinue = enable;
return this;
}
@Override
public HttpRequestBuilderImpl version(HttpClient.Version version) {
requireNonNull(version);
this.version = Optional.of(version);
return this;
}
HttpHeadersImpl () { return userHeaders; }
URI uri() { return uri; }
String method() { return method; }
boolean expectContinue() { return expectContinue; }
public HttpRequest.BodyProcessor body() { return body; }
Optional<HttpClient.Version> version() { return version; }
@Override
public HttpRequest.Builder GET() { return method("GET", null); }
@Override
public HttpRequest.Builder POST(BodyProcessor body) {
return method("POST", body);
}
@Override
public HttpRequest.Builder DELETE(BodyProcessor body) {
return method("DELETE", body);
}
@Override
public HttpRequest.Builder PUT(BodyProcessor body) {
return method("PUT", body);
}
@Override
public HttpRequest.Builder method(String method, BodyProcessor body) {
this.method = requireNonNull(method);
this.body = body;
return this;
}
@Override
public HttpRequest build() {
return new HttpRequestImpl(this);
}
@Override
public HttpRequest.Builder timeout(Duration duration) {
requireNonNull(duration);
if (duration.isNegative() || Duration.ZERO.equals(duration))
throw new IllegalArgumentException("Invalid duration: " + duration);
this.duration = duration;
return this;
}
Duration duration() { return duration; }
}