kstaken 02/03/21 22:26:19
Added: java/scratchpad/src/org/apache/xindice/server/rpc Client.java RPCDefaultMessage.java RPCMessage.java RPCMessageHandler.java RPCMessageInterface.java java/scratchpad/src/org/apache/xindice/server/rpc/messages GetDocument.java Log: Prototype message oriented XML-RPC interface Revision Changes Path 1.1 xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/Client.java Index: Client.java =================================================================== package org.apache.xindice.server.rpc; /* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xindice" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999-2001, The dbXML * Group, L.L.C., http://www.dbxmlgroup.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * $Id: Client.java,v 1.1 2002/03/22 06:26:19 kstaken Exp $ */ import java.util.*; import org.apache.xmlrpc.*; /** * */ public class Client { public static Hashtable run(Hashtable message) throws Exception { XmlRpcClient client = new XmlRpcClient("http://localhost:4080/"); Vector params = new Vector(); params.addElement(message); return (Hashtable) client.execute("run", params); } public static void main(String[] args) throws Exception { Hashtable message = new Hashtable(); message.put("message", "GetDocument"); message.put("key", "test"); message.put("collection", "/db/test3"); Hastable result = run(message); System.out.println(result.get("result")); System.out.println(((String) result.get("result")).getEncoding()); } } 1.1 xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/RPCDefaultMessage.java Index: RPCDefaultMessage.java =================================================================== package org.apache.xindice.server.rpc; /* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xindice" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999-2001, The dbXML * Group, L.L.C., http://www.dbxmlgroup.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * $Id: RPCDefaultMessage.java,v 1.1 2002/03/22 06:26:19 kstaken Exp $ */ import java.util.Hashtable; import org.apache.xindice.core.*; /** * @author kstaken */ public class RPCDefaultMessage implements RPCMessage { public Hashtable execute(Hashtable message) throws Exception { return null; } /** * Retrieves a Collection instance based on the path provided in name. * * @param name The collection to retrieve * @return The Collection value * @exception Exception * @author kstaken */ protected Collection getCollection( String name ) throws Exception { // name must start with a / if ( name.startsWith( "/" ) ) { // find the database name. We just skip the first slash int colIndex = name.indexOf( '/', 1 ); // We assume there's no collection specified String dbName = name.substring( 1 );; String colName = "/"; // if colIndex isn't -1 then we need to pick out the db and collection if ( colIndex != -1 ) { dbName = name.substring( 1, colIndex ); // The rest of the name locates the collection colName = name.substring( colIndex + 1 ); } Database db = Database.getDatabase( dbName ); if ( db == null ) { throw new Exception( "Database " + dbName + " could not be found" ); } Collection col = db.getCollection( colName ); if ( col == null ) { throw new Exception( "Collection " + colName + " could not be found" ); } return col; } else { throw new Exception( "Collection name must begin with a '/'" ); } } } 1.1 xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/RPCMessage.java Index: RPCMessage.java =================================================================== package org.apache.xindice.server.rpc; /* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xindice" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999-2001, The dbXML * Group, L.L.C., http://www.dbxmlgroup.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * $Id: RPCMessage.java,v 1.1 2002/03/22 06:26:19 kstaken Exp $ */ import java.util.*; /** * */ public interface RPCMessage { public Hashtable execute(Hashtable message) throws Exception; } 1.1 xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/RPCMessageHandler.java Index: RPCMessageHandler.java =================================================================== package org.apache.xindice.server.rpc; /* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xindice" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999-2001, The dbXML * Group, L.L.C., http://www.dbxmlgroup.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * $Id: RPCMessageHandler.java,v 1.1 2002/03/22 06:26:19 kstaken Exp $ */ /** * */ import org.apache.xindice.core.*; import org.apache.xindice.server.*; import org.apache.xindice.util.*; import java.io.*; import java.util.*; import org.apache.xmlrpc.*; /** * XMLRPCHandler provides XML-RPC access to basic operations for Xindice */ public final class RPCMessageHandler extends SimpleScriptComponent implements ScriptFilter { protected String[] databases = null; protected XmlRpcServer xmlrpc; public RPCMessageHandler() { xmlrpc = new XmlRpcServer(); xmlrpc.addHandler ("$default", new RPCMessageInterface()); } public void setKernel(Kernel kernel) { super.setKernel(kernel); databases = Database.listDatabases(); } public void setConfig(Configuration config) { this.config = config; String name = config.getAttribute(NAME); } public boolean isPackageFiltered(String name) { return true; } public boolean run(Script script, Gateway gateway) { String pathinfo = gateway.getRequestHeader("PATH_INFO"); // We only handle POST requests if ( ! gateway.getRequestHeader("REQUEST_METHOD").equals("POST") ) { return true; } DataOutputStream dos = new DataOutputStream(gateway.getOutputStream()); try { byte[] result = xmlrpc.execute(gateway.getInputStream()); gateway.setResponseHeader("Content-type", "text/xml"); gateway.setResponseHeader("Content-length", Integer.toString(result.length)); dos.write(result); dos.flush(); gateway.send(200); gateway.logMessage(LogManager.LOG_GENERAL, gateway.getRequestHeader("REQUEST_METHOD") + " " + pathinfo + " " + gateway.getResponseHeader("Content-length")); } catch ( Exception e ) { gateway.sendException(this.getName(), e); } return false; } } 1.1 xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/RPCMessageInterface.java Index: RPCMessageInterface.java =================================================================== package org.apache.xindice.server.rpc; /* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xindice" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999-2001, The dbXML * Group, L.L.C., http://www.dbxmlgroup.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * $Id: RPCMessageInterface.java,v 1.1 2002/03/22 06:26:19 kstaken Exp $ */ import java.util.*; /** * */ public final class RPCMessageInterface { public static final String MESSAGE_PARAM = "message"; public Hashtable run(Hashtable message) throws Exception { // The method determines what class we load to handle the message. RPCMessage handler = null; try { handler = (RPCMessage) Class.forName("org.apache.xindice.server.rpc.messages." + message.get(MESSAGE_PARAM)).newInstance(); } catch (Exception e) { e.printStackTrace(); } return handler.execute(message); } } 1.1 xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/messages/GetDocument.java Index: GetDocument.java =================================================================== package org.apache.xindice.server.rpc.messages; /* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xindice" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999-2001, The dbXML * Group, L.L.C., http://www.dbxmlgroup.com. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * $Id: GetDocument.java,v 1.1 2002/03/22 06:26:19 kstaken Exp $ */ import java.util.Hashtable; import org.w3c.dom.*; import org.apache.xindice.core.*; import org.apache.xindice.xml.*; import org.apache.xindice.server.rpc.*; /** * */ public class GetDocument extends RPCDefaultMessage { public static final String RESULT_PARAM = "result"; public static final String KEY_PARAM = "key"; public static final String COLLECTION_PARAM = "collection"; // TODO: should have a simple way to validate params. public Hashtable execute(Hashtable message) throws Exception { Collection col = getCollection( (String) message.get(COLLECTION_PARAM) ); Document doc = col.getDocument( (String) message.get(KEY_PARAM) ); if ( doc == null ) { throw new Exception( "Document not found " + (String) message.get(KEY_PARAM) ); } Hashtable result = new Hashtable(); result.put(RESULT_PARAM, TextWriter.toString( doc )); return result; } }