Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -13,4 +13,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [How To Get Cookies From the Apache HttpClient Response](https://www.baeldung.com/java-apache-httpclient-cookies)
|
||||
- [Enabling Logging for Apache HttpClient](https://www.baeldung.com/apache-httpclient-enable-logging)
|
||||
- [Apache HttpClient vs. CloseableHttpClient](https://www.baeldung.com/apache-httpclient-vs-closeablehttpclient)
|
||||
- [Expand Shortened URLs with Apache HttpClient](https://www.baeldung.com/apache-httpclient-expand-url)
|
||||
- More articles: [[<-- prev]](../apache-httpclient)
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
package com.baeldung.httpclient.expandUrl;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import org.apache.hc.client5.http.classic.methods.HttpHead;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpHeaders;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HttpClientExpandUrlLiveTest {
|
||||
|
||||
private static CloseableHttpClient httpClient;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
httpClient = HttpClientBuilder.create()
|
||||
.disableRedirectHandling()
|
||||
.build();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void tearDown() throws IOException {
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenShortenedOnce_whenUrlIsExpanded_thenCorrectResult() throws IOException {
|
||||
final String expectedResult = "https://www.baeldung.com/rest-versioning";
|
||||
final String actualResult = expandSingleLevel("http://bit.ly/3LScTri");
|
||||
assertThat(actualResult, equalTo(expectedResult));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenShortenedMultiple_whenUrlIsExpanded_thenCorrectResult() throws IOException {
|
||||
final String expectedResult = "https://www.baeldung.com/rest-versioning";
|
||||
final String actualResult = expand("http://t.co/e4rDDbnzmk");
|
||||
assertThat(actualResult, equalTo(expectedResult));
|
||||
}
|
||||
|
||||
private String expand(final String urlArg) throws IOException {
|
||||
String originalUrl = urlArg;
|
||||
String newUrl = expandSingleLevel(originalUrl);
|
||||
while (!originalUrl.equals(newUrl)) {
|
||||
originalUrl = newUrl;
|
||||
newUrl = expandSingleLevel(originalUrl);
|
||||
}
|
||||
|
||||
return newUrl;
|
||||
}
|
||||
|
||||
final String expandSafe(final String urlArg) throws IOException {
|
||||
String originalUrl = urlArg;
|
||||
String newUrl = expandSingleLevelSafe(originalUrl).getRight();
|
||||
final List<String> alreadyVisited = Lists.newArrayList(originalUrl, newUrl);
|
||||
while (!originalUrl.equals(newUrl)) {
|
||||
originalUrl = newUrl;
|
||||
final Pair<Integer, String> statusAndUrl = expandSingleLevelSafe(originalUrl);
|
||||
newUrl = statusAndUrl.getRight();
|
||||
final boolean isRedirect = statusAndUrl.getLeft() == 301 || statusAndUrl.getLeft() == 302;
|
||||
if (isRedirect && alreadyVisited.contains(newUrl)) {
|
||||
throw new IllegalStateException("Likely a redirect loop");
|
||||
}
|
||||
alreadyVisited.add(newUrl);
|
||||
}
|
||||
|
||||
return newUrl;
|
||||
}
|
||||
|
||||
private Pair<Integer, String> expandSingleLevelSafe(final String url) throws IOException {
|
||||
try {
|
||||
HttpHead request = new HttpHead(url);
|
||||
Pair<Integer, String> resp = httpClient.execute(request, response -> {
|
||||
final int statusCode = response.getCode();
|
||||
if (statusCode != 301 && statusCode != 302) {
|
||||
return new ImmutablePair<>(statusCode, url);
|
||||
}
|
||||
final Header[] headers = response.getHeaders(HttpHeaders.LOCATION);
|
||||
Preconditions.checkState(headers.length == 1);
|
||||
final String newUrl = headers[0].getValue();
|
||||
|
||||
return new ImmutablePair<>(statusCode, newUrl);
|
||||
});
|
||||
return resp;
|
||||
} catch (final IllegalArgumentException uriEx) {
|
||||
return new ImmutablePair<>(500, url);
|
||||
}
|
||||
}
|
||||
|
||||
private String expandSingleLevel(final String url) throws IOException {
|
||||
try {
|
||||
HttpHead request = new HttpHead(url);
|
||||
String expandedUrl = httpClient.execute(request, response -> {
|
||||
final int statusCode = response.getCode();
|
||||
if (statusCode != 301 && statusCode != 302) {
|
||||
return url;
|
||||
}
|
||||
final Header[] headers = response.getHeaders(HttpHeaders.LOCATION);
|
||||
Preconditions.checkState(headers.length == 1);
|
||||
|
||||
return headers[0].getValue();
|
||||
});
|
||||
return expandedUrl;
|
||||
} catch (final IllegalArgumentException uriEx) {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.httpclient;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpStatus;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HttpClientCancelRequestLiveTest {
|
||||
|
||||
private static final String SAMPLE_URL = "http://www.github.com";
|
||||
|
||||
@Test
|
||||
void whenRequestIsCanceled_thenCorrect() throws IOException {
|
||||
HttpGet request = new HttpGet(SAMPLE_URL);
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
httpClient.execute(request, response -> {
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
System.out.println("----------------------------------------");
|
||||
System.out.println(response.getCode());
|
||||
if (entity != null) {
|
||||
System.out.println("Response content length: " + entity.getContentLength());
|
||||
}
|
||||
System.out.println("----------------------------------------");
|
||||
// Do not feel like reading the response body
|
||||
// Call abort on the request object
|
||||
request.abort();
|
||||
|
||||
assertThat(response.getCode()).isEqualTo(HttpStatus.SC_OK);
|
||||
return response;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-28
@@ -1,8 +1,8 @@
|
||||
package com.baeldung.httpclient.base;
|
||||
|
||||
import com.baeldung.httpclient.ResponseUtil;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpHeaders;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@@ -10,7 +10,6 @@ import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.conn.ConnectTimeoutException;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -73,30 +72,4 @@ public class HttpClientLiveTest {
|
||||
assertThat(headers, not(emptyArray()));
|
||||
}
|
||||
|
||||
// tests - cancel request
|
||||
|
||||
@Test
|
||||
public final void whenRequestIsCanceled_thenCorrect() throws IOException {
|
||||
instance = HttpClients.custom().build();
|
||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||
response = instance.execute(request);
|
||||
|
||||
try {
|
||||
final HttpEntity entity = response.getEntity();
|
||||
|
||||
System.out.println("----------------------------------------");
|
||||
System.out.println(response.getStatusLine());
|
||||
if (entity != null) {
|
||||
System.out.println("Response content length: " + entity.getContentLength());
|
||||
}
|
||||
System.out.println("----------------------------------------");
|
||||
|
||||
// Do not feel like reading the response body
|
||||
// Call abort on the request object
|
||||
request.abort();
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ This module contains articles about Apache HttpClient 4.5
|
||||
|
||||
### Relevant Articles
|
||||
|
||||
- [Apache HttpClient – Cancel Request](https://www.baeldung.com/httpclient-cancel-request)
|
||||
- [Apache HttpClient with SSL](https://www.baeldung.com/httpclient-ssl)
|
||||
- [Apache HttpClient Timeout](https://www.baeldung.com/httpclient-timeout)
|
||||
- [Custom HTTP Header with the Apache HttpClient](https://www.baeldung.com/httpclient-custom-http-header)
|
||||
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.httpclient;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HttpClientCancelRequestV4LiveTest {
|
||||
|
||||
private static final String SAMPLE_URL = "http://www.github.com";
|
||||
|
||||
private CloseableHttpClient instance;
|
||||
|
||||
private CloseableHttpResponse response;
|
||||
|
||||
@Before
|
||||
public final void before() {
|
||||
instance = HttpClientBuilder.create().build();
|
||||
}
|
||||
|
||||
@After
|
||||
public final void after() throws IllegalStateException, IOException {
|
||||
ResponseUtil.closeResponse(response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenRequestIsCanceled_thenCorrect() throws IOException {
|
||||
instance = HttpClients.custom().build();
|
||||
final HttpGet request = new HttpGet(SAMPLE_URL);
|
||||
response = instance.execute(request);
|
||||
|
||||
try {
|
||||
final HttpEntity entity = response.getEntity();
|
||||
|
||||
System.out.println("----------------------------------------");
|
||||
System.out.println(response.getStatusLine());
|
||||
if (entity != null) {
|
||||
System.out.println("Response content length: " + entity.getContentLength());
|
||||
}
|
||||
System.out.println("----------------------------------------");
|
||||
|
||||
// Do not feel like reading the response body
|
||||
// Call abort on the request object
|
||||
request.abort();
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -24,7 +24,7 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ApacheHttpClientRetryIntegrationTest {
|
||||
public class ApacheHttpClientRetryLiveTest {
|
||||
|
||||
private Integer requestCounter;
|
||||
private CloseableHttpClient httpClient;
|
||||
+3
-3
@@ -3,14 +3,14 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>lambda</artifactId>
|
||||
<artifactId>lambda-function</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>lambda</name>
|
||||
<name>lambda-function</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>aws-lambda</artifactId>
|
||||
<artifactId>aws-lambda-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>aws-lambda</artifactId>
|
||||
<name>aws-lambda</name>
|
||||
<artifactId>aws-lambda-modules</artifactId>
|
||||
<name>aws-lambda-modules</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
@@ -14,9 +14,9 @@
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>lambda</module>
|
||||
<module>shipping-tracker/ShippingFunction</module>
|
||||
<module>todo-reminder/ToDoFunction</module>
|
||||
<module>lambda-function</module>
|
||||
<module>shipping-tracker-lambda/ShippingFunction</module>
|
||||
<module>todo-reminder-lambda/ToDoFunction</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
+1
-1
@@ -9,7 +9,7 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>aws-lambda</artifactId>
|
||||
<artifactId>aws-lambda-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user