Author: rogerrut
Date: Wed Dec 7 13:49:17 2005
New Revision: 354869
URL: http://svn.apache.org/viewcvs?rev=354869&view=rev
Log:
Update of perl-bridge
--> performance improvements
--> better handling of java scripts
--> encoding of query parameters
Modified:
portals/bridges/trunk/common/src/java/org/apache/portals/bridges/common/ScriptPostProcess.java
portals/bridges/trunk/perl/src/java/org/apache/portals/bridges/perl/PerlPortlet.java
Modified:
portals/bridges/trunk/common/src/java/org/apache/portals/bridges/common/ScriptPostProcess.java
URL:
http://svn.apache.org/viewcvs/portals/bridges/trunk/common/src/java/org/apache/portals/bridges/common/ScriptPostProcess.java?rev=354869&r1=354868&r2=354869&view=diff
==============================================================================
---
portals/bridges/trunk/common/src/java/org/apache/portals/bridges/common/ScriptPostProcess.java
(original)
+++
portals/bridges/trunk/common/src/java/org/apache/portals/bridges/common/ScriptPostProcess.java
Wed Dec 7 13:49:17 2005
@@ -178,12 +178,21 @@
url = nqurl.toString();
}
+ /*
+ * If the URL is an anchor don't
replace it
+ * with a portlet action
+ */
+ if (url.charAt(0) == '#')
+ {
+
finalPage.append(url).append(strQuote);
+ }
+ else
+ {
+ // Prepend the Action URL
+
actionURL.setParameter(actionParameterName, url);
- // Prepend the Action URL
-
actionURL.setParameter(actionParameterName, url);
-
-
finalPage.append(actionURL.toString()).append(strQuote);
-
+
finalPage.append(actionURL.toString()).append(strQuote);
+ }
//Remainder
page = page.substring(ixRefEnd+1);
}
Modified:
portals/bridges/trunk/perl/src/java/org/apache/portals/bridges/perl/PerlPortlet.java
URL:
http://svn.apache.org/viewcvs/portals/bridges/trunk/perl/src/java/org/apache/portals/bridges/perl/PerlPortlet.java?rev=354869&r1=354868&r2=354869&view=diff
==============================================================================
---
portals/bridges/trunk/perl/src/java/org/apache/portals/bridges/perl/PerlPortlet.java
(original)
+++
portals/bridges/trunk/perl/src/java/org/apache/portals/bridges/perl/PerlPortlet.java
Wed Dec 7 13:49:17 2005
@@ -60,6 +60,8 @@
import org.apache.jetspeed.rewriter.rules.Ruleset;
import org.apache.jetspeed.rewriter.xml.SaxParserAdaptor;
+import org.apache.portals.bridges.common.ScriptPostProcess;
+
/**
* This portlet is executes a Perl/cgi files in a portlet.
@@ -166,10 +168,7 @@
public void processAction(ActionRequest actionRequest, ActionResponse
actionResponse) throws PortletException, IOException
{
String perlParameter =
actionRequest.getParameter(PerlParameters.ACTION_PARAMETER_PERL);
- //TODO: Remove Debug
- System.out.println("Action parameter for perl " +
perlParameter);
-
- /*
+ /*
* If the perlParameter is not empty create a PerlParameter object and
attach it to the session
*/
if ( perlParameter != null && perlParameter.length() > 0)
@@ -182,7 +181,11 @@
int ixQuery = perlParameter.indexOf('?');
if ( ixQuery != -1)
{
- cgi.setScriptName(perlParameter.substring(0,ixQuery));
+ // Remove leading slash
+ if (perlParameter.charAt(0) == '/')
+
cgi.setScriptName(perlParameter.substring(1,ixQuery));
+ else
+
cgi.setScriptName(perlParameter.substring(0,ixQuery));
String queryArguments =
perlParameter.substring(ixQuery+1);
System.out.println("ProcessRequest -- Script " +
perlParameter.substring(0,ixQuery) + " Query string " + queryArguments);
@@ -214,12 +217,27 @@
// PERL_PARAMETER already processed just ignore
it
if
(name.compareToIgnoreCase(PerlParameters.ACTION_PARAMETER_PERL) != 0)
{
- value =
actionRequest.getParameter(name);
-
- // TODO: Remove debug
- System.out.println("Query: " + name +
"=" + value );
-
- cgi.addQueryArgument(name + "=" +
value);
+ // Same parameter name can have multiple
values (multi select view box)
+ String [] values = actionRequest.
getParameterValues(name);
+
+ for (int ii=0; ii < values.length; ii++)
+ {
+
+ value = values[ii];
+
+ if (value !=null &&
value.length() > 0)
+ {
+ /*
+ * If the query parameter
contains any & replace it with %26 (URL encoding)
+ */
+ value =
urlEncoding(value, "&", "%26");
+ value =
urlEncoding(value, "+", "%2b");
+ value =
urlEncoding(value, "\\", "%5c");
+
+
cgi.addQueryArgument(name + "=" + value);
+
+ }
+ }
}
}
// Add the PerlParameters to the session
@@ -235,9 +253,6 @@
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException
{
- // TODO: Remove debug
- System.out.println("Path info for request " +
((HttpServletRequest)((HttpServletRequestWrapper)
request).getRequest()).getPathInfo());
-
// Set the content type
response.setContentType("text/html");
@@ -259,6 +274,8 @@
try
{
perlParam =
(PerlParameters)request.getPortletSession().getAttribute(PerlParameters.PERL_PARAMETER,
PortletSession.APPLICATION_SCOPE);
+ // Remove perl object from session.
+
request.getPortletSession().removeAttribute(PerlParameters.PERL_PARAMETER,
PortletSession.APPLICATION_SCOPE);
}
catch (Exception e )
{
@@ -291,6 +308,71 @@
}
}
}
+ else
+ {
+ /*
+ * Check for a query string since the portlet could be
invoked form JAVA Script and therefore
+ * the parameters are not encoded in a Portlet Action or
Perl Session.
+ */
+ StringBuffer queries = new StringBuffer();
+
+ Enumeration names = request. getParameterNames();
+ for (Enumeration e = request. getParameterNames() ;
e.hasMoreElements() ;) {
+ String name = (String)e.nextElement();
+
+ //Same parameter name can have multiple values
(multi select view box)
+ String [] values = request.
getParameterValues(name);
+
+ for (int ii=0; ii < values.length; ii++)
+ {
+ String value = values[ii];
+
+ if (queries.length() > 0)
+ queries.append("&");
+
+ // Make sure that any ? are replaced
with & since JAVA SCRIPT
+ // might patch different elements together.
+ int ix = value.indexOf("?");
+ if (ix > -1)
+ {
+ String tmp =
value.substring(0,ix) + "&" + value.substring(ix+1);
+ value = tmp;
+ }
+ // Check if the query string defines
the argument file
+ if (name.compareToIgnoreCase("file") ==
0)
+ {
+ /* file=hello&arg=hello */
+ /* file=test.cgi */
+ String reminder = "";
+
+ int ixEnd = value.indexOf("&");
+ if (ixEnd > -1)
+ {
+ reminder =
value.substring(ixEnd +1);
+ perlScript =
value.substring(0,ixEnd);
+ }
+ else
+ {
+ perlScript = value;
+ }
+
+ if (reminder.length() > 0)
+
queries.append(reminder);
+ }
+ else
+ {
+
queries.append(name).append("=").append(value);
+ }
+ }
+ }
+ query = queries.toString();
+
+ //Cleanup of Buffer
+ queries.delete(0, queries.length());
+
+ System.out.println("Script [" + perlScript +"]");
+ System.out.println("Direct Query [" + queries.toString() +"]");
+ }
// Open the perl script and extract the perl executable path. It's the
same way as apache HTTP executes PERL
String perlExecutable = null;
@@ -362,35 +444,51 @@
String envQuery = "QUERY_STRING=" + query ;
String[] env = null;
- env = new String[]{"REQUEST_METHOD=GET", envQuery};
+ env = new String[]{"REQUEST_METHOD=GET", envQuery,
"LD_LIBRARY_PATH=/usr/local/groundwork/lib"};
if ( bDemoMode == true)
{
// Script info. This is for the Demo only
- writer.println("<P>The portlet executes the perl script
defined by the init-params. If you don't get an output make sure that the perl
executable defined in the script is valid.");
- writer.println("The executable is defined on the first
line of your
script.</P>Examples<ul><li><B>UNIX/Linux:</B>!/usr/bin/perl</li><li><B>Windows:</B>!c:\\bin\\perl\\perl.exe</li></ul>");
+ //writer.println("<P>The portlet executes the perl
script defined by the init-params. If you don't get an output make sure that
the perl executable defined in the script is valid.");
+ //writer.println("The executable is defined on the
first line of your
script.</P>Examples<ul><li><B>UNIX/Linux:</B>!/usr/bin/perl</li><li><B>Windows:</B>!c:\\bin\\perl\\perl.exe</li></ul>");
writer.println("<B><P>Perl Script:</B>" +
fullScriptPath + "<BR>");
- writer.println("<B>Perl executable:</B>" +
perlExecutable + "<BR>");
+ //writer.println("<B>Perl executable:</B>" +
perlExecutable + "<BR>");
writer.println("<B>Query String:</B>" + query + "</P>");
}
+
//Execute the perl script from the command line
if (command != null )
{
+
// Execute command in a separate process. The perl
output is written to the stdout
try
- {
+ {
+ long timeStart =0;
+ long timeEnd = 0;
+
+ if ( bDemoMode == true)
+ {
+ timeStart = System.currentTimeMillis();
+ }
// Start process
Process proc =
Runtime.getRuntime().exec(command,env);
-
+
// Get stdout of process and create a buffered
reader
InputStream in = proc.getInputStream();
- InputStreamReader isr = new
InputStreamReader(in, "UTF-8");
- //String enc = isr.getEncoding();
- //System.out.println("perl-encoding = " + enc);
- BufferedReader perlResult = new
BufferedReader(isr);
+ BufferedReader perlResult = new
BufferedReader(new InputStreamReader(in));
StringBuffer page = new StringBuffer();
+ if ( bDemoMode == true)
+ {
+ timeEnd = System.currentTimeMillis();
+ writer.println("<B>Execution Time
create process: </B>" + (timeEnd -timeStart) + " ms </P>");
+ timeStart = System.currentTimeMillis();
+ }
+
+ int BLOCK_SIZE = 8192;
+ char[] bytes = new char[BLOCK_SIZE];
+
//Wait until proc is done
boolean bProcDone = false;
while (bProcDone == false)
@@ -404,32 +502,48 @@
{
bProcDone = false; //Not done
yet
- // Read the buffer otherwise
the process will be blocked because it can't write to the stdout (max size of
buffer)
- int ln;
- while ((ln = perlResult.read())
!= -1)
+ // Read the buffer otherwise
the process will be blocked because it can't write to the stdout (max size of
buffer)
+ int len =
perlResult.read(bytes, 0, BLOCK_SIZE);
+
+ while (len > 0)
{
- char c = (char)ln;
- if (c != '\n' && c !=
'\r')
- page.append((char)ln);
+ page.append(bytes, 0,
len);
+ len =
perlResult.read(bytes, 0, BLOCK_SIZE);
}
}
}
// Perl execution done read the remaining
buffer
- int ln = -1;
-
- while ((ln = perlResult.read()) != -1)
+ int len = perlResult.read(bytes, 0, BLOCK_SIZE);
+ while (len > 0)
{
- char c = (char)ln;
- if (c != '\n' && c != '\r')
- page.append((char)ln);
+ page.append(bytes, 0, len);
+ len = perlResult.read(bytes, 0,
BLOCK_SIZE);
}
+
// Close stream
perlResult.close();
-
+
+ //Make sure process is destroyed
+ try
+ {
+ proc.destroy();
+ }
+ catch(Exception e)
+ {
+ System.out.println("Error killing perl
subprocess. Error " + e);
+ }
+
+ if ( bDemoMode == true)
+ {
+ timeEnd = System.currentTimeMillis();
+ writer.println("<B>Loading output of
perl: </B>" + (timeEnd -timeStart) + " ms </P>");
+ timeStart = System.currentTimeMillis();
+ }
/*
* Use rewriter for replacing all URL's with
portlet actions
*/
+ /*
if (rewriteController == null)
{
try
@@ -472,28 +586,44 @@
// Cache page
lastPage = new String(content);
+ */
- /*
- // Post Process for generated page
+
+ // Post Process for generated page
+ PortletURL actionURL =
response.createActionURL();
ScriptPostProcess processor = new
ScriptPostProcess();
processor.setInitalPage(page);
processor.postProcessPage(actionURL,
PerlParameters.ACTION_PARAMETER_PERL);
String finalPage = processor.getFinalizedPage();
+ if ( bDemoMode == true)
+ {
+ timeEnd = System.currentTimeMillis();
+ writer.println("<P><B>Rewriting perl:
</B>" + (timeEnd - timeStart) + " ms </P>");
+ }
+
// Write the page
writer.println(finalPage);
// Cache page
- lastPage = new String(finalPage);
- */
+ //lastPage = new String(finalPage);
+
}
catch(IOException ioe)
{
writer.println("<P><B>Exception while reading
perl output" + ioe.getMessage() + "</B></P>");
}
- }
+ catch(Exception e)
+ {
+ writer.println("<P><B>Exception while reading
perl output" + e + "</B></P>");
+ }
+ }
+ else
+ {
+ writer.println("<P><B>Error. Failed to run perl script
[" + perlScript + "]</B></P>");
+ }
}
/*
@@ -553,5 +683,37 @@
return byteOutputStream.toByteArray();
}
+
+ // Helper
+ private String urlEncoding(String url, String source, String replace)
+ {
+ String value = url;
+ int ix = value.indexOf(source);
+ if (ix != -1)
+ {
+ String replacement = "";
+
+ // Replace all & in the value
+ while (ix != -1)
+ {
+ replacement += value.substring(0, ix);
+ replacement += replace;
+ // Check if the & was the last char
+ if (value.length() > ix)
+ value = value.substring(ix+1);
+ else
+ value = ""; // End of string
+
+ // Check again
+ ix = value.indexOf(source);
+ }
+
+ // Reminder
+ replacement += value;
+ // Assignement
+ value = replacement;
+ }
+ return value;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]