* <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");
}
checkOpen();
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[].");
}
try {
if (res.getId() != null) {
col.setBinary(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 {
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
"A resource must be a document in order to be stored.");
}
} 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");
}
}