Skip to content

Commit

Permalink
[API] HTTP Client - Handling of custom response content types and dat…
Browse files Browse the repository at this point in the history
…a serialization (#4596)
  • Loading branch information
ThuF authored Jan 27, 2025
1 parent d3e13a7 commit 3d429b1
Showing 1 changed file with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
*/
package org.eclipse.dirigible.components.api.http;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
Expand All @@ -17,25 +26,28 @@
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.eclipse.dirigible.commons.api.helpers.GsonHelper;
import org.eclipse.dirigible.components.api.http.client.*;
import org.eclipse.dirigible.components.api.http.client.HttpClientHeader;
import org.eclipse.dirigible.components.api.http.client.HttpClientParam;
import org.eclipse.dirigible.components.api.http.client.HttpClientProxyUtils;
import org.eclipse.dirigible.components.api.http.client.HttpClientRequestOptions;
import org.eclipse.dirigible.components.api.http.client.HttpClientResponse;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
* Java face for HTTP operations.
*/
Expand Down Expand Up @@ -171,10 +183,10 @@ private static void prepareHeaders(HttpClientRequestOptions httpClientRequestOpt

/** The Constant recognizedTextMimeTypes. */
private static final HashSet<String> recognizedTextMimeTypes = new HashSet<>(Arrays.asList("application/json; charset=utf-8",
"text/json; charset=utf-8", "text/json", "application/CSV", "application/csv", "text/csv", ContentType.TEXT_PLAIN.getMimeType(),
ContentType.TEXT_HTML.getMimeType(), ContentType.TEXT_XML.getMimeType(), ContentType.APPLICATION_JSON.getMimeType(),
ContentType.APPLICATION_ATOM_XML.getMimeType(), ContentType.APPLICATION_XML.getMimeType(),
ContentType.APPLICATION_XHTML_XML.getMimeType()));
"text/json; charset=utf-8", "text/json", "application/CSV", "application/csv", "text/csv", "json",
ContentType.TEXT_PLAIN.getMimeType(), ContentType.TEXT_HTML.getMimeType(), ContentType.TEXT_XML.getMimeType(),
ContentType.APPLICATION_JSON.getMimeType(), ContentType.APPLICATION_ATOM_XML.getMimeType(),
ContentType.APPLICATION_XML.getMimeType(), ContentType.APPLICATION_XHTML_XML.getMimeType()));

/**
* Process http client response.
Expand All @@ -201,6 +213,12 @@ public static HttpClientResponse processHttpClientResponse(CloseableHttpResponse
String processedContentType = ContentType.getOrDefault(entity)
.getMimeType();
boolean isSupportedTextType = recognizedTextMimeTypes.contains(processedContentType);
if (!isSupportedTextType) {
Optional<String> recognizedTextMimeType = recognizedTextMimeTypes.stream()
.filter(e -> processedContentType.contains(e))
.findFirst();
isSupportedTextType = recognizedTextMimeType.isPresent();
}

if (!binary && isSupportedTextType) {
Charset charset = ContentType.getOrDefault(entity)
Expand Down Expand Up @@ -367,9 +385,12 @@ private static final HttpPost createPostTextRequest(String url, HttpClientReques
ContentType contentType = shouldParseContentType ? ContentType.parse(contentTypeString) : ContentType.create(contentTypeString);

EntityBuilder entityBuilder = EntityBuilder.create()
.setText(httpClientRequestOptions.getText())
.setBinary(httpClientRequestOptions.getText()
.getBytes(StandardCharsets.UTF_8))
.setContentType(contentType);

if (httpClientRequestOptions.isCharacterEncodingEnabled()) {
entityBuilder.setContentEncoding(httpClientRequestOptions.getCharacterEncoding());
}
httpPost.setEntity(entityBuilder.build());
return httpPost;
}
Expand Down Expand Up @@ -485,7 +506,8 @@ private static final HttpPut createPutTextRequest(String url, HttpClientRequestO
httpPut.setConfig(config);
prepareHeaders(httpClientRequestOptions, httpPut);
EntityBuilder entityBuilder = EntityBuilder.create()
.setText(httpClientRequestOptions.getText())
.setBinary(httpClientRequestOptions.getText()
.getBytes(StandardCharsets.UTF_8))
.setContentType(ContentType.create(httpClientRequestOptions.getContentType()));
if (httpClientRequestOptions.isCharacterEncodingEnabled()) {
entityBuilder.setContentEncoding(httpClientRequestOptions.getCharacterEncoding());
Expand Down Expand Up @@ -607,7 +629,8 @@ private static final HttpPatch createPatchTextRequest(String url, HttpClientRequ
httpPatch.setConfig(config);
prepareHeaders(httpClientRequestOptions, httpPatch);
EntityBuilder entityBuilder = EntityBuilder.create()
.setText(httpClientRequestOptions.getText())
.setBinary(httpClientRequestOptions.getText()
.getBytes(StandardCharsets.UTF_8))
.setContentType(ContentType.create(httpClientRequestOptions.getContentType()));
if (httpClientRequestOptions.isCharacterEncodingEnabled()) {
entityBuilder.setContentEncoding(httpClientRequestOptions.getCharacterEncoding());
Expand Down

0 comments on commit 3d429b1

Please sign in to comment.