Diego Magalhães
Diego Magalhães

Reputation: 725

Write a custom HtmlResponseWriter JSF

For many internal problems that doesn't count now, We have a Servlet filter that changes all outcome that's application/xhtml+xml and rewrite to text/html;charset=UTF-8 so even using the facelets it'll work with no problem with IE 6.0.

My question is on the HtmlResponseWriter, which is the component responsible for the rendering. Is it possible to extend it and override its methods so we accomplish the desired effect of the filter?

Thanks in advance.

Upvotes: 1

Views: 1209

Answers (1)

Zombies
Zombies

Reputation: 25862

Yes, we have extended JSF (actually Oracle ADF) components in order to meet special requirements that could not be done out of the box. You will need to get all of the source files of those renders and do a recursive search for the offending HTML you want removed, the application/xhtml+xml. This is just to make sure it is in fact inside the HtmlResponseWriter class. JSF component frameworks can be complex so you never know, there may be other instances where this header is rendered.

Since the HtmlResponseWriter isn't declared final like some components are, you can just extend this, and override the method where application/xhtml+xml is being printed and register it in faces-config.xml. The only obstacles to this is if there are private variables declared inside of HtmlResponseWriter being referenced in the method that you need to override. If that is the case you will either not be able to reference them in your reimplementation or you will have to completely re-build a new HtmlResponseWriter class (by extending the ResponseWriter and mimicking each method and instance variable). The benefit of extending the HTMLResponseWriter is that you will pick up any changes (from JSF updates) to it automatically (except in the overridden method of course).

Update: This is what I did for my faces-config.xml, but it is using Oracle ADF:

<?xml version="1.0" encoding="windows-1252"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee">
  <application>
    <default-render-kit-id>oracle.adf.rich</default-render-kit-id>
  </application>
  <render-kit>
    <render-kit-id>oracle.adf.rich</render-kit-id>
    <renderer>
      <component-family>org.apache.myfaces.trinidad.Input</component-family>
      <renderer-type>oracle.adf.rich.Text</renderer-type>
      <renderer-class>com.company.jsf.renders.text.CustomRenderer</renderer-class>
    </renderer>
  </render-kit>
</faces-config>

Upvotes: 1

Related Questions