Fetch host name from address bar using Java code - java

I need to retrieve the host name from the address bar using java code. For example, if "www.stackoverflow.com/questions", is there in the address bar, I have to get "www.stackoverflow.com". Please help me.

If you know a bit about format, it can be done very easily with this code :
String http = "www.stackoverflow.com/questions";
String url = http.substring(0, http.indexOf("/"));
System.out.println(url);
http = "http://www.stackoverflow.com/questions";
String nohttp = http.substring(7, http.length());
url = nohttp.substring(0, nohttp.indexOf("/"));
System.out.println(url);
Or you can use some "nicer" approach with methods :
public static void main(String[] args) {
System.out.println(getHostname("www.stackoverflow.com/questions"));
System.out.println(getHostname("http://www.stackoverflow.com/questions"));
}
public static boolean isHttp(String s){
if (s.indexOf("http://") == 0){
return true;
} else {
return false;
}
}
public static String getHostname(String url){
String nativeUrl = url;
if (isHttp(nativeUrl)){
url = url.substring(7);
}
url = url.substring(0, url.indexOf("/"));
if (isHttp(nativeUrl)){
url = "http://" + url;
}
return url;
}
Output
www.stackoverflow.com
http://www.stackoverflow.com

Related

How to count object values in Java [duplicate]

This question already has answers here:
Most efficient way to increment a Map value in Java
(28 answers)
Closed 6 months ago.
Sorry, I'm totally new to Java. I'm trying to count the same URLs present in objects and I'm not able to figure out how to count them.
I was reading this article https://www.geeksforgeeks.org/passing-and-returning-objects-in-java/
and wondered if this is a way to count URLs object or another way. I'm stuck here for two days.
Can anyone please guide me?
class Main() {
private HashMap<String, String> keyMap;
private static int count = 0;
public String newUrl(String url) {
//Completed: if the url is present in HashMap then return shortUrl else add it and return shortUrl
return shortUrl
}
public Integer countURL(String url){
//int count = Collections.frequency(keyMap.values(), url);
return count;
}
public static void main(String args[]) {
Main a = new Main();
String url = a.newUrl("http://example.com");
String url1 = a.newUrl("http://example2.com");
String url2 = a.newUrl("http://example2.com");
System.out.println(url);
System.out.println(url1);
System.out.println(url2);;
System.out.println(a.countURL("http://example2.com")); //2 if not then 0
}
}
You can add this to your newUrl method:
public String newUrl(String url){
if (!keyMap.containsKey(url)) {
keyMap.put(url, 1);
return url;
}else {
keyMap.replace(url, keyMap.get(url));
return url;
}
}
and in your countUrl method you can just return keyMap.get(url)
public Integer countURL(String url){
return keyMap.get(url);
}

Library for parsing URI from String with fragment before query

Is there a library that would parse fragment and query from String to URI even if the string has the fragment and query in a wrong order?
import java.net.URI;
public class UriTest {
public static void main(String[] args){
String validUriString = "http://localhost:4200/?name=value#/fragment";
String invalidUriString = "http://localhost:4200/#/fragment?name=value";
URI validUri = URI.create(validUriString);
URI invalidUri = URI.create(invalidUriString);
System.out.println("OK: "+validUri.getFragment());
System.out.println("OK: "+validUri.getQuery()+"\n");
System.out.println("NOT OK: "+invalidUri.getFragment());
System.out.println("NOT OK: "+invalidUri.getQuery());
}
}
Output:
OK: /fragment
OK: name=value
NOT OK: /fragment?name=value
NOT OK: null
Would be nice to get the same result in both cases.
No guarantees if this will work for all URLs, but you can try to check if the url is broken and fix it like this:
...
public static String fixUrl(String url) {
int fragmentIndex = url.indexOf("#");
int queryIndex = url.indexOf("?");
if(fragmentIndex < queryIndex && fragmentIndex > -1)
return url.substring(0,fragmentIndex) + url.substring(queryIndex) + url.substring(fragmentIndex, queryIndex);
else
return url;
}
public static void main (String[] args) {
String validUriString = "http://localhost:4200/?name=value#/fragment";
String invalidUriString = "http://localhost:4200/#/fragment?name=value";
URI validUri = URI.create(fixUrl(validUriString));
URI invalidUri = URI.create(fixUrl(invalidUriString));
System.out.println("OK: "+validUri.getFragment());
System.out.println("OK: "+validUri.getQuery()+"\n");
System.out.println("OK: "+invalidUri.getFragment());
System.out.println("OK: "+invalidUri.getQuery());
}

Get HTTP URL from href-attribute (All possible ways for links)

I am trying to do a simple script that will convert any possible HTML link into HTTP URL, such as
http://example.com //example.com /index.html ./index.html index.html
I already tried function that I found in another answer:
public static Integer isAbsoluteURL (String url) throws java.net.MalformedURLException {
final URL baseHTTP = new URL("http://example.com");
final URL baseFILE = new URL("file:///");
if (url.length() > 0) {
if (url.substring(0, 1) == "/") {
return 0;
}
}
URL frelative;
URL hrelative;
try {
frelative = new URL(baseFILE, url);
hrelative = new URL(baseHTTP, url);
} catch (MalformedURLException e) {
System.out.println("MalformedURLException found");
return 3;
}
if (frelative.equals(hrelative)) {
return 0;
} else {
return 1;
}
}
I want to get absolute links, but the code don't work for ./, //(without http[s]).
Thanks.
I'd prefer to use spring's UriComponentBuilder which will take care about everything:
https://www.baeldung.com/spring-uricomponentsbuilder

Java constructor chaining with method reference

I've encountered a situation that looked more or less like this while refactoring code; rewritten here for the sake of the example:
public class UrlProbe {
private final OkHttpClient http;
private final String url;
private final Function<Response, Object> transform;
private Object cachedValue;
public UrlProbe(OkHttpClient http, String url) {
this(http, url, this::debuggingStringTransform);
}
public UrlProbe(OkHttpClient http, String url, Function<Response, Object> transform) {
this.http = http;
this.url = url;
this.transform = transform;
}
private Object debuggingStringTransform(Response response) {
String newValue = response.body().toString();
System.out.println("Debugging new value from url " + url + ": " + newValue);
return newValue;
}
public synchronized Object probe() {
if (cachedValue != null) {
return cachedValue;
}
try (Response response = http.newCall(new Request.Builder().url(url).get().build()).execute()) {
cachedValue = transform.apply(response);
return cachedValue;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
This code will not compile because we cannot reference this before supertype constructor has been called:
public UrlProbe(OkHttpClient http, String url) {
this(http, url, this::debuggingStringTransform);
}
The following will not compile either:
public UrlProbe(OkHttpClient http, String url) {
this(http, url, response -> debuggingStringTransform(response));
}
The only way I've found around this is to explicitly use a non-chaining constructor:
public UrlProbe(OkHttpClient http, String url) {
this.http = http;
this.url = url;
this.transform = this::debuggingStringTransform;
}
While it makes sense to restrict the use of this in constructors chaining arguments, I find it surprising in this particular context as there doesn't appear to be any kind of evaluation of the object being constructed caused by the use of this when it comes to method references and the contents of a lambda expression.
Is there a rationale behind this restriction other than it is because JLS §8.8.7.1 says so?
Allowing referencing this scope that early would break code that looks like this
public class UrlProbe {
final String url;
final String param2;
public UrlProbe(String url) {
this(url, this::debuggingStringTransform);
}
public UrlProbe(String url, Function<String, String> transform) {
this(url, transform.apply("")); // <-- What should happen when url is referenced here?
}
public UrlProbe(String url, String param2) {
this.url = url;
this.param2 = param2;
}
private String debuggingStringTransform(String response) {
System.out.println("Debugging new value from url " + url + ": " + response);
return response;
}
}
That's at least one way of violating the rules.
IntelliJ shows this on the tooltip for why this code is "bad":
Cannot reference 'this' before supertype constructor has been called
This makes sense. You're in the middle of constructing your object and the method reference as defined only exists after the class is instantiated. It can't realistically exist before it's fully instantiated, since from a semantic standpoint, you can't actually do anything with it until it's "ready".
If you wanted to get around this, you could change that function to a static one, since there's no state required for it:
public UrlProbe(OkHttpClient http, String url) {
this(http, url, UrlProbe::debuggingStringTransform);
}
private static Object debuggingStringTransform(Response response) {
String newValue = response.body().toString();
System.out.println("Debugging new value from url " + url + ": " + newValue);
return newValue;
}
...although admittedly it looks weird to see a private static method.
Alternatively, have this function exist elsewhere in the same package, like in a static class at the bottom of this one:
public UrlProbe(OkHttpClient http, String url) {
this(http, url, Functions::debuggingStringTransform);
}
static class Functions {
static Object debuggingStringTransform(Response response) {
String newValue = response.body().toString();
System.out.println("Debugging new value from url " + url + ": " + newValue);
return newValue;
}
}

Google Search with JSoup

i trie to search in google with JSoup. The problem that i have is, that the variable query shows not the URL i want when i start searching.
Also, how does Jsoup search ? Looking for Title or URL or what ?
public class Start {
public static void main(String[] args) {
try {
new Google().Searching("Möbel Beck GmbH & Co.KG");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public class Google implements Serializable {
private static final long serialVersionUID = 1L;
private static Pattern patternDomainName;
private Matcher matcher;
private static final String DOMAIN_NAME_PATTERN = "([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}";
static {
patternDomainName = Pattern.compile(DOMAIN_NAME_PATTERN);
}
public void Searching(String searchstring) throws IOException {
Google obj = new Google();
Set<String> result = obj.getDataFromGoogle(searchstring);
for (String temp : result) {
if (temp.contains(searchstring)) {
System.out.println(temp + " ----> CONTAINS");
} else {
System.out.println(temp);
}
}
System.out.println(result.size());
}
public String getDomainName(String url) {
String domainName = "";
matcher = patternDomainName.matcher(url);
if (matcher.find()) {
domainName = matcher.group(0).toLowerCase().trim();
}
return domainName;
}
private Set<String> getDataFromGoogle(String query) {
Set<String> result = new HashSet<String>();
String request = "https://www.google.com/search?q=" + query;
System.out.println("Sending request..." + request);
try {
// need http protocol, set this as a Google bot agent :)
Document doc = Jsoup.connect(request)
.userAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)").timeout(6000)
.get();
// get all links
Elements links = doc.select("a[href]");
for (Element link : links) {
String temp = link.attr("href");
if (temp.startsWith("/url?q=")) {
// use regex to get domain name
result.add(getDomainName(temp));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
Parsing google sites directly is not a good idea. You can try Google API
https://developers.google.com/web-search/docs/#java-access

Categories