jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edgarp...@apache.org
Subject svn commit: r447879 - in /jackrabbit/trunk/contrib/jcr-commands: ./ src/bin/ src/java/org/apache/jackrabbit/command/ src/java/org/apache/jackrabbit/command/cli/ src/java/org/apache/jackrabbit/command/ext/ src/java/org/apache/jackrabbit/command/fs/ src/...
Date Tue, 19 Sep 2006 15:19:30 GMT
Author: edgarpoce
Date: Tue Sep 19 08:19:29 2006
New Revision: 447879

URL: http://svn.apache.org/viewvc?view=rev&rev=447879
Log:
- maven 2 descriptor added
- linux run script added
- register node type command added
- JcrCommandsServlet added. an interface to invoke jcr commands from html forms.


Added:
    jackrabbit/trunk/contrib/jcr-commands/pom.xml
    jackrabbit/trunk/contrib/jcr-commands/src/bin/run.sh   (with props)
    jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/fs/FileToInputStream.java
    jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/
    jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/JcrCommandServlet.java
    jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/JcrServletWebContext.java
Modified:
    jackrabbit/trunk/contrib/jcr-commands/project.xml
    jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/CommandHelper.java
    jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/cli/command-line.xml
    jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/cli/command.xml
    jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/ext/RegisterNodeType.java
    jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/resources.properties
    jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/version/Checkin.java

Added: jackrabbit/trunk/contrib/jcr-commands/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/pom.xml?view=auto&rev=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/pom.xml (added)
+++ jackrabbit/trunk/contrib/jcr-commands/pom.xml Tue Sep 19 08:19:29 2006
@@ -0,0 +1,136 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+	Copyright 2006 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.
+	
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+	<modelVersion>4.0.0</modelVersion>
+	<prerequisites>
+		<maven>2.0.4</maven>
+	</prerequisites>
+
+	<!-- POM Identification -->
+
+	<groupId>org.apache.jackrabbit</groupId>
+	<artifactId>jcr-commands</artifactId>
+	<version>1.1-SNAPSHOT</version>
+	<packaging>jar</packaging>
+
+	<name>JCR commands</name>
+
+	<description>JCR commands</description>
+
+	<!-- Dependencies -->
+
+	<dependencies>
+
+		<dependency>
+			<groupId>javax.jcr</groupId>
+			<artifactId>jcr</artifactId>
+			<version>1.0</version>
+			<scope>compile</scope>
+		</dependency>
+
+		<dependency>
+			<groupId>javax.servlet</groupId>
+			<artifactId>servlet-api</artifactId>
+			<scope>provided</scope>
+			<version>2.3</version>
+		</dependency>
+		
+		<dependency>
+			<groupId>org.apache.jackrabbit</groupId>
+			<artifactId>jackrabbit-core</artifactId>
+			<version>1.1-SNAPSHOT</version>
+			<scope>compile</scope>
+		</dependency>
+
+	    <dependency>
+	      <groupId>org.apache.derby</groupId>
+	      <artifactId>derby</artifactId>
+	      <version>10.1.3.1</version>
+	    </dependency>
+    
+		<dependency>
+			<groupId>org.apache.jackrabbit</groupId>
+			<artifactId>jackrabbit-jcr-rmi</artifactId>
+			<version>1.1-SNAPSHOT</version>
+			<type>jar</type>
+		</dependency>
+
+		<dependency>
+			<groupId>commons-chain</groupId>
+			<artifactId>commons-chain</artifactId>
+			<version>1.0</version>
+			<scope>compile</scope>
+		</dependency>
+
+		<dependency>
+			<groupId>commons-cli</groupId>
+			<artifactId>commons-cli</artifactId>
+			<version>1.0</version>
+		</dependency>
+
+		<dependency>
+			<groupId>log4j</groupId>
+			<artifactId>log4j</artifactId>
+			<version>1.2.12</version>
+			<scope>compile</scope>
+		</dependency>
+
+		<dependency>
+			<groupId>commons-jexl</groupId>
+			<artifactId>commons-jexl</artifactId>
+			<version>1.0</version>
+			<scope>compile</scope>
+		</dependency>
+
+	</dependencies>
+
+	<build>
+		<sourceDirectory>src/java</sourceDirectory>
+		<resources>
+			<resource>
+				<directory>src/java</directory>
+				<includes>
+					<include>**/*.xml</include>
+					<include>**/*.dtd</include>
+					<include>**/*.properties</include>
+				</includes>
+			</resource>
+		</resources>
+		<pluginManagement>
+			<plugins>
+				<!-- Compiler -->
+				<plugin>
+					<groupId>org.apache.maven.plugins</groupId>
+					<artifactId>maven-compiler-plugin</artifactId>
+					<inherited>true</inherited>
+					<configuration>
+						<source>1.5</source>
+						<target>1.5</target>
+						<debug>true</debug>
+						<showDeprecation>true</showDeprecation>
+						<showWarnings>true</showWarnings>
+						<optimize>false</optimize>
+					</configuration>
+				</plugin>
+			</plugins>
+		</pluginManagement>
+	</build>
+</project>

Modified: jackrabbit/trunk/contrib/jcr-commands/project.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/project.xml?view=diff&rev=447879&r1=447878&r2=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/project.xml (original)
+++ jackrabbit/trunk/contrib/jcr-commands/project.xml Tue Sep 19 08:19:29 2006
@@ -16,6 +16,13 @@
         </mailingList>
     </mailingLists>
     <dependencies>
+    
+    	<dependency>
+			<groupId>javax.servlet</groupId>
+			<artifactId>servlet-api</artifactId>
+			<version>2.3</version>
+		</dependency>
+		
         <dependency>
             <groupId>commons-chain</groupId>
             <artifactId>commons-chain</artifactId>
@@ -121,11 +128,10 @@
             <type>jar</type>
         </dependency>
         <dependency>
-            <groupId>jsr170</groupId>
+            <groupId>javax.jcr</groupId>
             <artifactId>jcr</artifactId>
             <version>1.0</version>
             <type>jar</type>
-            <url>http://www.day.com/maven/jsr170/jars/jcr-1.0.jar</url>
         </dependency>
         <dependency>
             <groupId>org.apache.jackrabbit</groupId>

Added: jackrabbit/trunk/contrib/jcr-commands/src/bin/run.sh
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/src/bin/run.sh?view=auto&rev=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/src/bin/run.sh (added)
+++ jackrabbit/trunk/contrib/jcr-commands/src/bin/run.sh Tue Sep 19 08:19:29 2006
@@ -0,0 +1,14 @@
+for i in ../lib/*.jar
+do
+   # if the directory is empty, then it will return the input string
+   # this is stupid, so case for it
+   if [ "$i" != "../lib/*.jar" ] ; then
+     if [ -z "$LOCALCLASSPATH" ] ; then
+       LOCALCLASSPATH=$i
+     else
+       LOCALCLASSPATH="$i":"$LOCALCLASSPATH"
+     fi
+   fi
+ done
+
+java -classpath ../classes/:$LOCALCLASSPATH org.apache.jackrabbit.command.cli.JcrClient

Propchange: jackrabbit/trunk/contrib/jcr-commands/src/bin/run.sh
------------------------------------------------------------------------------
    svn:executable = *

Modified: jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/CommandHelper.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/CommandHelper.java?view=diff&rev=447879&r1=447878&r2=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/CommandHelper.java
(original)
+++ jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/CommandHelper.java
Tue Sep 19 08:19:29 2006
@@ -41,16 +41,16 @@
         .getBundle(CommandHelper.class.getPackage().getName() + ".resources");
 
     /** Current node key */
-    private static final String CURRENT_NODE_KEY = "jcr.current";
+    public static final String CURRENT_NODE_KEY = "jcr.current";
 
     /** repository key */
-    private static final String REPOSITORY_KEY = "jcr.repository";
+    public static final String REPOSITORY_KEY = "jcr.repository";
 
     /** session key */
-    private static final String SESSION_KEY = "jcr.session";
+    public static final String SESSION_KEY = "jcr.session";
 
     /** session key */
-    private static final String OUTPUT_KEY = "jcr.output";
+    public static final String OUTPUT_KEY = "jcr.output";
 
     /**
      * should never get called

Modified: jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/cli/command-line.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/cli/command-line.xml?view=diff&rev=447879&r1=447878&r2=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/cli/command-line.xml
(original)
+++ jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/cli/command-line.xml
Tue Sep 19 08:19:29 2006
@@ -3,7 +3,7 @@
   <!ELEMENT commands (command+)>
 
   <!ELEMENT command (alias*, argument*, option*, flag*)>
-  <!ATTLIST command
+  <!ATTLIST command    
   name NMTOKEN #REQUIRED
   description CDATA #IMPLIED
   impl NMTOKEN #IMPLIED >
@@ -579,6 +579,8 @@
   </command>
 
   <command name="registernamespace" impl="registerNamespace">
+    <alias name="registerns"/>  
+    <alias name="regns"/>  
     <argument
       name="prefix"
       position="0"
@@ -591,6 +593,23 @@
       description="word.uri"/>
   </command>
 
+  <command name="registernodetype" impl="registerNodeType">
+    <alias name="registernt"/>  
+    <alias name="regnt"/>  
+    <argument
+      name="srcFsPath"
+      position="0"
+      arg-name="common.fspath"
+      description="common.fspath.desc"
+      required="true"/>
+    <argument
+      name="encoding"
+      position="1"
+      arg-name="common.encoding"
+      description="common.encoding.desc"
+      required="false"/>
+  </command>
+
   <command name="removeitem" impl="removeItem">
     <alias name="remove"/>
     <alias name="delete"/>
@@ -850,6 +869,7 @@
   </command>
 
   <command name="startjackrabbit" impl="startJackrabbit">
+    <alias name="startjr"/>
     <alias name="start"/>
     <argument
       name="config"
@@ -866,6 +886,7 @@
   </command>
 
   <command name="stopjackrabbit" impl="stopJackrabbit">
+    <alias name="stopjr"/>
     <alias name="stop"/>
   </command>
 

Modified: jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/cli/command.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/cli/command.xml?view=diff&rev=447879&r1=447878&r2=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/cli/command.xml
(original)
+++ jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/cli/command.xml
Tue Sep 19 08:19:29 2006
@@ -173,6 +173,14 @@
 		<command name="registerNamespace"
 			className="org.apache.jackrabbit.command.namespace.RegisterNamespace"/>
 
+		<chain name="registerNodeType">
+			<command name="readFile"
+				className="org.apache.jackrabbit.command.fs.FileToInputStream"
+				destKey="cndStream"/>
+			<command name="register"
+				className="org.apache.jackrabbit.command.ext.RegisterNodeType"/>
+		</chain>
+		
 		<command name="removeItem"
 			className="org.apache.jackrabbit.command.core.RemoveItem"/>
 						

Modified: jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/ext/RegisterNodeType.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/ext/RegisterNodeType.java?view=diff&rev=447879&r1=447878&r2=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/ext/RegisterNodeType.java
(original)
+++ jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/ext/RegisterNodeType.java
Tue Sep 19 08:19:29 2006
@@ -16,19 +16,100 @@
  */
 package org.apache.jackrabbit.command.ext;
 
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.Iterator;
+import java.util.List;
+
 import org.apache.commons.chain.Command;
 import org.apache.commons.chain.Context;
+import org.apache.jackrabbit.command.CommandHelper;
+import org.apache.jackrabbit.core.nodetype.NodeTypeDef;
+import org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl;
+import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
+import org.apache.jackrabbit.core.nodetype.compact.CompactNodeTypeDefReader;
 
 /**
  * Register a node type
  */
 public class RegisterNodeType implements Command {
 
-    /**
-     * {@inheritDoc}
-     */
-    public boolean execute(Context arg0) throws Exception {
-        throw new UnsupportedOperationException("not implemented yet");
-    }
+	private String cndStreamKey = "cndStream";
+
+	private String cndName = "cndName";
+
+	private String encodingKey = "encoding";
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public boolean execute(Context ctx) throws Exception {
+		InputStream is = (InputStream) ctx.get(cndStreamKey);
+		Reader reader = null;
+		if (ctx.containsKey(this.encodingKey)) {
+			reader = new InputStreamReader(is, "utf-8");
+		} else {
+			reader = new InputStreamReader(is, (String) ctx
+					.get(this.encodingKey));
+		}
+
+		String cndName = null;
+		if (ctx.containsKey(this.cndName)) {
+			cndName = (String) ctx.get(this.cndName);
+		} else {
+			cndName = "";
+		}
+
+		CompactNodeTypeDefReader cndReader = new CompactNodeTypeDefReader(
+				reader, cndName);
+
+		// Get the List of NodeTypeDef objects
+		List ntdList = cndReader.getNodeTypeDefs();
+
+		// Get the NodeTypeManager from the Workspace.
+		// Note that it must be cast from the generic JCR NodeTypeManager to the
+		// Jackrabbit-specific implementation.
+		NodeTypeManagerImpl ntmgr = (NodeTypeManagerImpl) CommandHelper
+				.getSession(ctx).getWorkspace().getNodeTypeManager();
+
+		// Acquire the NodeTypeRegistry
+		NodeTypeRegistry ntreg = ntmgr.getNodeTypeRegistry();
+
+		// Loop through the prepared NodeTypeDefs
+		for (Iterator i = ntdList.iterator(); i.hasNext();) {
+
+			// Get the NodeTypeDef...
+			NodeTypeDef ntd = (NodeTypeDef) i.next();
+
+			// ...and register it
+			ntreg.registerNodeType(ntd);
+		}
+
+		return false;
+	}
+
+	public String getCndName() {
+		return cndName;
+	}
+
+	public void setCndName(String cndName) {
+		this.cndName = cndName;
+	}
+
+	public String getCndStreamKey() {
+		return cndStreamKey;
+	}
+
+	public void setCndStreamKey(String cndStreamKey) {
+		this.cndStreamKey = cndStreamKey;
+	}
+
+	public String getEncodingKey() {
+		return encodingKey;
+	}
 
+	public void setEncodingKey(String encodingKey) {
+		this.encodingKey = encodingKey;
+	}
 }

Added: jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/fs/FileToInputStream.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/fs/FileToInputStream.java?view=auto&rev=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/fs/FileToInputStream.java
(added)
+++ jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/fs/FileToInputStream.java
Tue Sep 19 08:19:29 2006
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.command.fs;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+
+import org.apache.commons.chain.Command;
+import org.apache.commons.chain.Context;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Puts an java.io.InputStream in the context from an Fs path
+ * 
+ */
+public class FileToInputStream implements Command {
+
+	/** logger */
+	private static Log log = LogFactory.getLog(FileToInputStream.class);
+
+	// ---------------------------- < keys >
+
+	/** file key */
+	private String srcFsPathKey = "srcFsPath";
+
+	/** target context key */
+	private String destKey = "dest";
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public boolean execute(Context ctx) throws Exception {
+		String from = (String) ctx.get(this.srcFsPathKey);
+		File file = new File(from);
+		log.debug("putting " + file.getAbsolutePath() + " InputStream under "
+				+ this.destKey);
+		InputStream is = new BufferedInputStream(new FileInputStream(file));
+		ctx.put(this.destKey, is);
+		return false;
+	}
+
+	public String getDestKey() {
+		return destKey;
+	}
+
+	public void setDestKey(String destKey) {
+		this.destKey = destKey;
+	}
+
+	public String getSrcFsPathKey() {
+		return srcFsPathKey;
+	}
+
+	public void setSrcFsPathKey(String srcFsPathKey) {
+		this.srcFsPathKey = srcFsPathKey;
+	}
+}

Modified: jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/resources.properties
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/resources.properties?view=diff&rev=447879&r1=447878&r2=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/resources.properties
(original)
+++ jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/resources.properties
Tue Sep 19 08:19:29 2006
@@ -92,6 +92,7 @@
 cmd.refresh.keepChanges            = keep or discard the changes flag
 cmd.refreshlock                    = Reset the Lock timer
 cmd.registernamespace              = Register a namespace
+cmd.registernodetype               = Register node types from a cnd file
 cmd.removeitem                     = Remove the item at the given path
 cmd.removeitems                    = Remove any Item under the given Node that match the
given name pattern
 cmd.removelocktoken                = Remove the given Lock token to the current Session
@@ -125,6 +126,8 @@
 cmd.xpathquery                     = XPath query
 cmd.xpathquery.statement           = statement
 
+common.encoding              = Encoding
+common.encoding.desc         = see http://en.wikipedia.org/wiki/Character_encoding
 common.exportview.from       = Source node
 common.exportview.noRecurse  = if it's true it doesn't include the child nodes
 common.exportview.skipBinary = if it's true the binary properties will be serialized as if
they are empty

Modified: jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/version/Checkin.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/version/Checkin.java?view=diff&rev=447879&r1=447878&r2=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/version/Checkin.java
(original)
+++ jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/version/Checkin.java
Tue Sep 19 08:19:29 2006
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.command.version;
 
+import javax.jcr.version.Version;
+
 import org.apache.commons.chain.Command;
 import org.apache.commons.chain.Context;
 import org.apache.commons.logging.Log;
@@ -32,6 +34,9 @@
     // ---------------------------- < keys >
     /** node path */
     private String pathKey = "path";
+    
+    /** target version number **/
+    private String targetVersion = "version";
 
     /**
      * {@inheritDoc}
@@ -41,7 +46,8 @@
         if (log.isDebugEnabled()) {
             log.debug("cheking in node at " + path);
         }
-        CommandHelper.getNode(ctx, path).checkin();
+        Version v = CommandHelper.getNode(ctx, path).checkin();
+        ctx.put(this.targetVersion, v.getName()) ;
         return false;
     }
 
@@ -59,4 +65,12 @@
     public void setPathKey(String pathKey) {
         this.pathKey = pathKey;
     }
+
+	public String getTargetVersion() {
+		return targetVersion;
+	}
+
+	public void setTargetVersion(String targetVersion) {
+		this.targetVersion = targetVersion;
+	}
 }

Added: jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/JcrCommandServlet.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/JcrCommandServlet.java?view=auto&rev=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/JcrCommandServlet.java
(added)
+++ jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/JcrCommandServlet.java
Tue Sep 19 08:19:29 2006
@@ -0,0 +1,192 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.command.web;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.util.Properties;
+
+import javax.jcr.Repository;
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+import javax.naming.InitialContext;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.chain.Catalog;
+import org.apache.commons.chain.Command;
+import org.apache.commons.chain.config.ConfigParser;
+import org.apache.commons.chain.impl.CatalogFactoryBase;
+import org.apache.jackrabbit.command.CommandHelper;
+import org.apache.log4j.Logger;
+
+/**
+ * <p>
+ * Servlet that handler jcr requests with commons-chain.<br>
+ * Usage scenarion example: JCR Ajax server.
+ * </p>
+ */
+public class JcrCommandServlet extends HttpServlet {
+
+	private static final long serialVersionUID = 1L;
+
+	private static Logger log = Logger.getLogger(JcrCommandServlet.class);
+
+	private static String CATALOG_KEY = "catalog";
+
+	private static String JNDI_PROPERTIES_KEY = "jndi.properties";
+
+	private static String JNDI_ADDRESS_KEY = "jndi.address";
+
+	private static String USER_KEY = "user";
+
+	private static String PASSWORD_KEY = "password";
+
+	public static String COMMAND_KEY = "command";
+
+	public static String OUTPUT_FLAVOR_KEY = "flavor";
+
+	private static String DEFAULT_OUTPUT_FLAVOR = "text/plain";
+
+	/**
+	 * jcr repository
+	 */
+	private Repository repository;
+
+	/**
+	 * user name to login
+	 */
+	private String user;
+
+	/**
+	 * password to login
+	 */
+	private String password;
+
+	/**
+	 * commons chain catalog
+	 */
+	private Catalog catalog;
+
+	public void service(HttpServletRequest req, HttpServletResponse res)
+			throws ServletException, IOException {
+
+		// read parameters
+		String cmdName = req.getParameter(COMMAND_KEY);
+		String flavor = req.getParameter(OUTPUT_FLAVOR_KEY);
+		if (flavor == null) {
+			flavor = DEFAULT_OUTPUT_FLAVOR;
+		}
+
+		// set content type
+		res.setContentType(flavor);
+
+		// process command
+		Session session = null;
+		try {
+
+			if (this.user == null) {
+				session = this.repository.login(req.getRemoteUser());
+			} else {
+				session = this.repository.login(new SimpleCredentials(
+						this.user, this.password.toCharArray()));
+			}
+
+			PrintWriter pw = null;
+			try {
+				// create context
+				JcrServletWebContext ctx = new JcrServletWebContext(session,
+						getServletContext(), req, res);
+
+				// get writer
+				pw = CommandHelper.getOutput(ctx);
+
+				// lookup command
+				Command cmd = catalog.getCommand(cmdName);
+				if (cmd == null) {
+					throw new IllegalArgumentException("command " + cmdName
+							+ " not found");
+				}
+
+				// execute command
+				cmd.execute(ctx);
+
+				// save changes
+				session.save();
+
+				// write success message
+				pw.write("success");
+
+			} catch (Exception e) {
+				// write error message
+				pw.write("an error occured\n");
+				pw.write(e.getMessage());
+
+			} finally {
+				pw.close();
+
+			}
+
+		} catch (Exception e) {
+			throw new ServletException(e);
+
+		} finally {
+			if (session != null) {
+				session.logout();
+			}
+
+		}
+	}
+
+	public void init(ServletConfig cfg) throws ServletException {
+		super.init(cfg);
+
+		String jndiProperties = cfg.getInitParameter(JNDI_PROPERTIES_KEY);
+		String jndiAddress = cfg.getInitParameter(JNDI_ADDRESS_KEY);
+		this.user = cfg.getInitParameter(USER_KEY);
+		this.password = cfg.getInitParameter(PASSWORD_KEY);
+
+		try {
+
+			// parse catalog
+			ConfigParser parser = new ConfigParser();
+			parser.parse(JcrCommandServlet.class.getClassLoader().getResource(
+					cfg.getInitParameter(CATALOG_KEY)));
+			catalog = CatalogFactoryBase.getInstance().getCatalog();
+
+			// get repository
+			InputStream is = new ByteArrayInputStream(jndiProperties
+					.getBytes("UTF-8"));
+			Properties props = new Properties();
+			props.load(is);
+			InitialContext ctx = new InitialContext(props);
+			this.repository = (Repository) ctx.lookup(jndiAddress);
+
+		} catch (Exception e) {
+			String msg = "unable to get repository through jndi";
+			log.error(msg, e);
+			throw new ServletException(msg, e);
+
+		}
+	}
+
+}

Added: jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/JcrServletWebContext.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/JcrServletWebContext.java?view=auto&rev=447879
==============================================================================
--- jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/JcrServletWebContext.java
(added)
+++ jackrabbit/trunk/contrib/jcr-commands/src/java/org/apache/jackrabbit/command/web/JcrServletWebContext.java
Tue Sep 19 08:19:29 2006
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.command.web;
+
+import java.io.PrintWriter;
+
+import javax.jcr.Session;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.chain.web.servlet.ServletWebContext;
+import org.apache.jackrabbit.command.CommandHelper;
+
+/**
+ * Commons chain context that wraps an httpservletrequest and exposes attributes
+ * from servlet scopes (request, session, application) as context variables.
+ * 
+ */
+public class JcrServletWebContext extends ServletWebContext {
+
+	private static final long serialVersionUID = 1L;
+
+	private Session session;
+
+	private PrintWriter out;
+
+	public JcrServletWebContext(Session session, ServletContext ctx,
+			HttpServletRequest req, HttpServletResponse res) {
+		super(ctx, req, res);
+		this.session = session;
+		try {
+			this.out = res.getWriter();
+		} catch (Exception e) {
+			throw new IllegalArgumentException(
+					"the given response fails to provide an outputstream");
+		}
+	}
+
+	public Object get(Object key) {
+
+		// known context variables
+		try {
+
+			if (key.equals(CommandHelper.REPOSITORY_KEY)) {
+				return session.getRepository();
+			}
+
+			if (key.equals(CommandHelper.SESSION_KEY)) {
+				return session;
+			}
+
+			if (key.equals(CommandHelper.CURRENT_NODE_KEY)) {
+				return session.getRootNode();
+			}
+
+			if (key.equals(CommandHelper.OUTPUT_KEY)) {
+				return this.out;
+			}
+
+		} catch (Exception e) {
+			return null;
+		}
+
+		// lookup in the underlying map
+		if (super.get(key)!=null) {
+			return super.get(key) ;
+		}
+
+		// lookup in request parameters
+		if (key instanceof String
+				&& this.getRequest().getParameter((String) key) != null) {
+			return this.getRequest().getParameter((String) key);
+		}
+
+		// lookup in request attributes
+		if (key instanceof String && this.getRequestScope().get(key) != null) {
+			return this.getRequestScope().get(key);
+		}
+
+		// lookup in session attributes
+		if (key instanceof String && this.getSessionScope().get(key) != null) {
+			return this.getSessionScope().get(key);
+		}
+
+		// lookup in application attributes
+		if (key instanceof String
+				&& this.getApplicationScope().get(key) != null) {
+			return this.getSessionScope().get(key);
+		}
+		
+		return null ;
+
+	}
+}



Mime
View raw message