use of org.alfresco.jlan.server.filesys.NetworkFile in project alfresco-repository by Alfresco.
the class ContentDiskDriver2 method createFile.
@Override
public NetworkFile createFile(NodeRef rootNode, String path, long allocationSize, boolean isHidden) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("createFile :" + path);
}
try {
NodeRef dirNodeRef;
String folderName;
String[] paths = FileName.splitPath(path);
if (paths[0] != null && paths[0].length() > 1) {
// lookup parent directory
dirNodeRef = getNodeForPath(rootNode, paths[0]);
folderName = paths[1];
} else {
dirNodeRef = rootNode;
folderName = path;
}
boolean soft = false;
NodeRef existing = fileFolderService.searchSimple(dirNodeRef, folderName);
if (existing != null) {
if (nodeService.hasAspect(existing, ContentModel.ASPECT_SOFT_DELETE)) {
logger.debug("existing node has soft delete aspect");
soft = true;
}
}
NodeRef nodeRef = null;
if (soft) {
nodeRef = existing;
} else {
nodeRef = cifsHelper.createNode(dirNodeRef, folderName, ContentModel.TYPE_CONTENT);
nodeService.addAspect(nodeRef, ContentModel.ASPECT_NO_CONTENT, null);
lockKeeper.addLock(nodeRef);
}
if (isHidden) {
// yes is hidden
if (logger.isDebugEnabled()) {
logger.debug("Set hidden aspect, nodeRef:" + nodeRef);
}
hiddenAspect.hideNodeExplicit(nodeRef);
}
File file = TempFileProvider.createTempFile("cifs", ".bin");
TempNetworkFile netFile = new TempNetworkFile(file, path);
netFile.setChanged(true);
Serializable created = nodeService.getProperty(nodeRef, ContentModel.PROP_CREATED);
if (created != null && created instanceof Date) {
Date d = (Date) created;
if (logger.isDebugEnabled()) {
logger.debug("replacing create date to date:" + d);
}
netFile.setCreationDate(d.getTime());
netFile.setModifyDate(d.getTime());
}
// Always allow write access to a newly created file
netFile.setGrantedAccess(NetworkFile.READWRITE);
netFile.setAllowedAccess(NetworkFile.READWRITE);
if (netFile != null) {
long id = DefaultTypeConverter.INSTANCE.convert(Long.class, nodeService.getProperty(nodeRef, ContentModel.PROP_NODE_DBID));
netFile.setFileId((int) (id & 0xFFFFFFFFL));
}
if (logger.isDebugEnabled()) {
logger.debug("Created file: path=" + path + " node=" + nodeRef + " network file=" + netFile);
}
return netFile;
} catch (org.alfresco.repo.security.permissions.AccessDeniedException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Create file - access denied, " + path);
}
throw new AccessDeniedException("Unable to create file " + path);
} catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Create file - content I/O error, " + path);
}
throw ex;
} catch (ContentIOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Create file - content I/O error, " + path);
}
throw new DiskFullException("Unable to create file " + path);
} catch (AlfrescoRuntimeException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Create file error", ex);
}
throw new IOException("Unable to create file " + path, ex);
}
}
use of org.alfresco.jlan.server.filesys.NetworkFile in project alfresco-repository by Alfresco.
the class ContentDiskDriver2 method setFileInformation.
/**
* Set file information
*
* @param sess SrvSession
* @param tree TreeConnection
* @param name String
* @param info FileInfo
* @exception IOException
*/
public void setFileInformation(SrvSession sess, final TreeConnection tree, final String name, final FileInfo info) throws IOException {
// Get the device context
final ContentContext ctx = (ContentContext) tree.getContext();
if (logger.isDebugEnabled()) {
logger.debug("setFileInformation name=" + name + ", info=" + info);
}
NetworkFile networkFile = info.getNetworkFile();
try {
// Get the file/folder node
NodeRef nodeRef = getNodeForPath(tree, name);
if (permissionService.hasPermission(nodeRef, PermissionService.WRITE) == AccessStatus.DENIED) {
if (logger.isDebugEnabled()) {
logger.debug("write access denied to :" + name);
}
throw new AccessDeniedException("No write access to " + name);
}
// Inhibit versioning for this transaction
getPolicyFilter().disableBehaviour(ContentModel.ASPECT_VERSIONABLE);
/*
* Which DeleteOnClose flag has priority?
* SetDeleteOnClose is not set or used in this method.
* The NTProtocolHandler sets the deleteOnClose in both
* info and the NetworkFile - it's the one in NetworkFile that results in the file being deleted.
*/
if (info.hasSetFlag(FileInfo.SetDeleteOnClose) && info.hasDeleteOnClose()) {
if (logger.isDebugEnabled()) {
logger.debug("Set Delete On Close for :" + name);
}
// Check for delete permission
if (permissionService.hasPermission(nodeRef, PermissionService.DELETE) == AccessStatus.DENIED) {
throw new PermissionDeniedException("No delete access to :" + name);
}
// Check if the node is locked
lockService.checkForLock(nodeRef);
if (fileFolderService.exists(nodeRef)) {
// Check if it is a folder that is being deleted, make sure it is empty
boolean isFolder = true;
ContentFileInfo cInfo = getCifsHelper().getFileInformation(nodeRef, false, isLockedFilesAsOffline);
if (cInfo != null && cInfo.isDirectory() == false) {
isFolder = false;
}
// Check if the folder is empty
if (isFolder == true && getCifsHelper().isFolderEmpty(nodeRef) == false) {
throw new DirectoryNotEmptyException(name);
}
}
}
if (info.hasSetFlag(FileInfo.SetAttributes)) {
if (logger.isDebugEnabled()) {
logger.debug("Set attributes" + name + ", file attrs = " + info.getFileAttributes());
}
// TODO MER Think we may need to implement, Temporary, Hidden, System, Archive
if (info.isSystem()) {
logger.debug("Set system aspect (not yet implemented)" + name);
}
if (info.isTemporary()) {
logger.debug("Set temporary aspect (not yet implemented)" + name);
}
if (info.isHidden()) {
// yes is hidden
if (logger.isDebugEnabled()) {
logger.debug("Set hidden aspect" + name);
}
hiddenAspect.hideNodeExplicit(nodeRef);
} else {
// not hidden
if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_HIDDEN)) {
if (logger.isDebugEnabled()) {
logger.debug("Reset hidden aspect" + name);
}
hiddenAspect.unhideExplicit(nodeRef);
}
}
}
if (info.hasSetFlag(FileInfo.SetAllocationSize)) {
if (logger.isDebugEnabled()) {
logger.debug("Set allocation size" + name + info.getAllocationSize());
}
// Not yet implemented
}
if (info.hasSetFlag(FileInfo.SetFileSize)) {
if (logger.isDebugEnabled()) {
logger.debug("Set file size" + name + info.getSize());
}
// Not yet implemented
}
if (info.hasSetFlag(FileInfo.SetMode)) {
if (logger.isDebugEnabled()) {
logger.debug("Set Mode" + name + info.getMode());
}
// Not yet implemented - set the unix mode e.g. 777
}
// Set the creation and modified date/time
Map<QName, Serializable> auditableProps = new HashMap<QName, Serializable>(5);
if (info.hasSetFlag(FileInfo.SetCreationDate) && info.hasCreationDateTime()) {
// Set the creation date on the file/folder node
Date createDate = new Date(info.getCreationDateTime());
auditableProps.put(ContentModel.PROP_CREATED, createDate);
if (logger.isDebugEnabled()) {
logger.debug("Set creation date" + name + ", " + createDate);
}
}
if (info.hasSetFlag(FileInfo.SetModifyDate) && info.hasModifyDateTime()) {
// Set the modification date on the file/folder node
Date modifyDate = new Date(info.getModifyDateTime());
auditableProps.put(ContentModel.PROP_MODIFIED, modifyDate);
// Set the network file so we don't reverse this change in close file.
if (networkFile != null && !networkFile.isReadOnly()) {
networkFile.setModifyDate(info.getModifyDateTime());
if (networkFile instanceof TempNetworkFile) {
TempNetworkFile tnf = (TempNetworkFile) networkFile;
tnf.setModificationDateSetDirectly(true);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Set modification date" + name + ", " + modifyDate);
}
}
// Change Date is last write ?
if (info.hasSetFlag(FileInfo.SetChangeDate) && info.hasChangeDateTime()) {
Date changeDate = new Date(info.getChangeDateTime());
if (logger.isDebugEnabled()) {
logger.debug("Set change date (Not implemented)" + name + ", " + changeDate);
}
}
if (info.hasSetFlag(FileInfo.SetAccessDate) && info.hasAccessDateTime()) {
Date accessDate = new Date(info.getAccessDateTime());
if (logger.isDebugEnabled()) {
logger.debug("Set access date (Not implemented)" + name + ", " + accessDate);
}
}
// Did we have any cm:auditable properties?
if (auditableProps.size() > 0) {
getPolicyFilter().disableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
nodeService.addProperties(nodeRef, auditableProps);
// DEBUG
if (logger.isDebugEnabled()) {
logger.debug("Set auditable props: " + auditableProps + " file=" + name);
}
}
return;
} catch (org.alfresco.repo.security.permissions.AccessDeniedException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Set file information - access denied, " + name);
}
// Convert to a filesystem access denied status
throw new AccessDeniedException("Set file information " + name);
} catch (AlfrescoRuntimeException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Open file error", ex);
}
throw new IOException("Set file information " + name, ex);
}
}
use of org.alfresco.jlan.server.filesys.NetworkFile in project alfresco-repository by Alfresco.
the class NonTransactionalRuleContentDiskDriver method createFile.
@Override
public NetworkFile createFile(SrvSession sess, TreeConnection tree, FileOpenParams params) throws IOException {
try {
int attr = params.getAttributes();
if (logger.isDebugEnabled()) {
int sharedAccess = params.getSharedAccess();
String strSharedAccess = SharingMode.getSharingModeAsString(sharedAccess);
logger.debug("createFile:" + params.getPath() + ", isDirectory: " + params.isDirectory() + ", isStream: " + params.isStream() + ", readOnlyAccess: " + params.isReadOnlyAccess() + ", readWriteAccess: " + params.isReadWriteAccess() + ", writeOnlyAccess:" + params.isWriteOnlyAccess() + ", attributesOnlyAccess:" + params.isAttributesOnlyAccess() + ", sequentialAccessOnly:" + params.isSequentialAccessOnly() + ", requestBatchOpLock:" + params.requestBatchOpLock() + ", requestExclusiveOpLock:" + params.requestExclusiveOpLock() + ", isDeleteOnClose:" + params.isDeleteOnClose() + ", sharedAccess: " + strSharedAccess + ", allocationSize: " + params.getAllocationSize() + ", isHidden:" + FileAttribute.isHidden(attr) + ", isSystem:" + FileAttribute.isSystem(attr));
}
long creationDateTime = params.getCreationDateTime();
if (creationDateTime != 0) {
logger.debug("creationDateTime is set:" + new Date(creationDateTime));
}
ContentContext tctx = (ContentContext) tree.getContext();
NodeRef rootNode = tctx.getRootNode();
String[] paths = FileName.splitPath(params.getPath());
String folder = paths[0];
String file = paths[1];
DriverState driverState = getDriverState(sess);
EvaluatorContext ctx = getEvaluatorContext(driverState, folder);
Operation o = new CreateFileOperation(file, rootNode, params.getPath(), params.getAllocationSize(), FileAttribute.isHidden(attr));
Command c = ruleEvaluator.evaluate(ctx, o);
Object ret = commandExecutor.execute(sess, tree, c);
if (ret != null && ret instanceof NetworkFile) {
return (NetworkFile) ret;
} else {
// Error - contact broken
logger.error("contract broken - NetworkFile not returned. " + ret == null ? "Return value is null" : ret);
return null;
}
} catch (org.alfresco.repo.security.permissions.AccessDeniedException ade) {
throw new AccessDeniedException("Unable to create file " + params.getPath(), ade);
}
}
use of org.alfresco.jlan.server.filesys.NetworkFile in project alfresco-repository by Alfresco.
the class ScenarioOpenFileInstance method newOpenFileCallbackCommand.
/**
* Called for open file.
*/
private ResultCallback newOpenFileCallbackCommand() {
return new ResultCallback() {
@Override
public void execute(Object result) {
if (result instanceof NetworkFile) {
// Now update the state of this scenario - we have an open fileHandle
NetworkFile fileHandle = (NetworkFile) result;
state = InternalState.OPEN;
if (isReadOnly(fileHandle)) {
openReadOnlyCount++;
fileHandleReadOnly = fileHandle;
if (logger.isDebugEnabled()) {
logger.debug("file opened read only:" + result + ", name:" + name);
}
} else {
openReadWriteCount++;
fileHandleReadWrite = fileHandle;
if (logger.isDebugEnabled()) {
logger.debug("file opened read write :" + result + ", name:" + name);
}
}
}
}
@Override
public TxnReadState getTransactionRequired() {
return TxnReadState.TXN_NONE;
}
};
}
use of org.alfresco.jlan.server.filesys.NetworkFile in project alfresco-repository by Alfresco.
the class ContentDiskDriverTest method testMimetypeWithSufficientData.
// testScenarioMountainLionWord2011EditByEditor_ALF_16257
/**
* Guess mimetype with sufficient data - originally related to ACE-4523
* Simulate creating a plain text document via Alfresco Share
* then updating it via CIFS/FTP
*
* 1. create a document called "foo" with just a little data with an explicit (incorrect) mimetype set.
* 2. update the document with different text
* 3. check the mimetype of the test doc has changed (since it can be guessed)
*/
public void testMimetypeWithSufficientData() throws Exception {
logger.debug("testMimetypeWithInsufficiantData");
// a file without a clue about mimetype
final String FILE_NAME = "foo";
class TestContext {
NetworkFile firstFileHandle;
String mimetype;
}
;
final TestContext testContext = new TestContext();
final String TEST_DIR = TEST_ROOT_DOS_PATH + "\\testMimetypeWithSufficientData";
// this is a made up mimetype - so there is no way that it could be guessed.
final String TEST_MIMETYPE = "text\bar";
ServerConfiguration scfg = new ServerConfiguration("testServer");
TestServer testServer = new TestServer("testServer", scfg);
final SrvSession testSession = new TestSrvSession(666, testServer, "test", "remoteName");
DiskSharedDevice share = getDiskSharedDevice();
final TreeConnection testConnection = testServer.getTreeConnection(share);
final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
/**
* Clean up just in case garbage is left from a previous run
*/
RetryingTransactionCallback<Void> deleteGarbageFileCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME);
return null;
}
};
try {
tran.doInTransaction(deleteGarbageFileCB);
} catch (Exception e) {
// expect to go here
}
logger.debug("a) create new file");
RetryingTransactionCallback<Void> createFileCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
/**
* Create the test directory we are going to use
*/
FileOpenParams createRootDirParams = new FileOpenParams(TEST_ROOT_DOS_PATH, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
FileOpenParams createDirParams = new FileOpenParams(TEST_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
driver.createDirectory(testSession, testConnection, createRootDirParams);
driver.createDirectory(testSession, testConnection, createDirParams);
/**
* Create the file we are going to test
*/
FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams);
assertNotNull(testContext.firstFileHandle);
driver.closeFile(testSession, testConnection, testContext.firstFileHandle);
ClassPathResource fileResource = new ClassPathResource("filesys/ContentDiskDriverTestTxt1.txt");
// Add the test content via the content writer to simulate being created via Share.
NodeRef file1NodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
ContentWriter contentWriter2 = contentService.getWriter(file1NodeRef, ContentModel.PROP_CONTENT, true);
// this is a made up mimetype - so there is no way that it could be guessed.
contentWriter2.setMimetype(TEST_MIMETYPE);
contentWriter2.putContent(fileResource.getFile());
return null;
}
};
tran.doInTransaction(createFileCB, false, true);
/**
* b) Update the file via CIFS
*/
logger.debug("b) update file via CIFS");
RetryingTransactionCallback<Void> updateFileCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Exception {
FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
NetworkFile file = driver.openFile(testSession, testConnection, createFileParams);
assertNotNull(file);
String testContent = "Bar";
byte[] testContentBytes = testContent.getBytes();
file.writeFile(testContentBytes, testContentBytes.length, 0, 0);
driver.closeFile(testSession, testConnection, file);
return null;
}
};
tran.doInTransaction(updateFileCB, false, true);
logger.debug("c) validate results");
/**
* Now validate everything is correct
*/
RetryingTransactionCallback<Void> validateCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
NodeRef shuffledNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
Map<QName, Serializable> props = nodeService.getProperties(shuffledNodeRef);
ContentData data = (ContentData) props.get(ContentModel.PROP_CONTENT);
/**
* Validate mimetype has changed - we can guess that it is text !
*/
assertEquals("mimeType is wrong", MimetypeMap.MIMETYPE_TEXT_PLAIN, data.getMimetype());
return null;
}
};
tran.doInTransaction(validateCB, true, true);
logger.debug("end testMimetypeWithSufficientData");
}
Aggregations