Using a SOAPHandler with spring-boot SOAP web service - java

I have created an application by following the guide on
http://spring.io/guides/gs/producing-web-service/
Executing the webservice 'getCountry' works fine but now I need to intercept the SOAP message somehow before getCountry is executed.
I created a class that implements 'SOAPHandler' but somehow I have to tell spring-boot to use this handler before passing the request to getCountry.
Any idea how to do that?

you need to implement an interceptor like:
public class YourClientInterceptor implements ClientInterceptor{
#Override
public boolean handleRequest(MessageContext messageContext)
{
//here you get your request before it is sending
messageContext.getRequest()
...
return true;
}
}
and when you create your WebServiceTemplate you do:
ClientInterceptor[] interceptors = {new YourClientInterceptor ()};
yourWebServiceTemplate.setInterceptors(interceptors);
They work similar with handler.

Related

Spring WebFlux - Unable to return HTTP response in HandlerAdapter

I'm trying to recreate HttpServletResponse#getWriter functionality in Spring WebFlux without success.
In Spring MVC, I would extend HandlerInterceptorAdapter, use mentioned getWriter for writing target response and return false from preHandle method. I'm trying to use the following code to recreate the same functionality. See following excerpts to see what I've tried so far without success.
#Component
public class CustomInterceptor extends RequestMappingHandlerAdapter
#Override
public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
exchange.getResponse().writeWith(s -> Mono.just(new SomeDto()));
// creates Internal Server Error, custom WebExceptionHandler not called
return Mono.error(new RuntimeException());
// creates Internal Server Error, custom WebExceptionHandler not called
throw new RuntimeException();
// ignores response write
return super.handle(exchange, handler);
}
}
I've also tried to use a WebFilter, there is a possibility to change the response, however it's not possible to access HandlerMethod object, which is needed for further processing.
Any idea how to do this using Spring WebFlux? All ideas are welcome. Thank you.
It is possible to handle the request/response in the new reactive WebFilter, eg.
#Component
public class SecurityWebFilter implements WebFilter{
#Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
if(!exchange.getRequest().getQueryParams().containsKey("user")){
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
}
return chain.filter(exchange);
}
}
Check my complete webfilter sample.

CXF-Proxy Client and #Suspended AsyncResponse

I recently learned that with JAX-RS 2.0 long running service endpoints can make use of the #Suspended annotation and AsyncResponse to free resources for incoming requests while the actual work is done in the background. All client examples - at least the ones I found so far - are either calling such endpoints directly (plain http-call) or make use of the JAX-RS client API. However I was not able to figure out how to use this with the proxy-based API.
Given a REST endpoint that uses #Suspended:
public interface HeavyLiftingService {
#GET
#Path("/heavylifting")
public void heavyLifting(#Suspended final AsyncResponse aResponse);
}
its implementation using Spring:
#Component
public class HeavyLiftingServiceImpl implements HeavyLiftingService {
#Override
#Async
public void heavyLifting(#Suspended final AsyncResponse aResponse) {
final Result result = doHeavyLifting();
aResponse.resume(result);
}
}
And a proxy-based client, that wants to obtain the result:
HeavyLiftingService proxy = JAXRSClientFactory.create("https://some-server.xyz", HeavyLiftingService.class);
proxy.heavyLifting(null); // what to put in here?
Result result = null; // how can I get the result?
Obviously there are two problems:
What do I need to provide to the heavyLifting method as value for the AsyncResponse parameter?
How can I get the result as the return type of methods using #Suspended has to be void?
And another question is how exceptions in the service method are handled. Will an exception automatically resume the response and return a corresponding error status?

Calling webservice from another webservice in same war - apache cxf

I have two webs services or endpoints having one method each and each method is mapped with one URL. I am calling one webservice URL from REST client and in this method i want to call method in another web service which is mapped to URL. How can this be achieved in apache CXf ?
I tried using HttpClient to call another webservice from one but I am getting 404, if I use complete URL and getting 302 code but no response if I use relative URL. what might be issue and what is correct approach ?
You can try to call directly the other controller without httprequest.
for example you have the two next controllers with all annotations you need
#RestController
#RequestMapping("/a")
public class A{
#RequestMapping(...)
public void toCall(){
//your code
}
}
you want to call the method toCall of controller A from controller B
#RestController
#RequestMapping("/b")
public class B{
#RequestMapping(...)
public void method(){
A a = new A();
a.toCall();
}
}

Calling a soap service through spring integration along with http headers

I am working on spring integration for calling a soap service. I am able to call a soap service using outbound gateway successfully. Now I need to call the same soap service, with same request and now I need add some parameters in http headers.Can anybody help me out in achieving this task . Thank you very much in advance.
You can achieve with request-callback injection to the <int-ws:outbound-gateway>:
public class AddHeaderWebServiceMessageCallback implements WebServiceMessageCallback {
public void doWithMessage(WebServiceMessage message) {
CommonsHttpConnection connection = (CommonsHttpConnection) context.getConnection();
PostMethod postMethod = connection.getPostMethod();
postMethod.addRequestHeader( "foo", "bar" );
}
}
Or... If you need to do that dinamically for each requestMessage you should overcome that with the custom SoapHeaderMapper. Where you can extract CommonsHttpConnection from the TransportContextHolder.getTransportContext().getConnection()

Save Web Service variable into MuleSoft Message

I have an http endpoint redirecting to a REST Java web service.
I am receiving a application/x-www-form-urlencoded request with some attributes embedded within the body of the request.
Inside the web service I would like to update the mule message status with those attributes.
Since RequestContext.getEventContext() is now deprecated and Doc says to implement Callable instead, however seems not working to me.The onCall method is never invoked.
Any idea ?
Below my code:
enter code here
#Path("/restClass")
public class HelloREST implements Callable{
private String industry;
private String lob;
private String nuixlegalentity;
private org.apache.log4j.Logger log = Logger.getLogger(LoadClass.class);
#POST
#Path("/setPayload")
#Consumes("application/x-www-form-urlencoded")
public void setMessage(#FormParam("industry") String industryParam, #FormParam("lob") String lobParam,#FormParam("nuixlegalentity") String nuixlegalentityParam){
log.debug("**************INSIDE SETMESSAGE******************");
industry=industryParam;
lob=lobParam;
nuixlegalentity=nuixlegalentityParam;
}
#Override
public Object onCall(MuleEventContext eventContext) throws Exception{
log.debug("**************INSIDE ONCALL******************");
eventContext.getSession().setProperty("industry","industry");
eventContext.getSession().setProperty("lob",lob);
eventContext.getSession().setProperty("nuixlegalentity",nuixlegalentity);
return eventContext.getMessage();
}
}
}
I assume you're using this class as a resource with the Jersey transport. In that case, Mule will call the JAX-RS annotated methods, based on the incoming request, and thus will not call onCall. Therefore implementing Callable is of no use.
Using RequestContext.getEventContext() is the only way possible to get the EventContext in a Jersey-handled resource.
To this date, MuleSoft hasn't provided a workable replacement for cases like this one so, even if RequestContext.getEventContext() is deprecated, you unfortunately have no other choice than using it.

Categories