use of org.nuxeo.ecm.core.api.PathRef in project nuxeo-filesystem-connectors by nuxeo.
the class WebDavClientTest method testMoveWithRenaming.
@Test
public void testMoveWithRenaming() throws Exception {
// create a fake bin tmp file which will finally be a docx file
String name = "tmpfile.tmp";
String mimeType = MimetypeRegistry.DEFAULT_MIMETYPE;
byte[] bytes = "Fake BIN".getBytes("UTF-8");
String expectedType = "File";
doTestPutFile(name, bytes, mimeType, expectedType);
PathRef pathRef = new PathRef("/workspaces/workspace/" + name);
assertTrue(session.exists(pathRef));
DocumentModel doc = session.getDocument(pathRef);
assertEquals(name, doc.getTitle());
Blob blob = (Blob) doc.getPropertyValue("file:content");
assertEquals(name, blob.getFilename());
assertEquals(MimetypeRegistry.DEFAULT_MIMETYPE, blob.getMimeType());
// rename it to a docx file
String newName = "sample.docx";
HttpMove request = new HttpMove(ROOT_URI + name, ROOT_URI + newName, false);
int status;
try (CloseableHttpResponse response = client.execute(request, context)) {
status = response.getStatusLine().getStatusCode();
}
assertEquals(HttpStatus.SC_CREATED, status);
TransactionHelper.commitOrRollbackTransaction();
TransactionHelper.startTransaction();
doc = session.getDocument(pathRef);
assertEquals(newName, doc.getTitle());
blob = (Blob) doc.getPropertyValue("file:content");
assertEquals(newName, blob.getFilename());
assertEquals("application/vnd.openxmlformats-officedocument.wordprocessingml.document", blob.getMimeType());
}
use of org.nuxeo.ecm.core.api.PathRef in project nuxeo-filesystem-connectors by nuxeo.
the class WebDavClientTest method doTestPutFile.
protected void doTestPutFile(String name, byte[] bytes, String mimeType, String expectedType) throws Exception {
InputStream is = new ByteArrayInputStream(bytes);
HttpPut request = new HttpPut(ROOT_URI + name);
request.setEntity(new InputStreamEntity(is, bytes.length, ContentType.create(mimeType)));
int status;
try (CloseableHttpResponse response = client.execute(request, context)) {
status = response.getStatusLine().getStatusCode();
}
assertEquals(HttpStatus.SC_CREATED, status);
// check using Nuxeo Core APIs
TransactionHelper.commitOrRollbackTransaction();
TransactionHelper.startTransaction();
PathRef pathRef = new PathRef("/workspaces/workspace/" + name);
assertTrue(session.exists(pathRef));
DocumentModel doc = session.getDocument(pathRef);
assertEquals(expectedType, doc.getType());
assertEquals(name, doc.getTitle());
BlobHolder bh = doc.getAdapter(BlobHolder.class);
assertNotNull(bh);
Blob blob = bh.getBlob();
assertNotNull(blob);
assertEquals(bytes.length, blob.getLength());
assertEquals(mimeType, blob.getMimeType());
assertArrayEquals(bytes, blob.getByteArray());
}
use of org.nuxeo.ecm.core.api.PathRef in project nuxeo-filesystem-connectors by nuxeo.
the class WebDavClientTest method testOverwriteExistingFile.
@Test
public // NXP-12735: disabled because failing under windows + pgsql
void testOverwriteExistingFile() throws Exception {
// this file already exists
String name = "test.txt";
String mimeType = "text/plain";
PathRef pathRef = new PathRef("/workspaces/workspace/" + name);
assertTrue(session.exists(pathRef));
byte[] bytes = new byte[] { 1, 2, 3, 4, 5 };
String expectedType = "File";
doTestPutFile(name, bytes, mimeType, expectedType);
}
use of org.nuxeo.ecm.core.api.PathRef in project nuxeo-filesystem-connectors by nuxeo.
the class ExistingResource method copyOrMove.
private Response copyOrMove(String method, @HeaderParam("Destination") String destination, @HeaderParam("Overwrite") String overwrite) {
if (backend.isLocked(doc.getRef()) && !backend.canUnlock(doc.getRef())) {
return Response.status(423).build();
}
destination = encode(destination.getBytes(), "ISO-8859-1");
try {
destination = new URI(destination).getPath();
} catch (URISyntaxException e) {
throw new NuxeoException(e);
}
Backend root = BackendHelper.getBackend("/", request);
Set<String> names = new HashSet<String>(root.getVirtualFolderNames());
Path destinationPath = new Path(destination);
String[] segments = destinationPath.segments();
int removeSegments = 0;
for (String segment : segments) {
if (names.contains(segment)) {
break;
} else {
removeSegments++;
}
}
destinationPath = destinationPath.removeFirstSegments(removeSegments);
String destPath = destinationPath.toString();
String davDestPath = destPath;
Backend destinationBackend = BackendHelper.getBackend(davDestPath, request);
destPath = destinationBackend.parseLocation(destPath).toString();
log.debug("to " + davDestPath);
// Remove dest if it exists and the Overwrite header is set to "T".
int status = 201;
if (destinationBackend.exists(davDestPath)) {
if ("F".equals(overwrite)) {
return Response.status(412).build();
}
destinationBackend.removeItem(davDestPath);
status = 204;
}
// Check if parent exists
String destParentPath = getParentPath(destPath);
PathRef destParentRef = new PathRef(destParentPath);
if (!destinationBackend.exists(getParentPath(davDestPath))) {
return Response.status(409).build();
}
if ("COPY".equals(method)) {
DocumentModel destDoc = backend.copyItem(doc, destParentRef);
backend.renameItem(destDoc, getNameFromPath(destPath));
} else if ("MOVE".equals(method)) {
if (backend.isRename(doc.getPathAsString(), destPath)) {
backend.renameItem(doc, getNameFromPath(destPath));
} else {
backend.moveItem(doc, destParentRef);
}
}
backend.saveChanges();
return Response.status(status).build();
}
use of org.nuxeo.ecm.core.api.PathRef in project nuxeo-drive-server by nuxeo.
the class NuxeoDriveManagerImpl method addToLocallyEditedCollection.
@Override
public void addToLocallyEditedCollection(CoreSession session, DocumentModel doc) {
// Add document to "Locally Edited" collection, creating if if not
// exists
CollectionManager cm = Framework.getService(CollectionManager.class);
DocumentModel userCollections = cm.getUserDefaultCollections(doc, session);
DocumentRef locallyEditedCollectionRef = new PathRef(userCollections.getPath().toString(), LOCALLY_EDITED_COLLECTION_NAME);
DocumentModel locallyEditedCollection = null;
if (session.exists(locallyEditedCollectionRef)) {
locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
cm.addToCollection(locallyEditedCollection, doc, session);
} else {
cm.addToNewCollection(LOCALLY_EDITED_COLLECTION_NAME, "Documents locally edited with Nuxeo Drive", doc, session);
locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
}
// Register "Locally Edited" collection as a synchronization root if not
// already the case
Set<IdRef> syncRootRefs = getSynchronizationRootReferences(session);
if (!syncRootRefs.contains(new IdRef(locallyEditedCollection.getId()))) {
registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
}
}
Aggregations