Spring Integration HTTP Inbound Validation - spring-integration

I am using SpringBoot 2.0 with Spring Integration 5.0.3 and have an issue with my HTTP.inboundGateway. My goal is to validate the JSON posted to the gateway, because the request pojo consists of mandatory fields.
#Bean
public IntegrationFlow notifyUpdateVehicleFlow() {
return IntegrationFlows.from(Http.inboundGateway("/update")
.requestMapping(r -> r.methods(HttpMethod.POST))
.requestPayloadType(RequestPojo.class)
.requestChannel(updateChannel())
.replyChannel(updateReplyChannel()))
.get();
}
Is there an easy way to validate fields in the pojo have been set? What I have already tested is using #NotNull SpringValidation but it seems not to be supported with Spring Integration.
Greetings,
smoothny

There is no such a functionality in the Spring Integration. You can use .filter() downstream that Http.inboundGateway() and really perform Validator.validate() from there on the payload.
If you think it must be done somehow on the Http.inboundGateway() and you have strong requirements and clean description, feel free to raise a JIRA on the matter and we will discuss what can be done from the Framework perspective.

Related

Spring Integration Java DSL InBound and Outbound Error Handling

I am trying to build an integration solution where
IntegrationFlows
.from(inBoundGateway)
.enrichHeaders(enrichHeaders())
.transform(dto to externaldto)
.handle(outBoundGateway, advice -> advice.advice(retryAdvice()))
.transform(exetrnaldto to dto)
.get()
#Bean
RequestHandlerRetryAdvice rhra
rhra.setRecoveryCalBack(errorMessgaeRecoverer());
#Bean
ErrorMessageSendingRecoverer errorMessgaeRecoverer
and my outboundgateway is defined as
Http.outboundGateway(uri, resttemplate)
...
.get()
new RestTemplate(requestFactory)
where requestFactory is
TrustStratgey ts = new TrustStratgey(){
public boolean isTrusted(...){
return true;
}
}
SSLContext context = SSLContexts,custom().loaddTrustMaterials(null, ts);
SSLContextFactory cf = new SSLContextFactory(context, new NoopHostnameVerifier());
HttpClientBuilder clientBuilder ..
clientBuilder.setSSLSocketFactory()
Happy path works fine, the problem i am facing is with not so happy path.
When Api call returns Error response .transform(exetrnaldto to dto) fails and client get 500
I want to translate error resposne json as well to my json
How do i handle error situations.
My questions are;
How to handle errors.
In error conditions how to stop flow not to transform
How to send status code in response from outbound to client (this is important)
How to handle error like typical #Controller Advice #ErrorHandler mechanism or similar.
Hope Garry get to see this post, couldnt find any answers, i looked through many books and forums, feels like Java DSL is not widely used or commented yet.
There is nothing Spring Integration Java DSL specific in your question.
It is really more about how to handle HTTP error on the server side (MVC - HttpInboundGateway) and on the client side - RestTemplate and HttpOutboundGateway.
The Java DSL is just a syntax sugar over existing technologies and solutions.
So, the #Controller Advice #ErrorHandler is an MVC, server side. You definitely can make it working with the HttpInboundGateway, but it is indeed wrong direction for the client side REST calls.
It looks like you would like to catch a REST error call for that your .handle(outBoundGateway). Consider to use an ExpressionEvaluatingRequestHandlerAdvice along side with that retryAdvice. So, you will be able with its failureChannel to handle an exception and transform it into a desired JSON respectively.
See more in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#expression-advice

Equivalent Spring #Controller with #RequestMapping("/endpoint"), but using Java EE 8 only

So I'm working on implementing Oath2 authentication to allow my app to access Intuit Quickbooks company resources (items, customers, etc).
Intuit provides working examples using Spring, but I'm developing my app using JavaEE 8 with GlassFish5.
The Spring sample app callback contoller is structured as follows:
#Controller
public class CallbackController {
...
#RequestMapping("/oauth2redirect")
public String callBackFromOAuth(#RequestParam("code") String authCode, #RequestParam("state") String state, #RequestParam(value = "realmId", required = false) String realmId, HttpSession session) {
...
//after successful validation
return "connected";
This is the redirect handler controller; which address it's configured at the intuit portal (in this case, http://localhost:8080/oauth2redirect) that will be called after user approves the app and intuit will send back authorization code to this url.
So I'm a bit stuck finding what's the equivalent Spring callback redirect handler in JavaEE.
Is a #WebServlet or #WebService needed here? But then it wouldn't integrate nicely with JSF to just return the "connected" string so that it redirects to desired page (in this case connected.xhtml).
Not looking for workarounds, but the correct and standard way of implementing this on JavaEE. If you can point me to some sample apps out there or tutorials I would greatly appreciate it.
Thanks a lot in advance!
Here's full source code for callback handler controller and the full sample app.
There is at least not a really good alternative in JSF. Yes, you could 'abuse' JSF but there are other, better standards for this and this is (almost) what Spring also does. If you read the Spring Specs , you'll see the word 'Rest' being used a lot.
Well, there is a real java standard called 'Jax-RS' that is the standardized counterpart of what you do in spring.
This provides a decent analysis of the two So Jax-RS is the way to go.
But a #WebServlet or #WebService integrate perfectly with JSF. You can store any authentication information in the session and use that from JSF. No problem at all.

How to Mock the resultset of int-jdbc:stored-proc-outbound-gateway in spring integration

I am using "int-jdbc:stored-proc-outbound-gateway" in spring integration to get the details from the 11i stored procedure by directly hitting db using the datasource.
Now, I creating a Junit for the spring integration and need to mock the response as similar to the stored procedure result but hitting actual db.
Plz tell me achieve this scenario in the spring integration ?
Thanks in Adavance .
You can mock the StoredProcExcecutor which is constructor-injected into the gateway. The gateway invokes the executeStoredProcedure(Message<?> message) method.
If you are using Java configuration, you can simply inject the mock; if you are using XML, it's a bit more tricky, but you can use a DirectFieldAccessor to replace the SPE with a mock.

Spring Integration DSL Channel Support

In the current release 1.0.2 of spring integration dsl I can see some of the basic channels are not present like ReST/HTTP, TCP/UDP, JDBC, MQTT, etc.
Just wanted to know whether this protocols/channel are in roadmap or it has been excluded deliberately.
PS: I might be sounding stupid with posted question but just wanted to know the reason.
From one side you should understand that it is enough big work to address them all. For example HTTP module is on our radar for the 1.1 release.
From other side the Spring Integration Java DSL is just an edition to the existing Spring Java & Annotation configuration, so any #Bean definition is valid there, too.
With those desired protocols you can go ahead and configure their components as #Bean and refer them from the .handle() or .from() EIP-methods.
For example:
#Bean
public MessageSource<Object> jdbcMessageSource() {
return new JdbcPollingChannelAdapter(this.dataSource, "SELECT * FROM foo");
}
#Bean
public IntegrationFlow myFlow() {
return IntegrationFlows.from(jdbcMessageSource())
.split(...)
.transform(...)
.handle(new MqttPahoMessageHandler("tcp://localhost:1883", "si-test-out"))
.get();
}

Spring Integration MessageChannel ID

I need to dynamically assign messages to MessageChannels in my Spring Integration Context.
I can do this by getting the MessageChannel bean from the context when I know the names of the MessageChannel I want.
What I need to do is programatically find the name/id of the message channel(s) that are set in my ChannelAdapter/Service.
However, the MessageChannel API does not have a getName() or getId() method associated with it.
Is there a way to find this piece of information?
Thanks in advance.
Let's take look at this task from other side!
What is the reason to get deal with such low API like channels?
Why just don't use the Router pattern on the matter?
If I understand correctly, you want to have some dinamic routing, where you determine a destination channel by some Message property.
So it might be enough just use an expression router:
<int:router input-channel="input" expression="payload.theChannel"/>

Resources