Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert URL toURI when there are unwise characters?

Tags:

java

http

url

uri

I've got URL object with path containing unwise characters (RFC 2396) in my case it is "|" (pipe) character. Now I need to safely convert that to URI, but URL.toURI() throws an exception.

I've read URL documentation but this part is for me confusing:

The URL class does not itself encode or decode any URL components according to the escaping mechanism defined in RFC2396. It is the responsibility of the caller to encode any fields, which need to be escaped prior to calling URL, and also to decode any escaped fields, that are returned from URL. Furthermore, because URL has no knowledge of URL escaping, it does not recognize equivalence between the encoded or decoded form of the same URL.

So how should I do it? What is the pattern here to encode this characters during conversion? Do I need create encoded copy of my URL object?

like image 532
Marek R Avatar asked Nov 23 '12 13:11

Marek R


People also ask

How to convert URL into URI?

The getURI() function of URL class converts the URL object to a URI object. Any URL which compiles with RFC 2396 can be converted to URI. URLs which are not in the specified format will generate an error if converted to URI format. Parameter: This method do not accept any parameter.

What is toURI in Java?

Java URL toURI() method The toURI() method returns URI equivalent to given URL. This method functions in the same way as new URI (this. toString()).


2 Answers

OK, I come up with something like this:

URI uri = new URI(url.getProtocol(), 
                  null /*userInfo*/,
                  url.getHost(), 
                  url.getPort(), 
                  (url.getPath()==null)?null:URLDecoder.decode(url.getPath(), "UTF-8"),
                  (url.getQuery()==null)?null:URLDecoder.decode(url.getQuery(), "UTF-8"),
                  null /*fragment*/);

Looks like it works, here is an example. Can some one confirm that this is proper solution?

Edit: initial solution had some problems when there was a query so I've fixed it.

like image 199
Marek R Avatar answered Oct 25 '22 07:10

Marek R


Use URL encoding?

From your example, you currently have:

URL url = new URL("http", "google.com", 8080, "/crapy|path with-unwise_characters.jpg");

Instead, I would use:

String path = "/crapy|path with-unwise_characters.jpg"
URL url = new URL("http", "google.com", 8080, URLEncoder.encode(path, "UTF-8"));

This should work and handle all unwise characters in the path as per the standard URL encoding.

like image 24
Anurag Kapur Avatar answered Oct 25 '22 09:10

Anurag Kapur