Source code


package com.indent.portals.test;

import java.io.IOException;

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

/**
 * @author Satya
 * 
 * 
 * Preferences needed:
 * 
 * 1. jsppage
 * **********
 * Points to a url like /indent/iframe.jsp
 * This should know how to draw an iframe
 * this page is tightly coupled to this portlet
 * see the embedded attributes in the request
 * 
 * 2.src
 * *******
 * Source of the frame
 * make sure you end it with something like ?a=b
 * Additional parameters will be appended
 * 
 * 3.userAttributeNames
 * ********************
 * Additional user attributes you want to retrieve such as email
 * Not made use of yet 
 * 
 * Attribute Names in the render request for the jsp page
 * ********************************************************
 * indent.iframe.src
 * indent.iframe.userattributenames
 * 
 */
public class IndentIFramePortlet extends GenericPortlet {
   
   public static String PREF_SRC="src";
   public static String PREF_USER_ATTRIBUTE_NAMES="userAttributeNames";

   public void processAction(ActionRequest req, ActionResponse res)
   throws IOException, PortletException 
   {
      PortletPreferences prefs=req.getPreferences();
       boolean editOK;
       String errorMsg = null;
       
       //Set the src preference
       String iframeSrc = req.getParameter("srcTextField");
       if (!isValid(iframeSrc))
       {
          System.out.println("error: you have to have a text field");
          editOK = false;
           res.setRenderParameter("error","you have to have a src field");
           return;
       }
       prefs.setValue("src",iframeSrc);
       
       //Set the html attributes
       String attributeNames = req.getParameter("userAttributeNamesTextField");
       attributeNames = attributeNames.trim();
       
       prefs.setValue("userAttributeNames",attributeNames);
       
       try {
          System.out.println("Saving preferences");
          prefs.store();
          editOK = true;
       }
       catch (ValidatorException ex) {
          editOK = false;
          errorMsg = ex.getMessage();
       }
       if (editOK) {
           res.setPortletMode(PortletMode.VIEW);
       }
       else {
           res.setRenderParameter("error","Failed to save preferences");
       }
   }

   private boolean isValid(String s)
   {
      if (s==null) return false;
      if (s.trim().equals(""))
      {
         //empty string
         return false;
      }
      return true;
   }
   public void doView(RenderRequest req, RenderResponse res)
   throws IOException, PortletException 
   {
      res.setContentType("text/html");
      
      //Read
      PortletPreferences prefs = req.getPreferences();
      String jsppage = prefs.getValue("jsppage",null);
      String src = prefs.getValue("src",null);
      String userAttributeNames = prefs.getValue("userAttributeNames",null);
      System.out.println("jsppage:" + jsppage);
      if (jsppage == null)
      {
         throw new PortletException("jsp page name is needed");
      }
      if (src == null)
      {
         throw new PortletException("src attribute for an iframe is needed");
      }
      if (userAttributeNames == null)
      {
         userAttributeNames = "";
      }
      req.setAttribute("src",src);
      req.setAttribute("userattributenames",userAttributeNames);
      includeJsp(jsppage,req,res);
   }
   public void includeJsp(String jsppage,RenderRequest req, RenderResponse res)
   throws IOException,PortletException
   {
      PortletRequestDispatcher rd =
         getPortletContext().getRequestDispatcher(jsppage);
      rd.include(req,res);
   }
   public void doEdit(RenderRequest req, RenderResponse res)
   throws IOException, PortletException
   {
      res.setContentType("text/html");
      PortletPreferences prefs = req.getPreferences();
      String src = prefs.getValue(IndentIFramePortlet.PREF_SRC,null);
      req.setAttribute("src",src);
      
      String attributes = prefs.getValue(
           IndentIFramePortlet.PREF_USER_ATTRIBUTE_NAMES,null);
      req.setAttribute(
          IndentIFramePortlet.PREF_USER_ATTRIBUTE_NAMES,attributes);
      
      includeJsp("/indent/iframe-edit.jsp",req,res);
   }
   
}//eof-class

Portlet definition



   <portlet>
      <portlet-name>IndentIFramePorlet2</portlet-name>
      <display-name>IndentIFramePorlet2DisplayName</display-name>
      <portlet-class>com.indent.portals.test.IndentIFramePortlet</portlet-class>
      <expiration-cache>0</expiration-cache>
      <supports>
         <mime-type>text/html</mime-type>
         <portlet-mode>edit</portlet-mode>
      </supports>
      <portlet-info>
         <title>Indent IFrame Portlet 2 Title</title>
         <short-title>Indent IFrame Portlet 2 Short Title</short-title>
         <keywords>Indent IFrame Portlet 2</keywords>
      </portlet-info>
      <portlet-preferences>
         <preference>
            <name>jsppage</name>
            <value>/indent/iframe.jsp</value>
         </preference>
         <preference>
            <name>src</name>
            <value>http://www.knowledgefolders.com/akc/test?a=b</value>
         </preference>
         <preference>
            <name>userAttributeNames</name>
            <value>abc,def</value>
         </preference>
      </portlet-preferences>
      <security-role-ref>
         <role-name>power-user</role-name>
      </security-role-ref>
      <security-role-ref>
         <role-name>user</role-name>
      </security-role-ref>
   </portlet>

View jsp code



<%@ include file="/html/portlet/init.jsp" %>

<%
String userId = user.getUserId();
String groupId = user.getGroup().getGroupId() ;
String companyId = user.getActualCompanyId() ;
String src = (String)renderRequest.getAttribute("src");
String attributeNames = 
  (String)renderRequest.getAttribute("userattributenames");
attributeNames = attributeNames.trim();

if (!(attributeNames.equals("")))
{
   List results = RoleLocalServiceUtil.getUserRoles(user.getUserId());
   Role role = (Role)results.get(0);
   String roleid = role.getRoleId();

   src += "&profile_user_id=" + userId;
   src += "&profile_group_id=" + groupId;
   src += "&profile_company_id=" + companyId;
   src += "&role=" + roleid;
}
%>

<iframe src="<%=src%>" width="100%" height="300" 
    hspace="0" scrolling="auto" vspace="0" border="0" 
   bordercolor="#000000">
</iframe>

Edit jsp code



<%@ include file="/html/portlet/init.jsp" %>
<!--
***********************************************
* iframe-edit.jsp
***********************************************
1. edit the property called "src",userAttributeNames
-->
<%
String src = (String)renderRequest.getAttribute("src");
String userAttributeNames = 
   (String)renderRequest.getAttribute("userAttributeNames");
String error = (String)renderRequest.getParameter("error");

%>
<h3>Edit Preferences for JSP Portlet</h3>

<form method="post" action="<portlet:actionURL/>">

<p>Specify src for iframe to display output
<p>
<input type="text" name="srcTextField" value="<%=src%>">

<p>Specify userAttributeNames for iframe: a comma separated list
<p>
<input type="text" name="userAttributeNamesTextField" 
     value="<%=userAttributeNames%>">

<p>
<input type="submit" value="Submit" name="SubmitButton"/>
</form>

<% if (error != null) 
{
%>
<h4>Error found</h4>
<p><%=error%>
<%}%>