use of org.alfresco.jlan.server.filesys.NetworkFile in project alfresco-repository by Alfresco.
the class ContentDiskDriverTest method testScenarioMSWord2007Save.
// testScenarioMSWord2003SaveShuffleWithBackup
/**
* This test tries to simulate the cifs shuffling that is done to
* support MS Word 2007
*
* a) TEST.DOCX
* b) Save new to 00000001.TMP
* c) Rename TEST.DOCX to 00000002.TMP
* d) Rename 000000001.TMP to TEST.DOCX
* e) Delete 000000002.TMP
*/
public void testScenarioMSWord2007Save() throws Exception {
logger.debug("testScenarioMSWord2007SaveShuffle");
final String FILE_NAME = "TEST.DOCX";
final String FILE_OLD_TEMP = "788A1D3D.tmp";
final String FILE_NEW_TEMP = "19ECA1A.tmp";
class TestContext {
NetworkFile firstFileHandle;
NetworkFile newFileHandle;
// node ref of test.doc
NodeRef testNodeRef;
}
;
final TestContext testContext = new TestContext();
final String TEST_ROOT_DIR = "\\ContentDiskDriverTest";
final String TEST_DIR = "\\ContentDiskDriverTest\\testScenarioMSWord2007Save";
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();
/**
* Create a file in the test directory
*/
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_DIR, 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 use
*/
FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams);
assertNotNull(testContext.firstFileHandle);
// no need to test lots of different properties, that's already been tested above
testContext.testNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
nodeService.setProperty(testContext.testNodeRef, TransferModel.PROP_ENABLED, true);
return null;
}
};
tran.doInTransaction(createFileCB, false, true);
/**
* a) Write some content to the test file
*/
RetryingTransactionCallback<Void> writeFileCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
String testContent = "MS Word 2007 shuffle test";
byte[] testContentBytes = testContent.getBytes();
testContext.firstFileHandle.writeFile(testContentBytes, testContentBytes.length, 0, 0);
testContext.firstFileHandle.close();
return null;
}
};
tran.doInTransaction(writeFileCB, false, true);
/**
* b) Save the new file
*/
RetryingTransactionCallback<Void> saveNewFileCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NEW_TEMP, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
testContext.newFileHandle = driver.createFile(testSession, testConnection, createFileParams);
assertNotNull(testContext.newFileHandle);
String testContent = "MS Word 2007 shuffle test This is new content";
byte[] testContentBytes = testContent.getBytes();
testContext.newFileHandle.writeFile(testContentBytes, testContentBytes.length, 0, 0);
testContext.newFileHandle.close();
return null;
}
};
tran.doInTransaction(saveNewFileCB, false, true);
/**
* c) rename the old file
*/
RetryingTransactionCallback<Void> renameOldFileCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
driver.renameFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME, TEST_DIR + "\\" + FILE_OLD_TEMP);
return null;
}
};
tran.doInTransaction(renameOldFileCB, false, true);
/**
* d) Move the new file into place, stuff should get shuffled
*/
RetryingTransactionCallback<Void> moveNewFileCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
driver.renameFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NEW_TEMP, TEST_DIR + "\\" + FILE_NAME);
return null;
}
};
tran.doInTransaction(moveNewFileCB, false, true);
RetryingTransactionCallback<Void> deleteOldFileCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + FILE_OLD_TEMP);
return null;
}
};
tran.doInTransaction(deleteOldFileCB, false, true);
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);
assertTrue("node does not contain shuffled ENABLED property", props.containsKey(TransferModel.PROP_ENABLED));
assertEquals("name wrong", FILE_NAME, nodeService.getProperty(shuffledNodeRef, ContentModel.PROP_NAME));
return null;
}
};
tran.doInTransaction(validateCB, false, true);
}
use of org.alfresco.jlan.server.filesys.NetworkFile in project alfresco-repository by Alfresco.
the class ContentDiskDriverTest method testScenarioMacMountainLionPreview_MNT_317.
// testScenarioMacMountainLionPreview_MNT_263
/**
* This test tries to simulate the cifs shuffling that is done
* from Save from Mac Mountain Lion by Preview when document is opened/saved few time a row
*
* a) Temp file created in temporary folder (temp\image.jpg)
* b) Original file is renamed for deletion(test\image.jpg -> test\.smbdeleteAAA1b994.4)
* c) Renamed file has got deleteOnClose flag
* d) Renamed file is closed.
* e) Temp file is moved into original file location(temp\image.jpg -> test\image.jgp)
*/
public void testScenarioMacMountainLionPreview_MNT_317() throws Exception {
logger.debug("testScenarioMacMountainLionPreview_MNT_317");
final String FILE_NAME = "image.jpg";
final String TEMP_FILE_NAME = ".smbdeleteAAA1b994.4";
final String UPDATED_TEXT = "Mac Lion Preview Updated Content";
class TestContext {
NetworkFile firstFileHandle;
NetworkFile tempFileHandle;
// node ref image.jpg
NodeRef testNodeRef;
}
;
final TestContext testContext = new TestContext();
final String TEST_ROOT_DIR = "\\ContentDiskDriverTest";
final String TEST_DIR = "\\ContentDiskDriverTest\\testScenarioMountainLionPreview";
final String TEST_TEMP_DIR = "\\ContentDiskDriverTest\\testScenarioMountainLionPreview\\.Temporary Items";
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();
/**
* Create a file in the test directory
*/
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_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
FileOpenParams createDirParams = new FileOpenParams(TEST_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
FileOpenParams createTempDirParams = new FileOpenParams(TEST_TEMP_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
driver.createDirectory(testSession, testConnection, createRootDirParams);
driver.createDirectory(testSession, testConnection, createDirParams);
driver.createDirectory(testSession, testConnection, createTempDirParams);
/**
* Create the file we are going to use
*/
FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams);
assertNotNull(testContext.firstFileHandle);
String testContent = "Mac Lion Preview Content";
byte[] testContentBytes = testContent.getBytes();
driver.writeFile(testSession, testConnection, testContext.firstFileHandle, testContentBytes, 0, testContentBytes.length, 0);
driver.closeFile(testSession, testConnection, testContext.firstFileHandle);
/**
* Create the temp file we are going to use
*/
FileOpenParams createTempFileParams = new FileOpenParams(TEST_TEMP_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
testContext.tempFileHandle = driver.createFile(testSession, testConnection, createTempFileParams);
assertNotNull(testContext.tempFileHandle);
testContent = UPDATED_TEXT;
testContentBytes = testContent.getBytes();
driver.writeFile(testSession, testConnection, testContext.tempFileHandle, testContentBytes, 0, testContentBytes.length, 0);
driver.closeFile(testSession, testConnection, testContext.tempFileHandle);
/**
* Also add versionable to target file
*/
testContext.testNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
nodeService.addAspect(testContext.testNodeRef, ContentModel.ASPECT_VERSIONABLE, null);
return null;
}
};
tran.doInTransaction(createFileCB, false, true);
RetryingTransactionCallback<Void> renameFileCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
FileOpenParams openFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
testContext.tempFileHandle = driver.openFile(testSession, testConnection, openFileParams);
driver.renameFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME, TEST_DIR + "\\" + TEMP_FILE_NAME);
return null;
}
};
tran.doInTransaction(renameFileCB, false, true);
/**
* Delete file via deleteOnClose flag.
*/
RetryingTransactionCallback<Void> deleteOnCloseCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
FileInfo info = new FileInfo();
info.setFileInformationFlags(FileInfo.SetDeleteOnClose);
info.setDeleteOnClose(true);
testContext.tempFileHandle.setDeleteOnClose(true);
driver.setFileInformation(testSession, testConnection, TEST_DIR + "\\" + TEMP_FILE_NAME, info);
assertNotNull(testContext.tempFileHandle);
logger.debug("this close should result in a file being deleted");
driver.closeFile(testSession, testConnection, testContext.tempFileHandle);
return null;
}
};
tran.doInTransaction(deleteOnCloseCB, false, true);
// /**
// * Delete file directly.
// */
// RetryingTransactionCallback<Void> deleteTargetFileCB = new RetryingTransactionCallback<Void>()
// {
//
// @Override
// public Void execute() throws Throwable
// {
//
// driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + TEMP_FILE_NAME);
// return null;
// }
// };
// tran.doInTransaction(deleteTargetFileCB, false, true);
RetryingTransactionCallback<Void> moveRenamedTempFileCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
driver.renameFile(testSession, testConnection, TEST_TEMP_DIR + "\\" + FILE_NAME, TEST_DIR + "\\" + FILE_NAME);
return null;
}
};
tran.doInTransaction(moveRenamedTempFileCB, false, true);
RetryingTransactionCallback<Void> validateCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
NodeRef shuffledNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
assertTrue("node is not versionable", nodeService.hasAspect(shuffledNodeRef, ContentModel.ASPECT_VERSIONABLE));
assertEquals("shuffledNode ref is different", shuffledNodeRef, testContext.testNodeRef);
assertEquals("Unexpected content size", contentService.getReader(shuffledNodeRef, ContentModel.PROP_CONTENT).getSize(), UPDATED_TEXT.length());
return null;
}
};
tran.doInTransaction(validateCB, false, true);
}
use of org.alfresco.jlan.server.filesys.NetworkFile in project alfresco-repository by Alfresco.
the class ContentDiskDriver method openFile.
/**
* Open a file or folder
*
* @param sess SrvSession
* @param tree TreeConnection
* @param params FileOpenParams
* @return NetworkFile
* @exception IOException
*/
public NetworkFile openFile(SrvSession sess, TreeConnection tree, FileOpenParams params) throws IOException {
// Create the transaction
beginReadTransaction(sess);
ContentContext ctx = (ContentContext) tree.getContext();
try {
// Not a pseudo file, try and open a normal file/folder node
NodeRef nodeRef = getNodeForPath(tree, params.getPath());
if (params.hasAccessMode(AccessMode.NTRead) && permissionService.hasPermission(nodeRef, PermissionService.READ) == AccessStatus.DENIED)
throw new AccessDeniedException("No read access to " + params.getFullPath());
if (params.hasAccessMode(AccessMode.NTWrite) && permissionService.hasPermission(nodeRef, PermissionService.WRITE) == AccessStatus.DENIED)
throw new AccessDeniedException("No write access to " + params.getFullPath());
// Check for delete access
// if ( params.hasAccessMode(AccessMode.NTDelete) &&
// permissionService.hasPermission(nodeRef, PermissionService.DELETE) == AccessStatus.DENIED)
// throw new AccessDeniedException("No delete access to " + params.getFullPath());
// Check if the file has a lock
String lockTypeStr = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_TYPE);
if (params.hasAccessMode(AccessMode.NTWrite) && lockTypeStr != null)
throw new AccessDeniedException("File is locked, no write access to " + params.getFullPath());
// Check if there is a file state for the file
FileState fstate = null;
if (ctx.hasStateCache()) {
// Check if there is a file state for the file
fstate = ctx.getStateCache().findFileState(params.getPath());
if (fstate != null) {
if (fstate.exists() == false)
throw new FileNotFoundException();
} else {
// Create a file state for the path
fstate = ctx.getStateCache().findFileState(params.getPath(), true);
}
// Check if the current file open allows the required shared access
boolean nosharing = false;
String noshrReason = null;
if (params.getAccessMode() == AccessMode.NTFileGenericExecute && params.getPath().toLowerCase().endsWith(".exe") == false) {
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE)) {
logger.debug("Execute access mode, path" + params.getPath());
logger.debug(" Fstate=" + fstate);
}
throw new AccessDeniedException("Invalid access mode");
}
if (fstate.getOpenCount() > 0 && params.isAttributesOnlyAccess() == false) {
if (params.getSecurityLevel() == WinNT.SecurityImpersonation && params.getProcessId() == fstate.getProcessId())
nosharing = false;
else if (params.isReadOnlyAccess() && (fstate.getSharedAccess() & SharingMode.READ) != 0)
nosharing = false;
else if ((params.isReadWriteAccess() || params.isWriteOnlyAccess()) && (fstate.getSharedAccess() & SharingMode.WRITE) == 0) {
nosharing = true;
noshrReason = "Sharing mode disallows write";
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Sharing mode disallows write access path=" + params.getPath());
} else if (fstate.getSharedAccess() == SharingMode.NOSHARING) {
nosharing = true;
noshrReason = "Sharing mode exclusive";
} else if ((fstate.getSharedAccess() & params.getSharedAccess()) != params.getSharedAccess()) {
nosharing = true;
noshrReason = "Sharing mode mismatch";
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Local share mode=0x" + Integer.toHexString(fstate.getSharedAccess()) + ", params share mode=0x" + Integer.toHexString(params.getSharedAccess()));
} else if (params.getSharedAccess() == SharingMode.NOSHARING) {
nosharing = true;
noshrReason = "Requestor wants exclusive mode";
}
}
if (nosharing == true) {
if (params.getPath().equals("\\") == false) {
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Sharing violation path=" + params.getPath() + ", sharing=0x" + Integer.toHexString(fstate.getSharedAccess()) + ",reason=" + noshrReason);
throw new FileSharingException("File already open, " + params.getPath());
}
}
// Update the file sharing mode and process id, if this is the first file open
fstate.setSharedAccess(params.getSharedAccess());
fstate.setProcessId(params.getProcessId());
if (logger.isDebugEnabled() && fstate.getOpenCount() == 0 && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Path " + params.getPath() + ", sharing=0x" + Integer.toHexString(params.getSharedAccess()) + ", PID=" + params.getProcessId());
}
// Check if the node is a link node
NodeRef linkRef = (NodeRef) nodeService.getProperty(nodeRef, ContentModel.PROP_LINK_DESTINATION);
AlfrescoNetworkFile netFile = null;
if (linkRef == null) {
if (tree.openFileCount() > 0 && params.isAttributesOnlyAccess() == false) {
// Search the open file table for this session/virtual circuit
int idx = 0;
while (idx < tree.getFileTableLength() && netFile == null) {
// Get the current file from the open file table
NetworkFile curFile = tree.findFile(idx);
if (curFile != null && curFile instanceof ContentNetworkFile) {
// Check if the file is the same path and process id
ContentNetworkFile contentFile = (ContentNetworkFile) curFile;
if (contentFile.getProcessId() == params.getProcessId() && contentFile.getFullName().equalsIgnoreCase(params.getFullPath())) {
if ((params.isReadWriteAccess() && contentFile.getGrantedAccess() == NetworkFile.READWRITE) || (params.isReadOnlyAccess() && contentFile.getGrantedAccess() == NetworkFile.READONLY)) {
// Found a match, re-use the open file
netFile = contentFile;
// Increment the file open count, last file close will actually close the file/stream
contentFile.incrementOpenCount();
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Re-use existing file open Path " + params.getPath() + ", PID=" + params.getProcessId() + ", params=" + (params.isReadOnlyAccess() ? "ReadOnly" : "Write") + ", file=" + (contentFile.getGrantedAccess() <= NetworkFile.READONLY ? "ReadOnly" : "Write"));
} else if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Not re-using file path=" + params.getPath() + ", readWrite=" + (params.isReadWriteAccess() ? "true" : "false") + ", readOnly=" + (params.isReadOnlyAccess() ? "true" : "false") + ", grantedAccess=" + contentFile.getGrantedAccessAsString());
}
}
// Update the file table index
idx++;
}
}
if (netFile == null) {
// Create a new network file for the open request
netFile = ContentNetworkFile.createFile(nodeService, contentService, mimetypeService, cifsHelper, nodeRef, params.getPath(), params.isReadOnlyAccess(), params.isAttributesOnlyAccess(), sess);
}
} else {
// Get the CIFS server name
String srvName = null;
SMBServer cifsServer = (SMBServer) sess.getServer().getConfiguration().findServer("CIFS");
if (sess instanceof SMBSrvSession) {
SMBSrvSession smbSess = (SMBSrvSession) sess;
srvName = smbSess.getShareHostName();
} else if (cifsServer != null) {
// Use the CIFS server name in the URL
srvName = cifsServer.getServerName();
} else {
// Use the local server name in the URL
srvName = InetAddress.getLocalHost().getHostName();
}
// Convert the target node to a path, convert to URL format
String path = getPathForNode(tree, linkRef);
path = path.replace(FileName.DOS_SEPERATOR, '/');
// Build the URL file data
StringBuilder urlStr = new StringBuilder();
urlStr.append("[InternetShortcut]\r\n");
urlStr.append("URL=file://");
urlStr.append(srvName);
urlStr.append("/");
urlStr.append(tree.getSharedDevice().getName());
urlStr.append(path);
urlStr.append("\r\n");
// Create the in memory pseudo file for the URL link
byte[] urlData = urlStr.toString().getBytes();
// Get the file information for the link node
FileInfo fInfo = cifsHelper.getFileInformation(nodeRef, isReadOnly, isLockedFilesAsOffline);
// Set the file size to the actual data length
fInfo.setFileSize(urlData.length);
// Create the network file using the in-memory file data
netFile = new LinkMemoryNetworkFile(fInfo.getFileName(), urlData, fInfo, nodeRef);
netFile.setFullName(params.getPath());
}
if (netFile != null) {
long id = DefaultTypeConverter.INSTANCE.convert(Long.class, nodeService.getProperty(nodeRef, ContentModel.PROP_NODE_DBID));
netFile.setFileId((int) (id & 0xFFFFFFFFL));
// Indicate the file is open
netFile.setClosed(false);
}
if (params.isOverwrite() && netFile != null) {
// Truncate the file to zero length
netFile.truncateFile(0L);
}
if (ctx.hasStateCache()) {
if (fstate == null)
fstate = ctx.getStateCache().findFileState(params.getPath(), true);
if (netFile.getGrantedAccess() > NetworkFile.ATTRIBUTESONLY)
fstate.incrementOpenCount();
fstate.setFilesystemObject(nodeRef);
// Store the state with the file
netFile.setFileState(fstate);
if (fstate.hasFileSize())
netFile.setFileSize(fstate.getFileSize());
}
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Opened network file: path=" + params.getPath() + " file open parameters=" + params + " network file=" + netFile);
return netFile;
} catch (org.alfresco.repo.security.permissions.AccessDeniedException ex) {
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Open file - access denied, " + params.getFullPath());
throw new AccessDeniedException("Open file " + params.getFullPath());
} catch (RuntimeException ex) {
if (logger.isDebugEnabled() && ctx.hasDebug(AlfrescoContext.DBG_FILE))
logger.debug("Open file error", ex);
throw new IOException("Open file " + params.getFullPath());
}
}
use of org.alfresco.jlan.server.filesys.NetworkFile in project alfresco-repository by Alfresco.
the class ContentDiskDriverTest method testCreateFile.
/**
* Test Create File
*/
public void testCreateFile() throws Exception {
logger.debug("testCreatedFile");
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();
class TestContext {
NodeRef testNodeRef;
}
;
final TestContext testContext = new TestContext();
/**
* Step 1 : Create a new file in read/write mode and add some content.
*/
int openAction = FileAction.CreateNotExist;
final String FILE_NAME = "testCreateFileX.new";
final String FILE_PATH = "\\" + FILE_NAME;
FileOpenParams params = new FileOpenParams(FILE_PATH, openAction, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
final NetworkFile file = driver.createFile(testSession, testConnection, params);
assertNotNull("file is null", file);
assertFalse("file is read only, should be read-write", file.isReadOnly());
assertFalse("file is not closed ", file.isClosed());
RetryingTransactionCallback<Void> writeStuffCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
byte[] stuff = "Hello World".getBytes();
driver.writeFile(testSession, testConnection, file, stuff, 0, stuff.length, 0);
driver.closeFile(testSession, testConnection, file);
return null;
}
};
tran.doInTransaction(writeStuffCB);
RetryingTransactionCallback<Void> validateCB = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
NodeRef companyHome = repositoryHelper.getCompanyHome();
NodeRef newNode = nodeService.getChildByName(companyHome, ContentModel.ASSOC_CONTAINS, FILE_NAME);
testContext.testNodeRef = newNode;
assertNotNull("can't find new node", newNode);
Serializable content = nodeService.getProperty(newNode, ContentModel.PROP_CONTENT);
assertNotNull("content is null", content);
return null;
}
};
tran.doInTransaction(validateCB);
// now validate that the new node is in the correct location and has the correct name
FileInfo info = driver.getFileInformation(testSession, testConnection, FILE_PATH);
assertNotNull("info is null", info);
NodeRef n2 = getNodeForPath(testConnection, FILE_PATH);
assertEquals("get Node For Path returned different node", testContext.testNodeRef, n2);
/**
* Step 2 : Negative Test Attempt to create the same file again
*/
try {
driver.createFile(testSession, testConnection, params);
fail("File exists not detected");
} catch (FileExistsException fe) {
// expect to go here
}
// Clean up so we could run the test again
driver.deleteFile(testSession, testConnection, FILE_PATH);
/**
* Step 3 : create a file in a new directory in read only mode
*/
String FILE2_PATH = TEST_ROOT_DOS_PATH + FILE_PATH;
FileOpenParams dirParams = new FileOpenParams(TEST_ROOT_DOS_PATH, openAction, AccessMode.ReadOnly, FileAttribute.NTDirectory, 0);
driver.createDirectory(testSession, testConnection, dirParams);
FileOpenParams file2Params = new FileOpenParams(FILE2_PATH, openAction, AccessMode.ReadOnly, FileAttribute.NTNormal, 0);
NetworkFile file2 = driver.createFile(testSession, testConnection, file2Params);
// clean up so we could run the test again
driver.deleteFile(testSession, testConnection, FILE2_PATH);
}
use of org.alfresco.jlan.server.filesys.NetworkFile in project alfresco-repository by Alfresco.
the class ContentDiskDriverTest method testCSVExcel2003SaveShuffle.
/**
* Excel 2003 CSV file with Versionable file
*
* CreateFile csv.csv and 5EE27101
* Add versionable aspect
* RenameFile oldPath:\Espaces Utilisateurs\System\csv.csv, newPath:\Espaces\Utilisateurs\System\5EE27101
* CreateFile name=\Espaces Utilisateurs\System\csv.csv
* Add content
*/
public void testCSVExcel2003SaveShuffle() throws Exception {
logger.debug("testCSVExcel2003SaveShuffle");
final String FILE_NAME = "csv.csv";
final String FILE_TITLE = "csv";
final String FILE_DESCRIPTION = "This is a test document to test CIFS shuffle";
final String FILE_TEMP = "AAAA0000";
class TestContext {
NetworkFile firstFileHandle;
NetworkFile newFileHandle;
NodeRef testNodeRef;
Serializable testCreatedDate;
}
;
final TestContext testContext = new TestContext();
final String TEST_DIR = TEST_ROOT_DOS_PATH + "\\testMSExcel2003CSVShuffle";
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 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
}
/**
* Create a file in the test directory
*/
RetryingTransactionCallback<Void> createTestFileFirstTime = 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 use
*/
FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams);
assertNotNull(testContext.firstFileHandle);
testContext.testNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
nodeService.setProperty(testContext.testNodeRef, ContentModel.PROP_TITLE, FILE_TITLE);
nodeService.setProperty(testContext.testNodeRef, ContentModel.PROP_DESCRIPTION, FILE_DESCRIPTION);
return null;
}
};
tran.doInTransaction(createTestFileFirstTime, false, true);
/**
* Write some content to the test file. Add versionable aspect
*/
RetryingTransactionCallback<Void> writeToTestFileAndAddVersionableAspect = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
String testContent = "MS Excel 2003 for CSV shuffle test";
byte[] testContentBytes = testContent.getBytes();
testContext.firstFileHandle.writeFile(testContentBytes, testContentBytes.length, 0, 0);
testContext.firstFileHandle.close();
testContext.testCreatedDate = nodeService.getProperty(testContext.testNodeRef, ContentModel.PROP_CREATED);
nodeService.addAspect(testContext.testNodeRef, ContentModel.ASPECT_VERSIONABLE, Collections.<QName, Serializable>singletonMap(ContentModel.PROP_VERSION_TYPE, org.alfresco.service.cmr.version.VersionType.MINOR));
return null;
}
};
tran.doInTransaction(writeToTestFileAndAddVersionableAspect, false, true);
/**
* rename the test file to the temp
*/
RetryingTransactionCallback<Void> renameTestFileToTemp = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
driver.renameFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME, TEST_DIR + "\\" + FILE_TEMP);
return null;
}
};
tran.doInTransaction(renameTestFileToTemp, false, true);
/**
* create the test file one more
*/
RetryingTransactionCallback<Void> createTestFileOneMore = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
FileOpenParams params = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, FileAction.TruncateExisting, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
NetworkFile file = driver.createFile(testSession, testConnection, params);
driver.closeFile(testSession, testConnection, file);
return null;
}
};
tran.doInTransaction(createTestFileOneMore, false, true);
/**
* Write the new content
*/
RetryingTransactionCallback<Void> writeUpdate = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
testContext.newFileHandle = driver.openFile(testSession, testConnection, createFileParams);
assertNotNull(testContext.newFileHandle);
String testContent = "MS Word 2003 for CSV shuffle test This is new content";
byte[] testContentBytes = testContent.getBytes();
testContext.newFileHandle.writeFile(testContentBytes, testContentBytes.length, 0, 0);
testContext.newFileHandle.close();
return null;
}
};
tran.doInTransaction(writeUpdate, false, true);
// Check results
RetryingTransactionCallback<Void> validate = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
NodeRef shuffledNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
// Check versionable aspect, version label and nodeRef
assertTrue("VERSIONABLE aspect not present", nodeService.hasAspect(shuffledNodeRef, ContentModel.ASPECT_VERSIONABLE));
assertEquals("nodeRef changed", testContext.testNodeRef, shuffledNodeRef);
// Check the titled aspect is correct
assertEquals("name wrong", FILE_NAME, nodeService.getProperty(shuffledNodeRef, ContentModel.PROP_NAME));
assertEquals("title wrong", FILE_TITLE, nodeService.getProperty(shuffledNodeRef, ContentModel.PROP_TITLE));
assertEquals("description wrong", FILE_DESCRIPTION, nodeService.getProperty(shuffledNodeRef, ContentModel.PROP_DESCRIPTION));
return null;
}
};
tran.doInTransaction(validate, true, true);
/**
* Clean up just in case garbage is left from a previous run
*/
RetryingTransactionCallback<Void> deleteTestFile = new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME);
return null;
}
};
tran.doInTransaction(deleteTestFile, false, true);
}
Aggregations