You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2010/05/04 18:15:05 UTC

svn commit: r940931 - in /incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver: AbstractFileServer.java FileServer.java util/StaticWebResource.java

Author: reto
Date: Tue May  4 16:15:04 2010
New Revision: 940931

URL: http://svn.apache.org/viewvc?rev=940931&view=rev
Log:
CLEREZZA-200 making it easier to serve static files from a jax-rs resource

Added:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/AbstractFileServer.java
Removed:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/util/StaticWebResource.java
Modified:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/FileServer.java

Added: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/AbstractFileServer.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/AbstractFileServer.java?rev=940931&view=auto
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/AbstractFileServer.java (added)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/AbstractFileServer.java Tue May  4 16:15:04 2010
@@ -0,0 +1,60 @@
+/*
+ *  Copyright 2010 reto.
+ * 
+ *  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.
+ *  under the License.
+ */
+
+package org.apache.clerezza.web.fileserver;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.WebApplicationException;
+import org.wymiwyg.commons.util.dirbrowser.PathNode;
+
+/**
+ * An abstract superclass of jax-rs resources serving files. Subclasses
+ * overwrite the <code>getPathNode</code>-method to return the root of their 
+ * hierachy.
+ *
+ * @author reto
+ */
+public abstract class AbstractFileServer {
+
+	/**
+	 * This method return the root of the served hierarchy. For example
+	 * if the instance is to server all Files in /var/www, this method would
+	 * return an instance of org.wymiwyg.commons.util.dirbrowser.FilePathNode
+	 * initialized with "/var/www".
+	 *
+	 * @return the root of the served hierarchy
+	 */
+	protected abstract PathNode getRootNode();
+
+	@Path("{path:.*}")
+	@GET
+	public PathNode getNode(@PathParam("path") String path) {
+		String[] pathSections = path.split("/");
+		PathNode current = getRootNode();
+		for (String pathSection : pathSections) {
+			current = current.getSubPath(pathSection);
+			if (!current.exists()) {
+				throw new WebApplicationException(404);
+			}
+		}
+		return current;
+
+	}
+
+}

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/FileServer.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/FileServer.java?rev=940931&r1=940930&r2=940931&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/FileServer.java (original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.web.fileserver/src/main/java/org/apache/clerezza/web/fileserver/FileServer.java Tue May  4 16:15:04 2010
@@ -18,35 +18,95 @@
  */
 package org.apache.clerezza.web.fileserver;
 
+import java.io.File;
+import java.net.URL;
 import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.WebApplicationException;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.wymiwyg.commons.util.dirbrowser.FilePathNode;
 import org.wymiwyg.commons.util.dirbrowser.PathNode;
 
 /**
+ * A utility class providing a FileServer that can be configured in different 
+ * ways. It provides an alternative to {@link AbstractFileServer} for jax-rs
+ * resources serviceng static files to override.
  *
  * @author reto
  */
 @Path("/")
-public class FileServer {
-	private PathNode pathNode;
+public class FileServer extends AbstractFileServer {
+	private PathNode rootPathNode;
 
+	/**
+	 * Construct a new instance that has to be configured using one of the 
+	 * configure-methods.
+	 */
+	public FileServer() {}
+
+	/**
+	 * creates a new instance configured with specified pathnode as root
+	 *
+	 * @param pathNode the root of the PathNode hierarchy
+	 */
 	public FileServer(PathNode pathNode) {
-		this.pathNode = pathNode;
+		this.rootPathNode = pathNode;
 	}
 
-	@Path("{path}")
-	public PathNode getNode(@PathParam("path") String path) {
-		String[] pathSections = path.split("/");
-		PathNode current = pathNode;
-		for (String pathSection : pathSections) {
-			current = current.getSubPath(pathSection);
-			if (!current.exists()) {
-				throw new WebApplicationException(404);
-			}
-		}
-		return current;
 
+	/**
+	 * configures the instance with the specified PathNode
+	 *
+	 * @param pathNode the root of the PathNode hierarchy
+	 */
+	public void configure(PathNode pathNode) {
+		this.rootPathNode = pathNode;
+	}
+
+	/**
+	 * Configures the instance to use the 'staticweb' folder next to (sub-)class
+	 * in the bundle associated to the specified context.
+	 *
+	 * @param context The scr component context of the bundle containing the
+	 *		'staticweb' directory where the subclass is located.
+	 */
+	public void configure(BundleContext context) {
+		configure(context, "staticweb");
+	}
+
+	/**
+	 * Sets up a path in a bundle or file system to be exposed over a
+	 * <code>org.apache.clerezza.web.fileserver.FileServer</code>. The path is
+	 * relative to the locationof the class.
+	 *
+	 * @param context The bundle context of the bundle containing the path.
+	 * @param path the path where the file are to be exposed
+	 */
+	public void configure(BundleContext context, String path) {
+		PathNode pathNode;
+		Bundle bundle = context.getBundle();
+		URL resourceDir = getClass().getResource(path);
+		pathNode = new BundlePathNode(bundle, resourceDir.getPath());
+		configure(pathNode);
+	}
+	/**
+	 * configures the instance to use the specified directory as root
+	 *
+	 * @param rootDir the root of the serverd hierarchy
+	 */
+	public void configure(File rootDir) {
+		configure(new FilePathNode(rootDir));
+	}
+
+	/**
+	 * resets the configuration
+	 */
+	public void reset() {
+		rootPathNode = null;
+	}
+
+	@Override
+	protected PathNode getRootNode() {
+		return rootPathNode;
 	}
 
 }