Is it possible to get everything after the root domain name from an HTTP request using Java?
So if the url being requested is http://example.com/my-path then I'd like to get the value of my-path
I'm using the PlayFramework so if there's a header called "Path" or something like that it should be easier to get it with:
String path = request.headers.get("path");
But this page suggests that such a thing doesn't exist:
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
If there's a different solution to get the same result, that would also be appreciated. If it is possible it will provide a solution to this question as well:
http://stackoverflow.com/questions/28503129/redirect-example-com-to-www-example-com-in-playframework-1-2-x
To get everything after the domain, use uri:
String path = request.uri();
Or if you want to cut out query string parameters, use path:
String path = request.path();
Related
I'm writing a page with a list of files. Every file should be previewable and downloadable. Therefore every file needs two anchor-links. One for download and one for preview. The download-link was no problem as the mediatype is always "application/octet-stream" but the preview link must pass the filename with extension as the request url so that the browser can create a specific request for the mediatype that is given by the filename extension. In addition to that filename I want to pass a query parameter which is the id of the file, as there can be multiple files with the same name.
So my controller method for preview looks something like this:
#GetMapping(value = "/show/{filename}", produces = {MediaType.IMAGE_PNG_VALUE,MediaType.IMAGE_JPEG_VALUE, MediaType.APPLICATION_PDF_VALUE, MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_PLAIN_VALUE})
public #ResponseBody byte[] show(#RequestParam("fileid") Long fileId, Model model)
{
...
}
The thymeleaf template anchor looks like that:
<a th:href="#{/files/show/${file.filename}(fileid=${file.id})}" style="height:24px;" title="Preview">
As seen the filename should be a route-parameter and the id is a query-parameter. Thymeleaf however creates the following url:
/files/show/${file.filename}?fileid=13
It successfully inserts the fileid and creates a queryformat but it fails to replace the filename.
Am I doing something wrong? What is the correct format for a thymeleaf link of that kind?
The standard url syntax supports both path paramters and normal parameters in the same expression. In general, I would avoid string concatenation when building links.
Here's how I would format this:
th:href="#{/files/show/{filename}(filename=${file.filename}, fileid=${file.id})}"
Ok...After more research I could solve it myself. The right form of the thymeleaf URL is one of the two:
th:href="#{'/files/show/' + ${file.filename}(fileid=${file.id})}"
of
th:href="#{|/files/show/${file.filename}(fileid=${file.id})|}"
Both will work.
Is there a way to delete a resource from AWS S3 using the java sdk by URL?
I know you can delete a resource using a bucket name and keyName like this:
s3client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
The issue is that I only have access to the resourceURL, so I would need to manipulate the string to extract the bucketname and keyname.
But if there was a way to delete by passing the url would be much cleaner.
There doesn't appear to be a way to simply pass the URL.
There's this, though:
AmazonS3URI
public AmazonS3URI(String str)
Creates a new AmazonS3URI by parsing the given string. String will be URL encoded before generating the URI.
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3URI.html
You can call getKey and getBucket on it to extract the strings you need. It's still messy, but at least it looks like you don't have to write your own parser.
How would I get the full URL of a JSP page.
For example the URL might be http://www.example.com/news.do/?language=nl&country=NL
If I do things like the following I always get news.jsp and not .do
out.print(request.getServletPath());
out.print(request.getRequestURI());
out.print(request.getRequest());
out.print(request.getContextPath());
You need to call request.getRequestURL():
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
Given URL = http:/localhost:8080/sample/url.jsp?id1=something&id2=something&id3=something
request.getQueryString();
it returns id1=something&id2=something&id3=something
See This
I found a solution. It may not be perfect solution. But it working solution.
String qs = request.getQueryString();
String foo = request.getRequestURL().toString()+"?"+qs;
I should use link. And it means that method GET and all parameters I can see in url. How I can hide parametrs from url like that
http://localhost:8080/MyApp/action.do?method=delete&id=0
And how after that I can get parametrs in java class.
You can't hide GET parameters from a URL. GET name value pairs have to be part of a request. However one thing you can certainly try is to encrypt your querystring into a string which only your code can decode on your host, for example
http://localhost:8080/MyApp/action.do?method=delete&id=0
could be
http://localhost:8080/MyApp/action.do?param=[ENCRYPTED/OBFUSCATED STRING]
then your application at the above url can decode that and convert back into name value pairs
That's the idea of GET method - to path parameters through URL, probably you should use POST.
Is there any way I can get url address of an application from java code, I mean complete address not only value from getContextPath(). Something like http://localhost:8080/etc
Try with getRequestUrl().
I hope it helps you
In a servlet or JSP, you can call javax.servlet.http.HttpUtils.getRequestURL(request)
It returns a StringBuffer containing the entire URL up to the servlet
From the javadoc
Reconstructs the URL the client used to make the request, using information in the HttpServletRequest object. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
If you only want up to the context path, you'll have to remove your servlet path
There is no method that returns the entire URL including the query parameters. You need to use something like:
req.getRequestURL()+"?"+req.getQueryString();
Or if you don't have query parameters you can use getRequestURL
It is pretty confusing, but here is a graphic that helps sort it out: