Content types in Generic Transformations

satya - Fri May 18 2012 11:43:29 GMT-0400 (Eastern Daylight Time)

background information on generic transformations

background information on generic transformations

satya - Fri May 18 2012 14:19:43 GMT-0400 (Eastern Daylight Time)

Karma doesn't have to be bad!! it can be good too

I have been coding my framework aspire for a long time now. My experience has been to design features so that they are horizontal or in simplistic terms reusable, especially through declarative configuration (where applicable)

For example these generic transformations is one such facility.

If I did things right in the "past" the code comes back to help me and saves me a lot of time in the "present".

satya - Fri May 18 2012 14:23:26 GMT-0400 (Eastern Daylight Time)

So what happened?

For a long time I could go to any webpage in the site you are looking at and get the contents of that page in many generic formats. Some of these are

xml
object xml
embedded xml

etc. My primary browser has always been ie. No solid reason. It came with a new pc. I didn' see a strong reason to switch so there it stayed. These transformations would look fine in the browser and I see the XML nicely formatted. No runaway lines.

Recently a few folks have been using Chrome and tell me they don't see XML but runaway sentence.

So I have suspected I am not setting the content types to "text/xml" or some such thing. It was a long time ago these transformations were coded.

After 2 years, I thought I would fix this quirky little thing.

So I went to see the java code.

satya - Fri May 18 2012 14:24:50 GMT-0400 (Eastern Daylight Time)

The class hierarchy is


IGenericTransform
  AGenericHttpTransform
     ClassicXML....
     ObjectXML...
     ...and other

satya - Fri May 18 2012 14:25:36 GMT-0400 (Eastern Daylight Time)

I see this in the Object xml transform


public class ObjectXMLGenericTransform
        extends AHttpGenericTransform
        implements IFormHandlerTransform, IhdsDOMConverter {

    // from AHttpGenericTransform
    protected String getDerivedHeaders(HttpServletRequest request)
    {
        return "Content-Type=text/html";
    }

satya - Fri May 18 2012 14:26:24 GMT-0400 (Eastern Daylight Time)

That would be wrong....so I changed it to


// from AHttpGenericTransform
    protected String getDerivedHeaders(HttpServletRequest request)
    {
        return "Content-Type=text/xml";
    }

satya - Fri May 18 2012 14:27:09 GMT-0400 (Eastern Daylight Time)

Then it occurred to me, is that the best my past self could do???

Then it occurred to me, is that the best my past self could do???

satya - Fri May 18 2012 14:27:55 GMT-0400 (Eastern Daylight Time)

So I look around and in the abstract base class


public abstract class AHttpGenericTransform 
implements IHttpGenericTransform, IInitializable
{

    private String m_headers = null;
    public AHttpGenericTransform() {
    }

    /**
     * Make sure the derived class gives this class a chance
     * @param requestName
     */
    public void initialize(String requestName)
    {
        //Read the content headers
        m_headers =   AppObjects.getValue(requestName + ".headers",null);
    }

    public String getHeaders(HttpServletRequest request)
    {
        //Give precedence to headers in the config file
        if (m_headers != null) return m_headers;

        //If the config file headers dont exist
        // use the headers from the derived classes
        String derivedHeaders = getDerivedHeaders(request);
        if (derivedHeaders != null) return derivedHeaders;

        return null;
    }
    protected String getDerivedHeaders(HttpServletRequest request)
    {
        return null;
    }
}

satya - Fri May 18 2012 14:28:25 GMT-0400 (Eastern Daylight Time)

So it wasn't bad, I did make a provision to read the headers from a config file as well

So it wasn't bad, I did make a provision to read the headers from a config file as well

satya - Fri May 18 2012 14:30:45 GMT-0400 (Eastern Daylight Time)

Here is what I could have done in the config file instead of messing with java code


#XML output
GenericTransform.Classic-xml.classname=com.ai.generictransforms.ClassicXMLGenericTransform
GenericTransform.Object-xml.classname=com.ai.generictransforms.ObjectXMLGenericTransform
GenericTransform.Embedded-xml.classname=com.ai.generictransforms.EmbeddedXMLGenericTransform

##Just add these lines
GenericTransform.Classic-xml.headers=Content-Type=text/xml
GenericTransform.Object-xml.headers=Content-Type=text/xml
GenericTransform.Embedded-xml.headers=Content-Type=text/xml

satya - Fri May 18 2012 14:32:51 GMT-0400 (Eastern Daylight Time)

Principles of Good Programming Karma


1. Rely on interfaces
2. Get implementations from config files
3. Enable behavior change at run time
4. Have your framework always provide 30% more than what you need