You are viewing a plain text version of this content. The canonical link for it is here.
Posted to portalapps-dev@portals.apache.org by ta...@apache.org on 2009/03/28 00:21:57 UTC
svn commit: r759390 -
/portals/applications/gems/src/main/java/org/apache/portals/gems/file/FilePortlet.java
Author: taylor
Date: Fri Mar 27 23:21:57 2009
New Revision: 759390
URL: http://svn.apache.org/viewvc?rev=759390&view=rev
Log:
https://issues.apache.org/jira/browse/JS2-943
File Portlet Cleanup
Here is the new configuration:
Init Parameters:
* useLanguage : boolean, should this portlet use a language fallback algorithm, defaults to false
* useFilesystem : boolean, should we look in the file system or in a webapp relative path, defaults to false
* folder : string, base location of the folder to start searching in, applies to webapp and filesystem mounts
* portalParam : string, the portal parameter passed in containing the name of the file (or path) to serve up
*
* Preferences:
* file : string, the name of the file to serve up
*
* Portal provided render attributes:
* file : string, the name of the file to serve up. overriding the preference
Modified:
portals/applications/gems/src/main/java/org/apache/portals/gems/file/FilePortlet.java
Modified: portals/applications/gems/src/main/java/org/apache/portals/gems/file/FilePortlet.java
URL: http://svn.apache.org/viewvc/portals/applications/gems/src/main/java/org/apache/portals/gems/file/FilePortlet.java?rev=759390&r1=759389&r2=759390&view=diff
==============================================================================
--- portals/applications/gems/src/main/java/org/apache/portals/gems/file/FilePortlet.java (original)
+++ portals/applications/gems/src/main/java/org/apache/portals/gems/file/FilePortlet.java Fri Mar 27 23:21:57 2009
@@ -21,234 +21,94 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
-import javax.portlet.PortletPreferences;
-import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServletRequest;
-
-import org.apache.jetspeed.CommonPortletServices;
-import org.apache.jetspeed.Jetspeed;
-import org.apache.jetspeed.PortalReservedParameters;
-import org.apache.jetspeed.administration.PortalConfiguration;
-import org.apache.jetspeed.om.folder.Folder;
-import org.apache.jetspeed.request.RequestContext;
+
import org.apache.portals.bridges.common.GenericServletPortlet;
/**
* FilePortlet
*
+ * Init Parameters:
+ * useLanguage : boolean, should this portlet use a language fallback algorithm, defaults to false
+ * useFilesystem : boolean, should we look in the file system or in a webapp relative path, defaults to false
+ * folder : string, base location of the folder to start searching in, applies to webapp and filesystem mounts
+ * portalParam : string, the portal parameter passed in containing the name of the file (or path) to serve up
+ *
+ * Preferences:
+ * file : string, the name of the file to serve up
+ *
+ * Portal provided render attributes:
+ * file : string, the name of the file to serve up. overriding the preference
+ *
* @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
* @version $Id$
*/
public class FilePortlet extends GenericServletPortlet
{
- public static final String PARAM_USE_LANGUAGE = "use-language";
- /**
- * Name of portlet preference for source file url
- */
- public static final String PARAM_SOURCE_FILE = "file";
- /**
- * Name of portlet preference for source file url
- */
- public static final String PARAM_SOURCE_BASE_PATH = "basepath";
- /**
- * Name of portlet preference for source file url
- */
- public static final String PARAM_SOURCE_FILE_PATH = "filepath";
- /**
- * Is the file stored in the webapp or outside of the webapp? valid values "webapp" and "filesystem", defaults to webapp
- */
- public static final String USE_FILE_SYSTEM = "useFileSystem";
- public static final String PARAM_LOCATION = "location";
- private boolean webappLocation = true;
- /**
- * Default URL for the source file
- */
- private String defaultSourceFile = null;
- /**
- * Default base URL for the source file
- */
- private String defaultSourceBasePath = null;
- private boolean useLanguage = false;
- private PortalConfiguration configuration;
-
+ private String defaultPortalParam = "org.apache.jetspeed.Path";
+ final private String PATH_SEPARATOR = "/";
+
public void init(PortletConfig config) throws PortletException
{
super.init(config);
- configuration = (PortalConfiguration) getPortletContext().getAttribute(CommonPortletServices.CPS_PORTAL_CONFIGURATION);
- String use = configuration.getString(PARAM_USE_LANGUAGE);
- if (use != null && use.equalsIgnoreCase("true"))
- this.useLanguage = true;
- String useFileSystem = configuration.getString(USE_FILE_SYSTEM);
- if (useFileSystem != null && useFileSystem.equalsIgnoreCase("true"))
- {
- this.defaultSourceBasePath = configuration.getString(PARAM_LOCATION);
- if (defaultSourceBasePath != null && defaultSourceBasePath.length() > 0)
- {
- webappLocation = false;
- }
- else
- {
- webappLocation = true;
- }
- }
- else
- {
- webappLocation = true;
- }
- }
-
- private RequestContext getRequestContext(PortletRequest request)
- {
- return (RequestContext) request.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
- }
-
- private HttpServletRequest getHttpServletRequest(PortletRequest pRequest)
- {
- return getRequestContext(pRequest).getRequest();
- }
-
- private InputStream getFile(String appName, String fileName)
- {
- ServletContext portalAppContext = ((ServletConfig) Jetspeed.getComponentManager().getComponent("ServletConfig")).getServletContext();
- ServletContext portletAppContext = portalAppContext.getContext(appName);
- InputStream stream = null;
- if (portletAppContext != null)
- {
- stream = portletAppContext.getResourceAsStream(fileName);
- }
- else
- {
- stream = portalAppContext.getResourceAsStream(fileName);
- }
- return stream;
}
-
- private InputStream getLocalFile(String fileName)
+
+ private boolean getBooleanFromParameter(String param)
{
- InputStream is = null;
- String filePath = defaultSourceBasePath.concat(fileName);
- try
- {
- is = new FileInputStream(filePath);
+ if (param == null)
+ return false;
+ return param.equalsIgnoreCase("true");
+ }
+
+ class InitParameterState
+ {
+ boolean useLanguage = false;
+ boolean useFileSystem = false;
+ String folder;
+ String portalParam;
+
+ InitParameterState(PortletConfig config)
+ {
+ this.useLanguage = getBooleanFromParameter(config.getInitParameter("useLanguage"));
+ this.useFileSystem = getBooleanFromParameter(config.getInitParameter("useFilesystem"));
+ this.folder = config.getInitParameter("folder");
+ if (this.folder == null)
+ this.folder = "";
+ String portalParam = config.getInitParameter("portalParam");
+ if (portalParam == null)
+ this.portalParam = defaultPortalParam;
}
- catch (Exception e)
- {
- // File not found;
- }
- return is;
}
-
+
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
{
- // NOTE: this is Jetspeed specific
- HttpServletRequest req = getHttpServletRequest(request);
- String fileName;
- String appName;
- if (req.getSession().getAttribute("file") != null)
+ InitParameterState state = new InitParameterState(this.getPortletConfig());
+ String fileName = (String)request.getAttribute(state.portalParam);
+ if (fileName == null)
+ fileName = request.getPreferences().getValue("file", null);
+ if (fileName == null)
{
- fileName = (String) req.getSession().getAttribute("file");
- }
- else
- {
- fileName = req.getParameter("file");
- }
- RequestContext jsReqContext = (RequestContext)request.getAttribute( PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE );
- if (fileName != null && !fileName.equals(""))
- {
- InputStream is = null;
- try
- {
- setContentType(fileName, response,jsReqContext);
- fileName = getFilePath(fileName);
- is = new FileInputStream(fileName);
- if (is == null)
- {
- byte[] bytes = ("File " + fileName + " not found.").getBytes();
- response.getPortletOutputStream().write(bytes);
- return;
- }
- drain(is, response.getPortletOutputStream());
- response.getPortletOutputStream().flush();
- is.close();
- req.getSession().removeAttribute("file");
- }
- catch (Exception e)
- {
- if (is != null)
- is.close();
- byte[] bytes = ("File " + fileName + " not found.").getBytes();
- req.getSession().removeAttribute("file");
- response.setContentType("text/html");
- response.getPortletOutputStream().write(bytes);
- return;
- }
- }
- else
- {
- String path = (String) request.getAttribute(PortalReservedParameters.PATH_ATTRIBUTE);
- if (null == path)
- {
- PortletPreferences prefs = request.getPreferences();
- path = prefs.getValue(PARAM_SOURCE_FILE, this.defaultSourceFile);
- }
- else
- {
- if (path.endsWith(".css"))
- path = null;
- }
- if (null == path && this.defaultSourceBasePath != null)
- {
- String filepath = request.getParameter(PARAM_SOURCE_FILE_PATH);
- if (filepath == null)
- {
- filepath = (String) request.getAttribute(PARAM_SOURCE_FILE_PATH);
- }
- if (filepath != null)
- {
- path = ((this.defaultSourceBasePath.length() > 0) ? (this.defaultSourceBasePath + "/") : "") + filepath;
- }
- }
- if (null == path)
- {
- response.setContentType("text/html");
- response.getWriter().println("Could not find source document.");
- }
- else
- {
- setContentType(path, response,jsReqContext);
- appName = getApplicationName(request);
- List paths = fallback(path, request.getLocale().getLanguage(), appName);
- renderFile(response, paths, appName);
- }
+ response.setContentType("text/html");
+ response.getWriter().println("<p>A source document was not provided.</p>"); // TODO: localize
+ return;
}
+ String path = this.concatenatePaths(state.folder, fileName);
+ setContentType(path, response);
+ List<String> paths = fallback(path, request.getLocale().getLanguage(), state);
+ renderFile(response, paths, state);
}
- protected List fallback(String path, String language, String appName)
+ protected List<String> fallback(String path, String language, InitParameterState state)
{
- List paths = new LinkedList();
- if (this.useLanguage)
+ List<String> paths = new LinkedList<String>();
+ if (state.useLanguage)
{
- if (appName != null)
- {
- if (path.indexOf(appName) > -1)
- {
- path = path.replace(appName, "");
- }
- }
- if (webappLocation)
- {
- path = concatenatePaths("/WEB-INF/", path);
- }
String fallbackPath = path;
File temp = new File(path);
String parentPath = temp.getParent();
@@ -260,16 +120,12 @@
}
else
{
- if (webappLocation)
- {
- path = concatenatePaths("/WEB-INF/", path);
- }
paths.add(path);
}
return paths;
}
- protected void setContentType(String path, RenderResponse response,RequestContext jsReqContext)
+ protected void setContentType(String path, RenderResponse response)
{
// Note these content types need to be added to the portlet.xml
if (path.endsWith(".html"))
@@ -294,9 +150,10 @@
}
else if (path.endsWith(".xls"))
{
+ File f = new File(path);
response.setContentType("application/vnd.ms-excel");
- jsReqContext.getResponse().setHeader( "Content-Type", "application/vnd.ms-excel" );
- jsReqContext.getResponse().setHeader( "Content-Disposition", "inline; filename=thedata.xls" );
+ response.addProperty( "Content-Type", "application/vnd.ms-excel" );
+ response.addProperty( "Content-Disposition", "inline; filename=" + f.getName());
}
else if (path.endsWith(".psml") || path.endsWith(".link"))
{
@@ -308,36 +165,38 @@
}
}
- protected void renderFile(RenderResponse response, List paths, String appName) throws PortletException, IOException
+ protected void renderFile(RenderResponse response, List<String> paths, InitParameterState state) throws PortletException, IOException
{
boolean drained = false;
- Iterator it = paths.iterator();
- while (it.hasNext())
+ for (String fileName : paths)
{
- String fileName = (String) it.next();
InputStream is = null;
try
{
- if (this.webappLocation)
+ if (state.useFileSystem)
{
- is = getFile(appName, fileName);
+ is = new FileInputStream(fileName);
}
else
{
- is = getLocalFile(fileName);
+ is = getPortletContext().getResourceAsStream(fileName);
}
if (is != null)
{
drain(is, response.getPortletOutputStream());
response.getPortletOutputStream().flush();
is.close();
+ is = null;
drained = true;
break;
}
}
catch (Exception e)
+ {}
+ finally
{
- // do nothing, find next file
+ if (is != null)
+ is.close();
}
}
if (!drained)
@@ -372,19 +231,8 @@
}
}
- private String getFilePath(String path)
- {
- String pageRoot = System.getProperty("java.io.tmpdir");
- String sep = System.getProperty("file.separator");
- if (sep == null || sep.equals(""))
- sep = "/";
- String ar[] = path.split("_");
- if (ar.length == 1)
- return pageRoot + sep + path;
- return pageRoot + sep + ar[0] + sep + ar[1];
- }
- protected static String concatenatePaths(String base, String path)
+ protected String concatenatePaths(String base, String path)
{
String result = "";
if (base == null)
@@ -402,9 +250,9 @@
return base;
}
}
- if (base.endsWith(Folder.PATH_SEPARATOR))
+ if (base.endsWith(PATH_SEPARATOR))
{
- if (path.startsWith(Folder.PATH_SEPARATOR))
+ if (path.startsWith(PATH_SEPARATOR))
{
result = base.concat(path.substring(1));
return result;
@@ -412,26 +260,13 @@
}
else
{
- if (!path.startsWith(Folder.PATH_SEPARATOR))
+ if (!path.startsWith(PATH_SEPARATOR))
{
- result = base.concat(Folder.PATH_SEPARATOR).concat(path);
+ result = base.concat(PATH_SEPARATOR).concat(path);
return result;
}
}
return base.concat(path);
}
- private String getApplicationName(RenderRequest request)
- {
- String url = null;
- String urls[] = ((HttpServletRequest) request).getPathInfo().split(Folder.PATH_SEPARATOR);
- if (urls.length > 2)
- {
- if (!urls[1].startsWith(configuration.getString("portalurl.navigationalstate.parameter.name")))
- {
- url = Folder.PATH_SEPARATOR + urls[1];
- }
- }
- return url;
- }
}
\ No newline at end of file