Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version 1.2.4 update httpclient #53

Merged
merged 2 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ezyhttp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
</parent>

<artifactId>ezyhttp-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import com.tvd12.ezyhttp.core.codec.BodyDeserializer;
import com.tvd12.ezyhttp.core.codec.BodySerializer;
import com.tvd12.ezyhttp.core.codec.DataConverters;
import com.tvd12.ezyhttp.core.constant.ContentTypes;
import com.tvd12.ezyhttp.core.constant.Headers;
import com.tvd12.ezyhttp.core.constant.HttpMethod;
import com.tvd12.ezyhttp.core.constant.StatusCodes;
import com.tvd12.ezyhttp.core.constant.*;
import com.tvd12.ezyhttp.core.data.MultiValueMap;
import com.tvd12.ezyhttp.core.exception.*;
import com.tvd12.ezyhttp.core.json.ObjectMapperBuilder;
Expand All @@ -35,10 +32,13 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.zip.GZIPInputStream;

import static com.tvd12.ezyfox.io.EzyStrings.isBlank;
import static com.tvd12.ezyfox.util.EzyFileUtil.getFileExtension;
import static com.tvd12.ezyhttp.client.concurrent.DownloadCancellationToken.ALWAYS_RUN;
import static com.tvd12.ezyhttp.core.constant.Headers.ACCEPT_ENCODING;
import static com.tvd12.ezyhttp.core.constant.Headers.CONTENT_ENCODING;

public class HttpClient extends EzyLoggable {

Expand Down Expand Up @@ -83,7 +83,8 @@ public ResponseEntity request(
String url,
RequestEntity entity,
Map<Integer, Class<?>> responseTypes,
int connectTimeout, int readTimeout
int connectTimeout,
int readTimeout
) throws Exception {
if (url == null) {
throw new IllegalArgumentException("url can not be null");
Expand Down Expand Up @@ -115,6 +116,12 @@ public ResponseEntity request(
);
}
}
if (connection.getRequestProperty(ACCEPT_ENCODING) == null) {
connection.setRequestProperty(
ACCEPT_ENCODING,
ContentEncoding.GZIP.getValue()
);
}
Object requestBody = null;
if (method != HttpMethod.GET && entity != null) {
requestBody = entity.getBody();
Expand Down Expand Up @@ -156,9 +163,12 @@ public ResponseEntity request(
if (responseContentType == null) {
responseContentType = ContentTypes.APPLICATION_JSON;
}
InputStream inputStream = responseCode >= 400
? connection.getErrorStream()
: connection.getInputStream();
InputStream inputStream = decorateInputStream(
connection,
responseCode >= 400
? connection.getErrorStream()
: connection.getInputStream()
);
Object responseBody = null;
if (inputStream != null) {
try {
Expand Down Expand Up @@ -197,7 +207,8 @@ protected byte[] serializeRequestBody(
protected Object deserializeResponseBody(
String contentType,
int contentLength,
InputStream inputStream, Class<?> responseType
InputStream inputStream,
Class<?> responseType
) throws IOException {
BodyDeserializer deserializer = dataConverters.getBodyDeserializer(contentType);
Object body;
Expand Down Expand Up @@ -365,7 +376,12 @@ private String download(
Files.deleteIfExists(downloadingFilePath);
Files.createFile(downloadingFilePath);

try (InputStream inputStream = connection.getInputStream()) {
try (
InputStream inputStream = decorateInputStream(
connection,
connection.getInputStream()
)
) {
try (FileOutputStream outputStream = new FileOutputStream(downloadingFile)) {
int bytesRead;
byte[] buffer = new byte[1024];
Expand Down Expand Up @@ -467,7 +483,12 @@ private void download(
if (responseCode >= 400) {
throw processDownloadError(connection, fileURL, responseCode);
}
try (InputStream inputStream = connection.getInputStream()) {
try (
InputStream inputStream = decorateInputStream(
connection,
connection.getInputStream()
)
) {
int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = inputStream.read(buffer)) != -1) {
Expand Down Expand Up @@ -587,8 +608,11 @@ private DownloadFileResult download(
.toFile();
EzyFileUtil.createFileIfNotExists(storeFile);
try (
InputStream inputStream = connection.getInputStream();
OutputStream outputStream = new FileOutputStream(storeFile)
InputStream inputStream = decorateInputStream(
connection,
connection.getInputStream()
);
OutputStream outputStream = Files.newOutputStream(storeFile.toPath())
) {
int bytesRead;
byte[] buffer = new byte[1024];
Expand Down Expand Up @@ -625,12 +649,37 @@ private void decorateConnection(
}
}

private InputStream decorateInputStream(
HttpURLConnection connection,
InputStream inputStream
) throws Exception {
return decorateInputStream(
connection.getHeaderField(CONTENT_ENCODING),
inputStream
);
}

private InputStream decorateInputStream(
String contentEncoding,
InputStream inputStream
) throws IOException {
ContentEncoding contentEncodingEnum = ContentEncoding
.ofValue(contentEncoding);
if (contentEncodingEnum == ContentEncoding.GZIP) {
return new GZIPInputStream(inputStream);
}
return inputStream;
}

private Exception processDownloadError(
HttpURLConnection connection,
String fileURL,
int responseCode
) throws Exception {
InputStream inputStream = connection.getErrorStream();
InputStream inputStream = decorateInputStream(
connection,
connection.getErrorStream()
);
Object responseBody = "";
if (inputStream != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public Builder headers(Map<String, String> headers) {
return this;
}

public Builder accept(String contentEncoding) {
return header(Headers.ACCEPT_ENCODING, contentEncoding);
}

public Builder contentType(String contentType) {
return header(Headers.CONTENT_TYPE, contentType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.tvd12.ezyhttp.core.codec.BodyDeserializer;
import com.tvd12.ezyhttp.core.codec.SingletonStringDeserializer;
import com.tvd12.ezyhttp.core.codec.TextBodyConverter;
import com.tvd12.ezyhttp.core.constant.ContentEncoding;
import com.tvd12.ezyhttp.core.constant.ContentTypes;
import com.tvd12.ezyhttp.core.constant.StatusCodes;
import com.tvd12.ezyhttp.core.exception.*;
Expand Down Expand Up @@ -140,6 +141,38 @@ public void callTest() throws Exception {
Asserts.assertEquals(expectation, actual);
}

@Test
public void callAcceptGzipTest() throws Exception {
// given
HttpClient sut = HttpClient.builder()
.objectMapper(new Object())
.objectMapper(new ObjectMapper())
.build();

String who = RandomUtil.randomAlphabetString(128);
PostRequest request = new PostRequest()
.setConnectTimeout(-1)
.setReadTimeout(1500000)
.setEntity(
RequestEntity.builder()
.body(new TestRequest(who))
.header("hello", "world")
.header("foo", "bar")
.accept(ContentEncoding.GZIP.getValue())
.build()
)
.setResponseType(TestResponse.class)
.setResponseType(StatusCodes.OK, TestResponse.class)
.setURL(URI.create("http://127.0.0.1:18081/greet"));

// when
TestResponse actual = sut.call(request);

// then
TestResponse expectation = new TestResponse("Greet " + who + "!");
Asserts.assertEquals(expectation, actual);
}

@Test
public void callPostFormTest() throws Exception {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ public void test() throws Exception {
String fileURL = "https://youngmonkeys.org";
int readTimeout = RandomUtil.randomInt();
int connectionTimeout = RandomUtil.randomInt();
DownloadRequest sut = new DownloadRequest();

MultiValueMap headers = MultiValueMap.builder()
.setValue("hello", "world")
.build();

sut.setFileURL(fileURL);
sut.setFileURL(new URL(fileURL));
sut.setFileURL(URI.create(fileURL));
sut.setReadTimeout(readTimeout);
sut.setConnectTimeout(connectionTimeout);
sut.setHeaders(headers);
DownloadRequest sut = new DownloadRequest()
.setFileURL(fileURL)
.setFileURL(new URL(fileURL))
.setFileURL(URI.create(fileURL))
.setReadTimeout(readTimeout)
.setConnectTimeout(connectionTimeout)
.setHeaders(headers);

// when
// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import lombok.AccessLevel;
import lombok.Setter;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;

Expand Down Expand Up @@ -65,7 +67,10 @@ public void start() {
List<Connector> connectors = new ArrayList<>();
connectors.add(connector);
server.setConnectors(connectors.toArray(new Connector[0]));
ServletContextHandler servletHandler = newServletHandler();
Handler servletHandler = newServletHandler();
GzipHandler gzipHandler = newGzipHandler();
gzipHandler.setHandler(servletHandler);
servletHandler = gzipHandler;
server.setHandler(servletHandler);
EzyProcessor.processSilently(server::start);
logger.info("http server started on: {}:{}", host, port);
Expand All @@ -83,4 +88,11 @@ protected ServletContextHandler newServletHandler() {
));
return servletHandler;
}

protected GzipHandler newGzipHandler() {
GzipHandler gzipHandler = new GzipHandler();
gzipHandler.setMinGzipSize(128);
gzipHandler.setIncludedMethods("GET", "POST");
return gzipHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tvd12.ezyfox.io.EzyStrings;
import com.tvd12.ezyhttp.core.codec.JsonBodyConverter;
import com.tvd12.ezyhttp.core.constant.ContentTypes;
import com.tvd12.ezyhttp.core.constant.StatusCodes;

import javax.servlet.ServletException;
Expand Down Expand Up @@ -57,6 +58,7 @@ protected void doHandle(HttpServletRequest req, HttpServletResponse resp) throws
}

String message = "{\"message\":\"Greet " + who + "!\"}";
resp.setContentType(ContentTypes.APPLICATION_JSON);
resp.getOutputStream().write(message.getBytes());
resp.setStatus(StatusCodes.OK);
break;
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>
</parent>

<artifactId>ezyhttp-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.tvd12.ezyfox.stream.EzyInputStreams;
import com.tvd12.ezyhttp.core.data.BodyData;

import static com.tvd12.ezyfox.stream.EzyInputStreams.DEFAULT_BUFFER_SIZE;

public interface BodyDeserializer {

default <T> T deserialize(
Expand Down Expand Up @@ -34,15 +36,10 @@ default String deserializeToString(
InputStream inputStream,
int contentLength
) throws IOException {
byte[] bytes;
int readBytes;
if (contentLength > 0) {
bytes = new byte[contentLength];
readBytes = inputStream.read(bytes);
} else {
bytes = EzyInputStreams.toByteArray(inputStream);
readBytes = bytes.length;
}
return new String(bytes, 0, readBytes, StandardCharsets.UTF_8);
byte[] bytes = EzyInputStreams.toByteArray(
inputStream,
contentLength > 0 ? contentLength : DEFAULT_BUFFER_SIZE
);
return new String(bytes, StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ public enum ContentEncoding {

private final String mimeType;
private final String value;

private static final Map<String, ContentEncoding> VALUE_BY_MIME_TYPE =
EzyEnums.enumMap(ContentEncoding.class, it -> it.mimeType);

private static final Map<String, ContentEncoding> VALUE_BY_VALUE_LOWERCASE =
EzyEnums.enumMap(ContentEncoding.class, it -> it.value.toLowerCase());

ContentEncoding(String mimeType, String value) {
this.mimeType = mimeType;
this.value = value;
}

public static ContentEncoding ofValue(String value) {
if (value == null) {
return null;
}
return VALUE_BY_VALUE_LOWERCASE.get(value.toLowerCase());
}

public static ContentEncoding ofMimeType(String mimeType) {
return VALUE_BY_MIME_TYPE.get(mimeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public final class Headers {

public static final String ACCEPT_ENCODING = "Accept-Encoding";
public static final String ACCEPT_RANGES = "accept-ranges";
public static final String CONTENT_ENCODING = "Content-Encoding";
public static final String CONTENT_RANGE = "Content-Range";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void deserializeToStringWithContentLengthTest() throws Exception {
String actual = sut.deserializeToString(inputStream, contentLength);

// then
Asserts.assertEquals("ab", actual);
Asserts.assertEquals("abc", actual);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ public void getterTest() {
ContentType.GZIP.getMimeType()
);
}

@Test
public void ofValueNullTest() {
Asserts.assertNull(ContentEncoding.ofValue(null));
}

@Test
public void offNonNullValueTest() {
Asserts.assertEquals(
ContentEncoding.ofValue("gzip"),
ContentEncoding.GZIP
);
}
}
Loading
Loading