use of org.exist.util.StringInputSource in project exist by eXist-db.
the class CopyResourceRecoveryTest method store.
private void store(final String testCollectionName, final String subCollection) throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, URISyntaxException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
Collection testCollection;
DocumentImpl doc;
try (final Txn transaction = transact.beginTransaction()) {
final Collection root = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test"));
assertNotNull(root);
broker.saveCollection(transaction, root);
testCollection = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test").append(testCollectionName));
assertNotNull(testCollection);
broker.saveCollection(transaction, testCollection);
final Collection subTestCollection = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test").append(testCollectionName).append(subCollection));
assertNotNull(subTestCollection);
broker.saveCollection(transaction, subTestCollection);
final String sample;
try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
assertNotNull(is);
sample = InputStreamUtil.readString(is, UTF_8);
}
broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(sample), MimeType.XML_TYPE, subTestCollection);
doc = subTestCollection.getDocument(broker, XmldbURI.create("test.xml"));
transact.commit(transaction);
}
try (final Txn transaction = transact.beginTransaction()) {
broker.copyResource(transaction, doc, testCollection, XmldbURI.create("new_test.xml"));
broker.saveCollection(transaction, testCollection);
transact.commit(transaction);
}
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class StoreResourceTest method replaceXmlDoc.
private void replaceXmlDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
final BrokerPool pool = existWebServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final Txn transaction = pool.getTransactionManager().beginTransaction();
final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content), MimeType.XML_TYPE, col);
transaction.commit();
}
// check the replaced document is correct
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK)) {
final Serializer serializer = broker.borrowSerializer();
try {
final String docXml = serializer.serialize(lockedDoc.getDocument());
final Diff diff = DiffBuilder.compare(Input.fromString(content)).withTest(Input.fromString(docXml)).build();
assertFalse(diff.toString(), diff.hasDifferences());
} finally {
broker.returnSerializer(serializer);
}
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class StoreResourceTest method replaceBinDoc.
private void replaceBinDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
final BrokerPool pool = existWebServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final Txn transaction = pool.getTransactionManager().beginTransaction();
final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content.getBytes(UTF_8)), MimeType.BINARY_TYPE, col);
transaction.commit();
}
// check the replaced document is correct
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK);
final InputStream is = broker.getBinaryResource((BinaryDocument) lockedDoc.getDocument());
final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
os.write(is);
assertArrayEquals(content.getBytes(UTF_8), os.toByteArray());
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class StoreResourceTest method setup.
@Before
public void setup() throws EXistException, PermissionDeniedException, LockException, SAXException, IOException, AuthenticationException {
final BrokerPool pool = existWebServer.getBrokerPool();
// create user1 resources
final Subject user1 = pool.getSecurityManager().authenticate(USER1_NAME, USER1_PWD);
try (final DBBroker broker = pool.get(Optional.of(user1));
final Txn transaction = pool.getTransactionManager().beginTransaction();
final Collection collection = broker.openCollection(TEST_COLLECTION_URI, Lock.LockMode.WRITE_LOCK)) {
final String u1d3xml = "<empty3/>";
broker.storeDocument(transaction, USER1_DOC1, new StringInputSource(u1d3xml), MimeType.XML_TYPE, collection);
chmod(broker, transaction, TEST_COLLECTION_URI.append(USER1_DOC1), USER1_DOC1_MODE);
chgrp(broker, transaction, TEST_COLLECTION_URI.append(USER1_DOC1), GROUP1_NAME);
final String u1d3bin = "bin3";
broker.storeDocument(transaction, USER1_BIN_DOC1, new StringInputSource(u1d3bin.getBytes(UTF_8)), MimeType.TEXT_TYPE, collection);
chmod(broker, transaction, TEST_COLLECTION_URI.append(USER1_BIN_DOC1), USER1_BIN_DOC1_MODE);
chgrp(broker, transaction, TEST_COLLECTION_URI.append(USER1_BIN_DOC1), GROUP1_NAME);
transaction.commit();
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class MoveResourceRecoveryTest method storeAborted.
private void storeAborted() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, URISyntaxException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
try (final Txn transaction = transact.beginTransaction()) {
final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
assertNotNull(test2);
broker.saveCollection(transaction, test2);
final String sample;
try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
sample = InputStreamUtil.readString(is, UTF_8);
}
broker.storeDocument(transaction, XmldbURI.create("new_test2.xml"), new StringInputSource(sample), MimeType.XML_TYPE, test2);
transact.commit(transaction);
}
final Txn transaction = transact.beginTransaction();
final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
final DocumentImpl doc = test2.getDocument(broker, XmldbURI.create("new_test2.xml"));
assertNotNull(doc);
final Collection test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
broker.moveResource(transaction, doc, test, XmldbURI.create("new_test2.xml"));
broker.saveCollection(transaction, test);
// NOTE: do not commit the transaction
pool.getJournalManager().get().flush(true, false);
}
}
Aggregations