From 9048d5215bc608d77834903f858ba9cc63d9acd0 Mon Sep 17 00:00:00 2001 From: hariprasad108 Date: Mon, 19 Jun 2017 00:27:34 +0200 Subject: [PATCH] Difference between url and uri bael 864 hariprasad net (#2108) * BAEL-864, java.net.* * java-networking Bael-864, revision * Final and static keywords removed. * Bael-486 Static import for JUnit into pom.xml was added. * Bael-486, pom.xml was renamed on networking-pom.xml to deactivate it. * Maven pom.xml changed. * Delete java-networing project. * Bael-684, rearanged. --- core-java/pom.xml | 721 +++++++++--------- .../baeldung/javanetworking/url/URLDemo.java | 69 ++ core-java/src/main/java/log4j.properties | 9 + .../javanetworking/url/test/URLDemoTest.java | 106 +++ 4 files changed, 547 insertions(+), 358 deletions(-) create mode 100644 core-java/src/main/java/com/baeldung/javanetworking/url/URLDemo.java create mode 100644 core-java/src/main/java/log4j.properties create mode 100644 core-java/src/test/java/com/baeldung/javanetworking/url/test/URLDemoTest.java diff --git a/core-java/pom.xml b/core-java/pom.xml index 2267dba1e6..f6d2ab79e2 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -1,404 +1,409 @@ - 4.0.0 - com.baeldung - core-java - 0.1.0-SNAPSHOT - jar + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.baeldung + core-java + 0.1.0-SNAPSHOT + jar - core-java + core-java - + - - - net.sourceforge.collections - collections-generic - ${collections-generic.version} - - - com.google.guava - guava - ${guava.version} - + + + net.sourceforge.collections + collections-generic + ${collections-generic.version} + + + com.google.guava + guava + ${guava.version} + - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + - - commons-io - commons-io - ${commons-io.version} - + + commons-io + commons-io + ${commons-io.version} + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - - org.apache.commons - commons-math3 - ${commons-math3.version} - + + org.apache.commons + commons-math3 + ${commons-math3.version} + - - org.decimal4j - decimal4j - ${decimal4j.version} - + + org.decimal4j + decimal4j + ${decimal4j.version} + - - org.bouncycastle - bcprov-jdk15on - ${bouncycastle.version} - + + org.bouncycastle + bcprov-jdk15on + ${bouncycastle.version} + - - org.unix4j - unix4j-command - ${unix4j.version} - + + org.unix4j + unix4j-command + ${unix4j.version} + - - com.googlecode.grep4j - grep4j - ${grep4j.version} - - + + com.googlecode.grep4j + grep4j + ${grep4j.version} + + - + - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - + - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - org.slf4j - jcl-over-slf4j - ${org.slf4j.version} - - - - org.slf4j - log4j-over-slf4j - ${org.slf4j.version} - - - org.projectlombok - lombok - ${lombok.version} - provided - + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + org.slf4j + slf4j-log4j12 + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + - + - - org.hamcrest - hamcrest-all - 1.3 - test - + + org.hamcrest + hamcrest-all + 1.3 + test + - - junit - junit - ${junit.version} - test - + + junit + junit + ${junit.version} + test + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - - org.assertj - assertj-core - ${assertj.version} - test - + + org.assertj + assertj-core + ${assertj.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - - - com.jayway.awaitility - awaitility - ${avaitility.version} - test - + + org.mockito + mockito-core + ${mockito.version} + test + + + com.jayway.awaitility + awaitility + ${avaitility.version} + test + - - commons-codec - commons-codec - ${commons-codec.version} - + + commons-codec + commons-codec + ${commons-codec.version} + - - org.javamoney - moneta - 1.1 - + + org.javamoney + moneta + 1.1 + - - org.owasp.esapi - esapi - 2.1.0.1 - + + org.owasp.esapi + esapi + 2.1.0.1 + - - com.sun.messaging.mq - fscontext - ${fscontext.version} - - + + com.sun.messaging.mq + fscontext + ${fscontext.version} + + - - core-java - - - src/main/resources - true - - + + core-java + + + src/main/resources + true + + - + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - - org.apache.maven.plugins - maven-surefire-plugin - - - **/*IntegrationTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - - true - - + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + true + + - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + - - org.apache.maven.plugins - maven-jar-plugin - - - - true - libs/ - org.baeldung.executable.ExecutableMavenJar - - - - + + org.apache.maven.plugins + maven-jar-plugin + + + + true + libs/ + org.baeldung.executable.ExecutableMavenJar + + + + - - org.apache.maven.plugins - maven-assembly-plugin - - - package - - single - - - ${project.basedir} - - - org.baeldung.executable.ExecutableMavenJar - - - - jar-with-dependencies - - - - - + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + ${project.basedir} + + + org.baeldung.executable.ExecutableMavenJar + + + + jar-with-dependencies + + + + + - - org.apache.maven.plugins - maven-shade-plugin - - - - shade - - - true - - - org.baeldung.executable.ExecutableMavenJar - - - - - - + + org.apache.maven.plugins + maven-shade-plugin + + + + shade + + + true + + + org.baeldung.executable.ExecutableMavenJar + + + + + + - - com.jolira - onejar-maven-plugin - - - - org.baeldung.executable.ExecutableMavenJar - true - ${project.build.finalName}-onejar.${project.packaging} - - - one-jar - - - - + + com.jolira + onejar-maven-plugin + + + + org.baeldung.executable.ExecutableMavenJar + true + ${project.build.finalName}-onejar.${project.packaging} + + + one-jar + + + + - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - spring-boot - org.baeldung.executable.ExecutableMavenJar - - - - + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + spring-boot + org.baeldung.executable.ExecutableMavenJar + + + + - + - + - - - integration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*ManualTest.java - - - **/*IntegrationTest.java - - - - - - - json - - - - - - - + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*ManualTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + - - - 2.8.5 + + + 2.8.5 - - 1.7.21 - 1.1.7 + + 1.7.21 + 1.1.7 - - 21.0 - 3.5 - 1.55 - 1.10 - 3.6.1 - 1.0.3 - 2.5 - 4.1 - 4.01 - 0.4 - 1.8.7 - 1.16.12 - 4.6-b01 + + 21.0 + 3.5 + 1.55 + 1.10 + 3.6.1 + 1.0.3 + 2.5 + 4.1 + 4.01 + 0.4 + 1.8.7 + 1.16.12 + 4.6-b01 - - 1.3 - 4.12 - 1.10.19 - 3.6.1 - 1.7.0 + + 1.3 + 4.12 + 1.10.19 + 3.6.1 + 1.7.0 - - 3.6.0 - 2.19.1 + + 3.6.0 + 2.19.1 - + \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/javanetworking/url/URLDemo.java b/core-java/src/main/java/com/baeldung/javanetworking/url/URLDemo.java new file mode 100644 index 0000000000..b62b491548 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/javanetworking/url/URLDemo.java @@ -0,0 +1,69 @@ +package com.baeldung.javanetworking.url; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class URLDemo { + private static Logger log = LoggerFactory.getLogger(URLDemo.class); + + String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484"; + // parsed locator + String URLPROTOCOL = "https"; + // final static String URLAUTHORITY = "wordpress.org:443"; + String URLHOST = "wordpress.org"; + String URLPATH = "/support/topic/page-jumps-within-wordpress/"; + // final static String URLFILENAME = "/support/topic/page-jumps-within-wordpress/?replies=3"; + // final static int URLPORT = 443; + int URLDEFAULTPORT = 443; + String URLQUERY = "replies=3"; + String URLREFERENCE = "post-2278484"; + String URLCOMPOUND = URLPROTOCOL + "://" + URLHOST + ":" + URLDEFAULTPORT + URLPATH + "?" + URLQUERY + "#" + URLREFERENCE; + + URL url; + URLConnection urlConnection = null; + HttpURLConnection connection = null; + BufferedReader in = null; + String urlContent = ""; + + public String testURL(String urlString) throws IOException, IllegalArgumentException { + String urlStringCont = ""; + // comment the if clause if experiment with URL + /*if (!URLSTRING.equals(urlString)) { + throw new IllegalArgumentException("URL String argument is not proper: " + urlString); + }*/ + // creating URL object + url = new URL(urlString); + // get URL connection + urlConnection = url.openConnection(); + connection = null; + // we can check, if connection is proper type + if (urlConnection instanceof HttpURLConnection) { + connection = (HttpURLConnection) urlConnection; + } else { + log.info("Please enter an HTTP URL"); + throw new IOException("HTTP URL is not correct"); + } + // we can check response code (200 OK is expected) + log.info(connection.getResponseCode() + " " + connection.getResponseMessage()); + in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String current; + + while ((current = in.readLine()) != null) { + urlStringCont += current; + } + return urlStringCont; + } + + public static void main(String[] args) throws Exception { + URLDemo demo = new URLDemo(); + String content = demo.testURL(demo.URLCOMPOUND); + log.info(content); + } +} diff --git a/core-java/src/main/java/log4j.properties b/core-java/src/main/java/log4j.properties new file mode 100644 index 0000000000..5fe42d854c --- /dev/null +++ b/core-java/src/main/java/log4j.properties @@ -0,0 +1,9 @@ +# Set root logger level to DEBUG and its only appender to A1. +log4j.rootLogger=DEBUG, A1 + +# A1 is set to be a ConsoleAppender. +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +# A1 uses PatternLayout. +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n diff --git a/core-java/src/test/java/com/baeldung/javanetworking/url/test/URLDemoTest.java b/core-java/src/test/java/com/baeldung/javanetworking/url/test/URLDemoTest.java new file mode 100644 index 0000000000..98a2f915d2 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/javanetworking/url/test/URLDemoTest.java @@ -0,0 +1,106 @@ +package com.baeldung.javanetworking.url.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; + +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.baeldung.javanetworking.url.URLDemo; + +@FixMethodOrder +public class URLDemoTest { + private static Logger log = LoggerFactory.getLogger(URLDemo.class); + static String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484"; + // parsed locator + static String URLPROTOCOL = "https"; + String URLAUTHORITY = "wordpress.org:443"; + static String URLHOST = "wordpress.org"; + static String URLPATH = "/support/topic/page-jumps-within-wordpress/"; + String URLFILENAME = "/support/topic/page-jumps-within-wordpress/?replies=3"; + int URLPORT = 443; + static int URLDEFAULTPORT = 443; + static String URLQUERY = "replies=3"; + static String URLREFERENCE = "post-2278484"; + static String URLCOMPOUND = URLPROTOCOL + "://" + URLHOST + ":" + URLDEFAULTPORT + URLPATH + "?" + URLQUERY + "#" + URLREFERENCE; + + static URL url; + URLConnection urlConnection = null; + HttpURLConnection connection = null; + BufferedReader in = null; + String urlContent = ""; + + @BeforeClass + public static void givenEmplyURL_whenInitializeURL_thenSuccess() throws MalformedURLException { + url = new URL(URLCOMPOUND); + } + + // check parsed URL + @Test + public void givenURL_whenURLIsParsed_thenSuccess() { + assertNotNull("URL is null", url); + assertEquals("URL string is not equal", url.toString(), URLSTRING); + assertEquals("Protocol is not equal", url.getProtocol(), URLPROTOCOL); + assertEquals("Authority is not equal", url.getAuthority(), URLAUTHORITY); + assertEquals("Host string is not equal", url.getHost(), URLHOST); + assertEquals("Path string is not equal", url.getPath(), URLPATH); + assertEquals("File string is not equal", url.getFile(), URLFILENAME); + assertEquals("Port number is not equal", url.getPort(), URLPORT); + assertEquals("Default port number is not equal", url.getDefaultPort(), URLDEFAULTPORT); + assertEquals("Query string is not equal", url.getQuery(), URLQUERY); + assertEquals("Reference string is not equal", url.getRef(), URLREFERENCE); + } + + // Obtain the content from location + @Test + public void givenURL_whenOpenConnectionAndContentIsNotEmpty_thenSuccess() throws IOException { + try { + urlConnection = url.openConnection(); + } catch (IOException ex) { + urlConnection = null; + ex.printStackTrace(); + } + assertNotNull("URL Connection is null", urlConnection); + + connection = null; + assertTrue("URLConnection is not HttpURLConnection", urlConnection instanceof HttpURLConnection); + if (urlConnection instanceof HttpURLConnection) { + connection = (HttpURLConnection) urlConnection; + } + assertNotNull("Connection is null", connection); + + log.info(connection.getResponseCode() + " " + connection.getResponseMessage()); + + try { + in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + } catch (IOException ex) { + in = null; + ex.printStackTrace(); + } + assertNotNull("Input stream failed", in); + + String current; + try { + while ((current = in.readLine()) != null) { + urlContent += current; + } + } catch (IOException ex) { + urlContent = null; + ex.printStackTrace(); + } + assertNotNull("Content is null", urlContent); + assertTrue("Content is empty", urlContent.length() > 0); + } +}