I'm working Spring Integration application that works as a proxy to a bunch of third-party REST services.
@Bean
public IntegrationFlow proxyFlow() {
var parser = new SpelExpressionParser();
return IntegrationFlows
.from(Http.inboundGateway("/**")
.errorChannel("errorChannel")
.replyChannel("inboundReplyChannel")
.requestMapping(m -> m.methods(GET, PUT, POST, PATCH, DELETE)
.consumes("*/*"))
.mappedRequestHeaders("X-*", "HTTP_REQUEST_HEADERS")
.mappedResponseHeaders("X-*", "HTTP_RESPONSE_HEADERS")
.headerExpression("http_requestAttributes", "#requestAttributes")
.headerExpression("http_requestParams", "#requestParams")
.headerExpression("http_requestUri", "#requestAttributes.request.requestURI"))
// use business rules to populate headers for the outbound request
.handle(urlTranslatorEndpoint)
.handle(Http.outboundGateway(new SpelExpressionParser().parseExpression("headers.http_requestUrl"))
.httpMethodExpression(parser.parseExpression("headers.http_requestMethod"))
.mappedRequestHeaders("X-*", "HTTP_REQUEST_HEADERS")
.expectedResponseType(String.class)
.errorHandler(outboundErrorHandler)
.requestFactory(clientHttpRequestFactory))
.channel("inboundReplyChannel")
.get();
}
In a nutshell :
- the inbound URL is received using an inbound gateway
- converted using somes business rules
- the target service is reached through an outbound gateway and the response is sent back to the replychannel of the inbound gateway
Up to now, all target services return a response in the form of a JSON String, so the code above works fine.
I am considering adding other services to the list of target services. Those services return HTML (CSS and javascript included) pages.
Using expectedReponse(String.class)
in the code above, what is sent to the reply channel is the raw uninterpreted HTML response, for instance:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
<link rel="stylesheet" type="text/css" href="index.css" />
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script src="./swagger-initializer.js" charset="UTF-8"> </script>
</body>
</html>
How do I proceed to request all the necessary data (the html page and both js and css files) and serve all of it to the replychannel in one go? Is even Spring Integration HTTP a right tool for this purpose?