URL Path Parameter Encoding - java

I'm struggling with path param encoding with retrofit:
http://localhost:8080/nuxeo/api/v1 is my base url.
I have this Call #GET("path/{documentPath}")
Call<Document> fetchDocumentByPath(#Path("documentPath") String docPath);
As param, I'm setting the following: default-domain/blabla
I run the query against my tomcat app and I get this answer
Response{protocol=http/1.1, code=400, message=Bad Request, url=http://localhost:8080/nuxeo/api/v1/path/default-domain%2Fblabla}
Even if I put encode = true to say "don't encode my parameter, it's already encoded", it's still encoding it.
Moreover, in retrofit, this test retrofit2.RequestBuilderTest#getWithEncodedPathParam doesn't work if we put Request request = buildRequest(Example.class, "po/ng"); with the following assertion: assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/po/ng/");
Tomcat has restricted his URL validation for security reason: http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2007-0450.
So I'd like to send '/' directly in my path parameter without encoding it in %2F. How can I achieve it?
Thank you!

Since parent-2.0.0-beta4, the parameter of the annotation of #Path is now working properly.

Related

Receiving percentage encoded url

I am invoking a GET method on a percentage encoded URI , but my rest controller is not able to handle it. It throws Internal Server Error. How am i suppose to handle encoded uri on rest controller side using #RequestParam
Use String in #RequestBody, and use URLDecoder to decode in UTF-8.
Like this - URLDecoder.decode(value, StandardCharsets.UTF_8.toString()).
You can use base64 as well. Check this answer.

HttpURLConnection "enctype" POST [duplicate]

How can I set content type of HTTP Put as xxxx+xml?
I was referring to solution in this link Android, sending XML via HTTP POST (SOAP). Its fine when we set content type like this, i mean the xml is came along with the request:
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
but when i change type soap to something custom, the xml disappear on the request (i saw on the wireshark), like this:
httppost.setHeader("Content-Type","application/vnd.oma-pcc+xml;charset=UTF-8");
then, i tried put the xml only, so the request is ok again:
httppost.setHeader("Content-Type","application/xml;charset=UTF-8");
I want to know what exactly the rules for the content-type than come together with the xml type so that the xml still there.
Thanks.
Assuming you're using HTTPClient of 4.1.3 or greater -
When constructing you're entity, you have the option to specify the content being used for the POST or PUT operation for certain entities.
There is a ContentType object which should be used to specify this.
Using the factory method .create() you can specify the mimetype with a charset - the ContentType will be used by the framework to properly emit the header in question.
Example API call:
ContentType.create("application/vnd.oma-pcc+xml", CharSet.forName("UTF-8"));
NOTE Editing for HttpClient 4.1.2
In the case of 4.1.2, when you create your entity for the post or put operation, set the content type on the entity not the execution (HttpPost or HttpPut) using setContentType(String). This is deprecated in 4.1.3 and beyond.

Invoking a 'REST' service which have query parameters in the URL

I have to invoke a GET on a service which returns text/xml.
The endpoint is something like this:
http://service.com/rest.asp?param1=34&param2=88&param3=foo
When I hit this url directly on a browser (or some UI tool), all's good. I get a response.
Now, I am trying to use CXF WebClient to fetch the result using a piece of code like this:
String path = "rest.asp?param1=34&param2=88&param3=foo";
webClient.path(path)
.type(MediaType.APPLICATION_JSON)
.accept(MediaType.TEXT_XML_TYPE)
.get(Response.class);
I was debugging the code and found that the request being sent was url encoded which appears something like this:
http://service.com/rest.asp%3Fparam1=34%26param2=88%26param3=foo
Now, the problem is the server doesn't seem to understand this request with encoded stuff. It throws a 404. Hitting this encoded url on the browser also results in a 404.
What should I do to be able to get a response successfully (or not let the WebClient encode the url)?
Specify the parameters using the query method:
String path = "rest.asp";
webClient.path(path)
.type(MediaType.APPLICATION_JSON)
.accept(MediaType.TEXT_XML_TYPE)
.query("param1","34")
.query("param2","88")
.query("param3","foo")
.get(Response.class);
You will need to encode your URL. You can do it with the URLEncoder class as shown below:
Please replace your line
String path = "rest.asp?param1=34&param2=88&param3=foo";
with
String path = URLEncoder.encode("rest.asp?param1=34&param2=88&param3=foo");

Getting unescaped servlet path from a HttpRequest object

I have a custom proxy servlet that has to deal with URL-s that contain special characters (e.g. ; , . / in their) in their path. This is because it is a RESTful application that has ugly path params by design. (Don't comment it as it is not mine.)
My client, (actually wget, because browsers tend to show unescaped the URL) send a request to this URL:
http://localhost:8080/MyApplication/proxy/foo/ugly%3Apart%2Fcomes%3Bhere/children
//note: %2F = '/', %3A = ':', %3B = ';'
In my servlet (mapped to /proxy/*) when I try to forward the GET request, I am unable to reconstruct it because HttpRequest.getPathInfo() returns me the URL unescaped:
http://localhost:8080/MyApplication/proxy/foo/ugly:part/comes;here/children
And therefore the information of which /s and ;s were originally escaped or unescaped is lost. And that makes a difference for me, for example ; makes my URL a so called matrix URL, see http://www.w3.org/DesignIssues/MatrixURIs.html, or all the REST path parameters get shifted by slashes.
Actually I found this issue on a Glassfish server, so I'm not sure if different application servers treat this differently or not. I found only this in the Servlet API:
getPathInfo() Returns any extra path information associated with the
URL the client sent when it made this request.
How could I get the original, unescaped request URL that was sent by the client?
Have a look at HttpServletRequest's getRequestURI() and getRequestURL() methods.
If you need to remove context and servlet mappings, look at getContextPath() and getServletPath().

How to encode a URL that I want to pass as a query string

I'm trying to send a URL as paramter of a query string like this example:
http://localhost.com/myapp.jsp?pathToFileURL=http://192.168.0.1/my_file.pdf
What I did is I used encode URL to encode the path before sending it to the server, problem is im getting a "400 Invalid URI: noSlash" because of this.
From what I read the problem is the tomcat security and that I should add a parameter to the tomcat startup
-Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
But I can't modify the parameters of the tomcat, so is it possible to do it other way?
Thanks
You can do URLSafebase64 encoding at the client side and URLSafebase64 decoding at the server side.
Check URLEncoder class for more details:
http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html
You can test manually before coding using any of the online URL Encoder/Decoder. Just google for "URL Encoder/Decoder"
Complete stab in the dark but you could try escaping the slashes with backslashes or you could try replacing them with %2F which is the URL encoded version of forward slash.
Hope this helps.
Base64 the URL then on the receiving end base64 decode to get the original URL without any alteration

Categories

Resources