URI, URL BAEL-864. Article renamed and extended URI part. (#2217)
This commit is contained in:
parent
2863d0c3e0
commit
7bfceb34c7
|
@ -0,0 +1,91 @@
|
||||||
|
package com.baeldung.javanetworking.uriurl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class URIDemo {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(URIDemo.class);
|
||||||
|
|
||||||
|
String URISTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
|
||||||
|
// parsed locator
|
||||||
|
String URISCHEME = "https";
|
||||||
|
String URISCHEMESPECIFIC;
|
||||||
|
String URIHOST = "wordpress.org";
|
||||||
|
String URIAUTHORITY = "wordpress.org:443";
|
||||||
|
|
||||||
|
String URIPATH = "/support/topic/page-jumps-within-wordpress/";
|
||||||
|
int URIPORT = 443;
|
||||||
|
String URIQUERY = "replies=3";
|
||||||
|
String URIFRAGMENT = "post-2278484";
|
||||||
|
String URIUSERINFO;
|
||||||
|
String URICOMPOUND = URISCHEME + "://" + URIHOST + ":" + URIPORT + URIPATH + "?" + URIQUERY + "#" + URIFRAGMENT;
|
||||||
|
|
||||||
|
URI uri;
|
||||||
|
URL url;
|
||||||
|
BufferedReader in = null;
|
||||||
|
String URIContent = "";
|
||||||
|
|
||||||
|
private String getParsedPieces(URI uri) {
|
||||||
|
logger.info("*** List of parsed pieces ***");
|
||||||
|
URISCHEME = uri.getScheme();
|
||||||
|
logger.info("URISCHEME: " + URISCHEME);
|
||||||
|
URISCHEMESPECIFIC = uri.getSchemeSpecificPart();
|
||||||
|
logger.info("URISCHEMESPECIFIC: " + URISCHEMESPECIFIC);
|
||||||
|
URIHOST = uri.getHost();
|
||||||
|
URIAUTHORITY = uri.getAuthority();
|
||||||
|
logger.info("URIAUTHORITY: " + URIAUTHORITY);
|
||||||
|
logger.info("URIHOST: " + URIHOST);
|
||||||
|
URIPATH = uri.getPath();
|
||||||
|
logger.info("URIPATH: " + URIPATH);
|
||||||
|
URIPORT = uri.getPort();
|
||||||
|
logger.info("URIPORT: " + URIPORT);
|
||||||
|
URIQUERY = uri.getQuery();
|
||||||
|
logger.info("URIQUERY: " + URIQUERY);
|
||||||
|
URIFRAGMENT = uri.getFragment();
|
||||||
|
logger.info("URIFRAGMENT: " + URIFRAGMENT);
|
||||||
|
|
||||||
|
try {
|
||||||
|
url = uri.toURL();
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
logger.info("MalformedURLException thrown: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
logger.info("IllegalArgumentException thrown: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return url.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String testURIAsNew(String URIString) {
|
||||||
|
// creating URI object
|
||||||
|
try {
|
||||||
|
uri = new URI(URIString);
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
logger.info("URISyntaxException thrown: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
return getParsedPieces(uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String testURIAsCreate(String URIString) {
|
||||||
|
// creating URI object
|
||||||
|
uri = URI.create(URIString);
|
||||||
|
return getParsedPieces(uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
URIDemo demo = new URIDemo();
|
||||||
|
String contentCreate = demo.testURIAsCreate(demo.URICOMPOUND);
|
||||||
|
demo.logger.info(contentCreate);
|
||||||
|
String contentNew = demo.testURIAsNew(demo.URICOMPOUND);
|
||||||
|
demo.logger.info(contentNew);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,10 @@
|
||||||
package com.baeldung.javanetworking.url;
|
package com.baeldung.javanetworking.uriurl;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
package com.baeldung.javanetworking.uriurl.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.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
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.uriurl.URLDemo;
|
||||||
|
|
||||||
|
@FixMethodOrder
|
||||||
|
public class URIDemoTest {
|
||||||
|
private final Logger log = LoggerFactory.getLogger(URIDemoTest.class);
|
||||||
|
String URISTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
|
||||||
|
// parsed locator
|
||||||
|
static String URISCHEME = "https";
|
||||||
|
String URISCHEMESPECIFIC;
|
||||||
|
static String URIHOST = "wordpress.org";
|
||||||
|
static String URIAUTHORITY = "wordpress.org:443";
|
||||||
|
|
||||||
|
static String URIPATH = "/support/topic/page-jumps-within-wordpress/";
|
||||||
|
int URIPORT = 443;
|
||||||
|
static int URIDEFAULTPORT = 443;
|
||||||
|
static String URIQUERY = "replies=3";
|
||||||
|
static String URIFRAGMENT = "post-2278484";
|
||||||
|
static String URICOMPOUND = URISCHEME + "://" + URIHOST + ":" + URIDEFAULTPORT + URIPATH + "?" + URIQUERY + "#" + URIFRAGMENT;
|
||||||
|
|
||||||
|
static URI uri;
|
||||||
|
URL url;
|
||||||
|
BufferedReader in = null;
|
||||||
|
String URIContent = "";
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void givenEmplyURL_whenInitializeURL_thenSuccess() throws URISyntaxException {
|
||||||
|
uri = new URI(URICOMPOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
// check parsed URL
|
||||||
|
@Test
|
||||||
|
public void givenURI_whenURIIsParsed_thenSuccess() {
|
||||||
|
assertNotNull("URI is null", uri);
|
||||||
|
assertEquals("URI string is not equal", uri.toString(), URISTRING);
|
||||||
|
assertEquals("Scheme is not equal", uri.getScheme(), URISCHEME);
|
||||||
|
assertEquals("Authority is not equal", uri.getAuthority(), URIAUTHORITY);
|
||||||
|
assertEquals("Host string is not equal", uri.getHost(), URIHOST);
|
||||||
|
assertEquals("Path string is not equal", uri.getPath(), URIPATH);
|
||||||
|
assertEquals("Port number is not equal", uri.getPort(), URIPORT);
|
||||||
|
assertEquals("Query string is not equal", uri.getQuery(), URIQUERY);
|
||||||
|
assertEquals("Fragment string is not equal", uri.getFragment(), URIFRAGMENT);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.javanetworking.url.test;
|
package com.baeldung.javanetworking.uriurl.test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
@ -18,11 +18,11 @@ import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.baeldung.javanetworking.url.URLDemo;
|
import com.baeldung.javanetworking.uriurl.URLDemo;
|
||||||
|
|
||||||
@FixMethodOrder
|
@FixMethodOrder
|
||||||
public class URLDemoTest {
|
public class URLDemoTest {
|
||||||
private final Logger log = LoggerFactory.getLogger(URLDemo.class);
|
private final Logger log = LoggerFactory.getLogger(URLDemoTest.class);
|
||||||
static String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
|
static String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
|
||||||
// parsed locator
|
// parsed locator
|
||||||
static String URLPROTOCOL = "https";
|
static String URLPROTOCOL = "https";
|
Loading…
Reference in New Issue