Java WebSocket HandshakeRequest getParameterMap method - java

Does the method getParameterMap of the HandshakeRequest include path parameters? I can't seem to find a detailed documentation regarding this.

HandShakeRequest#getParameterMap() javadoc says following:
Return the request parameters associated with the request.
It says request parameters, not path parameters.
Let's check how an URL is composed:
http://example.com/context/foo/bar?foo=bar&bar=foo
----------------
Request URI
http://example.com/context/foo/bar?foo=bar&bar=foo
--------
Context path
http://example.com/context/foo/bar?foo=bar&bar=foo
--- ---
Path parameters
http://example.com/context/foo/bar?foo=bar&bar=foo
------- -------
Request parameters
When having only HandShakeRequest at hands, which doesn't have any method directly returning path parameters, your best bet is to use getRequestURI() and perform string manipulation (split, substring, etc) in order to extract path parameters.
The alternative is to move the task into Endpoint#onOpen() or #OnOpen, there path parameters are just directly available via Session#getPathParameters().

Related

Getting 404 Not Found, while calling JAX-RS Rest API method

I am trying to call REST API method (method been declared without #Path annotation).
Resource path for this API has been loaded in server start up itself using BaseRestServer.rootPath.
I expect that this API will get called by default, as there are no other apis available to process the request.
But when i call this api, I am getting '404 Not Found' as response.
code is something like, as given below,
**#Path("")**
public class JobResource{
#POST
#Consumes("application/job")
#Produces("application/job")
public Response postJob(
#Context HttpServletRequest hRequest, Job job){
}
}
resource path has been defined in the server file itself, as given below
BaseRestServer.rootPath = "/shared/job/"+companyName.
Reason on why i have not used resource path in the class is, that it allows only constant value inside #Path annotation.
But in my case, companyName value changes dynamically
When I pass this dynamic value inside #Path("/shared/job/"+companyName), i am getting compilation error as 'The Value for annotation attribute Path.value must be a constant expression'

Extract URI suffix as method parameter

I currently have the following route defined:
from("servlet:///my-api/v1/{param1}?matchOnUriPrefix=true")
.unmarshal().json(JsonLibrary.Jackson, Map.class)
.bean(myController, "myMethod(${in.headers.param1})")
.setHeader(Exchange.CONTENT_TYPE, simple("application/xml"));
This does not work, when the message reaches myController.class, param1 is null.
Is there a way to capture the suffix on a requested URI and then pass it on as a parameter for a bean method?
I know you can do this if you use the restlet component but I would like to use servlet.
No this is not possible, but it could be a nice addition to support. You are welcome to log a JIRA ticket: http://camel.apache.org/support.html
Today you would have to setup the route as
from("servlet:///my-api/v1/?matchOnUriPrefix=true")
and then grab the Exchange.HTTP_PATH header which should be the relative path, eg in your example {param1}.

How to get Grails HeaderParam attributes

I came for ruby/rails background. I am facing issues to get header attributes of rest call.
In case of rails, I used to write below code to list all requested header attributes.
puts request.headers.inspect
Could any body please suggest me what is the equivalent for Grails ?
The request object is an instance of the Servlet API's HttpServletRequest interface, so you can use the getHeader and getHeaderNames methods
This is an example of how to print all the headers - add it where you can access the request object (e.g. inside a controller method):
request.getHeaderNames().each {
println(it + ":" + request.getHeader(it))
}
Below is the code, which list all header attributes.
request.headerNames.each{
println it
}
attributes
accept
accept-encoding
content-type
api-key
time-stamp
signature
user-agent
host
Take a look here. On this page you can find "The request object is an instance of the Servlet API's HttpServletRequest interface". You can use getHeader and getHeaderNames methods
Also, remember in Groovy/Grails any getXXX method will treat XXX as a property. So, getHeader and getHeaderNames can be abbreviated to request.header 'someheadername' or request.headerNames

How do i transform subfolders request to params

I'm using Java server, and I need the sub-folders in the request to act like parameters.
example:
myhost/p/a/1
and I need the server to "understand" it like that:
myhost/p?a=1
How can I do that?
Thanks,
Koby
1: spring 3 mvc #RequestMapping tag can extract path values from uri
#RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(#PathVariable String ownerId, Model model) {
Owner owner = ownerService.findOwner(ownerId);
model.addAttribute("owner", owner);
return "displayOwner";
}
2: use UrlRewiter: http://www.tuckey.org/urlrewrite/. This can extract path parameters using regexp.
<rule>
<from>^/image/([A-Za-z0-9-]+).html\??(.*)?$</from>
<to>/image.html?imagecode=$1&$2</to>
</rule>
Create a filter -- in that filter getServletPath() then parse the path and forward the request to appropriate controller/servlet
Create a filter at say path /files/* see here,
In this filter add the logic that gets you the whole path after base URL -- i.e. your servlet path see here
You parse this path by splitting using "/" and then pass the array as the parameter to the servlet that want to use this path. see here for forwarding the request

single method in getRequestDispatcher() in ServletRequest and ServletContext interfaces

HI
I like to know there is a single method called getRequestDispatcher() in ServletRequest and ServletContext interfaces. What is the difference?
As stated in the Servlet API Javadocs,
The difference between this method [the ServletRequest one] and ServletContext.getRequestDispatcher(java.lang.String) is that this method can take a relative path.
You can pass a relative path to getRequestDispatcher() of ServletRequest but not to getRequestDispatcher() of ServletContext.
Example:
My current request is served from page - webapp/view/core/bar.jsp
and requested page - webapp/view/util/foo.jsp
request.getRequestDispatcher("../util/foo.jsp") is valid and will be evaluated to the path relative to current request.
servletContext.getRequestDispatcher("/view/util/foo.jsp") is valid and will evaluate from context root.
This is because ServletContext will not be aware of current request path.
If you decide to use '/' root to access your resources, then both ways are same.

Categories

Resources