use of org.nuxeo.ecm.core.api.DocumentModel 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.DocumentModel 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.DocumentModel in project nuxeo-filesystem-connectors by nuxeo.
the class WebDavRepoInit method populate.
@Override
public void populate(CoreSession session) {
LogFactory.getLog(WebDavRepoInit.class).trace("enter populate webdav");
DocumentModel ws = session.createDocumentModel("/", "workspaces", "WorkspaceRoot");
ws.setPropertyValue("dc:title", "Workspaces");
session.createDocument(ws);
DocumentModel w = session.createDocumentModel("/workspaces", "workspace", "Workspace");
w.setPropertyValue("dc:title", "Workspace");
session.createDocument(w);
createFile(w, "quality.jpg", "image/jpg", session);
createFile(w, "test.html", "text/html", session);
createFile(w, "test.txt", "text/plain", session);
session.save();
}
use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-filesystem-connectors by nuxeo.
the class AbstractVirtualBackend method registerSimpleBackends.
protected void registerSimpleBackends(List<DocumentModel> docs) {
List<String> paths = new ArrayList<String>();
for (DocumentModel doc : docs) {
paths.add(doc.getPathAsString());
}
List<String> heads = new ArrayList<String>();
for (int idx = 0; idx < paths.size(); idx++) {
String path = paths.get(idx);
if (isHead(path, paths, idx)) {
heads.add(path);
}
}
for (String head : heads) {
String headName = new Path(head).lastSegment();
String name = headName;
int idx = 1;
while (backendMap.containsKey(name)) {
name = headName + "-" + idx;
idx = idx + 1;
}
Backend backend = realBackendFactory.createBackend(name, head, new Path(this.rootUrl).append(name).toString(), getSession());
registerBackend(backend);
}
}
use of org.nuxeo.ecm.core.api.DocumentModel 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();
}
Aggregations