I have received many request for this portlet code, so I am posting it at
the bottom of the message.
Here is the scenario. I have created several services which require a user
to login so that I can offer user-specific information. An example is I have
an ASP page which delivers an HTML table of the user's todo list. I wanted
to display this HTML-table within a Jetspeed portlet.
What I needed was a portlet which would:
- post the Turbine userid and password to the service
- the different services look for different variable-name. IE - userid=,
username=, loginid=...
- in case of a non-logged in user, I need a default id/password (I use this
for the company calendar)
- The content should NOT be cached
What I would like to add is
- option as to whether the info is POSTed or GETed
- Same features when fetching RSS content.
Anyway, here is the code. Please let me know if you find bugs and I will do
my best to correct them.
Steve B.
PS - this D*** MS-Outlook sometimes adds "file:" in front of "//" comments
in my code. Please be sure this has not occured before attempting to
compile.
PPS - After compiling, add the LoginURL.class to the
org.apache.jetspeed.portal.portlets folder of the Jetspeed.jar
/*
* This portlet will pass the Turbine Userid and password to a URL for
fetching
* HTML content. The content returned should only be a HTML page fragment
(ie - no <HTML>
* nor <body> tags).
* In the Jetspeed.jcfg (portal library file), you should define the variable
names that
* the target URL will be reading for these values. You should also specify
the default
* userid and password jetspeed should send in the case of a non-logged in
user. Here is
* an example entry:
*
* <portlet-entry type="abstract" name="LoginURL" hidden="false"
application="false" admin="false">
* <classname>org.apache.jetspeed.portal.portlets.LoginURL</classname>
* </portlet-entry>
*
* <portlet-entry type="ref" parent="LoginURL" name="eResourceManagerLogin"
hidden="false" application="false" admin="false">
*
<url>http://myserver.mySite.com/eResourceManager/PortletScheduleDisplay.asp<
/url>
* <parameter value="userid" name="userid_varname"/>
* <parameter value="password" name="password_varname"/>
* <parameter value="velos_intranet" name="default_userid"/>
* <parameter value="passkey" name="default_password"/>
* <meta-info>
* <title>eResourceManager</title>
* <description>A listing of events for today</description>
* </meta-info>
* </portlet-entry>
*/
package org.apache.jetspeed.portal.portlets;
//Element Construction Set
import org.apache.ecs.html.*;
import org.apache.ecs.ConcreteElement;
import org.apache.ecs.ElementContainer;
import org.apache.ecs.StringElement;
//Jetspeed stuff
import org.apache.jetspeed.portal.*;
import org.apache.jetspeed.util.*;
import org.apache.jetspeed.cache.memory.*;
import org.apache.jetspeed.cache.disk.*;
//turbine
import org.apache.turbine.util.*;
//standard java stuff
import java.net.*;
import java.io.*;
//ecs stuff
import org.apache.ecs.*;
import org.apache.ecs.html.*;
/**
@author <a href="mailto:[EMAIL PROTECTED]">Steve Belt</a>
@version $Id: LoginURL.java,v 1.19 2000/11/08 21:19:56 belt Exp $
*/
public class LoginURL extends FileServerPortlet {
/**
@author <a href="mailto:[EMAIL PROTECTED]">Steve Belt</a>
@version $Id: LoginURL.java,v 1.19 2000/11/08 21:19:56 belt Exp $
*/
public void init() throws PortletException {
PortletConfig config = this.getPortletConfig();
RunData rundata = config.getRunData();
// Get the parameter names that need to be posted to the URL
String userid_varname =
his.getPortletConfig().getInitParameter( "userid_varname" );
if (userid_varname == null)
userid_varname = "userid";
String password_varname =
his.getPortletConfig().getInitParameter( "password_varname" );
if (password_varname == null)
password_varname = "password";
// Get the logged-in userid / password. If not defined, get the defaults
String userid = rundata.getUser().getUserName();
if ( userid == null )
userid = this.getPortletConfig().getInitParameter( "default_userid" );
String password = rundata.getUser().getPassword();
if ( password == null )
password =
this.getPortletConfig().getInitParameter( "default_password" );
String szPostString = userid_varname + "=" + userid +
"&" + password_varname + "=" + password;
// Here is a version of the szPostString which hides the password (for
logging)
String szSecurePostString = "";
for ( int x=0; x < password.length(); x++)
szSecurePostString += "*";
szSecurePostString = userid_varname + "=" + userid +
"&" + password_varname + "=" +szSecurePostString;
//fetch the URL as a String...
try {
Log.note("accessing non-cached URL site "+config.getURL());
Log.note("posting string = "+szSecurePostString);
this.setContent( new ClearElement( this.getURL( config.getURL(),
szPostString ) ) );
}
catch (Exception e)
{
throw new PortletException( e.getMessage() );
}
}
/**
* Interface method needed to be implemented in order to control caching
behaviour.
* When returning true, the served resource is cachable, otherwise it's not
cachable
* and the init method will always be called for each request.
* @return flag indicating whether or not the served resource is cachable
*/
public boolean isCacheable ( )
{
return false;
}
/**
@author <a href="mailto:[EMAIL PROTECTED]">Steve Belt</a>
@version $Id: LoginURL.java,v 1.19 2000/11/08 21:19:56 belt Exp $
*/
private String getURL(String url, String szPostString) throws
IOException {
int CAPACITY = 1024;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
// open connection to the url
URL srcUrl = new URL(url);
URLConnection connection = srcUrl.openConnection();
// send the string
connection.setDoOutput(true);
PrintWriter out = new PrintWriter( connection.getOutputStream());
out.println(szPostString);
out.close();
// Display the results
//now process the InputStream...
InputStream is = connection.getInputStream();
byte[] bytes = new byte[CAPACITY];
int readCount = 0;
while( ( readCount = is.read( bytes )) > 0 )
{
buffer.write( bytes, 0, readCount);
}
is.close();
return buffer.toString();
}
}
--
--------------------------------------------------------------
Please read the FAQ! <http://java.apache.org/faq/>
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Archives and Other: <http://java.apache.org/main/mail.html>
Problems?: [EMAIL PROTECTED]