From 7000dfe094d88cff47166f12584995de2c915eba Mon Sep 17 00:00:00 2001 From: rahul Date: Tue, 23 Jul 2019 21:49:03 +0530 Subject: [PATCH 1/2] Checking if a URL Exists in Java --- .../core-java-networking-2/.gitignore | 25 ++++++++++++ .../core-java-networking-2/README.md | 7 ++++ .../core-java-networking-2/pom.xml | 22 +++++++++++ .../java/com/baeldung/url/UrlChecker.java | 25 ++++++++++++ .../com/baeldung/url/UrlCheckerUnitTest.java | 39 +++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 core-java-modules/core-java-networking-2/.gitignore create mode 100644 core-java-modules/core-java-networking-2/README.md create mode 100644 core-java-modules/core-java-networking-2/pom.xml create mode 100644 core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/UrlChecker.java create mode 100644 core-java-modules/core-java-networking-2/src/test/java/com/baeldung/url/UrlCheckerUnitTest.java diff --git a/core-java-modules/core-java-networking-2/.gitignore b/core-java-modules/core-java-networking-2/.gitignore new file mode 100644 index 0000000000..374c8bf907 --- /dev/null +++ b/core-java-modules/core-java-networking-2/.gitignore @@ -0,0 +1,25 @@ +*.class + +0.* + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* +.resourceCache + +# Packaged files # +*.jar +*.war +*.ear + +# Files generated by integration tests +backup-pom.xml +/bin/ +/temp + +#IntelliJ specific +.idea/ +*.iml \ No newline at end of file diff --git a/core-java-modules/core-java-networking-2/README.md b/core-java-modules/core-java-networking-2/README.md new file mode 100644 index 0000000000..15cccf4a60 --- /dev/null +++ b/core-java-modules/core-java-networking-2/README.md @@ -0,0 +1,7 @@ +========= + +## Core Java Networking - 2 + +### Relevant Articles + +- [Checking if a URL Exists in Java](https://www.baeldung.com/checking-if-a-url-exists-with-java) diff --git a/core-java-modules/core-java-networking-2/pom.xml b/core-java-modules/core-java-networking-2/pom.xml new file mode 100644 index 0000000000..88e038d4c9 --- /dev/null +++ b/core-java-modules/core-java-networking-2/pom.xml @@ -0,0 +1,22 @@ + + 4.0.0 + core-java-networking-2 + 0.1.0-SNAPSHOT + core-java-networking-2 + jar + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../../parent-java + + + + + + + core-java-networking-2 + + diff --git a/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/UrlChecker.java b/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/UrlChecker.java new file mode 100644 index 0000000000..b99e74f8bf --- /dev/null +++ b/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/url/UrlChecker.java @@ -0,0 +1,25 @@ +package com.baeldung.url; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +public class UrlChecker { + + public int getResponseCodeForURL(String address) throws IOException { + return getResponseCodeForURLUsing(address, "GET"); + } + + public int getResponseCodeForURLUsingHead(String address) throws IOException { + return getResponseCodeForURLUsing(address, "HEAD"); + } + + private int getResponseCodeForURLUsing(String address, String method) throws IOException { + HttpURLConnection.setFollowRedirects(false); // Set follow redirects to false + final URL url = new URL(address); + HttpURLConnection huc = (HttpURLConnection) url.openConnection(); + huc.setRequestMethod(method); + return huc.getResponseCode(); + } + +} diff --git a/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/url/UrlCheckerUnitTest.java b/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/url/UrlCheckerUnitTest.java new file mode 100644 index 0000000000..5e295e65a0 --- /dev/null +++ b/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/url/UrlCheckerUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.url; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; + +import org.junit.Test; + +public class UrlCheckerUnitTest { + + @Test + public void givenValidUrl_WhenUsingHEAD_ThenReturn200() throws IOException { + UrlChecker tester = new UrlChecker(); + int responseCode = tester.getResponseCodeForURLUsingHead("http://www.example.com"); + assertEquals(200, responseCode); + } + + @Test + public void givenInvalidIUrl_WhenUsingHEAD_ThenReturn404() throws IOException { + UrlChecker tester = new UrlChecker(); + int responseCode = tester.getResponseCodeForURLUsingHead("http://www.example.com/unkownurl"); + assertEquals(404, responseCode); + } + + @Test + public void givenValidUrl_WhenUsingGET_ThenReturn200() throws IOException { + UrlChecker tester = new UrlChecker(); + int responseCode = tester.getResponseCodeForURL("http://www.example.com"); + assertEquals(200, responseCode); + } + + @Test + public void givenInvalidIUrl_WhenUsingGET_ThenReturn404() throws IOException { + UrlChecker tester = new UrlChecker(); + int responseCode = tester.getResponseCodeForURL("http://www.example.com/unkownurl"); + assertEquals(404, responseCode); + } + +} From 45f85e2c7d010d8cedeece71e270a7f81b8428d5 Mon Sep 17 00:00:00 2001 From: rahul Date: Thu, 25 Jul 2019 07:49:53 +0530 Subject: [PATCH 2/2] Changing the parent pom for core-java-networking-2 --- core-java-modules/core-java-networking-2/README.md | 7 ------- core-java-modules/core-java-networking-2/pom.xml | 8 +++----- core-java-modules/pom.xml | 1 + 3 files changed, 4 insertions(+), 12 deletions(-) delete mode 100644 core-java-modules/core-java-networking-2/README.md diff --git a/core-java-modules/core-java-networking-2/README.md b/core-java-modules/core-java-networking-2/README.md deleted file mode 100644 index 15cccf4a60..0000000000 --- a/core-java-modules/core-java-networking-2/README.md +++ /dev/null @@ -1,7 +0,0 @@ -========= - -## Core Java Networking - 2 - -### Relevant Articles - -- [Checking if a URL Exists in Java](https://www.baeldung.com/checking-if-a-url-exists-with-java) diff --git a/core-java-modules/core-java-networking-2/pom.xml b/core-java-modules/core-java-networking-2/pom.xml index 88e038d4c9..8a26f6ab9f 100644 --- a/core-java-modules/core-java-networking-2/pom.xml +++ b/core-java-modules/core-java-networking-2/pom.xml @@ -2,15 +2,13 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 core-java-networking-2 - 0.1.0-SNAPSHOT core-java-networking-2 jar - com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../../parent-java + com.baeldung.core-java-modules + core-java-modules + 1.0.0-SNAPSHOT diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 11a1003460..2dca62005d 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -17,6 +17,7 @@ pre-jpms core-java-exceptions core-java-optional + core-java-networking-2