Author: natalia Date: Mon Mar 31 18:53:39 2008 New Revision: 643240 URL: http://svn.apache.org/viewvc?rev=643240&view=rev Log: ResourceManagementService for insert/update operations
Added: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementService.java (with props) xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementServiceImpl.java (with props) xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/SetResource.java (with props) xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/UpdateResource.java (with props) xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/ResourceManagementTest.java (with props) Modified: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/XindiceCollection.java xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/embed/CollectionImpl.java xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/xmlrpc/CollectionImpl.java xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/InsertDocument.java xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/InsertResource.java xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/IntegrationTests.java Modified: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/XindiceCollection.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/XindiceCollection.java?rev=643240&r1=643239&r2=643240&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/XindiceCollection.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/XindiceCollection.java Mon Mar 31 18:53:39 2008 @@ -26,6 +26,8 @@ import org.apache.xindice.client.xmldb.services.TextQueryServiceImpl; import org.apache.xindice.client.xmldb.services.XPathQueryServiceImpl; import org.apache.xindice.client.xmldb.services.XUpdateQueryServiceImpl; +import org.apache.xindice.client.xmldb.services.ResourceManagementService; +import org.apache.xindice.client.xmldb.services.ResourceManagementServiceImpl; import org.apache.xindice.core.FaultCodes; import org.apache.xindice.core.meta.MetaData; import org.apache.xindice.xml.NodeSource; @@ -95,6 +97,9 @@ final MetaService meta = new MetaService(); registerService(meta); + final ResourceManagementService rms = new ResourceManagementServiceImpl(); + registerService(rms); + try { final CollectionManagementServiceImpl manager = new CollectionManagementServiceImpl(); registerService(manager); @@ -354,4 +359,8 @@ * @exception XMLDBException thrown if unable to obtain meta information */ public abstract void setMetaData(String name, MetaData meta) throws XMLDBException; + + public abstract void insertResource(Resource resource) throws XMLDBException; + + public abstract void updateResource(Resource resource) throws XMLDBException; } Modified: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/embed/CollectionImpl.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/embed/CollectionImpl.java?rev=643240&r1=643239&r2=643240&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/embed/CollectionImpl.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/embed/CollectionImpl.java Mon Mar 31 18:53:39 2008 @@ -24,6 +24,7 @@ import org.apache.xindice.client.xmldb.resources.BinaryResourceImpl; import org.apache.xindice.client.xmldb.resources.XMLResourceImpl; import org.apache.xindice.core.Collection; +import org.apache.xindice.core.DBException; import org.apache.xindice.core.Database; import org.apache.xindice.core.FaultCodes; import org.apache.xindice.core.data.Entry; @@ -33,7 +34,6 @@ import org.apache.xindice.util.Configuration; import org.apache.xindice.xml.TextWriter; import org.apache.xindice.xml.dom.DocumentImpl; - import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -171,66 +171,169 @@ * * @param res the resource to store in the database. * @exception XMLDBException with expected error codes.<br /> - * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor - * specific errors that occur.<br /> - * <code>ErrorCodes.INVALID_RESOURCE</code> if the <code>Resource</code> is - * not valid. - * <code>ErrorCodes.COLLECTION_CLOSED</code> if the <code>close</code> - * method has been called on the <code>Collection</code><br /> + * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor + * specific errors that occur.<br /> + * <code>ErrorCodes.INVALID_RESOURCE</code> if the <code>Resource</code> is + * not valid..<br /> + * <code>ErrorCodes.COLLECTION_CLOSED</code> if the <code>close</code> + * method has been called on the <code>Collection</code><br /> */ public void storeResource(Resource res) throws XMLDBException { if (res.getContent() == null) { - throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, - "No resource data"); + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "No resource data"); } checkOpen(); + + try { + if (res instanceof XMLResource) { + Node content = getXMLContent(res); + if (res.getId() != null) { + col.setDocument(res.getId(), (Document) content); + } else { + String name = col.insertDocument((Document) content).toString(); + ((XMLResourceImpl) res).setId(name); + } + + } else if (res instanceof BinaryResource) { + byte[] bytes = getBinaryContent(res); + if (res.getId() != null) { + col.setBinary(res.getId(), bytes); + } else { + String name = col.insertBinary(bytes).toString(); + ((BinaryResourceImpl) res).setId(name); + } - if (res instanceof BinaryResource) { - Object content = res.getContent(); - byte[] bytes; - if (content instanceof byte[]) { - bytes = (byte[]) content; } else { throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, - "The contents of a binary resource must have type byte[]."); + "Only XMLResource and BinaryResource supported"); } + } catch (DBException e) { + throw FaultCodes.createXMLDBException(e.faultCode, e.getMessage(), e); + } catch (XMLDBException e) { + throw e; + } catch (Exception e) { + throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_RESOURCE, + "Invalid resource: " + res.getId(), e); + } + } + + /** + * Inserts the provided resource into the database. + * + * @param res the resource to insert. + * @exception XMLDBException with expected error codes.<br /> + * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor + * specific errors that occur.<br /> + * <code>ErrorCodes.INVALID_RESOURCE</code> if the <code>Resource</code> is + * not valid..<br /> + * <code>ErrorCodes.COLLECTION_CLOSED</code> if the <code>close</code> + * method has been called on the <code>Collection</code><br /> + */ + public void insertResource(Resource res) throws XMLDBException { + if (res.getContent() == null) { + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "No resource data"); + } + + checkOpen(); - try { + try { + if (res instanceof BinaryResource) { + byte[] bytes = getBinaryContent(res); if (res.getId() != null) { - col.setBinary(res.getId(), bytes); + col.insertBinary(res.getId(), bytes); } else { String name = col.insertBinary(bytes).toString(); ((BinaryResourceImpl) res).setId(name); } - } catch (Exception e) { - throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_RESOURCE, - "Invalid resource:" + res.getId(), e); - } - - } else if (res instanceof XMLResource) { - try { - Node content = ((XMLResourceImpl) res).getContentAsDOM(); - if (content != null && content instanceof Document) { - if (res.getId() != null) { - col.setDocument(res.getId(), (Document) content); - } else { - String name = col.insertDocument((Document) content).toString(); - ((XMLResourceImpl) res).setId(name); - } + } else if (res instanceof XMLResource) { + Node content = getXMLContent(res); + if (res.getId() != null) { + col.insertDocument(res.getId(), (Document) content); } else { - throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, - "A resource must be a document in order to be stored."); + String name = col.insertDocument((Document) content).toString(); + ((XMLResourceImpl) res).setId(name); } - } catch (Exception e) { - throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_RESOURCE, - "Invalid resource: " + res.getId(), e); + + } else { + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, + "Only XMLResource and BinaryResource supported"); } + } catch (DBException e) { + throw FaultCodes.createXMLDBException(e.faultCode, e.getMessage(), e); + } catch (XMLDBException e) { + throw e; + } catch (Exception e) { + throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_RESOURCE, + "Invalid resource: " + res.getId(), e); + } + } + /** + * Updates the provided resource in the database. + * + * @param res the resource to update. + * @exception XMLDBException with expected error codes.<br /> + * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor + * specific errors that occur.<br /> + * <code>ErrorCodes.INVALID_RESOURCE</code> if the <code>Resource</code> is + * not valid.<br/> + * <code>ErrorCodes.COLLECTION_CLOSED</code> if the <code>close</code> + * method has been called on the <code>Collection</code><br /> + */ + public void updateResource(Resource res) throws XMLDBException { + if (res.getContent() == null) { + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "No resource data"); + } + + if (res.getId() == null) { + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Resource must have valid ID."); + } + + checkOpen(); + + try { + if (res instanceof BinaryResource) { + byte[] bytes = getBinaryContent(res); + col.updateBinary(res.getId(), bytes); + + } else if (res instanceof XMLResource) { + Node content = getXMLContent(res); + col.updateDocument(res.getId(), (Document) content); + + } else { + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, + "Only XMLResource and BinaryResource supported"); + } + } catch (DBException e) { + throw FaultCodes.createXMLDBException(e.faultCode, e.getMessage(), e); + } catch (XMLDBException e) { + throw e; + } catch (Exception e) { + throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_RESOURCE, + "Invalid resource: " + res.getId(), e); + } + } + + private byte[] getBinaryContent(Resource res) throws XMLDBException { + Object content = res.getContent(); + byte[] bytes; + if (content instanceof byte[]) { + bytes = (byte[]) content; } else { throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, - "Only XMLResource and BinaryResource supported"); + "The contents of a binary resource must have type byte[]."); + } + return bytes; + } + + private Node getXMLContent(Resource res) throws XMLDBException { + Node content = ((XMLResourceImpl) res).getContentAsDOM(); + if (content == null || !(content instanceof Document)) { + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, + "A resource must be a document in order to be stored."); } + return content; } /* see superclass for documentation */ Added: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementService.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementService.java?rev=643240&view=auto ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementService.java (added) +++ xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementService.java Mon Mar 31 18:53:39 2008 @@ -0,0 +1,63 @@ +/* + * 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. + * + * $Id$ + */ + +package org.apache.xindice.client.xmldb.services; + +import org.xmldb.api.base.Service; +import org.xmldb.api.base.XMLDBException; +import org.xmldb.api.base.Resource; + +/** + * Service for additional Xindice-specific resource operations. + * + * @version $Revision$, $Date$ + */ +public interface ResourceManagementService extends Service { + + /** + * Insert new resource into a collection. For this operation to succeed, + * collection must not contain a resource with the same key as resource + * being inserted. If a resource with that key already exists, method will + * throw XMLDBException. + * @param res Resource to be inserted + * @throws XMLDBException If there is a resource with the same key in the + * collection already. + */ + public void insertResource(Resource res) throws XMLDBException; + + /** + * Update existing resource. For this operation to succeed, collection + * must already contain a resource with the same key as resource passed + * as a parameter. Resource ID cannot be null. If there is no such + * resource, method will throw XMLDBException. + * @param res Resource to be updated + * @throws XMLDBException If there is no resource with the same key in the + * collection. + */ + public void updateResource(Resource res) throws XMLDBException; + + /** + * Store resource in a collection. If there is no resource with the same key + * in the collection, new resource will be created, otherwise existing + * resource will be updated. + * @param res Resource to be stored + * @throws XMLDBException Collection failed + */ + public void storeResource(Resource res) throws XMLDBException; +} Propchange: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementService.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementService.java ------------------------------------------------------------------------------ svn:keywords = Id Revision Author Date Added: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementServiceImpl.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementServiceImpl.java?rev=643240&view=auto ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementServiceImpl.java (added) +++ xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementServiceImpl.java Mon Mar 31 18:53:39 2008 @@ -0,0 +1,76 @@ +/* + * 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. + * + * $Id$ + */ + +package org.apache.xindice.client.xmldb.services; + +import org.apache.xindice.client.xmldb.CommonConfigurable; +import org.apache.xindice.client.xmldb.XindiceCollection; +import org.apache.xindice.util.XindiceRuntimeException; +import org.xmldb.api.base.Collection; +import org.xmldb.api.base.Resource; +import org.xmldb.api.base.XMLDBException; + +/** + * Service for additional Xindice-specific resource operations. + * + * @version $Revision$, $Date$ + */ +public class ResourceManagementServiceImpl extends CommonConfigurable implements ResourceManagementService { + + /** + * Collection that this service is going to use to store resources + */ + private XindiceCollection collection; + + public String getVersion() { + return "1.0"; + } + + public String getName() { + return "ResourceManagementService"; + } + + /** + * Sets collection that the service will use to store resources. The + * collection must be instance of XindiceCollection class because it + * uses Xindice-specific features. + * @param collection XindiceCollection object + * @throws XindiceRuntimeException If <code>collection</code> parameter is + * not instance of Xindice collection. + */ + public void setCollection(Collection collection) { + if (collection instanceof XindiceCollection) { + this.collection = (XindiceCollection) collection; + } else { + throw new XindiceRuntimeException("ResourceMangementService requires instance of XindiceCollection"); + } + } + + public void insertResource(Resource res) throws XMLDBException { + collection.insertResource(res); + } + + public void updateResource(Resource res) throws XMLDBException { + collection.updateResource(res); + } + + public void storeResource(Resource res) throws XMLDBException { + collection.storeResource(res); + } +} \ No newline at end of file Propchange: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementServiceImpl.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/services/ResourceManagementServiceImpl.java ------------------------------------------------------------------------------ svn:keywords = Id Revision Author Date Modified: xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/xmlrpc/CollectionImpl.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/xmlrpc/CollectionImpl.java?rev=643240&r1=643239&r2=643240&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/xmlrpc/CollectionImpl.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/client/xmldb/xmlrpc/CollectionImpl.java Mon Mar 31 18:53:39 2008 @@ -243,24 +243,89 @@ checkOpen(); try { - Map params = new HashMap(); - params.put(RPCDefaultMessage.COLLECTION, collPath); - params.put(RPCDefaultMessage.NAME, res.getId()); - params.put(RPCDefaultMessage.DOCUMENT, res.getContent()); + Map params = createParams(res); + String name = (String) runRemoteCommand("SetResource", params); + seResourcetId(res, name); + } catch (XMLDBException x) { + throw x; // propagate any xmldb exception. + } catch (Exception e) { + throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e); + } + } - String name = (String) runRemoteCommand("InsertResource", params); - if (res instanceof XMLResource) { - ((XMLResourceImpl) res).setId(name); - } else { - ((BinaryResourceImpl) res).setId(name); - } + /** + * Inserts the provided resource into the database. + * + * @param res the resource to insert into the database. + * @exception XMLDBException with expected error codes.<br /> + * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor + * specific errors that occur.<br /> + * <code>ErrorCodes.INVALID_RESOURCE</code> if the <code>Resource</code> is + * not valid. + * <code>ErrorCodes.COLLECTION_CLOSED</code> if the <code>close</code> + * method has been called on the <code>Collection</code><br /> + */ + public void insertResource(Resource res) throws XMLDBException { - } catch (XMLDBException x) { + if (res.getContent() == null) { + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "no resource data"); + } + checkOpen(); + try { + Map params = createParams(res); + String name = (String) runRemoteCommand("InsertResource", params); + seResourcetId(res, name); + } catch (XMLDBException x) { throw x; // propagate any xmldb exception. } catch (Exception e) { + throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e); + } + } + + /** + * Updates the provided resource in the database. + * + * @param res the resource to update in the database. + * @exception XMLDBException with expected error codes.<br /> + * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor + * specific errors that occur.<br /> + * <code>ErrorCodes.INVALID_RESOURCE</code> if the <code>Resource</code> is + * not valid. + * <code>ErrorCodes.COLLECTION_CLOSED</code> if the <code>close</code> + * method has been called on the <code>Collection</code><br /> + */ + public void updateResource(Resource res) throws XMLDBException { + if (res.getContent() == null) { + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "no resource data"); + } + + checkOpen(); + try { + Map params = createParams(res); + String name = (String) runRemoteCommand("UpdateResource", params); + seResourcetId(res, name); + } catch (XMLDBException x) { + throw x; // propagate any xmldb exception. + } catch (Exception e) { throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e); + } + } + + private Map createParams(Resource res) throws XMLDBException { + Map params = new HashMap(); + params.put(RPCDefaultMessage.COLLECTION, collPath); + params.put(RPCDefaultMessage.NAME, res.getId()); + params.put(RPCDefaultMessage.DOCUMENT, res.getContent()); + return params; + } + + private void seResourcetId(Resource res, String name) { + if (res instanceof XMLResource) { + ((XMLResourceImpl) res).setId(name); + } else { + ((BinaryResourceImpl) res).setId(name); } } Modified: xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/InsertDocument.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/InsertDocument.java?rev=643240&r1=643239&r2=643240&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/InsertDocument.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/InsertDocument.java Mon Mar 31 18:53:39 2008 @@ -33,7 +33,7 @@ private static final Log log = LogFactory.getLog(InsertDocument.class); public Map execute(Map message) throws Exception { - log.warn("Please use GetResource instead. GetResource supports same as GetDocument, plus binary resources"); + log.warn("Please use InsertResource instead. InsertResource supports same as InsertDocument, plus binary resources"); return super.execute(message); } } Modified: xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/InsertResource.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/InsertResource.java?rev=643240&r1=643239&r2=643240&view=diff ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/InsertResource.java (original) +++ xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/InsertResource.java Mon Mar 31 18:53:39 2008 @@ -29,6 +29,7 @@ import java.util.Map; /** + * Inserts new resource. * * @version $Revision$, $Date$ */ @@ -62,14 +63,14 @@ if (id == null) { id = col.insertBinary((byte[])obj).toString(); } else { - col.setBinary(id, (byte[])obj); + col.insertBinary(id, (byte[])obj); } } else { Document doc = DOMParser.toDocument((String) obj); if (id == null) { id = col.insertDocument(doc).toString(); } else { - col.setDocument(id, doc); + col.insertDocument(id, doc); } } Added: xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/SetResource.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/SetResource.java?rev=643240&view=auto ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/SetResource.java (added) +++ xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/SetResource.java Mon Mar 31 18:53:39 2008 @@ -0,0 +1,80 @@ +/* + * 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. + * + * $Id$ + */ + +package org.apache.xindice.server.rpc.messages; + +import org.apache.xindice.server.rpc.RPCDefaultMessage; +import org.apache.xindice.core.Collection; +import org.apache.xindice.xml.dom.DOMParser; +import org.w3c.dom.Document; + +import java.util.Map; +import java.util.HashMap; + +/** + * Inserts new resource or updates existing one. + * + * @version $Revision$, $Date$ + */ +public class SetResource extends RPCDefaultMessage { + + public Map execute(Map message) throws Exception { + if (!message.containsKey(COLLECTION)) { + throw new Exception(MISSING_COLLECTION_PARAM); + } + + if (!message.containsKey(NAME)) { + throw new Exception(MISSING_NAME_PARAM); + } + + if (!message.containsKey(DOCUMENT)) { + throw new Exception(MISSING_DOCUMENT_PARAM); + } + + Object obj = message.get(DOCUMENT); + if (obj == null) { + throw new Exception("Unable to parse Document"); + } + + String id = (String) message.get(NAME); + if ("".equals(id)) { + id = null; + } + + Collection col = getCollection((String) message.get(COLLECTION)); + if (obj instanceof byte[]) { + if (id == null) { + id = col.insertBinary((byte[])obj).toString(); + } else { + col.setBinary(id, (byte[])obj); + } + } else { + Document doc = DOMParser.toDocument((String) obj); + if (id == null) { + id = col.insertDocument(doc).toString(); + } else { + col.setDocument(id, doc); + } + } + + Map result = new HashMap(3); + result.put(RESULT, id); + return result; + } +} Propchange: xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/SetResource.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/SetResource.java ------------------------------------------------------------------------------ svn:keywords = Id Revision Author Date Added: xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/UpdateResource.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/UpdateResource.java?rev=643240&view=auto ============================================================================== --- xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/UpdateResource.java (added) +++ xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/UpdateResource.java Mon Mar 31 18:53:39 2008 @@ -0,0 +1,72 @@ +/* + * 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. + * + * $Id$ + */ + +package org.apache.xindice.server.rpc.messages; + +import org.apache.xindice.server.rpc.RPCDefaultMessage; +import org.apache.xindice.core.Collection; +import org.apache.xindice.xml.dom.DOMParser; +import org.w3c.dom.Document; + +import java.util.Map; +import java.util.HashMap; + +/** + * Updates existing resource. + * + * @version $Revision$, $Date$ + */ +public class UpdateResource extends RPCDefaultMessage { + + public Map execute(Map message) throws Exception { + if (!message.containsKey(COLLECTION)) { + throw new Exception(MISSING_COLLECTION_PARAM); + } + + if (!message.containsKey(NAME)) { + throw new Exception(MISSING_NAME_PARAM); + } + + if (!message.containsKey(DOCUMENT)) { + throw new Exception(MISSING_DOCUMENT_PARAM); + } + + Object obj = message.get(DOCUMENT); + if (obj == null) { + throw new Exception("Unable to parse Document"); + } + + String id = (String) message.get(NAME); + if ("".equals(id) || id == null) { + throw new Exception("Resource key is not set"); + } + + Collection col = getCollection((String) message.get(COLLECTION)); + if (obj instanceof byte[]) { + col.updateBinary(id, (byte[])obj); + } else { + Document doc = DOMParser.toDocument((String) obj); + col.updateDocument(id, doc); + } + + Map result = new HashMap(3); + result.put(RESULT, id); + return result; + } +} Propchange: xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/UpdateResource.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xml/xindice/trunk/java/src/org/apache/xindice/server/rpc/messages/UpdateResource.java ------------------------------------------------------------------------------ svn:keywords = Id Revision Author Date Modified: xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/IntegrationTests.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/IntegrationTests.java?rev=643240&r1=643239&r2=643240&view=diff ============================================================================== --- xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/IntegrationTests.java (original) +++ xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/IntegrationTests.java Mon Mar 31 18:53:39 2008 @@ -19,17 +19,17 @@ package org.apache.xindice.integration; +import junit.framework.TestSuite; import org.apache.xindice.integration.client.basic.CollectionTest; import org.apache.xindice.integration.client.basic.DocumentTest; import org.apache.xindice.integration.client.basic.IndexTest; import org.apache.xindice.integration.client.resources.BinaryResourceTest; import org.apache.xindice.integration.client.services.IndexedSearchTest; import org.apache.xindice.integration.client.services.MetaTest; +import org.apache.xindice.integration.client.services.ResourceManagementTest; +import org.apache.xindice.integration.client.services.TextQueryTest; import org.apache.xindice.integration.client.services.XPathQueryTest; import org.apache.xindice.integration.client.services.XUpdateQueryTest; -import org.apache.xindice.integration.client.services.TextQueryTest; - -import junit.framework.TestSuite; /** * @version $Revision$, $Date$ @@ -46,6 +46,7 @@ suite.addTestSuite(XPathQueryTest.class); suite.addTestSuite(TextQueryTest.class); suite.addTestSuite(MetaTest.class); + suite.addTestSuite(ResourceManagementTest.class); suite.addTestSuite(IndexedSearchTest.class); suite.addTestSuite(BinaryResourceTest.class); return suite; Added: xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/ResourceManagementTest.java URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/ResourceManagementTest.java?rev=643240&view=auto ============================================================================== --- xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/ResourceManagementTest.java (added) +++ xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/ResourceManagementTest.java Mon Mar 31 18:53:39 2008 @@ -0,0 +1,123 @@ +/* + * 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. + * + * $Id$ + */ + +package org.apache.xindice.integration.client.services; + +import org.apache.xindice.client.xmldb.services.ResourceManagementService; +import org.apache.xindice.integration.client.AbstractXmlDbClientTest; +import org.xmldb.api.base.Collection; +import org.xmldb.api.base.XMLDBException; +import org.xmldb.api.modules.XMLResource; + +/** + * Test ResourceManagementService + * + * @version $Revision$, $Date$ + */ +public class ResourceManagementTest extends AbstractXmlDbClientTest { + + static final String DOCUMENT_ID1 = "doc1"; + static final String DOCUMENT_ID2 = "doc2"; + + static final String DOCUMENT1 = + "<?xml version=\"1.0\"?>" + + "<person>" + + " <first>John</first>" + + " <last>Smith</last>" + + " <age>30</age>" + + " <phone type=\"work\">555-345-6789</phone>" + + "</person>"; + + static final String DOCUMENT2 = + "<?xml version=\"1.0\"?>" + + "<person>" + + " <first>John</first>" + + " <last>Smith</last>" + + " <age>31</age>" + + " <phone type=\"work\">555-345-6789</phone>" + + "</person>"; + + static final String ATTRIBUTE_ID = "MetaDataAttribute"; + + private ResourceManagementService service; + private Collection col; + + public void setUp() throws Exception { + super.setUp(); + + col = this.client.getCollection(TEST_COLLECTION_PATH); + service = (ResourceManagementService) col.getService("ResourceManagementService", "1.0"); + + this.client.insertDocument(TEST_COLLECTION_PATH, DOCUMENT_ID1, DOCUMENT1); + } + + public void tearDown() throws Exception { + this.client.removeDocument(TEST_COLLECTION_PATH, DOCUMENT_ID1); + + super.tearDown(); + } + + public void testInsert() throws Exception { + XMLResource res = (XMLResource) col.createResource(DOCUMENT_ID2, "XMLResource"); + res.setContent(DOCUMENT2); + service.insertResource(res); + assertNotNull(col.getResource(DOCUMENT_ID2)); + } + + public void testInsertExistingKey() throws Exception { + XMLResource res = (XMLResource) col.createResource(DOCUMENT_ID1, "XMLResource"); + res.setContent(DOCUMENT1); + try { + service.insertResource(res); + assertTrue("Inserted document with existing key", false); + } catch (XMLDBException e) { + } + } + + public void testUpdate() throws Exception { + XMLResource res = (XMLResource) col.createResource(DOCUMENT_ID2, "XMLResource"); + res.setContent(DOCUMENT2); + try { + service.updateResource(res); + assertTrue("Updated document with non-existing key", false); + } catch (XMLDBException e) { + } + } + + public void testUpdateExistingKey() throws Exception { + XMLResource res = (XMLResource) col.createResource(DOCUMENT_ID1, "XMLResource"); + res.setContent(DOCUMENT1); + service.updateResource(res); + assertNotNull(col.getResource(DOCUMENT_ID1)); + } + + public void testStore() throws Exception { + XMLResource res = (XMLResource) col.createResource(DOCUMENT_ID2, "XMLResource"); + res.setContent(DOCUMENT2); + service.storeResource(res); + assertNotNull(col.getResource(DOCUMENT_ID2)); + } + + public void testStoreExistingKey() throws Exception { + XMLResource res = (XMLResource) col.createResource(DOCUMENT_ID1, "XMLResource"); + res.setContent(DOCUMENT2); + service.storeResource(res); + assertNotNull(col.getResource(DOCUMENT_ID1)); + } +} \ No newline at end of file Propchange: xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/ResourceManagementTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xml/xindice/trunk/java/tests/src/org/apache/xindice/integration/client/services/ResourceManagementTest.java ------------------------------------------------------------------------------ svn:keywords = Id Revision Author Date