Extract URI suffix as method parameter - java

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}.

Related

How to generate "lang" parameter at the end of the url without making a request in each controller in Spring?

I want to add internationalization support to Spring project that I'm working on. It works when I add "lang" parameter at the end of the url like;
localhost:8080/someurl?lang=en
However, I can not generate the parameter at the end of the url, I need to make a parameter request from the controller.
I don't think it is a good idea to request lang parameter in each controller. I believe there is a better way to implement but I don't know what it is.
Do you have any suggestion where should I look for it?
Edit: I have a langKey field for the entity User so, I want to generate lang parameter by using the field langKey of the User.
I realized that applying the lang parameter only once is sufficient for the rest of the session so, there is no need to add lang parameter in each controller. It was enough for me to only add the lang parameter for the redirection after login according to users langKey. Then the rest of the session displayed according to language choice of the user stored in database.
I hope that helps for the others.

Jersey resource that matches any path

I am using Jersey v1.x and a Guice Servlet.
What I'm trying to do is bind a Jersey Resource that matches any #Path, such that I can use Jersey to respond with a 404.
I'm looking to do this, since my servlet consists of different components (e.g. a rest API that lives under /api, and a web UI that lives under /.
In Guice terms, that means I have several ServletModules that each set up one part of the servlet:
In my ApiServletModule: serve("/api").with(GuiceContainer.class, conf)
In my WebUiServletModule: serve("/").with(GuiceContainer.class, conf)
In this setup, I want to define what the 404 response body looks like for each part of the webapp (/api or /) from the codebase of each subproject responsible, without having to reimplement Jersey
So far I have tried to bind a resource that match #Path("/"), #Path("*") and #Path("/*"), but none of these seem to be picked up when I request /some/path/that/doesnt/exist
You need to use the regex format of the path expression, i.e.
#Path("{any: .*}")
You could inject List<PathSegment> to look at all the segments if you need them.
public Response getSomething(#PathParam("any") List<PathSegment> segments)
#peeskillet's answer is indeed correct, in the sense that it describes how you can create a Jersey resource that matches any path.
However, my goal of creating a resource that delivers 404 responses for whenever any other unmatched path is requested is not quite met by this answer:
At least in combination with Guice, will such a "match all"-resource intercept all requests, regardless of whether any more specific resources are available. Additionally, you cannot modify the HTTP response status code from within a resource.
For this purpose, Jersey has ExceptionMappers that can be implemented and loaded by adding the #Provider annotation. One particular type would be a ExceptionMapper<NotFoundException>, which is invoked when a Resource throws a NotFoundException. The ExceptionMapper can then decide what response to generate, including the status code.

Restygwt: malformed URI sequence

I've REST-based service on resygwt with API like this:
#Path("/search")
#GET
List<User> search(#QueryParam("login") String loginMask) throws RemoteException;
And I receive "malformed URI sequence" for this request:
http://devsys23:8080/rest/search?login=%25spa%20ce%25
This is rather strange, since in JavaDoc mentioned, that such requests should be supported by default:
Binds the value(s) of a HTTP query parameter to a resource method parameter,
resource class field, or resource class bean property.
Values are URL decoded unless this is disabled using the {#link Encoded}
annotation. A default value can be specified using the {#link DefaultValue}
annotation.
I've try to edit tomcat connector in server.xml with useBodyEncodingForURI and URIEncoding="UTF-8". Also org.springframework.web.filter.CharacterEncodingFilter was included and forceEncoding set, but it still doesn't work =(
What should I do to specify that login param should be decoded?
Thank you for your advice if any.
If you want to decode the value, you can create a ContainerResponseFilter, register it in your ResourceConfig and do something like
String loginValue= queryParams.get("login");
loginValue= URLDecoder.decode(loginValue, "UTF-8");
Maybe RestyGWT is encoding all the params by default.
Need to ask on the restyGWT google discussion : https://groups.google.com/forum/#!forum/restygwt

Remove invalid request parameter from URL with servlet filter

I have a web application written in Java which uses Struts 1.0. Sometimes when a URL is fired, I can see that it one of the request parameters does not have any name and value like the following ...
http://www.aaa.com/test.do?a=1&b=2&=&d=4&e=5
As can be seen, there is a '&=' which is essentially a parameter with no name and value. I'd like to remove this part from the URL before sending the request to the server. How can I achieve this? Should I use a filter or is there an easier way?

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

Categories

Resources