Skip to content

Commit 547b585

Browse files
hariprasad108maibin
authored andcommitted
Bael-864, Networking, core-java. (eugenp#2111)
1 parent ed926b3 commit 547b585

3 files changed

Lines changed: 184 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.javanetworking.url;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.net.HttpURLConnection;
7+
import java.net.URL;
8+
import java.net.URLConnection;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public class URLDemo {
14+
private final Logger log = LoggerFactory.getLogger(URLDemo.class);
15+
16+
String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
17+
// parsed locator
18+
String URLPROTOCOL = "https";
19+
// final static String URLAUTHORITY = "wordpress.org:443";
20+
String URLHOST = "wordpress.org";
21+
String URLPATH = "/support/topic/page-jumps-within-wordpress/";
22+
// final static String URLFILENAME = "/support/topic/page-jumps-within-wordpress/?replies=3";
23+
// final static int URLPORT = 443;
24+
int URLDEFAULTPORT = 443;
25+
String URLQUERY = "replies=3";
26+
String URLREFERENCE = "post-2278484";
27+
String URLCOMPOUND = URLPROTOCOL + "://" + URLHOST + ":" + URLDEFAULTPORT + URLPATH + "?" + URLQUERY + "#" + URLREFERENCE;
28+
29+
URL url;
30+
URLConnection urlConnection = null;
31+
HttpURLConnection connection = null;
32+
BufferedReader in = null;
33+
String urlContent = "";
34+
35+
public String testURL(String urlString) throws IOException, IllegalArgumentException {
36+
String urlStringCont = "";
37+
// comment the if clause if experiment with URL
38+
/*if (!URLSTRING.equals(urlString)) {
39+
throw new IllegalArgumentException("URL String argument is not proper: " + urlString);
40+
}*/
41+
// creating URL object
42+
url = new URL(urlString);
43+
// get URL connection
44+
urlConnection = url.openConnection();
45+
connection = null;
46+
// we can check, if connection is proper type
47+
if (urlConnection instanceof HttpURLConnection) {
48+
connection = (HttpURLConnection) urlConnection;
49+
} else {
50+
log.info("Please enter an HTTP URL");
51+
throw new IOException("HTTP URL is not correct");
52+
}
53+
// we can check response code (200 OK is expected)
54+
log.info(connection.getResponseCode() + " " + connection.getResponseMessage());
55+
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
56+
String current;
57+
58+
while ((current = in.readLine()) != null) {
59+
urlStringCont += current;
60+
}
61+
return urlStringCont;
62+
}
63+
64+
public static void main(String[] args) throws Exception {
65+
URLDemo demo = new URLDemo();
66+
String content = demo.testURL(demo.URLCOMPOUND);
67+
demo.log.info(content);
68+
}
69+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Set root logger level to DEBUG and its only appender to A1.
2+
log4j.rootLogger=DEBUG, A1
3+
4+
# A1 is set to be a ConsoleAppender.
5+
log4j.appender.A1=org.apache.log4j.ConsoleAppender
6+
7+
# A1 uses PatternLayout.
8+
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
9+
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.baeldung.javanetworking.url.test;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
import java.net.HttpURLConnection;
11+
import java.net.MalformedURLException;
12+
import java.net.URL;
13+
import java.net.URLConnection;
14+
15+
import org.junit.BeforeClass;
16+
import org.junit.FixMethodOrder;
17+
import org.junit.Test;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import com.baeldung.javanetworking.url.URLDemo;
22+
23+
@FixMethodOrder
24+
public class URLDemoTest {
25+
private final Logger log = LoggerFactory.getLogger(URLDemo.class);
26+
static String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
27+
// parsed locator
28+
static String URLPROTOCOL = "https";
29+
String URLAUTHORITY = "wordpress.org:443";
30+
static String URLHOST = "wordpress.org";
31+
static String URLPATH = "/support/topic/page-jumps-within-wordpress/";
32+
String URLFILENAME = "/support/topic/page-jumps-within-wordpress/?replies=3";
33+
int URLPORT = 443;
34+
static int URLDEFAULTPORT = 443;
35+
static String URLQUERY = "replies=3";
36+
static String URLREFERENCE = "post-2278484";
37+
static String URLCOMPOUND = URLPROTOCOL + "://" + URLHOST + ":" + URLDEFAULTPORT + URLPATH + "?" + URLQUERY + "#" + URLREFERENCE;
38+
39+
static URL url;
40+
URLConnection urlConnection = null;
41+
HttpURLConnection connection = null;
42+
BufferedReader in = null;
43+
String urlContent = "";
44+
45+
@BeforeClass
46+
public static void givenEmplyURL_whenInitializeURL_thenSuccess() throws MalformedURLException {
47+
url = new URL(URLCOMPOUND);
48+
}
49+
50+
// check parsed URL
51+
@Test
52+
public void givenURL_whenURLIsParsed_thenSuccess() {
53+
assertNotNull("URL is null", url);
54+
assertEquals("URL string is not equal", url.toString(), URLSTRING);
55+
assertEquals("Protocol is not equal", url.getProtocol(), URLPROTOCOL);
56+
assertEquals("Authority is not equal", url.getAuthority(), URLAUTHORITY);
57+
assertEquals("Host string is not equal", url.getHost(), URLHOST);
58+
assertEquals("Path string is not equal", url.getPath(), URLPATH);
59+
assertEquals("File string is not equal", url.getFile(), URLFILENAME);
60+
assertEquals("Port number is not equal", url.getPort(), URLPORT);
61+
assertEquals("Default port number is not equal", url.getDefaultPort(), URLDEFAULTPORT);
62+
assertEquals("Query string is not equal", url.getQuery(), URLQUERY);
63+
assertEquals("Reference string is not equal", url.getRef(), URLREFERENCE);
64+
}
65+
66+
// Obtain the content from location
67+
@Test
68+
public void givenURL_whenOpenConnectionAndContentIsNotEmpty_thenSuccess() throws IOException {
69+
try {
70+
urlConnection = url.openConnection();
71+
} catch (IOException ex) {
72+
urlConnection = null;
73+
ex.printStackTrace();
74+
}
75+
assertNotNull("URL Connection is null", urlConnection);
76+
77+
connection = null;
78+
assertTrue("URLConnection is not HttpURLConnection", urlConnection instanceof HttpURLConnection);
79+
if (urlConnection instanceof HttpURLConnection) {
80+
connection = (HttpURLConnection) urlConnection;
81+
}
82+
assertNotNull("Connection is null", connection);
83+
84+
log.info(connection.getResponseCode() + " " + connection.getResponseMessage());
85+
86+
try {
87+
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
88+
} catch (IOException ex) {
89+
in = null;
90+
ex.printStackTrace();
91+
}
92+
assertNotNull("Input stream failed", in);
93+
94+
String current;
95+
try {
96+
while ((current = in.readLine()) != null) {
97+
urlContent += current;
98+
}
99+
} catch (IOException ex) {
100+
urlContent = null;
101+
ex.printStackTrace();
102+
}
103+
assertNotNull("Content is null", urlContent);
104+
assertTrue("Content is empty", urlContent.length() > 0);
105+
}
106+
}

0 commit comments

Comments
 (0)