From eaf222dfa069b92b1ee6b6ae0143e15448e35bc3 Mon Sep 17 00:00:00 2001
From: DiegoMarti2 <150871541+DiegoMarti2@users.noreply.github.com>
Date: Sat, 13 Apr 2024 22:53:03 +0200
Subject: [PATCH] baeldung-articles : BAEL-6484 (#16398)
* updating the pom.xml file
Here i add a new module networking-5
* baeldung-articles : BAEL-6484
Finding the Redirected URL of a URL in Java.
---
.../core-java-networking-5/README.md | 2 +
.../core-java-networking-5/pom.xml | 59 +++++++++++++++++++
.../redirectedurl/RedirectedUrlUnitTest.java | 57 ++++++++++++++++++
core-java-modules/pom.xml | 1 +
4 files changed, 119 insertions(+)
create mode 100644 core-java-modules/core-java-networking-5/README.md
create mode 100644 core-java-modules/core-java-networking-5/pom.xml
create mode 100644 core-java-modules/core-java-networking-5/src/test/java/com/baeldung/redirectedurl/RedirectedUrlUnitTest.java
diff --git a/core-java-modules/core-java-networking-5/README.md b/core-java-modules/core-java-networking-5/README.md
new file mode 100644
index 0000000000..5d69c73138
--- /dev/null
+++ b/core-java-modules/core-java-networking-5/README.md
@@ -0,0 +1,2 @@
+## Relevant Articles:
+- [[<-- Prev]](/core-java-modules/core-java-networking-4)
diff --git a/core-java-modules/core-java-networking-5/pom.xml b/core-java-modules/core-java-networking-5/pom.xml
new file mode 100644
index 0000000000..978a294204
--- /dev/null
+++ b/core-java-modules/core-java-networking-5/pom.xml
@@ -0,0 +1,59 @@
+
+
+ 4.0.0
+ core-java-networking-5
+ jar
+ core-java-networking-5
+
+
+ com.baeldung.core-java-modules
+ core-java-modules
+ 0.0.1-SNAPSHOT
+
+
+
+
+ commons-validator
+ commons-validator
+ ${commons-validator.version}
+
+
+ org.jsoup
+ jsoup
+ ${jsoup.version}
+
+
+ org.apache.httpcomponents
+ httpclient
+ ${httpclient.version}
+
+
+ javax.ws.rs
+ javax.ws.rs-api
+ ${javax.ws.rs-api.version}
+
+
+ org.glassfish.jersey.core
+ jersey-common
+ ${jersey-common.version}
+ test
+
+
+ org.springframework
+ spring-web
+ ${spring-web.version}
+
+
+
+
+ 1.7
+ 1.17.2
+ 4.5.2
+ 2.1.1
+ 2.22.2
+ 6.0.6
+
+
+
\ No newline at end of file
diff --git a/core-java-modules/core-java-networking-5/src/test/java/com/baeldung/redirectedurl/RedirectedUrlUnitTest.java b/core-java-modules/core-java-networking-5/src/test/java/com/baeldung/redirectedurl/RedirectedUrlUnitTest.java
new file mode 100644
index 0000000000..1fc1dc3699
--- /dev/null
+++ b/core-java-modules/core-java-networking-5/src/test/java/com/baeldung/redirectedurl/RedirectedUrlUnitTest.java
@@ -0,0 +1,57 @@
+package com.baeldung.redirectedurl;
+
+import org.apache.http.client.config.RequestConfig;
+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.HttpClients;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import static org.junit.Assert.assertEquals;
+
+public class RedirectedUrlUnitTest {
+ String canonicalUrl = "http://www.baeldung.com/";
+ String expectedRedirectedUrl = "https://www.baeldung.com/";
+
+ @Test
+ public void givenOriginalUrl_whenFindRedirectUrlUsingHttpURLConnection_thenCorrectRedirectedUrlReturned() throws IOException {
+ URL url = new URL(canonicalUrl);
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.setInstanceFollowRedirects(true);
+ int status = connection.getResponseCode();
+ String redirectedUrl = null;
+ if (status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_MOVED_TEMP) {
+ redirectedUrl = connection.getHeaderField("Location");
+ }
+ connection.disconnect();
+ assertEquals(expectedRedirectedUrl, redirectedUrl);
+ }
+
+ @Test
+ public void givenOriginalUrl_whenFindRedirectUrlUsingHttpClient_thenCorrectRedirectedUrlReturned() throws IOException {
+ RequestConfig config = RequestConfig.custom()
+ .setRedirectsEnabled(false)
+ .build();
+ try (CloseableHttpClient httpClient = HttpClients.custom()
+ .setDefaultRequestConfig(config)
+ .build()) {
+ HttpGet httpGet = new HttpGet(canonicalUrl);
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
+ int statusCode = response.getStatusLine().getStatusCode();
+ String redirectedUrl = null;
+ if (statusCode == HttpURLConnection.HTTP_MOVED_PERM || statusCode == HttpURLConnection.HTTP_MOVED_TEMP) {
+ org.apache.http.Header[] headers = response.getHeaders("Location");
+ if (headers.length > 0) {
+ redirectedUrl = headers[0].getValue();
+ }
+ }
+ assertEquals(expectedRedirectedUrl, redirectedUrl);
+ }
+ }
+ }
+
+}
diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml
index f6c5f8191a..00c40151e4 100644
--- a/core-java-modules/pom.xml
+++ b/core-java-modules/pom.xml
@@ -165,6 +165,7 @@
core-java-networking
core-java-networking-2
core-java-networking-4
+ core-java-networking-5
core-java-nio
core-java-nio-2
core-java-numbers