Path variable having special characters like # in Spring boot - java

I'm using Spring Boot 2.4.6. For delete APIs getting 405 method not found. My endpoint is like: beauty/v1/sites/addressTemplates/:templateId
Path variable: ##$%#
Can someone please suggest what can be done to make this behavior as not complaining for 405? Please direct me to other questions in case I'm missing something.

I guess that your issue has nothing to do with Spring. Maybe you are trying to compose the whole URL by using reserved characters.
In a URL, a hash mark, number sign, or pound sign ( # ) points a browser to a specific spot in a page or website. It is used to separate the URI of an object from a fragment identifier. Source.
Which means that an URL which looks like:
beauty/v1/sites/addressTemplates/##$%#
is not exactly what you imagine it to be because # is interpreted in a special way. What you have to do is to percent encode the "special" path variable so it will look like this at the end:
beauty/v1/sites/addressTemplates/%23%40%24%25%23
Then Spring will not complain anymore and will resolve properly the endpoint.

Related

How do I respond to tickets with camel Jira Producer?

I'm following the examples here -> https://github.com/apache/camel-k-examples. Working on 05-knative-source-jira
When running this integration, I'm able to read and log new jira issues just fine, I fall down when I try to use info from the ticket, or respond to the ticket with the jira addComment producer.
I've tried just putting a static ticket number in for the IssueKey option, but I get build errors and can't even get the producer to run.
I've tried tinkering with the URI...
Ex: Changing URI to -> .to("jira://addComment?IssueKey=EQ-7") returns below on build
No signature of method: org.apache.camel.builder.ValueBuilder.to() is applicable for argument types: (String) values: [jira://addComment&IssueKey=EQ-7]
I've tried this with both ? and &, as well as adding properties to the URI with similar results.
I feel like I'm missing something pretty fundamental, so any docs pointers would be well appreciated.
Full integration here
// camel-k: language=groovy
from('knative:channel/jira')
.unmarshal()
.json()
.log('Recieved: ${body}')
.to('direct:ticket')
from("direct:ticket")
.setBody().simple("testing")
.to("jira://addComment?IssueKey=EQ-7")
I ended up sorting through enough docs to find the answer. I'll share details just for others who might find this (or if I google it again).
The key was to
a) Set the required headers for the issue key. Seting headers examples
b) Ensure that my properties are set correctly. I used a configmap to set my properties, and then referenced them as shown below in the URI. I believe this should also be possible through DSL but URI was easiest for me to just get working.
Functional Integration below.
from("direct:ticket")
.setHeader("IssueKey").simple('${body["key"]}')
.setBody().simple("We've recieved the ticket -- we'll update you soon!")
.to("jira://addComment?jiraUrl={{url}}&consumerKey={{consumer_key}}&accessToken={{access_token}}&privateKey={{private_key}}&verificationCode={{verification_code}}")

Spring 4 Path Parameter ending in a slash

I'm trying to get my Spring 4 application to allow Path Parameters that end with an escaped slash.
The #RequestMapping I've got is:
#RequestMapping("/{externalSystemId}/{externalRequestId}/events")
And the URL that I'm calling with is /dummy/ab%2F/events. So "externalRequestId" is the one that has the trailing encoded slash.
I've gotten to the point where the handler is called correctly, and where escaped slashes in the middle of the path segment work - e.g. /dummy/ab%2Fcd/events, but not at the end.
What I've done so far:
Added system properties to Tomcat:
-Dorg.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH=true
-Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
Custom extension of UrlPathHelper that replaces getServletPath with one that simply returns request.getRequestURI()
Configure the UrlPathHelper with:
urlDecode = false
removeSemicolonContent = false
Use this UrlPathHelper from mvc:path-matching, within mvc:annotation-driven
And I'm at a complete loss as to where to go next. I've even been debugging through both Spring and Tomcat to try and work it out, and I'm stumped. It looks like it's because UrlPathHelper.getPathWithinServletMapping() is calling request.getPathInfo(), and that is returning the path with the %2F stripped out. And that in turn is because somewhere else in Tomcat - I've lost the window now - it has decoded the %2F into a /, then replaced the "//" that is now present with a "/" instead.
This feels like it should be relatively straightforward, so what am I missing?
I've tried it on both Tomcat 7 and Tomcat 8, and with various versions of Spring 4 - currently on 4.3.14.
Problem solved.
The SlashFriendlyUrlPathHelper that I had that was the extension of UrlPathHelper was causing problems, because my webapp is not the Root webapp. Remove that, but keep every other change in place, and it starts to work properly.

change baseurl with okhttp

Recently,When I plan to rewrite mine company program. I use the RxJava+OkHttp+Retrofit. But I have a problem.
Our application has only one access interface which can provide the base address for all modules. The format is as follows.
My intention is to dynamically replace baseurl with interceptors, the code reads as follows:
That's my code but I found a question,the converted URL becomes so:
www.xxapi.com:80/authorize/login/{#version code} (This is wrong).
This is wrong. This version field should be behind the base address.The correct url like this:
www.xxapi.com:80/{#version code}/authorize/login (This is right).
I've tried many ways, and I can't change the location of the version field.
I really don't know how to go on,I'm sorry.
Replace the call to addPathSegment(version) with a call to setPathSegment(0, version).
https://square.github.io/okhttp/3.x/okhttp/okhttp3/HttpUrl.Builder.html#setPathSegment-int-java.lang.String-

Spring Security antMatcher expression not works with path variable

I need to provide certain role for accessing a URL in the following format:
/connector/{programId}/order/{anything here}
Where programId is an integer value so I'd tried the following and it doesn't work at all.
.antMatchers('/connector/{programId:\\d+}/order/**').access("hasRole('CONNECTOR')")
But when I used ** instead of the programId part it's working well. But how can I make it work with pathVariable (which is always an integer).
You should use RegexRequestMatcher instead of AntPathRequestMatcher.
.regexMatchers("/connector/(\\d+)/order/.*").access("hasRole('CONNECTOR')")
I am using:
.regexMatchers("/connector/(\\d+)/order/.*").access("hasRole('CONNECTOR')")
But server response 405 error

IOException: unexpected response from remote api

I'm having exactly the same problem as this person:
https://groups.google.com/forum/#!topic/google-appengine-java/4y90M9NlWsA
but there are no responses and I can't see anything else on the internet that helps. Does anyone know why this would happen?
RemoteApiOptions options = new RemoteApiOptions()
.server("localhost", 8888)
.credentials("blah#blah.blah", "");
RemoteApiInstaller installer = new RemoteApiInstaller();
installer.install(options);
The stacktrace is:
Exception in thread "main" java.io.IOException: unexpected response from remote api: {rtok: null, app_id: MyCorrectAppId}
at com.google.appengine.tools.remoteapi.RemoteApiInstaller.getAppIdFromServer(RemoteApiInstaller.java:326)
at com.google.appengine.tools.remoteapi.RemoteApiInstaller.loginImpl(RemoteApiInstaller.java:278)
at com.google.appengine.tools.remoteapi.RemoteApiInstaller.login(RemoteApiInstaller.java:239)
at com.google.appengine.tools.remoteapi.RemoteApiInstaller.install(RemoteApiInstaller.java:106)
at uk.blah.blah.MyClass.main(MyClass.java:114)
I've noticed you've set the <application> element inside appengine-web.xml to MyCorrectAppId. That's the problem. As Eugene rightly pointed out, the ID must be all lowercase, like mycorrectappid. Quoting Dan Sanderson's book Programming Google App Engine:
Because the application ID is used in the domain name, an ID can contain only lowercase letters, numbers, or hyphens, and must be shorter than 32 characters. Additionally, Google reserves every Gmail username as an application ID that only the corresponding Gmail user can register.
If you are working in Eclipse, you need to remember that when setting up a new web application project (there is an option towards the bottom of the "New Web Application Project" dialogue that allows you to specify the ID instead of leaving it blank).
For a standalone environment, I think you're probably fine if you leave the ID blank (it will default to "no_app_id").
Hope that helps!
According to the code of the RemoteApiInstaller.getAppIdFromServer method the exception unexpected response from remote api can be thrown when no app_id property found in the response body. And it seems that in your case it is probable that MyCorrectAppId does not match the PAIR_REGEXP pattern, i.e. it contains any characters other than digits, letters in lower case, tilde, underscore or minus sign.

Categories