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();
}

No comments: