URI Template variable containing a path? - java

I have a question about URI template variables.
I need to manage an URI with the form:
http://netlocation:port/application_path/{variable}
the variable can be a path itself, i.e. something like
this/variable/is/a/path
so that the complete URI appears to be
http://netlocation:port/application_path/this/variable/is/a/path
how can I manage that?

Use the "+" operator in order to avoid escaping the "/" character:
http://netlocation:port/application_path/{+foo}
You can try on URI Template Parser Online

You could use query parameters and just encode the path variable in the standard way:
http://netlocation:port/application_path?path=%2Fthis%2Fvariable%2Fisapath

Have you looked at http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/util/UriTemplate.html ?

Related

Spring URI patterns. Using slashes inside regex path

I want to create enpoint with dynamic uri using regex in uri pattern.
URI template: [host]/{regexpart}/endpoint
Examples of dynamic uri:
[host]/a/endpoint
[host]/b/endpoint
Also I want to use slashes inside regex part of uri path, like that:
[host]/a/b/endpoint
[host]/a/b/c/d/endpoint
I have enpoint like this:
#GetMapping(path = "/{regexpart:[a-z/]*}/endpoint")
public DeferredResult<ResponseEntity<?>> enpoint(#PathVariable("regexpart") String regexpart) {
// Some logic
}
But it isn't working. Is there way to define the regex to use slashes inside it? Thanks!
The way you have it written, the regex pattern is limited to be within 1 path segment.
What you may be looking for is something like
#GetMapping(path = "/**/endpoint")
the ** will allow for any characters across multiple path segments.
This will allow any valid path within your [host] that ends in /endpoint
If you need to limit that to certain characters (like just [a-z], this should get you on the right track.

Stuck on ControllerLinkBuilder

I am currently trying to encode part of the URL but not all of it
Here is a sample of it:
http://localhost:8080/resourceSearch?type=http%3A%2F%2Fexample.com%2FSSTG.owl%25252523Project&searchTerms={searchTerms}
For some reason it automatically encodes the last part with "searchTerms" whereas I want just the part prior to it to be encoded. The reason is because its a template URL that the client can replace the braces with the search term they want.
In Spring, we create the urls with query parameters and path variables using UriComponentsBuilder as below
Path Variable:
UriComponentsBuilder.newInstance()
.scheme("http")
.host("http://localhost:8080/contextpath/")
.path("/{variable1}/{variable2}/")
.build()
.expand("value1", "value2")
.encode();
Query parameter
.queryParam("value", "a") for http://localhost:8080/contextpath/?value=a

How to deal with URI and Uri in java?

I'm using a library that requires me to pass an Uri object as parameter.
Currently I'm doing this:
File file = new File(someFileDir, "test.pdf");
someObject.someFunction(file.toURI());
Note that the File object has the function toURI(). It returns an URI object.
Now my problem is that the function I call needs a Uri parameter, but it's complaining that I passed a URI object.
Aren't these things exactly the same? How can I output an Uri object? Or how do I convert URI to Uri?
Just deal with it as a string, It's easier.
you can use Uri.Builder() to create a Uri !
For you information, URI seem to come from the JAVA language (java.net.URI), and Uri, from the Android SDK specificaly (android.net.Uri). So I would advise you to use the "Uri" class...

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.

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