5.2.9. Contextual properties

ContextualPropertyManager service and plugins give you a way to access information of portal context, like site type, page name and node URI. You can also inject a property as you want.

Note

Such properties are accessed in the same way as public render parameters, but unlike public render parameters, contextual properties values cannot and should not be changed by the portlet.

In this example, you write a ContextualPropertyManager plugin that adds a parameter (called current_time), and a portlet that gets all the public contextual properties, including your one and the built-in ones.

The ContextualPropertyManager plugin project

  1. Create a new Maven project as follows:

  2. Edit pom.xml:

    
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.acme.samples</groupId>
      <artifactId>cp-plugin</artifactId>
      <version>1.0</version>
      <packaging>jar</packaging>

      <dependencies>
        <dependency>
          <groupId>org.gatein.portal</groupId>
          <artifactId>exo.portal.webui.portal</artifactId>
          <version>3.5.10.Final</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.exoplatform.kernel</groupId>
          <artifactId>exo.kernel.container</artifactId>
          <version>2.4.9-GA</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
    </project>
  3. Edit CPPlugin.java:

    package com.acme.samples;
    
    
    import java.util.Map;
    import java.util.Date;
    import org.exoplatform.portal.application.state.AbstractContextualPropertyProviderPlugin;
    import javax.xml.namespace.QName;
    import org.exoplatform.container.xml.InitParams;
    import org.exoplatform.portal.webui.application.UIPortlet;
    public class CPPlugin extends AbstractContextualPropertyProviderPlugin {
      private QName myQName;
      public CPPlugin (InitParams params) {
        super(params);
        this.myQName = new QName(namespaceURI, "current_time");
      }
      @Override
      public void getProperties(UIPortlet portletWindow, Map<QName, String[]> properties) {
        addProperty(properties, myQName, new Date(System.currentTimeMillis()).toString());
      }
    }
  4. Edit conf/portal/configuration.xml:

    
    <configuration>
      <external-component-plugins>
        <target-component>org.exoplatform.portal.application.state.ContextualPropertyManager</target-component>
        <component-plugin>
          <name>CPPlugin</name>
          <set-method>addPlugin</set-method>
          <type>com.acme.samples.CPPlugin</type>
          <priority>1</priority>
          <init-params>
            <value-param>
              <name>namespaceURI</name>
              <description>Namespace URI</description>
              <value>http://www.gatein.org/xml/ns/prp_1_0</value>
            </value-param>
          </init-params>
        </component-plugin>
      </external-component-plugins>
    </configuration>
  5. Build the project and install target/cp-plugin-1.0.jar to the lib folder of the server.

The portlet project

  1. Create a Maven project as follows:

  2. Edit pom.xml:

    
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.acme.samples</groupId>
      <artifactId>hello-portlet</artifactId>
      <version>1.0</version>
      <packaging>war</packaging>
      <build>
        <finalName>hello-portlet</finalName>
      </build>

      <dependencies>
        <dependency>
          <groupId>javax.portlet</groupId>
          <artifactId>portlet-api</artifactId>
          <version>2.0</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
    </project>
  3. Edit web.xml:

    
    <web-app>
      <display-name>hello-portlet</display-name>
    </web-app>
  4. Edit portlet.xml:

    
    <portlet-app version="2.0" xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
      <portlet>
        <portlet-name>Hello</portlet-name>
        <portlet-class>com.acme.samples.HelloPortlet</portlet-class>
        <supports>
          <mime-type>text/html</mime-type>
        </supports>
        <portlet-info>
          <title>Contextual properties</title>
        </portlet-info>
        <supported-public-render-parameter>navigation_uri</supported-public-render-parameter>
        <supported-public-render-parameter>page_name</supported-public-render-parameter>
        <supported-public-render-parameter>site_type</supported-public-render-parameter>
        <supported-public-render-parameter>site_name</supported-public-render-parameter>
        <supported-public-render-parameter>window_width</supported-public-render-parameter>
        <supported-public-render-parameter>window_height</supported-public-render-parameter>
        <supported-public-render-parameter>window_show_info_bar</supported-public-render-parameter>
        <supported-public-render-parameter>current_time</supported-public-render-parameter>
      </portlet>

      <public-render-parameter>
        <identifier>navigation_uri</identifier>
        <qname xmlns:prp='http://www.gatein.org/xml/ns/prp_1_0'>prp:navigation_uri</qname>
      </public-render-parameter>
        <public-render-parameter>
        <identifier>page_name</identifier>
        <qname xmlns:prp='http://www.gatein.org/xml/ns/prp_1_0'>prp:page_name</qname>
      </public-render-parameter>
      <public-render-parameter>
        <identifier>site_type</identifier>
        <qname xmlns:prp='http://www.gatein.org/xml/ns/prp_1_0'>prp:site_type</qname>
      </public-render-parameter>
      <public-render-parameter>
        <identifier>site_name</identifier>
        <qname xmlns:prp='http://www.gatein.org/xml/ns/prp_1_0'>prp:site_name</qname>
      </public-render-parameter>
      <public-render-parameter>
        <identifier>window_width</identifier>
        <qname xmlns:prp='http://www.gatein.org/xml/ns/prp_1_0'>prp:window_width</qname>
      </public-render-parameter>
      <public-render-parameter>
        <identifier>window_height</identifier>
        <qname xmlns:prp='http://www.gatein.org/xml/ns/prp_1_0'>prp:window_height</qname>
      </public-render-parameter>
      <public-render-parameter>
        <identifier>window_show_info_bar</identifier>
        <qname xmlns:prp='http://www.gatein.org/xml/ns/prp_1_0'>prp:window_show_info_bar</qname>
      </public-render-parameter>
      <public-render-parameter>
        <identifier>current_time</identifier>
        <qname xmlns:prp='http://www.gatein.org/xml/ns/prp_1_0'>prp:current_time</qname>
      </public-render-parameter>
    </portlet-app>
  5. Edit HelloPortlet.java by simply dispatching requests to view.jsp:

    package com.acme.samples;
    
    
    import java.io.IOException;
    import java.util.Date;
    import java.io.PrintWriter;
    import javax.portlet.GenericPortlet;
    import javax.portlet.PortletRequestDispatcher;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;
    import javax.portlet.PortletException;
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    public class HelloPortlet extends GenericPortlet {
      @Override
      public void doView(RenderRequest request, RenderResponse response) throws IOException, PortletException {
        PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher("/jsp/view.jsp");
        dispatcher.include(request, response);
      }
    }
  6. Edit view.jsp:

    <%
      String navigation_uri = request.getParameter("navigation_uri");
      String page_name = request.getParameter("page_name");
      String site_type = request.getParameter("site_type");
      String site_name = request.getParameter("site_name");
      String window_width = request.getParameter("window_width");
      String window_height = request.getParameter("window_height");
      String window_show_info_bar = request.getParameter("window_show_info_bar");
      String current_time = request.getParameter("current_time");
    %>
    
    <style>
      #contextual_properties td:last-child {font-style: italic}
      #contextual_properties tr, td {padding: 5px}
    </style>
    <table border="1" id="contextual_properties" style="width: auto; border-spacing: 5px">
      <tr><td>navigation_uri</td><td><%=navigation_uri%></td></tr>
      <tr><td>page_name</td><td><%=page_name%></td></tr>
      <tr><td>site_type</td><td><%=site_type%></td></tr>
      <tr><td>site_name</td><td><%=site_name%></td></tr>
      <tr><td>window_width</td><td><%=window_width%></td></tr>
      <tr><td>window_height</td><td><%=window_height%></td></tr>
      <tr><td>window_show_info_bar</td><td><%=window_show_info_bar%></td></tr>
      <tr><td>current_time</td><td><%=current_time%></td></tr>
    </table>

After deployment, add the portlet to a page and test:

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus