is there a way to match only a part from a route.
I need a route definition which should match the following routes
/route
/route/part1
...
The part after "/route" is dynamic and variable. Every route which starts with "/route" should be matched.
My try:
GET /route/<[a-z]+> controllers.Assets.at(path="/public/web", file="static.html")
Error: Action not found For request 'GET /route/home'
GET /route/*file controllers.Assets.at(path="/public/web", file="static.html")
Error: It is not allowed to specify a fixed or default value for parameter: 'file' extracted from the path
Rename and move /public/web/static.html to /app/views/staticFile.scala.html
Change the route to
GET /route/*path controllers.Application.staticFile(path: String)
Add the method to /app/controllers/Application.java
public static Result staticFile(String path) {
return ok(views.html.staticFile.render());
}
Related
I want to add filter for this path with wildcards /{login}/cart/* in webfilter config Spring application, but wildcard for {login} is not working
#Bean
public FilterRegistrationBean<UserFilter> homeFilter() {
FilterRegistrationBean<UserFilter> UserFilterBean = new FilterRegistrationBean<>();
UserFilterBean.setFilter(new UserFilter());
UserFilterBean.addUrlPatterns("/{login}/cart/*");
return UserFilterBean;
}
Adding more details to #TongChen's comment
Did you try UserFilterBean.addUrlPatterns("/\\blogin\\b/cart/*"); ?
spring uses AntPathMatcher, please take a look at this complete documentation - https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html
I am copying text from the same official documentation's examples part:
Examples
com/t?st.jsp — matches com/test.jsp but also com/tast.jsp or com/txst.jsp
com/*.jsp — matches all .jsp files in the com directory
com/**/test.jsp — matches all test.jsp files underneath the com path
org/springframework/**/*.jsp — matches all .jsp files underneath the org/springframework path
org/**/servlet/bla.jsp — matches org/springframework/servlet/bla.jsp but also org/springframework/testing/servlet/bla.jsp and org/servlet/bla.jsp
com/{filename:\\w+}.jsp will match com/test.jsp and assign the value test to the filename variable
I have not tried this approach till now, I am going purely based on documentation,
Here is additional documentation that says this approach will work for #RequestMapping for sure, but whether it will be applicable to filter or not is something to be tried - 16.3.2.2 URI Template Patterns with Regular Expressions
https://docs.spring.io/spring/docs/3.1.0.RELEASE/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates-regex
Example from documentation to handle :
If you want to handle "/spring-web/spring-web-3.0.5.jar" with a regex then this is the solution:
#RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")
public void handle(#PathVariable String version, #PathVariable String extension) {
// ...
}
}
Based on all these I am hopeful that UserFilterBean.addUrlPatterns("/\\blogin\\b/cart/*"); might be a solution for you
I am new to REST API's.
I am developing a REST API.
In the following API the parameters I take is cloud-id.
This is the API Call:
#GET
#Path("{cloud-id}")
#Produces("application/json")
public Object Getall(#PathParam("cloud-id") String cloudID) {
if(cloudID!=null){
//return some details
}else{
//return something else
}
}
Happy Path:
http://example.com/sampleCloudID
This also works fine
http://example.com/(sampleCloudID)
It gives a 404 as expected
But when I give the URI as
http://example.com/{sampleCloudID}
ERROR:
You specified too few path parameters in the request.
In case the input I receive is {samplecloudID} I expect the service to return a 404, but I am unable to reach my resource if the path variable is in {}.
Why are curly braces giving me a error but normal parenthesis give 404 as expected ?
If you need to send special characters as part of the URL you need to encode them.
try using http://example.com/%7BsampleCloudID%7D
This should let your controller get the {}
This wikipedia article should give you details.
RFC 1738 states that certain characters are unsafe in URLs:
Unsafe:
Characters can be unsafe for a number of reasons. [...] Other
characters are unsafe because gateways and other transport agents are
known to sometimes modify such characters. These characters are "{",
"}", "|", "", "^", "~", "[", "]", and "`".
All unsafe characters must always be encoded within a URL. For
example, the character "#" must be encoded within URLs even in systems
that do not normally deal with fragment or anchor identifiers, so that
if the URL is copied into another system that does use them, it will
not be necessary to change the URL encoding.
source: https://meta.stackexchange.com/questions/79057/curly-brackets-in-urls?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
You could write code this way in which you did't get any error on changing type of your path variable
#GetMapping(value = { "/your-path/{cloud-id}", produces = "application/json")
public Object Getall(#PathVariable(value = "cloud-id", required = false) String cloudID) {
//do your stuff
}
I have a strange problem
this is what I have in routes files
GET /path/list controllers.path.getPaths()
GET /path/:id controllers.path.get(id:Int)
when I try to go <domain>/path/list the following error shows up:
For request 'GET /path/list' [Cannot parse parameter id as Int: For
input string: "list"]
I also tried to change the order in routes file
GET /path/:id controllers.path.get(id:Int)
GET /path/list controllers.path.getPaths()
I still get the same error. so my question is
isn't route supposed to match the first path that matches?
what else could be the problem (e.g. java codes)?
From the code you've provided this should work. The routes are not ambiguous because (from Play documentation):
Many routes can match the same request. If there is a conflict, the first route (in declaration order) is used.
if your routes ordering looks like this:
GET /path/list controllers.path.getPaths()
GET /path/:id controllers.path.get(id:Int)
/path/list will match before attempting to extract/transform the id parameter id:Int from the path and throwing.
If you want Play to transform the incoming parameter into a specific Scala type, you can add an explicit type
The only way this would not work is if you attempted to visit a route that did not match list or was not an Int:
For request 'GET /path/lists' [Cannot parse parameter id as Int: For input string: "lists"]
When returning a string value from an incoming request in my network based app, I have a string like this
'post http://a.com\r\nHost: a.com\r\n'
Issue is that the host is always changing so I need to replace it with my defined host. To accomplish that I tried using regex but am stuck trying to find the 'host:a.com' chars in the string and replacing it with a defined valued.
I tried using this example www.javamex.com/tutorials/regular_expressions/search_replace_loop.shtml#.VUWvt541jqB changing the pattern compile to :([\\d]+) but it still remains unchanged.
My goal is to replace given chars in a string with a defined value and returning the new string with the defined value.
Any pointers?
EDIT:
Sample of a typical incoming request:
Post http://example.com\r\nHost: example.com\r\nConnection: close\r\n
Another incoming request might take this form:
GET http://example2.net\r\nContent-Length: 2\r\nConnection: close\r\nHost: example2.net\r\n
I want to replace it to this forms
Post http://example.com\r\nHost: mycustomhostvalue.com\r\nConnection: close\r\n
GET http://example2.net\r\nContent-Length: 2\r\nConnection: close\r\nHost: mycustomhostvalue.com\r\n
Use a regex to replace it, like this:
content = content.replaceAll("Host:\\s*(\\w)*\\.\\w*", "Host: newhost.com")
This will replace anything after Host: with newHost.com.
Note: as per comment by cfqueryparam, you may want to usea regex like this to cover .co.uk and such:
Host:\\s*.*?(?=\\\\r\\\\n)
I have a URI with path like this :
ftp://test:test#someftp/ready123/users/abc/#0#.
But the method getPath() on the URI returns this:
/ready123/users/abc/.
I need the whole path to be returned like this :
/ready123/users/abc/#0#
... So that I can change the working directory(CWD) to the folder #0#. The code is in a generic method and is jarred up which gets used by many other applications. So I have to be very careful when I make changes. I believe anything after # is considered as a fragment, but in this case it is actually the name of a folder.
How do I get the path /ready123/users/abc/#0# from the URI object ?
From the JavaDoc of URI class:
URI syntax and components At the highest level a URI reference
(hereinafter simply "URI") in string form has the syntax
[scheme:]scheme-specific-part[#fragment]
where square brackets [...] delineate optional components and the
characters : and # stand for themselves.
As such if you want to retrieve everything after the first # you need to use URI.getFragment()
You need to replace the hash character with %23
URI uir = new URI("ftp://test:test#someftp/ready123/users/abc/%230%23");
System.out.println(uir.getPath()); // returns '/ready123/users/abc/#0#'