move networking articles, update readme

This commit is contained in:
Loredana
2018-12-25 14:05:10 +02:00
parent 68f1cb23dd
commit fb66f1bc78
13 changed files with 19 additions and 15 deletions
@@ -0,0 +1,26 @@
package com.baeldung.console;
import java.io.Console;
public class ConsoleConsoleClass {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.print("No console available");
return;
}
String progLanguauge = console.readLine("Enter your favourite programming language: ");
console.printf(progLanguauge + " is very interesting!");
char[] pass = console.readPassword("To finish, enter password: ");
if ("BAELDUNG".equals(pass.toString().toUpperCase()))
console.printf("Good! Regards!");
else
console.printf("Nice try. Regards.");
}
}
@@ -0,0 +1,76 @@
package com.baeldung.console;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.regex.Pattern;
public class ConsoleScannerClass {
public static void main(String[] args) {
System.out.println("Please enter your name and surname: ");
Scanner scanner = new Scanner(System.in);
String nameSurname = scanner.nextLine();
System.out.println("Please enter your gender: ");
char gender = scanner.next().charAt(0);
System.out.println("Please enter your age: ");
int age = scanner.nextInt();
System.out.println("Please enter your height in meters: ");
double height = scanner.nextDouble();
System.out.println(nameSurname + ", " + age + ", is a great " + (gender == 'm' ? "guy" : "girl") + " with " + height + " meters height" + " and " + (gender == 'm' ? "he" : "she") + " reads Baeldung.");
System.out.print("Have a good");
System.out.print(" one!");
System.out.println("\nPlease enter number of years of experience as a developer: ");
BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in));
int i = 0;
try {
i = Integer.parseInt(buffReader.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("You are a " + (i > 5 ? "great" : "good") + " developer!");
int sum = 0, count = 0;
System.out.println("Please enter your college degrees. To finish, enter baeldung website url");
while (scanner.hasNextInt()) {
int nmbr = scanner.nextInt();
sum += nmbr;
count++;
}
int mean = sum / count;
System.out.println("Your average degree is " + mean);
if (scanner.hasNext(Pattern.compile("www.baeldung.com")))
System.out.println("Correct!");
else
System.out.println("Baeldung website url is www.baeldung.com");
if (scanner != null)
scanner.close();
}
}
@@ -1,91 +0,0 @@
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,70 +0,0 @@
package com.baeldung.javanetworking.uriurl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class URLDemo {
private final 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);
demo.log.info(content);
}
}