Author: julien(a)jboss.com
Date: 2007-03-21 14:22:58 -0400 (Wed, 21 Mar 2007)
New Revision: 6790
Added:
trunk/core-samples/src/main/org/jboss/portal/core/portlet/test/FSContentDrivenPortlet.java
trunk/core-samples/src/resources/portal-samples-war/dir1/
trunk/core-samples/src/resources/portal-samples-war/dir1/bar.txt
trunk/core-samples/src/resources/portal-samples-war/dir1/foo.txt
trunk/core-samples/src/resources/portal-samples-war/dir2/
trunk/core-samples/src/resources/portal-samples-war/dir2/foo.txt
Modified:
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet-instances.xml
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet.xml
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/web.xml
Log:
example of content driven portlet that use the war file content, will be used in the doco
Added:
trunk/core-samples/src/main/org/jboss/portal/core/portlet/test/FSContentDrivenPortlet.java
===================================================================
---
trunk/core-samples/src/main/org/jboss/portal/core/portlet/test/FSContentDrivenPortlet.java
(rev 0)
+++
trunk/core-samples/src/main/org/jboss/portal/core/portlet/test/FSContentDrivenPortlet.java 2007-03-21
18:22:58 UTC (rev 6790)
@@ -0,0 +1,274 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.core.portlet.test;
+
+import javax.portlet.GenericPortlet;
+import javax.portlet.PortletMode;
+import javax.portlet.PortletSecurityException;
+import javax.portlet.RenderResponse;
+import javax.portlet.RenderRequest;
+import javax.portlet.PortletException;
+import javax.portlet.PortletURL;
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import java.io.IOException;
+import java.io.File;
+import java.io.PrintWriter;
+import java.io.FileFilter;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.nio.channels.FileChannel;
+import java.nio.ByteBuffer;
+
+/**
+ * <p>An example of content driven portlet that display the files located in the
war file. The portlet does not implement
+ * any code to improve performance like caching in order to keep the code easier to
understand. It should not be
+ * used in production for a large scale portal.</p>
+ *
+ * <p>Content URI is defined as the canonical path of the file relative to the war
file context root.</p>
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class FSContentDrivenPortlet extends GenericPortlet
+{
+
+ /** The edit_content mode. */
+ public static final PortletMode EDIT_CONTENT_MODE = new
PortletMode("edit_content");
+
+ /**
+ * Additional dispatch that will call the
<code>doEditContent(RenderRequest,RenderResponse)</code> method.
+ */
+ protected void doDispatch(RenderRequest req, RenderResponse resp) throws
PortletException, PortletSecurityException, IOException
+ {
+ if (EDIT_CONTENT_MODE.equals(req.getPortletMode()))
+ {
+ doEditContent(req, resp);
+ }
+ else
+ {
+ super.doDispatch(req, resp);
+ }
+ }
+
+ /**
+ * Implements the edit content functionnality.
+ */
+ protected void doEditContent(RenderRequest req, RenderResponse resp) throws
PortletException, PortletSecurityException, IOException
+ {
+ // Get the uri value optionally provided by the portal
+ String uri = req.getParameter("content.uri");
+
+ // Get the working directory directory
+ File workingDir;
+ if (uri != null)
+ {
+ workingDir = getFile(uri).getParentFile();
+ }
+ else
+ {
+ // Otherwise try to get the current directory we are browsing, if no current dir
exist we use the root
+ String currentDir = req.getParameter("current_dir");
+ if (currentDir == null)
+ {
+ currentDir = "/";
+ }
+ workingDir = getFile(currentDir);
+ }
+
+ // Get the parent path
+ String parentPath = getContentURI(workingDir.getParentFile());
+
+ // Get the children of the selected file, we use a filter to retain only text files
and avoid WEB-INF dir
+ File[] children = workingDir.listFiles(filter);
+
+ // Configure the response
+ resp.setContentType("text/html");
+ PrintWriter writer = resp.getWriter();
+
+ //
+ writer.print("Directories:<br/>");
+ writer.print("<ul>");
+ PortletURL choseDirURL = resp.createRenderURL();
+ if (parentPath != null)
+ {
+ choseDirURL.setParameter("current_dir", parentPath);
+ writer.print("<li><a href=\"" + choseDirURL +
"\">..</a></li>");
+ }
+ for (int i = 0;i < children.length;i++)
+ {
+ File child = children[i];
+ if (child.isDirectory())
+ {
+ choseDirURL.setParameter("current_dir", getContentURI(child));
+ writer.print("<li><a href=\"" + choseDirURL +
"\">" + child.getName() + "</a></li>");
+ }
+ }
+ writer.print("</ul><br/>");
+
+ //
+ writer.print("Files:<br/>");
+ writer.print("<ul>");
+ PortletURL selectFileURL = resp.createActionURL();
+ selectFileURL.setParameter("content.action.select", "select");
+ for (int i = 0;i < children.length;i++)
+ {
+ File child = children[i];
+ if (child.isFile())
+ {
+ selectFileURL.setParameter("content.uri", getContentURI(child));
+ writer.print("<li><a href=\"" + selectFileURL +
"\">" + child.getName() + "</a></li>");
+ }
+ }
+ writer.print("</ul><br/>");
+
+ //
+ writer.close();
+ }
+
+
+ protected void doView(RenderRequest req, RenderResponse resp) throws PortletException,
PortletSecurityException, IOException
+ {
+ // Get the URI provided by the portal
+ String uri = req.getParameter("uri");
+
+ // Configure the response
+ resp.setContentType("text/html");
+ PrintWriter writer = resp.getWriter();
+
+ //
+ if (uri == null)
+ {
+ writer.print("No selected file");
+ }
+ else
+ {
+ File file = getFile(uri);
+ FileInputStream in = null;
+ try
+ {
+ in = new FileInputStream(file);
+ FileChannel channel = in.getChannel();
+ byte[] bytes = new byte[(int)channel.size()];
+ ByteBuffer buffer = ByteBuffer.wrap(bytes);
+ channel.read(buffer);
+ writer.write(new String(bytes, 0, bytes.length, "UTF8"));
+ }
+ catch (FileNotFoundException e)
+ {
+ writer.print("No such file " + uri);
+ getPortletContext().log("Cannot find file " + uri, e);
+ }
+ finally
+ {
+ if (in != null)
+ {
+ in.close();
+ }
+ }
+ }
+
+ //
+ writer.close();
+ }
+
+ public void processAction(ActionRequest req, ActionResponse resp) throws
PortletException, PortletSecurityException, IOException
+ {
+ if (EDIT_CONTENT_MODE.equals(req.getPortletMode()))
+ {
+ String contentURI = req.getParameter("content.uri");
+
+ // We just propagate the content URI as a render parameter for the doEditContent
method
+ if (contentURI != null)
+ {
+ resp.setRenderParameter("content.uri", contentURI);
+ }
+ }
+ }
+
+ /**
+ * Return a file from the specified path or null if the file cannot be determined.
+ *
+ * @param contentURI the file path
+ * @return the file or null
+ */
+ protected File getFile(String contentURI) throws IOException
+ {
+ String realPath = getPortletContext().getRealPath(contentURI);
+ if (realPath == null)
+ {
+ throw new IOException("Cannot access war file content");
+ }
+ File file = new File(realPath);
+ if (!file.exists())
+ {
+ throw new IOException("File " + contentURI + " does not
exist");
+ }
+ return file;
+ }
+
+ /**
+ * Return the content uri of the file or null if it cannot be determined.
+ *
+ * @param file the file to get the URI from
+ * @return the URI or null
+ */
+ protected String getContentURI(File file) throws IOException
+ {
+ String rootPath = getPortletContext().getRealPath("/");
+ if (rootPath == null)
+ {
+ throw new IOException("Cannot access war file content");
+ }
+
+ // Make it canonical
+ rootPath = new File(rootPath).getCanonicalPath();
+
+ // Get the portion of the path that is significant for us
+ String filePath = file.getCanonicalPath();
+ return filePath.length() >= rootPath.length() ?
filePath.substring(rootPath.length()) : null;
+ }
+
+ /**
+ * Avoid the WEB-INF directory and list only text files.
+ */
+ private FileFilter filter = new FileFilter()
+ {
+ public boolean accept(File file)
+ {
+ String name = file.getName();
+ if (file.isDirectory())
+ {
+ return !"WEB-INF".equals(name);
+ }
+ else if (file.isFile())
+ {
+ return name.endsWith(".txt");
+ }
+ else
+ {
+ return false;
+ }
+ }
+ };
+}
Modified:
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet-instances.xml
===================================================================
---
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet-instances.xml 2007-03-21
15:55:05 UTC (rev 6789)
+++
trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet-instances.xml 2007-03-21
18:22:58 UTC (rev 6790)
@@ -140,4 +140,10 @@
<portlet-ref>EncodingPortlet</portlet-ref>
</instance>
</deployment>
+ <deployment>
+ <instance>
+ <instance-id>FSContentDrivenPortletInstance</instance-id>
+ <portlet-ref>FSContentDrivenPortlet</portlet-ref>
+ </instance>
+ </deployment>
</deployments>
\ No newline at end of file
Modified: trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet.xml
===================================================================
--- trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet.xml 2007-03-21
15:55:05 UTC (rev 6789)
+++ trunk/core-samples/src/resources/portal-samples-war/WEB-INF/portlet.xml 2007-03-21
18:22:58 UTC (rev 6790)
@@ -334,6 +334,19 @@
<keywords>sample,test</keywords>
</portlet-info>
</portlet>
+ <portlet>
+ <description>File System Content Driven Portlet</description>
+ <portlet-name>FSContentDrivenPortlet</portlet-name>
+ <display-name>File System Content Driven Portlet</display-name>
+
<portlet-class>org.jboss.portal.core.portlet.test.FSContentDrivenPortlet</portlet-class>
+ <supports>
+ <mime-type>text/html</mime-type>
+ </supports>
+ <portlet-info>
+ <title>File Portlet</title>
+ <keywords>sample,test</keywords>
+ </portlet-info>
+ </portlet>
<user-attribute>
<name>user.name.nickName</name>
</user-attribute>
Modified: trunk/core-samples/src/resources/portal-samples-war/WEB-INF/web.xml
===================================================================
--- trunk/core-samples/src/resources/portal-samples-war/WEB-INF/web.xml 2007-03-21
15:55:05 UTC (rev 6789)
+++ trunk/core-samples/src/resources/portal-samples-war/WEB-INF/web.xml 2007-03-21
18:22:58 UTC (rev 6790)
@@ -29,4 +29,15 @@
<listener>
<listener-class> org.jboss.portal.portlet.session.SessionListener
</listener-class>
</listener>
+ <context-param>
+ <param-name>org.jboss.portal.content_type</param-name>
+ <param-value>filesystem</param-value>
+ </context-param>
+ <context-param>
+ <param-name>org.jboss.portal.portlet_instance</param-name>
+ <param-value>FSContentDrivenPortletInstance</param-value>
+ </context-param>
+ <listener>
+
<listener-class>org.jboss.portal.core.servlet.jsp.ContentTypeRegistration</listener-class>
+ </listener>
</web-app>
Added: trunk/core-samples/src/resources/portal-samples-war/dir1/bar.txt
===================================================================
--- trunk/core-samples/src/resources/portal-samples-war/dir1/bar.txt
(rev 0)
+++ trunk/core-samples/src/resources/portal-samples-war/dir1/bar.txt 2007-03-21 18:22:58
UTC (rev 6790)
@@ -0,0 +1 @@
+Bar content
\ No newline at end of file
Added: trunk/core-samples/src/resources/portal-samples-war/dir1/foo.txt
===================================================================
--- trunk/core-samples/src/resources/portal-samples-war/dir1/foo.txt
(rev 0)
+++ trunk/core-samples/src/resources/portal-samples-war/dir1/foo.txt 2007-03-21 18:22:58
UTC (rev 6790)
@@ -0,0 +1 @@
+Foo content
\ No newline at end of file
Added: trunk/core-samples/src/resources/portal-samples-war/dir2/foo.txt
===================================================================
--- trunk/core-samples/src/resources/portal-samples-war/dir2/foo.txt
(rev 0)
+++ trunk/core-samples/src/resources/portal-samples-war/dir2/foo.txt 2007-03-21 18:22:58
UTC (rev 6790)
@@ -0,0 +1 @@
+Foo content
\ No newline at end of file