com.hanframework.mojito.util.HttpRequestBuilder Maven / Gradle / Ivy
package com.hanframework.mojito.util;
import com.google.common.base.Strings;
import com.hanframework.mojito.protocol.http.HttpRequestFacade;
import io.netty.handler.codec.http.*;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
/**
* @author liuxin
* 2020-09-17 00:23
*/
public class HttpRequestBuilder {
private HttpVersion httpVersion = HttpVersion.HTTP_1_1;
private HttpMethod httpMethod;
private Map parameter = new HashMap<>();
private String uri;
private Map headers = new HashMap<>();
public HttpRequestBuilder GET(String uri, Map parameter) {
this.httpMethod = HttpMethod.GET;
this.parameter = parameter;
this.uri = uri;
return this;
}
public HttpRequestBuilder GET(URI uri) {
return GET(uri.toASCIIString(), new HashMap<>());
}
public HttpRequestBuilder GET(URI uri, Map parameter) {
return GET(uri.toASCIIString(), parameter);
}
public HttpRequestBuilder addHeader(String name, String value) {
if (Strings.isNullOrEmpty(name) || Strings.isNullOrEmpty(value)) {
return this;
}
headers.put(name, value);
return this;
}
public FullHttpRequest build() {
DefaultFullHttpRequest fullHttpRequest = new DefaultFullHttpRequest(httpVersion, httpMethod, uri);
headers.forEach((key, value) -> fullHttpRequest.headers().set(key, value));
fullHttpRequest.headers().set(HttpHeaderNames.CONTENT_LENGTH, fullHttpRequest.content().readableBytes());
return fullHttpRequest;
}
public HttpRequestFacade wrapBuild() {
return new HttpRequestFacade(build());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy