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
Related
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}.
I am using CXF - Rest service.
#GET
#Produces({"application/xml", "application/json"})
#Path("/search/")
R findUser(#QueryParam("email") String email);
I am invoking the GET call from Postman or cURL, something like this
http://localhost:8080/rest-service/search/?email=test+1#gmail.com
But when I debug the email field, I am getting the data field as test 1#gmail.com. I guess somewhere URL decoding is happening and because of that + is getting disappeared? How do I configure CXF/service to not to alter the URL parameters
Add the #Encoded annotation to your method which will disable the automatic decoding of parameters. See here
Disables automatic decoding of parameter values bound using
QueryParam, PathParam, FormParam or MatrixParam. Using this annotation
on a method will disable decoding for all parameters. Using this
annotation on a class will disable decoding for all parameters of all
methods.
My service method Produces one of this MediaTypes it may produce pdf or excel file or other.
#Produces({"application/pdf","application/vnd.ms-excel"...
My Question
My service returns response type with application/pdf always even if it produces excel. Why?
Than I rearranged MediaTypes.
#Produces({"application/vnd.ms-excel","application/pdf",...
Now it's giving type application/vnd.ms-excel for all responses,again Why?
I am using com.sun.jersey API for client and getting type by the use of
clientResponse.getType()
Probably I think I misunderstood the concept of #Produces annotation.
Please Clarify.
Following is code of my Service method.
response = Response.ok((Object) file);//file is Object of File
response.header("Content-Disposition","attachment; filename="+filename);
//filename can be a.pdf b.xlsx etc
return response.build();
JAX-RS methods should base the preferred content type on the value of the Accept header of your request. Failing that, it should default to the first specified.
While the JAX-RS spec is somewhat vague on the subject, the Jersey documentation is very clear in describing the selection mechanism.
As it said in the documenation:
#GET
#Produces({"application/xml", "application/json"})
public String doGetAsXmlOrJson() {
...
}
The doGetAsXmlOrJson method will get invoked if either of the media types "application/xml" and "application/json" are acceptable. If both are equally acceptable then the former will be chosen because it occurs first.
Also, you can use quality factor for specifying which media type is more preferable:
#Produces({"application/xml; qs=0.9", "application/json"}).
In any way, if you want to be sure about which media type is used, you should divide your methods into two different signatures.
The #Produces annotation is used by the JAX-RS implementation to bind the incoming request to one of your Resource Methods, based on the accept header of the request.
You can set the exact type of the response in the returned Response object using ResponseBuilder#type(MediaType) if you want to enforce one media type in particular.
If you want to match the accept header of the incoming request ("application/vnd.ms-excel" vs "application/pdf" in your case), you can retrieve that header by adding a parameter annotated with #HeaderParam("accept") in your Java method.
HTH.
My question is similar to getting blobstore to callback to endpoint method but s/he got no reply. Also I actually wrote my code. I created my callback url as
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String url = blobstoreService.createUploadUrl("/loadImages");
And then I created my endpoint as
#ApiMethod(name = "loadImages", httpMethod = HttpMethod.POST)
public void loadImages(javax.servlet.http.HttpServletRequest req) {
//…. get blob key etc.
}
Then I get an error on my server when the blobstore makes the callback call:
No handlers matched this URL. (404)
Does anyone know what to pass to blobstoreService.createUploadUrl so it is handled by my endpoint method?
I have also tried changing ”/loadImages” to "/_ah/spi/com.company.package.ApiName.loadImages” so that it looks like the other paths, but that didn’t work either. Then I tried "_ah/api/apiname/1/loadImages”: nothing.
Note: I am not looking for an alternative to the blobstore/endpoint. When I use simple servlet to receive the callback it works. But the servlet just cheapens my otherwise endpoint-only code.
I'm afraid you can only user servlets to handle this type of callback. I've been looking for solution for several hours and found this topic.
In short:
The form must include a file upload field, and the form's enctype must
be set to multipart/form-data. The API ... passes the rewritten request to your application on the
given path as a blob key.
As Endpoints doesn't (as far as I know) accept multipart/form-data as
a valid encoding, this won't work. The error messaging you see is
because the Endpoint is expecting JSON.
Due to backward compatibilities, all HTTP requests not having defined charset are converted to ISO-8859-1 by default. Our netty served Play! application correctly receives PUT requests with JSON body, if those requests have defined charset. In case it does not, those requests should be converted to UTF-8 somehow, preferably on global application level.
So I have created a Global.scala class which will be in charge of receiving of all inbound
requests and converting those 'undefined' to UTF-8, which will ensure all requests will be handled properly before getting into their appropriate modules.
import play.*;
public class Global extends GlobalSettings {
#Override
public Action onRequest(Request request, Method actionMethod) {
return super.onRequest(request, actionMethod);
}
}
in this 'filter' method now there is missing code piece which will query if request is PUT or POST and does not have character encoding defined, and will convert this request's body to UTF-8 from ISO-8859-1, otherwise it will convert request's body to UTF-8 from whatever charset it is in.
Apache Tomcat has this resolved thru Filters: http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q3
I have not found anything in Netty similar to Tomcat's features, only this Global interceptor on Play! level.
The Requestobject has a body() method that returns the body of the request.
That body, of type RequestBody, has an asRaw() method that returns a Http.RawBuffer.
From there you can obtain the byte[] of the body and process it as you want. For example, String allows you to create a String from a byte[] indicating the Charset (see documentation), and as you can obtain the charset from the headers in the request, you can then create the right String.
EDIT:
On answer to the comment, to apply this you probably want to use a BodyParser instead of the Global class