What's the difference between url.getFile() and getpath()? - java

In java.net.url there is a getFile() method and a getPath() method.
In my testing, they both return the same result: the full path and file after the domain name trailing slash.
For instance, http://www.google.com/x/y/z.html returns x/y/z.html for both methods.
Could someone elaborate on the Javadocs?

The URL.getFile() javadocs say this:
Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the concatenation of the value of getQuery(), if any. If there is no query portion, this method and getPath() will return identical results.
They will be the same unless there is a query string, e.g. a ?somename=value&somethingelse=value2 in the URL.

URL.getFile():
Gets the file name of this URL. The returned file portion will be the same as getPath(), plus the concatenation of the value of getQuery(), if any. If there is no query portion, this method and getPath() will return identical results.

File, in the general case, is longer
new URL("http://www.google.com/x/y/z.html?v=1#chapter1").getFile();
// returns: "/x/y/z.html?v=1"
, than Path:
new URL("http://www.google.com/x/y/z.html?v=1#chapter1").getPath();
// returns: "/x/y/z.html"

Related

Java-Jersey based RESTful webservice : What is the best way to handle path param for a file entity

I am writing a Java based REST web service using jersey. The entity for which I am writing the web service is a media file. A client requesting for a media file need to send the path and filename as the path param. The media path allowed can be up to a depth of five directories. Now the challenge is to write a single method to handle all the path depth possibilities. Using the path param is the only allowed choice in terms of the business scenario. Here is the method contract, which handles a media file request:
public Response getMediaFile(#PathParam("path") String path,
#PathParam("filename") String filename);
Problem with this method is that, if the request is like /media/filedir1/filedir2/filename then filename will not be fetch properly.
The solution I have implemented is that, I have overloaded this method to handle all the directories depth but I am not really convinced that this is the best solution:
public Response getMediaFile(#PathParam("path1") String path1,
#PathParam("path2") String path2,
#PathParam("filename") String filename);
public Response getMediaFile(#PathParam("path1") String path1,
#PathParam("path2") String path2,
#PathParam("path3") String path3,
#PathParam("filename") String filename);
And so on.
You should be able to use a regular expression in your #PathParam annotation to handle all the path filtering logic. For example, this will give you a filepath that is at most 5 directories down:
#Path("{path:(?:[^/]+/){0,4}[^/]+}")
Then you'd inject that value into a method as expected:
#Path("{path:(?:[^/]+/){0,4}[^/]+}")
/* Other attributes too... */
public Response getMediaFile(#PathParam("path") String path) {
File file=new File(MEDIA_HOME_DIR, path);
if(file.exists()) {
// Process file
}
else {
// No such file
}
}
The regular expression will handle the "five directory" limit, and if the number changes from five in the future it'll be easy to fix. You could easily filter the filenames more carefully too, if you needed to match only .jpg files (for example).
With that solved, you'll just have to serve the media. :)
The JAX-RS Specification tells us about URI Templates:
Template parameters can optionally specify the regular expression used to match their values. The default value matches any text and terminates at the end of a path segment
If you want to match across "the end of a path segment", use a proper regular expression. This one works for you:
#Path("{path:.*}/{filename}")

How to extract query string from a URL of a web-page using java

From the following URL in OathCallBack page I want extract access_token and token_type using Java. Any idea how to do it?
http://myserver.com/OathCallBack#state=/profile&access_token=ya29.AHES6ZQLqtYrPKuw2pMzURJtWuvINspm8-Vf5x-MZ5YzqVy5&token_type=Bearer&expires_in=3600
I tried the following, but unable to extract required information.
{
String scheme = req.getScheme(); // http
String serverName = req.getServerName(); // myserver.com
int serverPort = req.getServerPort(); // 80
String contextPath = req.getContextPath();
String servletPath = req.getServletPath();
String pathInfo = req.getPathInfo(); // return null and exception
String queryString = req.getQueryString(); // return null
}
<---------------------------------------------------------->
I am going to edit my question
Thank you every one for nice reply,
google did it,
you can refer to that link by URL
http://developers.google.com/accounts/docs/OAuth2Login
inside above URL page there is following link
http://accounts.google.com/o/oauth2/auth? scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww‌​.googleapis.com%2Fauth%2Fuserinfo.profile& state=%2Fprofile& redirect_uri=https%3A%2F%2Foauth2-login-demo.appspot.com%2Foauthcallback& response_type=token& client_id=812741506391.apps.googleusercontent.com
when you click on above link, then you will get your gmail login account access_token, and that token is after # sign
Some characters cannot be part of a URL (for example, the space) and some other characters have a special meaning in a URL: for example, the character # can be used to further specify a subsection (or fragment) of a document; the character = is used to separate a name from a value.
see http://en.wikipedia.org/wiki/Query_string for more:
It looks like the '#' should be a '?'.
In a normal URL, the parameters are passed as key value pairs following a '?' and multiple parameters chained together using '&'. A URL might look as follows:
http: //someserver.com/somedir/somepage.html?param1=value1&param2=value2&param3=value3.
Normally the Java servlet container would return everything after the '?' when calling getQueryString() but due to the absence of the '?' it returns null.
As #Sandeep Nair has suggested getRequestURL() should return this full URL to you and you could parse it using regular expressions to get the information you want. A possible regular expression to use would be along the lines of:
(?<=access_token=)[a-zA-Z0-9.-]*
However, getRequestURL() does NOT normally return the query string, so using this method is relying on the fact that there is a '#' rather and a '?' and is therefore probably not a great solution. See here.
I would advise that you find out why you are getting a '#' instead of a '?' and try to get this changed, if you can do this then the servlet container should manage the URL parameters for you and call to request.getAttribute("access_token") and request.getAttribute("token_type") (see here) will return both values as strings.
You get query string by calling
String queryString = req.getQueryString();
It correctly returns null in your case, as there is no query string. The characters after "#" are anchor specification, which is only visible to the browser and not sent to server.

How to encode path parameter using Java Jersey

How do you encode a path parameter (not form-url-encoded) but just a single URL that's appended in the format:
public String method(#PathParam("url") String url) {
}
There are lots of references to form URL encoding, but I want to simply encode a string as in the above.
Like mentioned in the previous answer URLEncoder can only be used for query paramaters, not path parameters. This matters e.g. for spaces which are a + in the query parameter but a %20 in the path.
org.springframework.web.util.UriUtils.encodePath()
can be used. Also using an org.apache.http.client.utils.URIBuilder would work. setPath is escaping the path part here. Also pure Java by using a constructor of java.net.Uri works.
Why would you want to *en*code it there, if anything wouldn't you want to *de*code it? In any case, you would call the standard URLEncoder.

Take off file path (home/user/file.txt = file.txt)?

So I've written a simple text editor in java, and it retrieves the file via showOpenDialog() and converts the filename into a string, so it can be displayed as the title:
String title = fc.getSelectedFile().toString();
But lets say I have the path "home/user/file.txt". How would I strip off the path and make it so the filename displays as "file.txt" only?
File getName() returns what you want i.e just the last name in the pathname's name sequence.
getSelectedFile() returns a File object; the easiest thing to do would be to just call getName() on the File object. If the path comes from someplace else, you could actually construct a File from it and then call getName().
String filename = title.substring(title.lastIndexOf("/"))
edit: brain's answer is better and more concise though :-)

Escaping & in a URL

I am using jsps and in my url I have a value for a variable like say "L & T". Now when I try to retrieve the value for it by using request.getParameter I get only "L". It recognizes "&" as a separator and thus it is not getting considered as a whole string.
How do I solve this problem?
java.net.URLEncoder.encode("L & T", "utf8")
this outputs the URL-encoded, which is fine as a GET parameter:
L+%26+T
A literal ampersand in a URL should be encoded as: %26
// Your URL
http://www.example.com?a=l&t
// Encoded
http://www.example.com?a=l%26t
You need to "URL encode" the parameters to avoid this problem. The format of the URL query string is:
...?<name>=<value>&<name>=<value>&<etc>
All <name>s and <value>s need to be URL encoded, which basically means transforming all the characters that could be interpreted wrongly (like the &) into %-escaped values. See this page for more information:
http://www.w3schools.com/TAGS/ref_urlencode.asp
If you're generating the problem URL with Java, you use this method:
String str = URLEncoder.encode(input, "UTF-8");
Generating the URL elsewhere (some templates or JS or raw markup), you need to fix the problem at the source.
You can use UriUtils#encode(String source, String encoding) from Spring Web. This utility class also provides means for encoding only some parts of the URL, like UriUtils#encodePath.

Categories

Resources