Tuesday, July 06, 2010

Reading LTPA Cookie from Portlet & Servlet and passing the LTPA cookie into remote WebSphere Server using REST call.

Sample code which shows how you can get the LTPA cookie value from portlet and servlet.

######### Reading from Servlet Starts #############

String ltpaToken = null;
Cookie[] cookies = httpServletRequest.getCookies();

for (int i = 0; i < cookies.length; i++)
{
if (cookies[i].getName().equals("LtpaToken"))
{
ltpaToken = cookies[i].getValue(); break;
}
}
######### Reading from Servlet Ends #############

########## Reading from Portlet Starts #############

String myCookie = renderRequest.getProperty("cookie");
String ltpaToken = "";
StringTokenizer st = new StringTokenizer (myCookie, "; ");
while(st.hasMoreTokens())
{
String key = st.nextToken();
if(key.indexOf("LtpaToken") > -1)
{
int equalSign = key.indexOf("=");
ltpaToken = key.substring(equalSign+1);
}
}

########## Reading from Portlet Ends#############
### Setting inside URLConnection object & sending to remote portal server ###
// After getting the ltpa token, setting into URL connection to pass the ltpa cookie to the external server url.
if (ltpaToken != null)
{
String cookie = "LtpaToken=" + ltpaToken.toString();
URL url = new URL("http://myportal:port/wps/myportal)
URLConnection conn = url.openConnection();
conn.setRequestProperty("Cookie", cookie);
conn.connect();
}

Tips - Softwares ( During our Work)

The following are the utility software which i felt it will be very useful for Web Developers.

Aptana Studio - Web Resource development such as HTML, CSS, JS e.t.c
7-zip - Extract format such as zip, war, rar e.t.c
Notepad++ - It has many cool features. ( It will open any huge size text file )
Ever Note - Repository to keep all your personal docs. There is a way to encode the file and it can be protected using password.

Will keep you posting with other softwares...

How to set & get a value from Portlet Preference while developing a portlet using WebSphere Portlet Factory

Below is the sample code which will give a clear idea about how you can set/get a value from Portlet Preference while developing a portlet using WebSphere Portlet Factory.

Create a LJO(Linked Java Object) builder and copy the below code and replace MY_DEPARTMENT value into the value you want to set & get from Portlet Preference.


########################## Sample Code Starts #############################

public class PortletPreferenceWPFSample {
PortletRequest portletRequest = null;

private static final String MY_DEPARTMENT="myDepartment";
private static final String DEFAULT_MY_DEPARTMENT="";

public void setPreferences(WebAppAccess webAppAccess , String myDepartment)
{
try{
// Getting HttpServletRequest from WebAppAcces object.
HttpServletRequest request = webAppAccess.getHttpServletRequest();
// Type casting into Portlet Request.
portletRequest = (PortletRequest) request.getAttribute(com.bowstreet.adapters.Constants.PORTLET_REQUEST);
javax.portlet.PortletPreferences preferences = portletRequest.getPreferences();


//Storing the myDepartment in to portlet preferences Object.
preferences.setValue(MY_DEPARTMENT,myDepartment);

preferences.store();


}catch(Exception err)
{
err.printStackTrace();
}

}


public String getPreferences(WebAppAccess webAppAccess)
{
HttpServletRequest request = webAppAccess.getHttpServletRequest();
portletRequest = (PortletRequest) request.getAttribute(com.bowstreet.adapters.Constants.PORTLET_REQUEST);

javax.portlet.PortletPreferences preferences = portletRequest.getPreferences();

String myDepartment= preferences.getValue(MY_DEPARTMENT,DEFAULT_MY_DEPARTMENT);

return myDepartment;

}
}


##### Function call to above method from remote place such as method or other LJO class #####
// Calling setPreferences method to store folder path into it. Where PortletPreferenceWPFSample_LJO is the LJO builder name for PortletPreferenceWPFSample class.
webAppAccess.callMethod("PortletPreferenceWPFSample_LJO.setPreferences", myDepartment);

########################## Sample Code Ends #############################