Skip to content
Closed
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 build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.32'

implementation 'org.apache.httpcomponents:httpclient:4.5.13'
implementation 'org.apache.httpcomponents.client5:httpclient5:5.3.1'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.3'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0','org.mockito:mockito-core:4.11.0'
Expand Down
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Sun Feb 22 22:18:26 EET 2026
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-9.0.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
8 changes: 4 additions & 4 deletions src/main/java/com/crowdin/client/core/CrowdinApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import com.crowdin.client.core.http.impl.http.ApacheHttpClient;
import com.crowdin.client.core.http.impl.json.JacksonJsonTransformer;
import com.crowdin.client.core.model.*;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.hc.client5.http.cookie.StandardCookieSpec;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -25,7 +25,7 @@ public CrowdinApi(Credentials credentials) {
this(credentials, ClientConfig.builder()
.httpClient(new ApacheHttpClient(credentials, new JacksonJsonTransformer(), Collections.emptyMap(), HttpClientBuilder
.create()
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(StandardCookieSpec.RELAXED).build())
.build()))
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,41 @@
import com.crowdin.client.core.http.exceptions.HttpException;
import com.crowdin.client.core.model.ClientConfig;
import com.crowdin.client.core.model.Credentials;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
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.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.hc.client5.http.classic.methods.*;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.cookie.StandardCookieSpec;
import org.apache.hc.client5.http.config.RequestConfig;
//import org.apache.http.client.methods.HttpUriRequest;
//import org.apache.http.client.methods.RequestBuilder;
//import org.apache.http.entity.ContentType;
//import org.apache.http.entity.InputStreamEntity;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.BasicCredentialsProvider;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;

import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.util.Timeout;

import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.classic.methods.HttpPost;

import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -38,6 +52,7 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class ApacheHttpClient implements HttpClient {

Expand All @@ -62,19 +77,20 @@ public ApacheHttpClient(Credentials credentials, JsonTransformer jsonTransformer
this.defaultHeaders = defaultHeaders;
this.proxy = proxy;
this.proxyCreds = proxyCreds;
RequestConfig.Builder requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD);
RequestConfig.Builder requestConfig = RequestConfig.custom().setCookieSpec(StandardCookieSpec.RELAXED);
if (timeoutMs != null) {
requestConfig.setConnectionRequestTimeout(timeoutMs);
requestConfig.setConnectTimeout(timeoutMs);
requestConfig.setSocketTimeout(timeoutMs);
Timeout timeout = Timeout.of(timeoutMs, TimeUnit.MILLISECONDS);
requestConfig.setConnectionRequestTimeout(timeout);
requestConfig.setConnectTimeout(timeout);
requestConfig.setResponseTimeout(timeout);
}
this.httpClient = (proxy != null)
? HttpClientBuilder.create()
.setProxy(new HttpHost(proxy.getHost(), proxy.getPort()))
.setDefaultRequestConfig(requestConfig.build())
.setDefaultCredentialsProvider((proxyCreds != null)
? new BasicCredentialsProvider() {{
setCredentials(new AuthScope(proxy.getHost(), proxy.getPort()), new UsernamePasswordCredentials(proxyCreds.getUsername(), proxyCreds.getPassword()));
setCredentials(new AuthScope(proxy.getHost(), proxy.getPort()), new UsernamePasswordCredentials(proxyCreds.getUsername(), proxyCreds.getPassword().toCharArray()));
}}
: new BasicCredentialsProvider())
.build()
Expand Down Expand Up @@ -119,10 +135,10 @@ private <T, V> T request(String url,
HttpRequestConfig config,
Class<T> clazz,
String method) throws HttpException, HttpBadRequestException {
HttpUriRequest request = this.buildRequest(method, url, data, config);
HttpUriRequestBase request = this.buildRequest(method, url, data, config);
String httpResponse = null;
try (CloseableHttpResponse response = httpClient.execute(request)) {
int statusCode = response.getStatusLine().getStatusCode();
int statusCode = response.getCode();
if (statusCode < 200 || statusCode >= 300) {
String error = this.toString(response.getEntity());
throw this.jsonTransformer.parse(error, CrowdinApiException.class);
Expand All @@ -142,30 +158,32 @@ private <T, V> T request(String url,
}
}

private <V> HttpUriRequest buildRequest(String httpMethod, String url, V data, HttpRequestConfig config) {
RequestBuilder requestBuilder = RequestBuilder.create(httpMethod);
requestBuilder.setUri(URI.create(this.appendUrlParams(url, config.getUrlParams())));
requestBuilder.addHeader("Authorization", "Bearer " + this.credentials.getToken());
private <V> HttpUriRequestBase buildRequest(String httpMethod, String url, V data, HttpRequestConfig config) {
//RequestBuilder requestBuilder = RequestBuilder.create(httpMethod);
HttpUriRequestBase request = new HttpUriRequestBase(httpMethod, URI.create(this.appendUrlParams(url, config.getUrlParams())));

//requestBuilder.setUri(URI.create(this.appendUrlParams(url, config.getUrlParams())));
request.addHeader("Authorization", "Bearer " + this.credentials.getToken());
if (data != null) {
HttpEntity entity;
if (data instanceof InputStream) {
entity = new InputStreamEntity((InputStream) data);
entity = new InputStreamEntity((InputStream) data, ContentType.APPLICATION_OCTET_STREAM);
} else if (data instanceof String) {
entity = new StringEntity((String) data, ContentType.APPLICATION_OCTET_STREAM);
} else {
entity = new StringEntity(this.jsonTransformer.convert(data), ContentType.APPLICATION_JSON);
}
requestBuilder.setEntity(entity);
request.setEntity(entity);
} else if (HttpPost.METHOD_NAME.equals(httpMethod)) {
requestBuilder.setEntity(new StringEntity("", ContentType.APPLICATION_JSON));
request.setEntity(new StringEntity("", ContentType.APPLICATION_JSON));
}
Map<String, Object> headers = new HashMap<>();
headers.putAll(config.getHeaders());
headers.putAll(this.defaultHeaders);
for (Map.Entry<String, ?> entry : headers.entrySet()) {
requestBuilder = requestBuilder.addHeader(entry.getKey(), entry.getValue().toString());
request.addHeader(entry.getKey(), entry.getValue().toString());
}
return requestBuilder.build();
return request;
}

private String toString(HttpEntity entity) throws IOException {
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/crowdin/client/ai/AIApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.framework.RequestMock;
import com.crowdin.client.framework.TestClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPatch;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.junit.jupiter.api.Test;

import java.util.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.framework.RequestMock;
import com.crowdin.client.framework.TestClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPatch;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.junit.jupiter.api.Test;

import java.util.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.crowdin.client.core.model.ResponseObject;
import com.crowdin.client.framework.RequestMock;
import com.crowdin.client.framework.TestClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/crowdin/client/bundles/BundlesApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import com.crowdin.client.sourcefiles.model.Branch;
import com.crowdin.client.sourcefiles.model.File;
import com.crowdin.client.sourcefiles.model.FileInfo;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPatch;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.crowdin.client.core.model.ResponseList;
import com.crowdin.client.framework.RequestMock;
import com.crowdin.client.framework.TestClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.junit.jupiter.api.Test;

import java.util.List;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/crowdin/client/core/CrowdinApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.crowdin.client.framework.TestClient;
import lombok.Data;
import lombok.var;
import org.apache.http.client.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import com.crowdin.client.core.model.BooleanInt;
import com.crowdin.client.core.model.Credentials;
import com.crowdin.client.labels.model.Label;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicStatusLine;
import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -85,7 +83,8 @@ private String getFile(String resourcePath) throws IOException {
public void testGetRequest() throws IOException {
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
//when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getCode()).thenReturn(200);
when(mockedResponse.getEntity()).thenReturn(new StringEntity(jsonResponse));

// Make a GET request and check the response
Expand All @@ -107,7 +106,8 @@ public void testGetRequest() throws IOException {
public void testGetRequestError() throws IOException {
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "FAIL"));
//when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "FAIL"));
when(mockedResponse.getCode()).thenReturn(200);
when(mockedResponse.getEntity()).thenReturn(new StringEntity(errorResponse));


Expand All @@ -119,7 +119,8 @@ public void testGetRequestError() throws IOException {
public void testDeleteRequest() throws IOException {
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
//when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getCode()).thenReturn(200);
when(mockedResponse.getEntity()).thenReturn(new StringEntity("Mocked response body"));


Expand All @@ -143,7 +144,8 @@ public void tesPostRequest() throws IOException {
requestData.setIsSystem(BooleanInt.TRUE);
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
//when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getCode()).thenReturn(200);
when(mockedResponse.getEntity()).thenReturn(new StringEntity(jsonResponse));

// Make a POST request and check the response
Expand All @@ -165,7 +167,8 @@ public void tesPostRequestWithInputStream() throws IOException {
InputStream requestData = new ByteArrayInputStream(jsonResponse.getBytes(StandardCharsets.UTF_8));
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
//when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getCode()).thenReturn(200);
when(mockedResponse.getEntity()).thenReturn(new StringEntity(jsonResponse));

// Make a POSt request and check the response
Expand All @@ -186,7 +189,8 @@ public void tesPostRequestWithInputStream() throws IOException {
public void tesPostRequestWithString() throws IOException {
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
//when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getCode()).thenReturn(200);
when(mockedResponse.getEntity()).thenReturn(new StringEntity(jsonResponse));

// Make a POST request and check the response
Expand All @@ -207,7 +211,8 @@ public void tesPostRequestWithString() throws IOException {
public void tesPostRequestWithNullData() throws IOException {
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
//when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getCode()).thenReturn(200);
when(mockedResponse.getEntity()).thenReturn(new StringEntity(jsonResponse));

// Make a POST request and check the response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.crowdin.client.dictionaries.model.Dictionary;
import com.crowdin.client.framework.RequestMock;
import com.crowdin.client.framework.TestClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPatch;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
Expand Down
Loading
Loading