Sunday, March 16, 2014

Two-Phase rendering support in IBM WebSphere Portal 7.0 and 8.0 with JSR 286 Portlets

When you work on portlets there are certain challenges when it comes to setting up cookies, http headers or changing the page title dynamically. IBM WebSphere Portal with JSR-286 portlets can solve this problem by enabling Two-Phase Rendering.

By default, two-phase rendering is turned off. To enable two-phase rendering for a portlet, you must update the portlet.xml deployment descriptor for the portlet.

Add the following entry to the file:

<portlet>
...
<container-runtime-option>
<name>javax.portlet.renderHeaders</name>
<value>true</value>
</container-runtime-option>
</portlet>

Changing or setting up Page Title of a JSR-286 Portlet dynamically

To write into the HTML head section of your JSR 286 portlet, for example, to change a page title, use the addProperty method on the PortletResponse.

Invoke the addProperty method to modify the HTML head section.

PortletReponse.addProperty(String key, org.w3c.dom.Element element)

Note: When modifying the HTML head section, you must invoke the addProperty method before the response headers are committed. This should occur no later than during the render headers sub phase of the render lifecycle phase.

Below is the example to explain this.
protected void doHeaders(RenderRequest request, RenderResponse response)
{
    Element title = response.createElement("title");
    title.setTextContent("My Portal Page Title");
    response.addProperty(MimeResponse.MARKUP_HEAD_ELEMENT, title);

}

Wednesday, March 12, 2014

Available implicit objects Liferay JSP page

On a normal JSP page, some objects are implicitly available. In addition, we can get several others in Liferay using the taglibs. But we don't know all.

Lets see about normal JSP first:

These objects are created by the container automatically and the container makes them available to us. Since these objects are created automatically by the container and are accessed using standard variables; and that is why, they are called implicit objects. They are parsed by the container. They are available only within the jspService method and not in any declaration.

1. request (javax.servlet.ServletRequest)
2. response (javax.servlet.ServletResponse)
3. out (javax.servlet.jsp.JspWriter)
4. pageContext (javax.servlet.jsp.PageContext)
5. session (javax.servlet.http.HttpS)
6. application (javax.servlet.ServletContext)
7. config (javax.servlet.ServletConfig)
8. page (java.lang.Object)
9. exception (java.lang.Throwable)

Now, lets eleborate the liferay context:

The following statements will give 14 default objects :

1. <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
2. <portlet:defineObjects />
3. actionRequest (javax.portlet.ActionRequest)
4. actionResponse (javax.portlet.ActionResponse)
5. eventRequest (javax.portlet.EventRequest)
6. eventResponse, (javax.portlet.EventResponse)
7. portletConfig, (javax.portlet.PortletConfig)
8. portletName, (java.lang.String portletName ;
9. portletPreferences, (javax.portlet.PortletPreferences)
10. portletPreferencesValues, (java.util.Map)
11. portletSession, (javax.portlet.PortletSession)
12. portletSessionScope, (java.util.Map)
13. renderRequest, (javax.portlet.RenderRequest)
14. renderResponse, (javax.portlet.RenderResponse)
15. resourceRequest, (javax.portlet.ResourceRequest)
16. resourceResponse (javax.portlet.ResourceResponse)

And the following statements will give 18 default objects :

<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<liferay-theme:defineObjects />


and the type of each object as follows :

1. themeDisplay (com.liferay.portal.theme.ThemeDisplay)
2. company (com.liferay.portal.model.Company)
3. account (com.liferay.portal.model.Account)
4. user (com.liferay.portal.model.User)
5. realUser (com.liferay.portal.model.User)
6. contact (com.liferay.portal.model.Contact)
7. layout (com.liferay.portal.model.Layout)
8. layouts (java.util.List)
9. plid (java.lang.Long)
10. layoutTypePortlet (com.liferay.portal.model.LayoutTypePortlet)
11. scopeGroupId (java.lang.Long)
12. permissionChecker (com.liferay.portal.security.permission.PermissionChecker)
13. locale (java.util.Locale)
14. timeZone (java.util.TimeZone)
15. theme (com.liferay.portal.model.Theme)
16. colorScheme (com.liferay.portal.model.ColorScheme)
17. portletDisplay (com.liferay.portal.theme.PortletDisplay)
18. portletGroupId (java.lang.Long)

These default objects can be acquired using pageContext which is an implicit object of JSP. Use the following code for getting default objects.

ActionRequest actionRequest = (ActionRequest) pageContext.findAttribute(“actionRequest”);

So, here are the objects that I found. Hope this will help.

How to fetch parameter from url in Liferay:

Example : 1
If the value is in request object we can get the parameter like this :

String articleId1 = actionRequest.getParameter("articleId");
System.out.println("String : Article ID : "+articleId1);


Example : 2

If you are getting the value from portal url :

String articleId = ParamUtil.getString(actionRequest, "articleId");
System.out.println("Article ID : "+articleId);

Example : 3

If you are getting the parameter from address bar :

HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(actionRequest));
String myArticleId = httpReq.getParameter("articleId");

Custom Attributes in Liferay

Follow the steps for making custom attributes-
Go to Control Panel -> User - > Custom Attribute -> Add Custom Attribute
An Attribute is a key and type of the field (once saved) cannot be changed at all...
Accessing Custom Attribute Pragmatically:
ThemeDisplay themeDisplay=(ThemeDisplay)request.getAttribute(Web_Keys.THEME_DISPLAY);
User user = themeDisplay.getUser();
System.out.println(user.getExpendoBridge().getAttribute("CustomAttributeName"));
Setting Attribute Pragmatically:
ThemeDisplay themeDisplay =(ThemeDisplay)request.getAttribute(Web_Keys.THEME_DISPLAY);
User user = themeDisplay.getUser();
System.out.println(user.getExpendoBridge().setAttribute("CustomAttributeName","Value"));
Adding custom Attribute form element at Sign In Portlet
write the following code in /root/html/portlet/login/create_account.jsp
<liferay ui:custom-attribute-list
                  className="com.liferay.portal.model.user"
                  editable="<%=true%>"
                  label="<%=true%>">
but this will not visible to due to permission. So, login as admin and provide permission to guest to see and modify custom field through control panel.