1
votes

We have a JSF2 Facelets templating implementation that provides the HTML "wrapper" for applications across the enterprise. Applications are deployed on WAS 7 and call our Facelets templating via a shared library.

We have a request to pull in left navigation via a xhtml file from an external web server - which requires going through our proxy. Since the included xhtml file will not be in the web project (it is to be managed separately and sits on a web server) - I'm trying to find a way to pull in the code across the proxy and have it render correctly. using the doesn't work because of the proxy.

I've written a simple bean that sets the proxy configurations and pulls the file's content. If I use the ResponseWriter to output the code -- the tags are not rendered in the HTML output:

<div class="aligncenter"> <a href="#"> <img src="/allweb/images/corporate/pcom/logout.gif" alt="Logout" width="138" height="30" /></a> </div>
<mycom:navBlockHeader type="custom" title="TPA Nav Pilot" />
<mycom:divStart expandableNav="true"/>
<mycom:navLink href="url" text="Link 1" />
<mycom:navSubLinkStart text="Link 2" href="#" expandable="true" expand_onload="true"/>
<mycom:navLink href="url" text="Link a"/>
<mycom:navLink href="url" text="Link b"/>
<mycom:navLink href="url" text="Link c"/>
<mycom:navSubLinkEnd/>
<mycom:navLink href="url" text="Link 3"/>
<mycom:divEnd />

I am calling the managed bean via a custom tag: <mycom:tpaLeftNav/>

The main managed bean code is:

public class LeftNavIncludeProxy extends UIComponentBase {
public void encodeBegin(FacesContext context) throws IOException{
    ResponseWriter writer = context.getResponseWriter();
    String include = "";
     System.setProperty("https.proxyHost", "proxy.mycompany.com");
     System.setProperty("https.proxyPort", "80");
     try{
         URL url = new URL("https://secure.mycompany.com/navigation/tpa/leftnav/leftnav-define.xhtml");
         BufferedReader bin = new BufferedReader(new InputStreamReader(url.openStream()));
         String line;
         while ((line = bin.readLine()) !=null){
            include += line;
         }
         include=include.toString();
         writer.write((String) include);
     }
     catch (Exception e){
         e.printStackTrace();
     }
}

}

I can easily retrieve the contents of the leftnav.xhtml file -- but when I try to write it back into the Facelets template -- it doesn't render the Facelets tags as HTML. It feels like I'm missing the render step but I'm not sure how to resolve this easily.

I'm sure this is a beginner question - my apologies if it is - I would appreciate any suggestions or thoughts on how to solve this.

Thanks. Mel

1

1 Answers

0
votes

You're writing the content of Facelets file as plain text to the response. This is indeed not going to work.

You should be using a ResourceResolver instead. Here's a kickoff example for your particular case:

public class ProxyFaceletsResourceResolver extends ResourceResolver {

    private ResourceResolver parent;

    public FaceletsResourceResolver(ResourceResolver parent) {
        this.parent = parent;
    }

    @Override
    public URL resolveUrl(String path) {
        if (path.startsWith("/proxy/")) {
            return new URL("https://secure.mycompany.com/" + path.split("/proxy/")[1]);
        }
        else {
            return parent.resolveUrl(path);
        }
    }

}

If you configure it in web.xml as follows

<context-param>
    <param-name>javax.faces.FACELETS_RESOURCE_RESOLVER</param-name>
    <param-value>com.example.ProxyFaceletsResourceResolver</param-value>
</context-param>

then you'll be able to get it by for example

<ui:include src="/proxy/navigation/tpa/leftnav/leftnav-define.xhtml" />