UriComponents returns IP instead of domain - java

I'm weak in network technologies and maybe you can help me. I have a simple code
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString()).build();
UriComponents newUriComponents = UriComponentsBuilder.newInstance().scheme(uriComponents.getScheme())
.host(uriComponents.getHost()).port(uriComponents.getPort()).build();
return newUriComponents.toUriString() + request.getContextPath();
This code should return link to my server with specific path. The problem is - on product server uriComponents.getHost() returns IP instead of domain name. Domain works when I go via browser to server. I can go to
http://exmaple.com/some/one/path and want to get in answer (in JSON, there are no redirections. just get request and json answer) - http://exmaple.com/some/another/path but code which I have showed returns - http://78.54.128.98.com/some/another/path (IP address just example). So I don't know why my code returns IP but not domain name. Only what I can to say more - in my local machine I don't have any problems with it. Code returns localhost, or if i add 127.0.0.1 exmaple.com to hosts file, my code will return correct exmaple.com, no any ip

This is not a problem of the URIComponents, it parses what it gets in input. More specifically looking at the source of UriComponentsBuilder.fromHttpUrl you see:
public static UriComponentsBuilder fromHttpUrl(String httpUrl) {
Assert.notNull(httpUrl, "HTTP URL must not be null");
Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
if (matcher.matches()) {
UriComponentsBuilder builder = new UriComponentsBuilder();
String scheme = matcher.group(1);
builder.scheme(scheme != null ? scheme.toLowerCase() : null);
builder.userInfo(matcher.group(4));
String host = matcher.group(5);
if (StringUtils.hasLength(scheme) && !StringUtils.hasLength(host)) {
throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
}
builder.host(host);
String port = matcher.group(7);
if (StringUtils.hasLength(port)) {
builder.port(port);
}
builder.path(matcher.group(8));
builder.query(matcher.group(10));
return builder;
}
else {
throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL");
}
}
where you can notice that a pattern matcher is defined on an expected structure of the url and parts are parsed according to the matcher. If you see IP it means that the url specified in input (request.getRequestURL().toString()) contained the IP address as a host.
This means that you should be looking for the guilty one above in the chain, starting by whoever calls this piece of code and following the links until you find the cause.

Related

Remove Everything Befor Third Forward Slash

I have the following strings:
http://somedomain.com/dir/sub/folder/file.txt
OR
https://10.0.0.1/dir/sub/folder/another_folder/file.txt
I want to remove everything before the third forward slash (remove the domain) and still keep the third forward slash.
Expected results:
/dir/sub/folder/file.txt
OR
/dir/sub/folder/another_folder/file.txt
Uri uri = Uri.parse("https://graph.facebook.com/me/home?limit=25&since=1374196005");
String protocol = uri.getScheme();
String server = uri.getAuthority();
String path = uri.getPath();
Set<String> args = uri.getQueryParameterNames();
String limit = uri.getQueryParameter("limit");
I think you need a path value
You can use the URL class that is what you are looking for.
The URL class provides several methods that let you query URL objects. You can get the protocol, authority, host name, port number, path, query, filename, and reference from a URL using these accessor methods
Use this :
URL aURL = new URL("https://10.0.0.1/dir/sub/folder/another_folder/file.txt");
aUrl.getPath();
Output result
path = /dir/sub/folder/another_folder/file.txt

get only the website name from domain name of url

I need to convert a list of URLS to their host name. SO tried the below mentioned code:
URL netUrl = new URL(url);
String host = netUrl.getHost();
The above mentioned code is producing output as shown below:
a95-101-128-242.deploy.akamaitechnologies.com
a23-1-242-192.deploy.static.akamaitechnologies.com
edge-video-shv-01-lht6.fbcdn.net
I want only the website name from the above output like as shown below:
akamaitechnologies
akamaitechnologies
fbcdn
Please someone help.
Thanks
If you want to parse a URL, use java.net.URI. java.net.URL has a bunch of problems -- its equals method does a DNS lookup which means code using it can be vulnerable to denial of service attacks when used with untrusted inputs.
public static String getDomainName(String url) throws URISyntaxException {
URI uri = new URI(url);
String domain = uri.getHost();
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
This should work.

Why does the javax.ws.rs.core.UriBuilder, fromUri() methods add an extra "/" at the end of hostname URI?

I am trying to generate a redirect URI from the request for an OAuth2 authorization code flow. The request contains a hostname along with the scope and an authorization code. example:
redirect_uri=myapp2://oauth2redirect&scope=email+profile
So, the hostname here is : myapp2://oauth2redirect
Now, when I execute the below code to generate a redirect uri for the application it adds an extra "/" (slash) in the end instead of continuing with the query parameters i.e.:
myapp2://oauth2redirect/?code=abcdefghijkl
The extra/unwanted "/" in myapp2://oauth2redirect/? is making the redirection fail.
Ideally it should be : myapp2://oauth2redirect?code=abcdefghijkl&scope=
public Response getResponse() {
String uri = oAuthrequest.getRedirectURI();
UriBuilder uriBuilder = UriBuilder.fromUri(uri)
.queryParam("code", code);
if (oAuthrequest.getState() != null) {
uriBuilder.queryParam("state", oAuthrequest.getState());
}
if(scopeNames != null && scopeNames.size() > 0) {
uriBuilder.queryParam("scope", StringUtil.toSingleString(scopeNames, " "));
}
logger.info("OAuth2 Authorization response success");
return Response.status(302).cookie(cookies).location(uriBuilder.build()).build();
}
I think that the UriBuilder.fromUri(uri) method adds the extra "/" in the uri as I have debugged and checked the value of the String field "uri" is correct. But, once this line is executed it adds up the extra "/" after the uri and then proceeds by appending the query parameters.
Well, I came up with a hacky solution: given that getRedirectURI() will return something like "myapp2://oauth2redirect"
// builds an URI object from the URI string
java.net.URI uri = java.net.URI.create(getRedirectURI());
// uses the authory(oauth2redirect) as the path to build the uri
UriBuilder uriBuilder = UriBuilder.fromPath(
// forcefully adds the double slashes (//), without this,
// at this point, the uri would be: myapp2:oauth2redirect
"//" + uri.getAuthority())
.scheme(uri.getScheme());
uriBuilder.queryParam("code", "myCode");
uriBuilder.build(); // produces the uri: myapp2://oauth2redirect?code=myCode

java.net.MalformedURLException: no protocol on URL based on a string modified with URLEncoder

So I was attempting to use this String in a URL :-
http://site-test.com/Meetings/IC/DownloadDocument?meetingId=c21c905c-8359-4bd6-b864-844709e05754&itemId=a4b724d1-282e-4b36-9d16-d619a807ba67&file=\\s604132shvw140\Test-Documents\c21c905c-8359-4bd6-b864-844709e05754_attachments\7e89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf
In this code: -
String fileToDownloadLocation = //The above string
URL fileToDownload = new URL(fileToDownloadLocation);
HttpGet httpget = new HttpGet(fileToDownload.toURI());
But at this point I get the error: -
java.net.URISyntaxException: Illegal character in query at index 169:Blahblahblah
I realised with a bit of googling this was due to the characters in the URL (guessing the &), so I then added in some code so it now looks like so: -
String fileToDownloadLocation = //The above string
fileToDownloadLocation = URLEncoder.encode(fileToDownloadLocation, "UTF-8");
URL fileToDownload = new URL(fileToDownloadLocation);
HttpGet httpget = new HttpGet(fileToDownload.toURI());
However, when I try and run this I get an error when I try and create the URL, the error then reads: -
java.net.MalformedURLException: no protocol: http%3A%2F%2Fsite-test.testsite.com%2FMeetings%2FIC%2FDownloadDocument%3FmeetingId%3Dc21c905c-8359-4bd6-b864-844709e05754%26itemId%3Da4b724d1-282e-4b36-9d16-d619a807ba67%26file%3D%5C%5Cs604132shvw140%5CTest-Documents%5Cc21c905c-8359-4bd6-b864-844709e05754_attachments%5C7e89c3cb-ce53-4a04-a9ee-1a584e157987%myDoc.pdf
It looks like I can't do the encoding until after I've created the URL else it replaces slashes and things which it shouldn't, but I can't see how I can create the URL with the string and then format it so its suitable for use. I'm not particularly familiar with all this and was hoping someone might be able to point out to me what I'm missing to get string A into a suitably formatted URL to then use with the correct characters replaced?
Any suggestions greatly appreciated!
You need to encode your parameter's values before concatenating them to URL.
Backslash \ is special character which have to be escaped as %5C
Escaping example:
String paramValue = "param\\with\\backslash";
String yourURLStr = "http://host.com?param=" + java.net.URLEncoder.encode(paramValue, "UTF-8");
java.net.URL url = new java.net.URL(yourURLStr);
The result is http://host.com?param=param%5Cwith%5Cbackslash which is properly formatted url string.
I have the same problem, i read the url with an properties file:
String configFile = System.getenv("system.Environment");
if (configFile == null || "".equalsIgnoreCase(configFile.trim())) {
configFile = "dev.properties";
}
// Load properties
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("/" + configFile));
//read url from file
apiUrl = properties.getProperty("url").trim();
URL url = new URL(apiUrl);
//throw exception here
URLConnection conn = url.openConnection();
dev.properties
url = "https://myDevServer.com/dev/api/gate"
it should be
dev.properties
url = https://myDevServer.com/dev/api/gate
without "" and my problem is solved.
According to oracle documentation
Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string
could not be parsed.
So it means it is not parsed inside the string.
You want to use URI templates. Look carefully at the README of this project: URLEncoder.encode() does NOT work for URIs.
Let us take your original URL:
http://site-test.test.com/Meetings/IC/DownloadDocument?meetingId=c21c905c-8359-4bd6-b864-844709e05754&itemId=a4b724d1-282e-4b36-9d16-d619a807ba67&file=\s604132shvw140\Test-Documents\c21c905c-8359-4bd6-b864-844709e05754_attachments\7e89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf
and convert it to a URI template with two variables (on multiple lines for clarity):
http://site-test.test.com/Meetings/IC/DownloadDocument
?meetingId={meetingID}&itemId={itemID}&file={file}
Now let us build a variable map with these three variables using the library mentioned in the link:
final VariableMap = VariableMap.newBuilder()
.addScalarValue("meetingID", "c21c905c-8359-4bd6-b864-844709e05754")
.addScalarValue("itemID", "a4b724d1-282e-4b36-9d16-d619a807ba67e")
.addScalarValue("file", "\\\\s604132shvw140\\Test-Documents"
+ "\\c21c905c-8359-4bd6-b864-844709e05754_attachments"
+ "\\7e89c3cb-ce53-4a04-a9ee-1a584e157987\\myDoc.pdf")
.build();
final URITemplate template
= new URITemplate("http://site-test.test.com/Meetings/IC/DownloadDocument"
+ "meetingId={meetingID}&itemId={itemID}&file={file}");
// Generate URL as a String
final String theURL = template.expand(vars);
This is GUARANTEED to return a fully functional URL!
Thanks to Erhun's answer I finally realised that my JSON mapper was returning the quotation marks around my data too! I needed to use "asText()" instead of "toString()"
It's not an uncommon issue - one's brain doesn't see anything wrong with the correct data, surrounded by quotes!
discoveryJson.path("some_endpoint").toString();
"https://what.the.com/heck"
discoveryJson.path("some_endpoint").asText();
https://what.the.com/heck
This code worked for me
public static void main(String[] args) {
try {
java.net.URL url = new java.net.URL("http://path");
System.out.println("Instantiated new URL: " + url);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
Instantiated new URL: http://path
Very simple fix
String encodedURL = UriUtils.encodePath(request.getUrl(), "UTF-8");
Works no extra functionality needed.

Java: Common way to validate and convert "host:port" to InetSocketAddress?

What is the common way in Java to validate and convert a string of the form host:port into an instance of InetSocketAddress?
It would be nice if following criteria were met:
No address lookups;
Working for IPv4, IPv6, and "string" hostnames;
(For IPv4 it's ip:port, for IPv6 it's [ip]:port, right? Is there some RFC which defines all these schemes?)
Preferable without parsing the string by hand.
(I'm thinking about all those special cases, when someone think he knows all valid forms of socket addresses, but forgets about "that special case" which leads to unexpected results.)
I myself propose one possible workaround solution.
Convert a string into URI (this would validate it automatically) and then query the URI's host and port components.
Sadly, an URI with a host component MUST have a scheme. This is why this solution is "not perfect".
String string = ... // some string which has to be validated
try {
// WORKAROUND: add any scheme to make the resulting URI valid.
URI uri = new URI("my://" + string); // may throw URISyntaxException
String host = uri.getHost();
int port = uri.getPort();
if (uri.getHost() == null || uri.getPort() == -1) {
throw new URISyntaxException(uri.toString(),
"URI must have host and port parts");
}
// here, additional checks can be performed, such as
// presence of path, query, fragment, ...
// validation succeeded
return new InetSocketAddress (host, port);
} catch (URISyntaxException ex) {
// validation failed
}
This solution needs no custom string parsing, works with IPv4 (1.1.1.1:123), IPv6 ([::0]:123) and host names (my.host.com:123).
Accidentally, this solution is well suited for my scenario. I was going to use URI schemes anyway.
A regex will do this quite neatly:
Pattern p = Pattern.compile("^\\s*(.*?):(\\d+)\\s*$");
Matcher m = p.matcher("127.0.0.1:8080");
if (m.matches()) {
String host = m.group(1);
int port = Integer.parseInt(m.group(2));
}
You can this in many ways such as making the port optional or doing some validation on the host.
It doesn't answer the question exactly, but this answer could still be useful others like me who just want to parse a host and port, but not necessarily a full InetAddress. Guava has a HostAndPort class with a parseString method.
Another person has given a regex answer which is what I was doing to do when originally asking the question about hosts. I will still do because it's an example of a regex that is slightly more advanced and can help determine what kind of address you are dealing with.
String ipPattern = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d+)";
String ipV6Pattern = "\\[([a-zA-Z0-9:]+)\\]:(\\d+)";
String hostPattern = "([\\w\\.\\-]+):(\\d+)"; // note will allow _ in host name
Pattern p = Pattern.compile( ipPattern + "|" + ipV6Pattern + "|" + hostPattern );
Matcher m = p.matcher( someString );
if( m.matches() ) {
if( m.group(1) != null ) {
// group(1) IP address, group(2) is port
} else if( m.group(3) != null ) {
// group(3) is IPv6 address, group(4) is port
} else if( m.group(5) != null ) {
// group(5) is hostname, group(6) is port
} else {
// Not a valid address
}
}
Modifying so that port is optional is pretty straight forward. Wrap the ":(\d+)" as "(?::(\d+))?" and then check for null for group(2), etc.
Edit: I'll note that there's no "common way" way that I'm aware of but the above is how I'd do it if I had to.
Also note: the IPv4 case can be removed if the host and IPv4 cases will actually be handled the same. I split them out because sometimes you can avoid an ultimate host look-up if you know you have the IP address.
new InetSocketAddress(
addressString.substring(0, addressString.lastIndexOf(":")),
Integer.parseInt(addressString.substring(addressString.lastIndexOf(":")+1, addressString.length));
? I probably made some little silly mistake. and I'm assuming you just wanted a new InetSocketAddress object out of the String in only that format. host:port
All kind of peculiar hackery, and elegant but unsafe solutions provided elsewhere. Sometimes the inelegant brute-force solution is the way.
public static InetSocketAddress parseInetSocketAddress(String addressAndPort) throws IllegalArgumentException {
int portPosition = addressAndPort.length();
int portNumber = 0;
while (portPosition > 1 && Character.isDigit(addressAndPort.charAt(portPosition-1)))
{
--portPosition;
}
String address;
if (portPosition > 1 && addressAndPort.charAt(portPosition-1) == ':')
{
try {
portNumber = Integer.parseInt(addressAndPort.substring(portPosition));
} catch (NumberFormatException ignored)
{
throw new IllegalArgumentException("Invalid port number.");
}
address = addressAndPort.substring(0,portPosition-1);
} else {
portNumber = 0;
address = addressAndPort;
}
return new InetSocketAddress(address,portNumber);
}
The open-source IPAddress Java library has a HostName class which will do the required parsing. Disclaimer: I am the project manager of the IPAddress library.
It will parse IPv4, IPv6 and string host names with or without ports. It will handle all the various formats of hosts and addresses. BTW, there is no single RFC for this, there are a number of RFCs that apply in different ways.
String hostName = "[a:b:c:d:e:f:a:b]:8080";
String hostName2 = "1.2.3.4:8080";
String hostName3 = "a.com:8080";
try {
HostName host = new HostName(hostName);
host.validate();
InetSocketAddress address = host.asInetSocketAddress();
HostName host2 = new HostName(hostName2);
host2.validate();
InetSocketAddress address2 = host2.asInetSocketAddress();
HostName host3 = new HostName(hostName3);
host3.validate();
InetSocketAddress address3 = host3.asInetSocketAddress();
// use socket address
} catch (HostNameException e) {
String msg = e.getMessage();
// handle improperly formatted host name or address string
}
URI can accomplish this:
URI uri = new URI(null, "example.com:80", null, null, null);
Unfortunately, there's a bug in current OpenJDK (or in the documentation) where the authority isn't properly validated. The documentation states:
The resulting URI string is then parsed as if by invoking the URI(String) constructor and then invoking the parseServerAuthority() method upon the result
That call to parseServerAuthority just doesn't happen unfortunately so the real solution here that properly validates is:
URI uri = new URI(null, "example.com:80", null, null, null).parseServerAuthority();
then
InetSocketAddress address = new InetSocketAddress(uri.getHost(), uri.getPort());

Categories

Resources