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
Related
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.
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.
I have a general question. We are upgrading some old code to v4.1.3 of spring-integration. Let's say we have a service activator that receives a domain object of type Foo, and returns the same object on the output channel (after changing some properties of that object).
Our existing code has signatures like below. Is it the right convention? If I don't need the headers, can I return my payload directly and will the framework wrap it into a Message?:
public Message<Foo> computeValue(Foo payload, #Headers Map<String, Object> headerMap) {
//...
return MessageBuilder.withPayload(payload).copyHeaders(headerMap).build();
}
Yes, you don't need do that. More over that is pointless because in case of Service Activator the Framework populates request headers to the reply message independently of your effort.
So, consider do not care about headers at all if you don't need to do some specific logic on the matter.
Im building a php json(p) rpc server and currently i am stuck with not finding sufficient information on how to handle/respond if a jsonp call throws an error inside the called function/method. With json rpc 2.0 its well documented - with jsonp its not. how to return errors from the application level of the service called?
Suppose a call like /service.php?var=test&callback=jsonpCallback is requested. in a positive case the result would be something like jsonpCallback({"result": "test"}). Suppose on the application level an exception is thrown? what do i return and how?
Do i return an error object inside the callback? or just a json (custom) error object as string? If there is something like an jsonp error object convention where to find it?
Does make sense to allow for a custom error callback in the GET request which can be called like: /service.php?var=test&callback=jsonpCallback&errorCallback=jsonpErrorCallback
Any advice highly appreciated.
What would be the easiest way to setup a request/response interceptor in ServiceStack that would execute for a particular service?
A request filter (IHasRequestFilter) works fine but a response filter (IHasResponseFilter) is not triggered if the service returns non 2xx status code. I need to retrieve the status code returned by the method as well as the response DTO (if any).
A custom ServiceRunner and overriding the OnBeforeExecute and OnAfterExecute methods seems to work fine but I find it pretty intrusive as the service runner need to be replaced for the entire application and I couldn't find a way clean way to isolate per functionality the tasks that need to be executed in those methods.
Is there some extension point in ServiceStack that I am missing that would allow me to execute some code before each service method and after each service method? A plugin would be ideal but how can I subscribe to some fictitious BeforeExecute and AfterExecute methods that would allow me to run some custom code?
UPDATE:
Just after posting the question I found out that global response filters are executed no matter what status code is returned by the service which is exactly what I needed. So one last question: Is it possible to retrieve the service type that will handle the request in a request filter? I need to check whether this service is decorated by some custom marker attribute.
I have found out a solution to my question about how to retrieve the service type in a custom request/response filter:
appHost.RequestFilters.Add((req, res, requestDto) =>
{
var metadata = EndpointHost.Metadata;
Type serviceType = metadata.GetServiceTypeByRequest(requestDto.GetType());
...
}
A custom ServiceRunner and overriding the OnBeforeExecute and OnAfterExecute methods seems to work fine but I find it pretty intrusive as the service runner need to be replaced for the entire application
Quick note, you can opt-in and choose only what requests should use a custom service runner, e.g:
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(
ActionContext actionContext)
{
return useCustomRunner(actionContext.RequestType)
? new MyServiceRunner<TRequest>(this, actionContext)
: base.CreateServiceRunner<TRequest>(actionContext);
}
IHttpRequest has OperationName. I think thats what you are after.