0

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?

Marc Tarin
  • 3,109
  • 17
  • 49

1 Answers1

0

So, is your idea to proxy all those HTML resources as well? Maybe you can replace all those urls in this reply HTML to proxy those resources as well? This way you would give a reply with an HTML with resources links to your proxy and when that URL is called your proxy would do that trick to propagate it down to the respective service for that resource.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • All resources would indeed be proxied from the same in-house webservice. But how about interpreting the HTML page and not returning it as a raw string as illustrated above? – Marc Tarin Jun 09 '23 at 12:28
  • You probably need to set a `contentType` header into an `text/html` for the reply message. Since you use there `.expectedResponseType(String.class)` the content type is sent to `text/plain`. An `enrichHeaders()` should do the trick after that your's `.handle(Http.)` – Artem Bilan Jun 09 '23 at 13:55