Groovy Bridge

## Jetspeed Groovy Portlet with Header Phase Support Guide

Jetspeed-2 provides an interface for a portlet to be able to support the pre-286 header phase. If you want your portlet to support this, just use GroovyPortletHeaderPhaseSupport class instead of GroovyPortlet, and implement SupportsHeaderPhase interface.

### 1. The Portlet Class supporting header phase

Create the file HelloGroovyHeaderSupport.groovy in a directory called groovy-simplest/WEB-INF/classes:

    import javax.portlet.GenericPortlet;
    import javax.portlet.PortletContext;
    import javax.portlet.PortletRequestDispatcher;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.PortletPreferences;
    import javax.portlet.ValidatorException;

    import org.apache.jetspeed.headerresource.HeaderResource;
    import org.apache.jetspeed.portlet.PortletHeaderRequest;
    import org.apache.jetspeed.portlet.PortletHeaderResponse;
    import org.apache.jetspeed.portlet.SupportsHeaderPhase;

    public class HelloGroovyHeaderSupport extends GenericPortlet implements SupportsHeaderPhase
    {
        public void doHeader(PortletHeaderRequest request, PortletHeaderResponse response)
        {
            // use header resource component to ensure header logic is included only once
            HeaderResource headerResource = response.getHeaderResource();
            headerResource.dojoEnable();

            headerResource.dojoAddCoreLibraryRequire( "dojo.lang.*" );
            headerResource.dojoAddCoreLibraryRequire( "dojo.event.*" );
            headerResource.dojoAddCoreLibraryRequire( "dojo.io.*" );
            headerResource.dojoAddCoreLibraryRequire( "dojo.widget.*" );
            headerResource.dojoAddCoreLibraryRequire( "dojo.widget.Button" );
         }

        public void doView(RenderRequest request, RenderResponse response)
        {
            response.setContentType("text/html");

            // Let's put a dojo widget button for simplicity here.
            response.getWriter().println """
                <button widgetId="helloGroovyButton" dojoType="Button" onclick="alert('Hello, Groovy');">
                Say Hello
                </button>
            """
        }

    }

For simplicity, in the above example, the doView() method just writes a simple HTML fragment to show DOJO button widget.

### 2. The portlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <portlet-app id="velocitysimplest" version="1.0">
      <portlet id="HelloGroovyHeaderSupport">
        <portlet-name>HelloGroovyHeaderSupport</portlet-name>
        <display-name>Hello Groovy with Header Support Display Name</display-name>
         <portlet-class>org.apache.portals.bridges.groovy.GroovyPortletHeaderPhaseSupport</portlet-class>
        <init-param>
          <name>script-source</name>
          <!-- Note: You can set script source in three ways.
                     The first is to use relative path uri,
                     the second is to use file: url,
                     and the last is to classpath: uri -->
          <!--
          <value>/WEB-INF/groovy/HelloGroovy.groovy</value>
          <value>file:/C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/demo/WEB-INF/groovy/HelloGroovy.groovy</value>
          -->
          <value>classpath:HelloGroovyHeaderSupport.groovy</value>
        </init-param>
        <!-- If auto-refresh is true, then a modification of script source applies instantly. -->
        <init-param>
          <name>auto-refresh</name>
          <value>true</value>
        </init-param>
        <supports>
          <mime-type>text/html</mime-type>
          <portlet-mode>VIEW</portlet-mode>
        </supports>
        <supported-locale>en</supported-locale>
        <portlet-info>
          <title>Hello Groovy with Header Support Title</title>
          <short-title>Hello Groovy with Header Support Short Title</short-title>
        </portlet-info>
      </portlet>
    </portlet-app>

The <portlet-class> was replaced with 'org.apache.portals.bridges.groovy.GroovyPortletHeaderPhaseSupport' instead of 'org.apache.portals.bridges.groovy.GroovyPortlet' to support header phase.