use of org.xmldb.api.modules.BinaryResource in project exist by eXist-db.
the class LoginModuleTest method beforeClass.
@BeforeClass
public static void beforeClass() throws XMLDBException {
root = DatabaseManager.getCollection("xmldb:exist://localhost:" + existWebServer.getPort() + "/xmlrpc" + XmldbURI.ROOT_COLLECTION, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
final BinaryResource res = (BinaryResource) root.createResource(XQUERY_FILENAME, "BinaryResource");
((EXistResource) res).setMimeType("application/xquery");
res.setContent(XQUERY);
root.storeResource(res);
final UserManagementService ums = (UserManagementService) root.getService("UserManagementService", "1.0");
ums.chmod(res, 0777);
final BasicCookieStore store = new BasicCookieStore();
client = HttpClientBuilder.create().setDefaultCookieStore(store).build();
}
use of org.xmldb.api.modules.BinaryResource in project exist by eXist-db.
the class XMLDBStore method evalWithCollection.
@Override
public Sequence evalWithCollection(Collection collection, Sequence[] args, Sequence contextSequence) throws XPathException {
String docName = args[1].isEmpty() ? null : args[1].getStringValue();
if (docName != null && docName.isEmpty()) {
docName = null;
} else if (docName != null) {
docName = new AnyURIValue(docName).toXmldbURI().toString();
}
final Item item = args[2].itemAt(0);
// determine the mime type
final boolean storeAsBinary = isCalledAs(FS_STORE_BINARY_NAME);
MimeType mimeType = null;
if (getSignature().getArgumentCount() == 4) {
final String strMimeType = args[3].getStringValue();
mimeType = MimeTable.getInstance().getContentType(strMimeType);
}
if (mimeType == null && docName != null) {
mimeType = MimeTable.getInstance().getContentTypeFor(docName);
}
if (mimeType == null) {
mimeType = (storeAsBinary || !Type.subTypeOf(item.getType(), Type.NODE)) ? MimeType.BINARY_TYPE : MimeType.XML_TYPE;
} else if (storeAsBinary) {
mimeType = new MimeType(mimeType.getName(), MimeType.BINARY);
}
Resource resource;
try {
if (Type.subTypeOf(item.getType(), Type.JAVA_OBJECT)) {
final Object obj = ((JavaObjectValue) item).getObject();
if (obj instanceof java.io.File) {
resource = loadFromFile(collection, ((java.io.File) obj).toPath(), docName, mimeType);
} else if (obj instanceof java.nio.file.Path) {
resource = loadFromFile(collection, (Path) obj, docName, mimeType);
} else {
LOGGER.error("Passed java object should be either a java.nio.file.Path or java.io.File");
throw new XPathException(this, "Passed java object should be either a java.nio.file.Path or java.io.File");
}
} else if (Type.subTypeOf(item.getType(), Type.ANY_URI)) {
try {
final URI uri = new URI(item.getStringValue());
resource = loadFromURI(collection, uri, docName, mimeType);
} catch (final URISyntaxException e) {
LOGGER.error("Invalid URI: {}", item.getStringValue());
throw new XPathException(this, "Invalid URI: " + item.getStringValue(), e);
}
} else {
if (mimeType.isXMLType()) {
resource = collection.createResource(docName, "XMLResource");
} else {
resource = collection.createResource(docName, "BinaryResource");
}
if (Type.subTypeOf(item.getType(), Type.STRING)) {
resource.setContent(item.getStringValue());
} else if (item.getType() == Type.BASE64_BINARY) {
resource.setContent(((BinaryValue) item).toJavaObject());
} else if (Type.subTypeOf(item.getType(), Type.NODE)) {
if (mimeType.isXMLType()) {
final ContentHandler handler = ((XMLResource) resource).setContentAsSAX();
handler.startDocument();
item.toSAX(context.getBroker(), handler, SERIALIZATION_PROPERTIES);
handler.endDocument();
} else {
try (final StringWriter writer = new StringWriter()) {
final SAXSerializer serializer = new SAXSerializer();
serializer.setOutput(writer, null);
item.toSAX(context.getBroker(), serializer, SERIALIZATION_PROPERTIES);
resource.setContent(writer.toString());
} catch (final IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
} else {
LOGGER.error("Data should be either a node or a string");
throw new XPathException(this, "Data should be either a node or a string");
}
((EXistResource) resource).setMimeType(mimeType.getName());
collection.storeResource(resource);
}
} catch (final XMLDBException e) {
LOGGER.error(e.getMessage(), e);
throw new XPathException(this, "XMLDB reported an exception while storing document: " + e.getMessage(), e);
} catch (final SAXException e) {
LOGGER.error(e.getMessage());
throw new XPathException(this, "SAX reported an exception while storing document", e);
}
if (resource == null) {
return Sequence.EMPTY_SEQUENCE;
} else {
try {
// TODO : use dedicated function in XmldbURI
return new StringValue(collection.getName() + "/" + resource.getId());
} catch (final XMLDBException e) {
LOGGER.error(e.getMessage());
throw new XPathException(this, "XMLDB reported an exception while retrieving the " + "stored document", e);
}
}
}
use of org.xmldb.api.modules.BinaryResource in project exist by eXist-db.
the class RemoteCollectionTest method createBinaryResourceFromString.
@Test
public void createBinaryResourceFromString() throws XMLDBException {
final Collection collection = getCollection();
final String resourceName = "testresource.bin";
final Resource resource = collection.createResource(resourceName, BinaryResource.RESOURCE_TYPE);
assertNotNull(resource);
assertEquals(collection, resource.getParentCollection());
final String bin = "binary data: " + System.currentTimeMillis();
resource.setContent(bin);
collection.storeResource(resource);
final Resource retrievedResource = collection.getResource(resourceName);
assertNotNull(retrievedResource);
assertEquals(BinaryResource.RESOURCE_TYPE, retrievedResource.getResourceType());
assertTrue(retrievedResource instanceof BinaryResource);
final byte[] result = (byte[]) retrievedResource.getContent();
assertNotNull(result);
assertEquals(bin, new String(result, UTF_8));
}
use of org.xmldb.api.modules.BinaryResource in project exist by eXist-db.
the class RemoteCollectionTest method createEmptyBinaryResource.
@Test
public void createEmptyBinaryResource() throws XMLDBException, IOException {
final Collection collection = getCollection();
final String resourceName = "empty.dtd";
final Resource resource = collection.createResource(resourceName, BinaryResource.RESOURCE_TYPE);
((EXistResource) resource).setMimeType("application/xml-dtd");
final byte[] bin = new byte[0];
try (final InputStream is = new UnsynchronizedByteArrayInputStream(bin)) {
final InputSource inputSource = new InputSource();
inputSource.setByteStream(is);
inputSource.setSystemId("empty.dtd");
resource.setContent(inputSource);
collection.storeResource(resource);
}
final Resource retrievedResource = collection.getResource(resourceName);
assertNotNull(retrievedResource);
assertEquals(BinaryResource.RESOURCE_TYPE, retrievedResource.getResourceType());
assertTrue(retrievedResource instanceof BinaryResource);
final byte[] result = (byte[]) retrievedResource.getContent();
assertNotNull(result);
assertArrayEquals(bin, result);
}
use of org.xmldb.api.modules.BinaryResource in project exist by eXist-db.
the class BinaryResourceUpdateTest method updateBinary.
@Test
public void updateBinary() throws XMLDBException, URISyntaxException {
for (int i = 0; i < REPEAT; i++) {
BinaryResource binaryResource = (BinaryResource) testCollection.createResource("test1.xml", "BinaryResource");
binaryResource.setContent(Paths.get(binFile.toURI()));
testCollection.storeResource(binaryResource);
Resource resource = testCollection.getResource("test1.xml");
assertNotNull(resource);
XMLResource xmlResource = (XMLResource) testCollection.createResource("test2.xml", "XMLResource");
xmlResource.setContent(Paths.get(xmlFile.toURI()));
testCollection.storeResource(xmlResource);
resource = testCollection.getResource("test2.xml");
assertNotNull(resource);
}
}
Aggregations