Skip to content

Commit 7bfceb3

Browse files
hariprasad108pivovarit
authored andcommitted
URI, URL BAEL-864. Article renamed and extended URI part. (eugenp#2217)
1 parent 2863d0c commit 7bfceb3

4 files changed

Lines changed: 161 additions & 4 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.javanetworking.uriurl;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.net.MalformedURLException;
6+
import java.net.URI;
7+
import java.net.URISyntaxException;
8+
import java.net.URL;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public class URIDemo {
14+
private final Logger logger = LoggerFactory.getLogger(URIDemo.class);
15+
16+
String URISTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
17+
// parsed locator
18+
String URISCHEME = "https";
19+
String URISCHEMESPECIFIC;
20+
String URIHOST = "wordpress.org";
21+
String URIAUTHORITY = "wordpress.org:443";
22+
23+
String URIPATH = "/support/topic/page-jumps-within-wordpress/";
24+
int URIPORT = 443;
25+
String URIQUERY = "replies=3";
26+
String URIFRAGMENT = "post-2278484";
27+
String URIUSERINFO;
28+
String URICOMPOUND = URISCHEME + "://" + URIHOST + ":" + URIPORT + URIPATH + "?" + URIQUERY + "#" + URIFRAGMENT;
29+
30+
URI uri;
31+
URL url;
32+
BufferedReader in = null;
33+
String URIContent = "";
34+
35+
private String getParsedPieces(URI uri) {
36+
logger.info("*** List of parsed pieces ***");
37+
URISCHEME = uri.getScheme();
38+
logger.info("URISCHEME: " + URISCHEME);
39+
URISCHEMESPECIFIC = uri.getSchemeSpecificPart();
40+
logger.info("URISCHEMESPECIFIC: " + URISCHEMESPECIFIC);
41+
URIHOST = uri.getHost();
42+
URIAUTHORITY = uri.getAuthority();
43+
logger.info("URIAUTHORITY: " + URIAUTHORITY);
44+
logger.info("URIHOST: " + URIHOST);
45+
URIPATH = uri.getPath();
46+
logger.info("URIPATH: " + URIPATH);
47+
URIPORT = uri.getPort();
48+
logger.info("URIPORT: " + URIPORT);
49+
URIQUERY = uri.getQuery();
50+
logger.info("URIQUERY: " + URIQUERY);
51+
URIFRAGMENT = uri.getFragment();
52+
logger.info("URIFRAGMENT: " + URIFRAGMENT);
53+
54+
try {
55+
url = uri.toURL();
56+
} catch (MalformedURLException e) {
57+
logger.info("MalformedURLException thrown: " + e.getMessage());
58+
e.printStackTrace();
59+
} catch (IllegalArgumentException e) {
60+
logger.info("IllegalArgumentException thrown: " + e.getMessage());
61+
e.printStackTrace();
62+
}
63+
return url.toString();
64+
}
65+
66+
public String testURIAsNew(String URIString) {
67+
// creating URI object
68+
try {
69+
uri = new URI(URIString);
70+
} catch (URISyntaxException e) {
71+
logger.info("URISyntaxException thrown: " + e.getMessage());
72+
e.printStackTrace();
73+
throw new IllegalArgumentException();
74+
}
75+
return getParsedPieces(uri);
76+
}
77+
78+
public String testURIAsCreate(String URIString) {
79+
// creating URI object
80+
uri = URI.create(URIString);
81+
return getParsedPieces(uri);
82+
}
83+
84+
public static void main(String[] args) throws Exception {
85+
URIDemo demo = new URIDemo();
86+
String contentCreate = demo.testURIAsCreate(demo.URICOMPOUND);
87+
demo.logger.info(contentCreate);
88+
String contentNew = demo.testURIAsNew(demo.URICOMPOUND);
89+
demo.logger.info(contentNew);
90+
}
91+
}

core-java/src/main/java/com/baeldung/javanetworking/url/URLDemo.java renamed to core-java/src/main/java/com/baeldung/javanetworking/uriurl/URLDemo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package com.baeldung.javanetworking.url;
1+
package com.baeldung.javanetworking.uriurl;
22

33
import java.io.BufferedReader;
44
import java.io.IOException;
55
import java.io.InputStreamReader;
66
import java.net.HttpURLConnection;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.net.URLConnection;
910

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.javanetworking.uriurl.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.URI;
13+
import java.net.URISyntaxException;
14+
import java.net.URL;
15+
import java.net.URLConnection;
16+
17+
import org.junit.BeforeClass;
18+
import org.junit.FixMethodOrder;
19+
import org.junit.Test;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import com.baeldung.javanetworking.uriurl.URLDemo;
24+
25+
@FixMethodOrder
26+
public class URIDemoTest {
27+
private final Logger log = LoggerFactory.getLogger(URIDemoTest.class);
28+
String URISTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
29+
// parsed locator
30+
static String URISCHEME = "https";
31+
String URISCHEMESPECIFIC;
32+
static String URIHOST = "wordpress.org";
33+
static String URIAUTHORITY = "wordpress.org:443";
34+
35+
static String URIPATH = "/support/topic/page-jumps-within-wordpress/";
36+
int URIPORT = 443;
37+
static int URIDEFAULTPORT = 443;
38+
static String URIQUERY = "replies=3";
39+
static String URIFRAGMENT = "post-2278484";
40+
static String URICOMPOUND = URISCHEME + "://" + URIHOST + ":" + URIDEFAULTPORT + URIPATH + "?" + URIQUERY + "#" + URIFRAGMENT;
41+
42+
static URI uri;
43+
URL url;
44+
BufferedReader in = null;
45+
String URIContent = "";
46+
47+
@BeforeClass
48+
public static void givenEmplyURL_whenInitializeURL_thenSuccess() throws URISyntaxException {
49+
uri = new URI(URICOMPOUND);
50+
}
51+
52+
// check parsed URL
53+
@Test
54+
public void givenURI_whenURIIsParsed_thenSuccess() {
55+
assertNotNull("URI is null", uri);
56+
assertEquals("URI string is not equal", uri.toString(), URISTRING);
57+
assertEquals("Scheme is not equal", uri.getScheme(), URISCHEME);
58+
assertEquals("Authority is not equal", uri.getAuthority(), URIAUTHORITY);
59+
assertEquals("Host string is not equal", uri.getHost(), URIHOST);
60+
assertEquals("Path string is not equal", uri.getPath(), URIPATH);
61+
assertEquals("Port number is not equal", uri.getPort(), URIPORT);
62+
assertEquals("Query string is not equal", uri.getQuery(), URIQUERY);
63+
assertEquals("Fragment string is not equal", uri.getFragment(), URIFRAGMENT);
64+
}
65+
}

core-java/src/test/java/com/baeldung/javanetworking/url/test/URLDemoTest.java renamed to core-java/src/test/java/com/baeldung/javanetworking/uriurl/test/URLDemoTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.javanetworking.url.test;
1+
package com.baeldung.javanetworking.uriurl.test;
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
@@ -18,11 +18,11 @@
1818
import org.slf4j.Logger;
1919
import org.slf4j.LoggerFactory;
2020

21-
import com.baeldung.javanetworking.url.URLDemo;
21+
import com.baeldung.javanetworking.uriurl.URLDemo;
2222

2323
@FixMethodOrder
2424
public class URLDemoTest {
25-
private final Logger log = LoggerFactory.getLogger(URLDemo.class);
25+
private final Logger log = LoggerFactory.getLogger(URLDemoTest.class);
2626
static String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
2727
// parsed locator
2828
static String URLPROTOCOL = "https";

0 commit comments

Comments
 (0)