
net.dongliu.cute.http.AbstractRequestBuilder Maven / Gradle / Ivy
The newest version!
package net.dongliu.cute.http;
import net.dongliu.cute.http.body.Bodies;
import net.dongliu.cute.http.body.Body;
import net.dongliu.cute.http.exception.JsonMarshallerNotFoundException;
import net.dongliu.cute.http.internal.Asserts;
import net.dongliu.cute.http.json.JsonMarshaller;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static net.dongliu.commons.collection.Collections2.convertToList;
abstract class AbstractRequestBuilder> {
private final @Nullable JsonMarshaller jsonMarshaller;
final Method method;
final URL url;
List headers = List.of();
List cookies = List.of();
List params = List.of();
Charset paramCharset = UTF_8;
@Nullable
Body> body = null;
@Nullable
PasswordAuthentication basicAuth = null;
// those fields can override client settings
Duration timeout = Duration.ofSeconds(10);
@Nullable
String userAgent = null;
@Nullable
String referer = null;
boolean acceptCompressed = true;
AbstractRequestBuilder(Method method, URL url, @Nullable JsonMarshaller jsonMarshaller) {
this.method = requireNonNull(method);
this.url = requireNonNull(url);
this.jsonMarshaller = jsonMarshaller;
}
/**
* The timeout for one request to return response, do not include connect time.
* This setting will override timeout setting in HttpClient.
*
* @param timeout must larger than zero
*/
public T timeout(Duration timeout) {
this.timeout = Asserts.checkTimeout(timeout);
return self();
}
/**
* The request user-agent.
* This setting will override timeout setting in HttpClient.
*
* @param userAgent cannot be null
*/
public T userAgent(String userAgent) {
this.userAgent = requireNonNull(userAgent);
return self();
}
/**
* Set request referer header.
*
* @param referer cannot be null
*/
public T referer(String referer) {
this.referer = requireNonNull(referer);
return self();
}
/**
* Set request headers.
*/
public T headers(Collection extends Header> headers) {
requireNonNull(headers);
this.headers = List.copyOf(headers);
return self();
}
/**
* Set request headers.
*/
public final T headers(Header... headers) {
headers(List.of(headers));
return self();
}
/**
* Set request headers.
*/
public final T headers(Map map) {
this.headers = convertToList(map.entrySet(), e -> Header.of(e.getKey(), e.getValue()));
return self();
}
/**
* Set request cookies.
*/
public T cookies(Collection extends Cookie> cookies) {
requireNonNull(cookies);
this.cookies = List.copyOf(cookies);
return self();
}
/**
* Set request cookies.
*/
public final T cookies(Cookie... cookies) {
cookies(List.of(cookies));
return self();
}
/**
* Set request cookies.
*/
public final T cookies(Map map) {
this.cookies = convertToList(map.entrySet(), e -> Cookie.of(e.getKey(), e.getValue()));
return self();
}
/**
* Set url query params.
*
* @param params the parameters
*/
public T params(Collection extends Param> params) {
requireNonNull(params);
this.params = List.copyOf(params);
return self();
}
/**
* Set url query params.
*
* @param params the parameters
* @param charset charset used to encode params to query string
*/
public T params(Collection extends Param> params, Charset charset) {
this.params = List.copyOf(requireNonNull(params));
this.paramCharset = charset;
return self();
}
/**
* Set url query params.
*
* @param params the parameters
*/
public final T params(Param... params) {
this.params = List.of(params);
return self();
}
/**
* Set url query params.
*
* @param map the parameters
*/
public final T params(Map map) {
this.params = convertToList(map.entrySet(), e -> Param.of(e.getKey(), e.getValue()));
return self();
}
/**
* Set url query params.
*
* @param map the parameters
* @param charset charset used to encode params to query string
*/
public final T params(Map map, Charset charset) {
this.params = convertToList(map.entrySet(), e -> Param.of(e.getKey(), e.getValue()));
this.paramCharset = charset;
return self();
}
/**
* Set request body.
* To get ordinary bodies, see {@link Bodies}
*/
public T body(Body> body) {
this.body = requireNonNull(body);
return self();
}
/**
* Set a json request body.
* This is a convenient method for set a {@link #body} by a json request body.
* This method uses a {@link JsonMarshaller} from the {@link Client}, If no JsonMarshaller was provided,
* A {@link JsonMarshallerNotFoundException} would be thrown.
*
* @param body the value to encode to json
*/
public T jsonBody(@Nullable Object body, Charset charset) {
requireNonNull(charset);
if (jsonMarshaller == null) {
throw new JsonMarshallerNotFoundException();
}
this.body = Bodies.json(body, charset, jsonMarshaller);
return self();
}
/**
* Set a json request body, with charset utf-8.
* This is a convenient method for set a {@link #body} by a json request body.
* This method uses a {@link JsonMarshaller} from the {@link Client}, If no JsonMarshaller was provided,
* A {@link JsonMarshallerNotFoundException} would be thrown.
*
* @param body the value to encode to json
*/
public T jsonBody(@Nullable Object body) {
return jsonBody(body, UTF_8);
}
/**
* Auto set Accept-Encoding header for request.
* This setting will override setting in ClientBuilder.
*/
public T acceptCompressed(boolean acceptCompressed) {
this.acceptCompressed = acceptCompressed;
return self();
}
/**
* Set http Basic Authentication
*/
public T basicAuth(String user, char[] password) {
return basicAuth(new PasswordAuthentication(requireNonNull(user), requireNonNull(password)));
}
/**
* Set http Basic Authentication
*/
public T basicAuth(PasswordAuthentication basicAuth) {
this.basicAuth = requireNonNull(basicAuth);
return self();
}
/**
* Build a immutable request.
*
* @return the request.
*/
public Request build() {
return new Request(this);
}
protected abstract T self();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy