I have a page component that most of the time adds attribute(s) to the hstRequest and passes on to the freemarker templates for rendering.
However, in some cases, I need the component to proxy an entire response and send the other response instead (for the entire page). Instead, i get the proxied response inlined in the middle of my page. How should I solve this cleanly?
public class MyComponent extends BaseHstComponent {
@Override
public void doBeforeRender(HstRequest hstRequest, HstResponse hstResponse) throws HstComponentException {
MyData data = dao.fetch(hstRequest.getPathInfo());
if (shouldProxy(data) ) {
// proper steps
}
}
}
First, configure the reverse-proxy servlet [1] independently from HST things. For example, /site/rproxy/** should be handled by the reverse-proxy servlet. i.e, /site/rproxy/example/a/b/c.html → http://www.example.com/a/b/c.html.
Second, you should be able to build the proxy request path (e.g, “/rproxy/a/b/c.html”) in your component.
Third, change the renderPath dynamically in your #doBeforeRender(). e.g, hstResponse.setRenderPath("/rproxy/a/b/c.html").
[INFO] [talledLocalContainer] 06.02.2019 15:58:54 WARN http-nio-8080-exec-6 [ResolvedSiteMapItemImpl.resolveComponentConfiguration:191] ResolvedSiteMapItemImpl cannot be created correctly, because the component configuration id hst:pages/pagenotfound cannot be found.
That’s because HstFilter is still taking the control when you make a request at /reverse-proxyservlet/…
As the request path should not be handled by HstFilter like normal HST sitemap items, and the reverse proxy servlet has nothing to do with HST-2, you should configure the path to be bypassed by HstFilter:
copy /hst:hst/hst:configurations/hst:default/hst:sitemap/binaries to /hst:hst/hst:configurations/hst:default/hst:sitemap/reverse-proxyservlet (including children).
remove hst:refId.
Then the reverse proxy servlet will work independently.
I have the proxy solution working. And I have the component setting the renderPath - but that only inlines the (proxied) response in the middle of the page via hstResponse.setRenderPath(proxyPath)
Is there a way to not include the header/footer of the page? I want the response to ONLY have the output of the proxy, kind of like how a siteMapItemHandler can change the resolvedSiteMapItem.
Normally, we would handle this in a SiteMapItemHandler to change the resolvedSiteMapItem - but I wouldn’t know if I want to change the siteMapItem until the component has called an API (to determine if I proxy or not). If I don’t proxy, the API call returns an object that the component reacts to.
By “the API”, do you mean the backend web service you wanted to do reverse-proxying in an HstComponent?
If so, what does the response look like? JSON, XML or HTML?