You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by ro...@apache.org on 2004/11/11 02:17:57 UTC
cvs commit: jakarta-jetspeed-2/portals-bridges/common/src/java/org/apache/portals/bridges/common ScriptPostProcess.java
rogerrut 2004/11/10 17:17:57
Modified: portals-bridges/common/src/java/org/apache/portals/bridges/common
ScriptPostProcess.java
Added: portals-bridges/php/src/java/org/apache/portals/bridges/php
PHPParameters.java ServletConfigImpl.java
PHPServletContextProviderImpl.java
PHPApplicationPortlet.java
portals-bridges/php project.properties .cvsignore
project.xml
Log:
Moving PHP portlet into portal-bridges/php
Added PortletActions to handle navigation through the PHP application
The PHP portlet can be invoked from another portlet passing PHPParameters in a session or through actions inside the PHP portlet.
The PHP portlet remembers its state (last execution)
The start page is defined as an INIT params
Revision Changes Path
1.1 jakarta-jetspeed-2/portals-bridges/php/src/java/org/apache/portals/bridges/php/PHPParameters.java
Index: PHPParameters.java
===================================================================
/*
* Copyright 2000-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.portals.bridges.php;
import org.apache.portals.bridges.common.ScriptRuntimeData;
/**
* PHPParameters
* Class holding information about the PHP scripts to execute.This class is typically attached to a PortletAction (as an attribute).
*
* @author <a href="mailto:rogerrut@apache.org">Roger Ruttimann</a>
* @version $Id: PHPParameters.java,v 1.1 2004/11/11 01:17:56 rogerrut Exp $
*/
public class PHPParameters extends ScriptRuntimeData {
/**
* Action Parameter for PHP requests
*/
public static final String ACTION_PARAMETER_PHP = "_PHP";
/**
* Session variable for PHP Parameters
*/
public static final String PHP_PARAMETER = "PHPParameter";
/**
*
*/
public PHPParameters() {
super();
setSessionParameterName(PHP_PARAMETER);
}
}
1.1 jakarta-jetspeed-2/portals-bridges/php/src/java/org/apache/portals/bridges/php/ServletConfigImpl.java
Index: ServletConfigImpl.java
===================================================================
/*
* Copyright 2000-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.portals.bridges.php;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.portlet.PortletConfig;
/**
* ServletConfigImpl Wrapper around PortletConfig
*
* @author <a href="mailto:rogerrut@apache.org">Roger Ruttimann</a>
* @version $Id: ServletConfigImpl.java,v 1.1 2004/11/11 01:17:56 rogerrut Exp $
*/
class ServletConfigImpl implements ServletConfig
{
private PortletConfig config;
private ServletContext context = null;
public ServletConfigImpl(PortletConfig config)
{
this.config = config;
}
public String getInitParameter(String arg0)
{
return config.getInitParameter(arg0);
}
public Enumeration getInitParameterNames()
{
return config.getInitParameterNames();
}
public synchronized ServletContext getServletContext()
{
return context;
}
public String getServletName()
{
return config.getPortletName();
}
public String toString()
{
return config.toString();
}
}
1.1 jakarta-jetspeed-2/portals-bridges/php/src/java/org/apache/portals/bridges/php/PHPServletContextProviderImpl.java
Index: PHPServletContextProviderImpl.java
===================================================================
/*
* Copyright 2000-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.portals.bridges.php;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.apache.portals.bridges.common.ServletContextProvider;
//import org.apache.jetspeed.container.JetspeedPortletContext;
/**
* PHPServletContextProviderImpl supplies a PHPPortlet access to the
* Servlet context of a Jetspeed Portlet.
* @author <a href="mailto:rogerrut@apache.org">Roger Ruttimann</a>
* @version $Id: PHPServletContextProviderImpl.java,v 1.1 2004/11/11 01:17:56 rogerrut Exp $
*/
public class PHPServletContextProviderImpl implements ServletContextProvider
{
public ServletContext getServletContext(GenericPortlet portlet)
{
// Not needed any more since the PHP is decoupled from Jetspeed
return null;
}
public HttpServletRequest getHttpServletRequest(GenericPortlet portlet, PortletRequest request)
{
return (HttpServletRequest) ((HttpServletRequestWrapper) request).getRequest();
}
public HttpServletResponse getHttpServletResponse(GenericPortlet portlet, PortletResponse response)
{
return (HttpServletResponse) ((HttpServletResponseWrapper) response).getResponse();
}
}
1.1 jakarta-jetspeed-2/portals-bridges/php/src/java/org/apache/portals/bridges/php/PHPApplicationPortlet.java
Index: PHPApplicationPortlet.java
===================================================================
/*
* Copyright 2000-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.portals.bridges.php;
import java.io.IOException;
import java.security.Principal;
import java.util.Enumeration;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.portals.bridges.common.ScriptPostProcess;
import org.apache.portals.bridges.common.ServletContextProvider;
import org.apache.portals.bridges.php.PHPParameters;
/**
* This portlet is executes a PHP application in a portlet.
*
* @author <a href="mailto:rogerrut@apache.org">Roger Ruttimann</a>
* @version $Id: PHPApplicationPortlet.java,v 1.1 2004/11/11 01:17:56 rogerrut Exp $
*/
public class PHPApplicationPortlet extends GenericPortlet {
/**
* INIT parameters required by the PHP Portlet:application and ServletContextProvider
*
* Name of class implementing {@link PHPServletContextProvider}
*/
public static final String PARAM_SERVLET_CONTEXT_PROVIDER = "ServletContextProvider";
/**
* Start page for this portlet it must be the path to the script (e.g hosts/index.php)
*/
public static final String START_PAGE = "StartPage";
// Local variables
private ServletContextProvider servletContextProvider;
private static final Log log = LogFactory.getLog(PHPApplicationPortlet.class);
// Servlet INFO needed for portlet
ServletConfigImpl servletConfig = null;
// PHP engine
com.itgroundwork.portlet.php.servlet phpServletImpl = null;
// INIT Parameters
private String startPage = null;
// caching status
private boolean bUseCachedParameters = false;
private String lastContextPath = null;
private String lastQuery = null;
private String lastURI = null;
//ID to identify portlet
private String portletID = null;
public void init(PortletConfig config) throws PortletException
{
super.init(config);
// Initialize config
servletConfig = new ServletConfigImpl(config);
//Get the INIT PARAMETERS for this portlet. If the values are missing
// throw an exception
startPage = config.getInitParameter(START_PAGE);
String contextProviderClassName = config.getInitParameter(PARAM_SERVLET_CONTEXT_PROVIDER);
if (startPage == null)
throw new PortletException("Portlet " + config.getPortletName()
+ " is incorrectly configured. Init parameter "
+ START_PAGE + " not specified");
if (contextProviderClassName == null)
throw new PortletException("Portlet " + config.getPortletName()
+ " is incorrectly configured. Init parameter "
+ PARAM_SERVLET_CONTEXT_PROVIDER + " not specified");
if (contextProviderClassName != null)
{
try
{
Class clazz = Class.forName(contextProviderClassName);
if (clazz != null)
{
Object obj = clazz.newInstance();
if (ServletContextProvider.class.isInstance(obj))
servletContextProvider = (ServletContextProvider) obj;
else
throw new PortletException("class not found");
}
} catch (Exception e)
{
if (e instanceof PortletException)
throw (PortletException) e;
e.printStackTrace();
throw new PortletException("Cannot load", e);
}
}
}
/**
* processAction()
* Checks action initiated by the php portlet (invoking other php scripts)
* @param actionRequest
* @param actionResponse
* @throws PortletException
* @throws IOException
*/
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException
{
String phpParameter = actionRequest.getParameter(PHPParameters.ACTION_PARAMETER_PHP);
/*
* If the phpParameter is not empty create a PHPParameters object and attach it to the session
*/
if ( phpParameter != null && phpParameter.length() > 0)
{
// Perl Parameter Object
PHPParameters phpScript = new PHPParameters();
// Separate the values before and after the Query Mark ?
int ixQuery = phpParameter.indexOf('?');
if ( ixQuery != -1)
{
phpScript.setScriptName(phpParameter.substring(0,ixQuery));
String queryArguments = phpParameter.substring(ixQuery+1);
System.out.println("ProcessRequest -- Script " + phpParameter.substring(0,ixQuery) + " Query string " + queryArguments);
int ixQuerySeparator = queryArguments.indexOf('&');
while ( ixQuerySeparator != -1)
{
phpScript.addQueryArgument(queryArguments.substring(0, ixQuerySeparator));
queryArguments = queryArguments.substring(ixQuerySeparator+1);
ixQuerySeparator = queryArguments.indexOf('&');
}
phpScript.addQueryArgument(queryArguments);
// Add the PerlParameters to the session
actionRequest.getPortletSession().setAttribute(PHPParameters.PHP_PARAMETER, phpScript, PortletSession.APPLICATION_SCOPE);
}
else
{
// No query string just the script name
phpScript.setScriptName(phpParameter);
// Get all the parameters from the request and add them as query arguments
Enumeration enum = actionRequest.getParameterNames();
String name, value;
while (enum.hasMoreElements())
{
name = (String)enum.nextElement();
// ACTION_PARAMETER_PHP already processed just ignore it
if (name.compareToIgnoreCase(PHPParameters.ACTION_PARAMETER_PHP) != 0)
{
value = actionRequest.getParameter(name);
phpScript.addQueryArgument(name + "=" + value);
}
}
// Add the PerlParameters to the session
actionRequest.getPortletSession().setAttribute(PHPParameters.PHP_PARAMETER, phpScript, PortletSession.APPLICATION_SCOPE);
}
}
}
/**
* doView
* Renders a PHP file in the portlet.
* The script parameters are in a PHPParameters object that is passed in the session
*/
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException
{
/*
* Extract the PHPParameter object from the session. If this is not provided use the values for startPage and application from the INIT Parameters
*/
String reqQuery;
String phpScript;
PHPParameters phpParam = null;
try
{
phpParam = (PHPParameters)request.getPortletSession().getAttribute(PHPParameters.PHP_PARAMETER, PortletSession.APPLICATION_SCOPE);
}
catch (Exception e )
{
phpParam = null;
}
if (phpParam != null)
{
// We got real parameters
bUseCachedParameters = false;
reqQuery = phpParam.getQueryString();
phpScript = phpParam.getScriptName();
}
else
{
/*
* No parameters were send to this page. Either it is the initial invocation (use init param) or
* iit was a refresh (use cached arguments)
*
* Setting the bUseCacheParameters means that nothing was provided from outside
*/
bUseCachedParameters = true;
reqQuery = "";
phpScript = this.startPage;
}
//ServletContext servletContext = servletContextProvider.getServletContext(this);
HttpServletRequest httpRequest = servletContextProvider.getHttpServletRequest(this, request);
HttpServletResponse httpResponse = servletContextProvider.getHttpServletResponse(this, response);
//initialize PHP engine
if ( phpServletImpl == null)
{
try
{
phpServletImpl = new com.itgroundwork.portlet.php.servlet();
if (phpServletImpl != null )
phpServletImpl.init(servletConfig);
}
catch(ServletException se)
{
httpResponse.getWriter().println("<p><b>Initializationof PHP servlet failed!</b> Error: " + se.getMessage() + "</p>");
}
}
// Make sure we have an user
String userName = "anon"; //default not logged in
Principal userPrincipal = request.getUserPrincipal();
if (userPrincipal != null )
userName = userPrincipal.getName();
boolean bShowInfoPage = false;
String cookieValue = null;
// Build the context path
String contextPath = request.getContextPath();
String pathTranslated = ((HttpServletRequest)((HttpServletRequestWrapper) request).getRequest()).getPathTranslated();
String rootContextPath = pathTranslated.substring(0, pathTranslated.indexOf("webapps") + 7) + contextPath + "/";
/*
* At this point we have all the information to run the PHP servlet:
* rootContextPath contains the file path to the PortletApplication root (e.g /home/user/tomcat/webapps/MyApplication/ )
* phpScript php script to execute. Includes the full path to the application (e.g hosts/index.php)
* reqQuery Query arguments passed to the scripts
*/
// Run parameters
String runURI, runQuery, runContext;
// First time call or invoked from another portlet
if ( bUseCachedParameters == true)
{
//If it is the first call create an URI
if ( lastURI == null || lastURI.length() == 0)
{
// Build the URI with the start page and the context
lastURI = contextPath + "/" + phpScript;
lastContextPath = rootContextPath + phpScript;
}
// Assign run values
runURI = lastURI;
runQuery = lastQuery;
runContext = lastContextPath;
}
else
{
// New request for this portlet render it for the new content
String adjURI = contextPath + "/" + phpScript;
String phpContext = rootContextPath + phpScript;
// Assign run values
runURI = adjURI;
runQuery = reqQuery;
runContext = phpContext;
}
// Invoke the PHP servlet and run it
try
{
if (phpServletImpl != null )
{
if (runQuery == null )
runQuery = "";
// Call into the php library.
// Cache the page in the servlet and don't write the output to the response since some post processing
// is needed before the page can be send back to the client
phpServletImpl.setUseInternalPage();
// Set the servlet parameters
phpServletImpl.setAdjustedURI(runURI);
phpServletImpl.setAuthenticatedUser(userName);
phpServletImpl.setAdjustedQuery(runQuery);
// execute the PHP script
phpServletImpl.service(httpRequest, httpResponse, runContext);
//Save last executed request info so that we remember when it was last called
lastQuery = runQuery;
lastContextPath = runContext;
lastURI = runURI;
//PostProcess:
// replace all relative links with actions
//Any HREFs and Form actions should be extended with the ActionURL
PortletURL actionURL = response.createActionURL();
// Get the buffered page from the PHP servlet
StringBuffer page = phpServletImpl.getSourcePage();
// Call into the PostProcess object which is the same for PERL and other script engine
// supported in the future.
ScriptPostProcess processor = new ScriptPostProcess();
processor.setInitalPage(page);
processor.postProcessPage(actionURL, PHPParameters.ACTION_PARAMETER_PHP);
String finalPage = processor.getFinalizedPage();
//Write the page to the HttpResponse
httpResponse.getWriter().println(finalPage);
}
else
{
httpResponse.getWriter().println("<br/><b>Error in PHP servlet. Couldn't create instance of com.itgroundwork.portlet.php.servlet. Make sure the jar is included in the same app as the portas-bridges-php jar file</b>" );
}
}
catch( ServletException se)
{
httpResponse.getWriter().println("<P><B>Error in PHP servlet.Servlet Exception: " + se.getMessage() + "</B>RunQuery=" + runQuery+ " RunContext=" + runContext + " RunURI=" + runURI + " </P><BR>");
throw new PortletException(se);
}
catch (IOException e)
{
httpResponse.getWriter().println("<P><B>Error in PHP servlet. IO Exception " + e.getMessage() + "</B>RunQuery=" + runQuery+ " RunContext=" + runContext + "Run URI=" + runURI + "</P><BR>");
}
}
}
1.1 jakarta-jetspeed-2/portals-bridges/php/project.properties
Index: project.properties
===================================================================
# Copyright 2004 The Apache Software Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# $Id: project.properties,v 1.1 2004/11/11 01:17:56 rogerrut Exp $
maven.multiproject.type=jar
maven.license.licenseFile=${basedir}/../../LICENSE.TXT
1.1 jakarta-jetspeed-2/portals-bridges/php/.cvsignore
Index: .cvsignore
===================================================================
target
1.1 jakarta-jetspeed-2/portals-bridges/php/project.xml
Index: project.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
$Id: project.xml,v 1.1 2004/11/11 01:17:56 rogerrut Exp $
-->
<project>
<extend>${basedir}/../../project.xml</extend>
<pomVersion>3</pomVersion>
<groupId>portals-bridges</groupId>
<id>portals-bridges-php</id>
<name>Apache Portals PHP Framework Bridge</name>
<currentVersion>0.1</currentVersion>
<package>org.apache.portals.bridges.php</package>
<description>
Apache Portals PHP Framework Bridge
</description>
<shortDescription>Apache Portals PHP Framework Bridge</shortDescription>
<repository>
<connection>scm:cvs:pserver:anoncvs@cvs.apache.org:/home/cvspublic:jakarta-jetspeed-2/portals-bridges/php</connection>
<url>http://cvs.apache.org/viewcvs/jakarta-jetspeed-2/portals-bridges/perl/</url>
</repository>
<developers>
<developer>
<name>Roger Ruttimann</name>
<id>rogerrut</id>
<email>rogerrut@apache.org</email>
<roles>
<role>Java Developer</role>
</roles>
</developer>
</developers>
<dependencies>
<dependency>
<id>itgroundwork</id>
<properties>
<war.bundle.jar>true</war.bundle.jar>
</properties>
<jar>phpportlet.jar</jar>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>portlet-api</groupId>
<artifactId>portlet-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>servletapi</groupId>
<artifactId>servletapi</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>portals-bridges</groupId>
<artifactId>portals-bridges-common</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/java</sourceDirectory>
</build>
<reports>
<report>maven-jdepend-plugin</report>
<!--
<report>maven-checkstyle-plugin</report>
-->
<report>maven-pmd-plugin</report>
<report>maven-changelog-plugin</report>
<report>maven-file-activity-plugin</report>
<report>maven-developer-activity-plugin</report>
<report>maven-license-plugin</report>
<report>maven-javadoc-plugin</report>
<report>maven-jxr-plugin</report>
<report>maven-junit-report-plugin</report>
<report>maven-jcoverage-plugin</report>
<report>maven-linkcheck-plugin</report>
<report>maven-tasklist-plugin</report>
</reports>
</project>
1.2 +7 -3 jakarta-jetspeed-2/portals-bridges/common/src/java/org/apache/portals/bridges/common/ScriptPostProcess.java
Index: ScriptPostProcess.java
===================================================================
RCS file: /home/cvs/jakarta-jetspeed-2/portals-bridges/common/src/java/org/apache/portals/bridges/common/ScriptPostProcess.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ScriptPostProcess.java 8 Nov 2004 22:30:28 -0000 1.1
+++ ScriptPostProcess.java 11 Nov 2004 01:17:56 -0000 1.2
@@ -151,7 +151,10 @@
}
else
{
- // No quote just the first no ASCII char
+ // Make sure that we don't parse over the tag end
+ ixTagEnd = page.indexOf(endTag);
+
+ // No quote just the first space or tagEnd index
ixRefEnd = 0;
StringBuffer nqurl = new StringBuffer();
boolean bEnd = false;
@@ -160,7 +163,7 @@
{
char c = page.charAt(ixRefEnd);
- if ( Character.isSpaceChar(c) == false )
+ if ( (Character.isSpaceChar(c) == false) && (ixRefEnd < ixTagEnd) )
{
ixRefEnd++;
nqurl.append(c);
@@ -173,6 +176,7 @@
}
// Get the string
url = nqurl.toString();
+
}
// Prepend the Action URL
---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org