Showing posts with label IBM WebSphere Portal. Show all posts
Showing posts with label IBM WebSphere Portal. Show all posts

Thursday 2 February 2017

Upgrading JSR 168 portlets to JSR 286 portlets specifications

How to upgrade JSR 168 portlets to JSR 286 specifications

Following steps below outline the minimal set of changes to be done for upgrading the portlets to JSR 286.
1. portlet.xml XSD reference changes
The portlet.xml should refer to the new xsd definitions.
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
id="myportlet_app_id">
to be changed to
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
version="2.0" 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"
id="myportlet_app_id">
note the changes highlighted. So after these changes we will refer to the new specifications defined in _2_0 XSD files.

2. portlet.xml - add default namespace for portlet
Im JSR 286 portlets we need to give hte default name space for the portlet as in the line below.
<default-namespace>
http://com.mycomp.myproj.portlet.MyPortletName/
</default-namespace>

3. Tag library change in all JSP files
The portlet 1.0 tag library used in JSR 168 to be replaced with the portlet 2.0 tag library as defined in JSR 286 portlet.
Tag library in portlet 1.0 specification:
<%@taglib prefix="portlet" uri="http://java.sun.com/portlet"%>

Tag library in portlet 2.0 specification:
<%@taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0"%>

Change all the JSPs in the portlet where tags are used.

This completes the minimal changes that are required for upgrading the JSR 168 portlets to JSR 286 portlets.

How to develop a very basic JSR 286 Portlet Web Application

how to develop a very basic JSR 286 Portlet Web Application

Steps behind developing a very basic JSR 286 Portlet Web Application that can be deployed to and tested with WebSphere Portal Servers 8.0 and above.

Technologies used:

  • Java EE 6
  • Portlets 2.0 (JSR 286 Portlets)
  • WebSphere Portlet Server 8.0 and above
  • MyEclipse 2015 CI

Step 1: Create a Web Project in MyEclipse

  • To do that, choose File > New > Web Project > Next > provide a name, choose Java EE 6 specification and finish the wizard. 

Step 2: Configure WebSphere Portal Server

  • Right click inside Servers view > New > Server > WebSphere Portal Server 8.0 or 8.5 > Next >
  • Provide Portal Server and AppServer installation directories > Next
  • Provide the username and password for the server
  • Finish the wizard and ensure the new Server is visible in the Servers view
  • If you require additional information on WebSphere Portal Server setup in MyEclipse, please click here.

Step 3: Install JSR 286 Portlet Support

  • Select the project and choose MyEclipse > Project Facets / Capabilities > Install JSR Portlets Facet > Next
  • Choose JSR 2.0 version and select WebSphere Portal Server as the target runtime so the portlet implementation is handled by WebSphere. Then finish the wizard.

how to develop a very basic JSR 286 Portlet Web Application

What this does:
> Installs Portlet Facet to the project.
> Adds the WebSphere Portal Server container to the project.
> Generates a portlet.xml inside WEB-INF folder.

The project is now ready for Portlet development.

Step 4: Create a new Portlet

  • Create a new Java package
  • Right click on the package > choose New > Other > Portlet > Next
  • Provide a name for the portlet, say "MyFirstPortlet"
  • Accept all other defaults and finish the wizard
What this does (screenshots below):
> Creates a new Portlet in the selected package with the doView method pointing to the newly created JSP.
> Creates a new JSP file inside WEB-INF/jsp with default content.
> Updates portlet.xml with the newly created portlet.

how to develop a very basic JSR 286 Portlet Web Application

how to develop a very basic JSR 286 Portlet Web Application

how to develop a very basic JSR 286 Portlet Web Application

Step 5: Modify View


  • Open the JSP file and replace the default content with a form that takes inputs from users, like below. Save and close the file.

how to develop a very basic JSR 286 Portlet Web Application















What this does:
renderResponse.createRenderURL() renders the portlet.
> The form takes user input and upon pressing Submit, dispatches the information to the Portlet for processing.

Step 6: Modify the Portlet


  • Let's modify the portlet to handle the form input from the JSP. Open the class and modify the doView method like below:

how to develop a very basic JSR 286 Portlet Web Application













What this does:
> request.getParameter(..) fetches all the parameter values provided by the user in the JSP.
> The input is then processed to show a simple message back to the user.
> A Try Again link is then added to take the user back to the form to provide a different input and test the portlet again.

Step 7: Deploy and run the portlet

  • Start the portal server by selecting the server in Servers view and then choosing Run.
  • Once the server is started, right click on it and choose Add/Remove Deployments... > select our Portlet project > Add > Finish the wizard.

Step 8: Adding the Portlet to the Portal server

  • To test our portlet, we first need to add our portlet to the WebSphere Portal Server. To do that, open a Web browser and enter http://localhost:<your port>/wps/myportal. Login with the username and password.
  • Turn on Edit Mode from the top right section of the portal page.


  • Press 'Create > Page' to create a new page to which our portlet will be added.
  • Press 'Create > Application'. Then in the search text field, enter "MyFirstPortlet" and hit search. When the results are shown, press the '+' next to our portlet to add it to the page created earlier. 
  • Our portlet should now be available for testing.

JSR 286 InterPortlet communication

JSR 286 InterPortlet communication

Interportlet communication(IPC) allows sharing of data between portlets.
In JSR 168, we use portlet session with Application Scope to share data between portltes and this is possible within a portlet application, because session can't be shared across two different context (web/portlet application). 


JSR 286 has introduced two ways to achieve IPC. 

1) Public Render Parameter

2) Eventing
Let's dicsuss one by one with their Concept, Implementation and Usage Scenario

1) Public Render Parameter

Concept:-

Public Render parameters allows you to share data using render parameters.

Source portlet will set data using actionResponse.setrenderPrameter in processAction phase and target portlet will receive data using renderRequest.getPramete("param name").

Note:- In JSR168 render parameter was also there but not public.

Public means, render prameter set in processAction of one portlet can be retrieved in render method of any other portlets in the same/different war.

Implementation:-

a) You will require to set public render parameter at portlet application level.

List all the render prameter you like to publish/share.

<public-render-parameter>

<identifier>id1</identifier>

<qname xmlns:x="http://sun.com/params">x:param1</qname>

</public-render-parameter>

<public-render-parameter>

<identifier>id2</identifier>
<qname xmlns:x="http://sun.com/params">x:param2</qname>
</public-render-parameter>


b) Need to specify render prameters you would need a portlet to share

Specify prameter within portelts that would like access those prameters

<portlet>

<portlet-name>PortletA</portlet-name>

<supported-public-render-parameter>id1</supported-public-render-parameter>

<supported-public-render-parameter>id2</supported-public-render-parameter>

<portlet>

<portlet>
<portlet-name>PortletB</portlet-name>
<supported-public-render-parameter>id1</supported-public-render-parameter>
</portlet>
<portlet>
<portlet-name>PortletC</portlet-name>
<supported-public-render-parameter>id2</supported-public-render-parameter>
<portlet>

Usage scenario:-
a) Since the public render parameters are encoded in the URL, so you will have option only to share data between portlets of type String and String arrays.

b) Since public render prameter are only available in render methods, so you will not be using it where processing is required in data.

c) Best to use in situations where, you have target portlet changing it's rendering view based on parameter retrieved without any processing on prameter value.

2) Eventing

Concept:-
Portlet event model is a loosely coupled, that allows creating portlets as stand-alone portlets that can be wired together with other portlets at runtime.
Source portlet publish an event and all other portlets (within same war or different war), can register for that event.
Once target portlet receives an event, it can peform processing on data and then display results based on business need.
Implementation:-
a) Portlet which want to publish events should have follwoing entry at application level in portlet.xml.

If IPC is beween two different war, then this event definition should present in portlet.xml of both war.

Setting event definition.

<portlet-app ...>

<portlet>

. . .

. . .

</portlet>

<event-definition>


<qname xmlns:x="https://engineernitesh.blogspot.com/Employee">x:Employee</qname>

<value-type>com.wps.Employee</value-type>

</event-definition>

</portlet-app>


b) Event publishing Portlet should specify events that they want to publish.
<portlet>

<description>SourcePortlet</description>

<portlet-name>SourcePortlet</portlet-name>

.................

<supported-publishing-event>

<qname xmlns:x="https://engineernitesh.blogspot.com/Employee">x:Employee</qname>
</supported-publishing-event>
</portlet>

Setting an event into processAction of publishing portlet

public class SourcePortlet extends GenericPortlet {

public void processAction(ActionRequest request, ActionResponse response)

throws PortletException,IOException {

QName qname = new QName("https://engineernitesh.blogspot.com/Employee" , "Employee");

Employee emp = new Employee();

//set values in Employee
response.setEvent(qname, emp);
}
}


c) Event processing portlets should specify events that they want to process
<portlet>

<description>TargetPortlet1</description>

<portlet-name>TargetPortlet1</portlet-name>

<supported-processing-event>

<qname xmlns:x="https://engineernitesh.blogspot.com/Employee">x:Employee</qname>

</supported-processing-event>
</portlet>


<portlet>

<description>TargetPortlet2</description>

<portlet-name>TargetPortlet2</portlet-name>

<supported-processing-event>

<qname xmlns:x="https://engineernitesh.blogspot.com/Employee">x:Employee</qname>

</supported-processing-event>
</portlet>

Event process portlet needs to override processEvent method

public class TargetPortlet extends GenericPortlet {

public void processEvent(EventRequest request, EventResponse response) {

Event event = request.getEvent();

if(event.getName().equals("Employee")){

Employee emp = (Employee )event.getValue();

//process emp here
}
}
}


Usage scenario:- 

a) Event technique should be used in situation where data shared needs to be processed in receiver portlet.

b) it is advisable to use public render parameters as they avoid the overhead for portlets event creation and wiring the portlets together.wiring portlets together is portal container specific and is an addtional overhead.

Saturday 21 January 2017

Difference between JSR 168 and JSR 286 Portlet life Cycle

Difference between JSR 168 and JSR 286 Portlet life Cycle

JSR 168 Portlet Life Cycle

1. void init(PortletConfig config):
This method is called by the portlet container to indicate to a portlet that the portlet is being placed into service. This method used to initialise a portlet. This method is called only one time during the portlet life cycle.

2. void render(RenderRequest request, RenderResponse response):

When a portlet is displayed in the portal page this method is called. Render method is responsible to generate HTML content. We can create render URL to invoke render method. For example

<portlet:renderURL var="myURL"/>
3. void processAction(ActionRequest request, ActionResponse response)

When we invoke action URL the processAction method is called. A process action is generally used to process submitted form or writing data to database. For example
<portlet:actionURL var="myURL" name="process"/>

As soon as a process action is invoked it invoke render method. If a portal page contains five portlets then all the five render methods will be called either in parallel or one after another.
4. void destroy():

Called by the portlet container to indicate to a portlet that the portlet is being taken out of service. The default implementation does nothing. When a portlet is undeployed this method is called by the portlet container.

JSR 286 Portlet Life Cycle

JSR 286 Portlet Life Cycle also have all above JSR 168 4 methods, there are 2 new method introduce in JSR 286.

5.  void processEvent(EventRequest request, EventResponse response):

Process Event method is used to process an event. In JSR 168 there was no no process event method. It was introduced in JSR 286. This portlet life cycle method has important role in implementing event based inter portlet communication (IPC). As soon as process event method is completed the portlet container invokes render method. 

6. void serveResource(ResourceRequest request, ResourceResponse response)

Called by the portlet container to allow the portlet to generate the resource content based on its current state. The portal / portlet container must not render any output in addition to the content returned by the portlet. The portal / portlet container should expect that the portlet may return binary content for a renderResource call. This portlet life cycle method is added in JSR 286. 
This method is basically required to generate non HTML based content like JSON, XML etc. We can implement AJAX using this method. A serveResource method can be invoked by creating resource URL. For example

<portlet:resourceURL var="myResourceUrl"/>