Attachment 'ResponseHeaderTransformer.java'

Download

   1 // $Id$
   2 package org.apache.cocoon.transformation;
   3 
   4 import java.io.*;
   5 import java.util.*;
   6 
   7 import org.apache.avalon.framework.parameters.Parameters;
   8 import org.apache.cocoon.environment.ObjectModelHelper;
   9 import org.apache.cocoon.environment.Response;
  10 import org.apache.cocoon.ProcessingException;
  11 import org.apache.cocoon.environment.SourceResolver;
  12 import org.apache.cocoon.transformation.AbstractTransformer;
  13 
  14 import org.xml.sax.Attributes;
  15 import org.xml.sax.ContentHandler;
  16 import org.xml.sax.SAXException;
  17 
  18 /**
  19  * Set HTTP response headers from an XML document.
  20  * Declaration in sitemap:
  21  * <pre>
  22  *  &lt;map:transformers default="xslt"&gt;
  23  *    &lt;map:transformer name="response-headers" src="org.apache.cocoon.transformation.ResponseHeaderTransformer"/&gt;
  24  *  &lt;/map:transformers&gt;
  25  * </pre>
  26  * Usage in pipeline:
  27  * <pre>
  28  *  &lt;map:transform type="response-headers"/&gt;
  29  * </pre>
  30  * It will look for elements in the "http://apache.org/cocoon/responseHeader/1.0"
  31  * namespace. The following elements are recognized (assuming xmlns:header="http://apache.org/cocoon/responseHeader/1.0"):
  32  * <dl>
  33  * <dt>&lt;header:set name="Xyz" value="some value"/&gt;</dt>
  34  * <dd>Sets the header "Xyz" to "some value"</dd>
  35  * </dl>
  36  */
  37 public class ResponseHeaderTransformer
  38   extends AbstractTransformer
  39 {
  40 
  41   private static final String NAMESPACE = "http://apache.org/cocoon/responseHeader/1.0";
  42   private static final String SET_ELEMENT = "set";
  43   private Response response;
  44 
  45   public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
  46     throws ProcessingException, SAXException, IOException
  47   {
  48     response = ObjectModelHelper.getResponse(objectModel);
  49   }
  50 
  51   public void startElement(String namespaceURI, String lName, String qName, Attributes attributes)
  52   throws SAXException
  53   {
  54     if (namespaceURI.equals(NAMESPACE) && lName.equals(SET_ELEMENT)) {
  55       String name = attributes.getValue("name");
  56       String value = attributes.getValue("value");
  57       response.setHeader(name, value);
  58     } else {
  59       super.startElement(namespaceURI, lName, qName, attributes);
  60     }
  61   }
  62 
  63   public void endElement(String namespaceURI, String lName, String qName)
  64   throws SAXException
  65   {
  66     if (namespaceURI.equals(NAMESPACE) && lName.equals(SET_ELEMENT)) {
  67     } else {
  68       super.endElement(namespaceURI, lName, qName);
  69     }
  70   }
  71 
  72   public void characters(char[] c, int start, int len)
  73   throws SAXException
  74   {
  75     super.characters(c, start, len);
  76   }
  77 
  78 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.