();
String nodeName = "leak-session-doc-" + GUID.generate();
properties.put(ContentModel.PROP_NAME, nodeName);
NodeRef nodeRef = nodeService.createNode(companyHomeNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName(ContentModel.USER_MODEL_URI, nodeName),
ContentModel.TYPE_CONTENT, properties).getChildRef();
contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true).putContent("WebDAVTestContent");
txn.commit();
txn = transactionService.getUserTransaction();
txn.begin();
nodeService.deleteNode(nodeRef);
txn.commit();
AuthenticationUtil.clearCurrentSecurityContext();
}
@Test
public void testPutContentToNonExistingFile() throws Exception
{
String fileName = "file-" + GUID.generate();
NodeRef fileNoderef = null;
try
{
executeMethod(WebDAV.METHOD_PUT, fileName, testDataFile, null);
ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home//cm:" + fileName + "\"");
fileNoderef = resultSet.getNodeRef(0);
resultSet.close();
assertTrue("File does not exist.", nodeService.exists(fileNoderef));
assertEquals("Filename is not correct", fileName, nodeService.getProperty(fileNoderef, ContentModel.PROP_NAME));
assertTrue("Expected return status is " + HttpServletResponse.SC_CREATED + ", but returned is " + response.getStatus(),
HttpServletResponse.SC_CREATED == response.getStatus());
InputStream updatedFileIS = fileFolderService.getReader(fileNoderef).getContentInputStream();
byte[] updatedFile = IOUtils.toByteArray(updatedFileIS);
updatedFileIS.close();
assertTrue("The content has to be equal", ArrayUtils.isEquals(testDataFile, updatedFile));
}
catch (Exception e)
{
fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
finally
{
if (fileNoderef != null)
{
nodeService.deleteNode(fileNoderef);
}
}
}
@Test
public void testPutContentToAnExistingFile() throws Exception
{
FileInfo testFileInfo = fileFolderService.create(companyHomeNodeRef, "file-" + GUID.generate(), ContentModel.TYPE_CONTENT);
try
{
executeMethod(WebDAV.METHOD_PUT, testFileInfo.getName(), testDataFile, null);
assertTrue("File does not exist.", nodeService.exists(testFileInfo.getNodeRef()));
assertEquals("Filename is not correct.", testFileInfo.getName(), nodeService.getProperty(testFileInfo.getNodeRef(), ContentModel.PROP_NAME));
assertTrue("Expected return status is " + HttpServletResponse.SC_NO_CONTENT + ", but returned is " + response.getStatus(),
HttpServletResponse.SC_NO_CONTENT == response.getStatus());
InputStream updatedFileIS = fileFolderService.getReader(testFileInfo.getNodeRef()).getContentInputStream();
byte[] updatedFile = IOUtils.toByteArray(updatedFileIS);
updatedFileIS.close();
assertTrue("The content has to be equal", ArrayUtils.isEquals(testDataFile, updatedFile));
}
catch (Exception e)
{
fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
finally
{
nodeService.deleteNode(testFileInfo.getNodeRef());
}
}
/**
* Updating a file in an non-existent path
*/
@Test
public void testPutContentBadPath() throws Exception
{
String fileName = "file-" + GUID.generate();
NodeRef fileNoderef = null;
try
{
// Add non-existent path
executeMethod(WebDAV.METHOD_PUT, "non/existent/path" + fileName, testDataFile, null);
fail("The PUT execution should fail with a 400 error");
}
catch (WebDAVServerException wse)
{
// The execution failed and it is expected
assertTrue(wse.getHttpStatusCode() == HttpServletResponse.SC_CONFLICT);
}
catch (Exception e)
{
fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
finally
{
if (fileNoderef != null)
{
nodeService.deleteNode(fileNoderef);
}
}
}
/**
* Creating a folder and trying to update it with a file
*/
@Test
public void testPutContentToFolder() throws Exception
{
FileInfo testFileInfo = fileFolderService.create(companyHomeNodeRef, "folder-" + GUID.generate(), ContentModel.TYPE_FOLDER);
try
{
executeMethod(WebDAV.METHOD_PUT, testFileInfo.getName(), testDataFile, null);
fail("The PUT execution should fail with a 400 error");
}
catch (WebDAVServerException wse)
{
// The execution failed and it is expected
assertTrue(wse.getHttpStatusCode() == HttpServletResponse.SC_BAD_REQUEST);
}
catch (Exception e)
{
fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
finally
{
nodeService.deleteNode(testFileInfo.getNodeRef());
}
}
/**
* Putting a content to a locked file
*
* Create and lock a file by admin
*
* Try to put the content by user
*/
@SuppressWarnings("deprecation")
@Test
public void testPutContentToLockedFIle() throws Exception
{
FileInfo testFileInfo = fileFolderService.create(companyHomeNodeRef, "file-" + GUID.generate(), ContentModel.TYPE_CONTENT);
lockService.lock(testFileInfo.getNodeRef(), LockType.WRITE_LOCK);
try
{
AuthenticationUtil.setFullyAuthenticatedUser(USER_NAME);
executeMethod(WebDAV.METHOD_PUT, testFileInfo.getName(), testDataFile, null);
fail("The PUT execution should fail with a 423 error");
}
catch (WebDAVServerException wse)
{
// The execution failed and it is expected
assertTrue(wse.getHttpStatusCode() == WebDAV.WEBDAV_SC_LOCKED);
}
catch (Exception e)
{
fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
finally
{
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
nodeService.deleteNode(testFileInfo.getNodeRef());
}
}
/**
* Putting a content and check versioning
*
* The node will be deleted in after test.
*/
@Test
public void testPutContentCheckVersions() throws Exception
{
assertEquals("The version should be 1.0 as no file modifications were done yet.", "1.0", nodeService.getProperty(versionableDoc, ContentModel.PROP_VERSION_LABEL));
// Lock the node
try
{
executeMethod(WebDAV.METHOD_LOCK, versionableDocName, davLockInfoFile, null);
assertEquals("The version should not advance", "1.0", nodeService.getProperty(versionableDoc, ContentModel.PROP_VERSION_LABEL));
}
catch (Exception e)
{
fail("Failed to lock a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
// Split to transactions to check the commit
txn.commit();
txn = transactionService.getUserTransaction();
txn.begin();
assertEquals("The version should not advance", "1.0", nodeService.getProperty(versionableDoc, ContentModel.PROP_VERSION_LABEL));
// Put non-zero content
// Construct IF HEADER
String lockToken = versionableDoc.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + AuthenticationUtil.getAdminUserName();
String lockHeaderValue = "(<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">)";
HashMap headers = new HashMap();
headers.put(WebDAV.HEADER_IF, lockHeaderValue);
try
{
executeMethod(WebDAV.METHOD_PUT, versionableDocName, testDataFile, headers);
assertEquals("The version should not advance", "1.0", nodeService.getProperty(versionableDoc, ContentModel.PROP_VERSION_LABEL));
}
catch (Exception e)
{
fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
// Split to transactions to check the commit
txn.commit();
txn = transactionService.getUserTransaction();
txn.begin();
assertEquals("The version should advance", "1.1", nodeService.getProperty(versionableDoc, ContentModel.PROP_VERSION_LABEL));
// Unlock
headers = new HashMap();
headers.put(WebDAV.HEADER_LOCK_TOKEN, "<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">");
try
{
executeMethod(WebDAV.METHOD_UNLOCK, versionableDocName, null, headers);
assertEquals("The version should not advance from 1.1", "1.1", nodeService.getProperty(versionableDoc, ContentModel.PROP_VERSION_LABEL));
}
catch (Exception e)
{
fail("Failed to unlock a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
// Split to transactions to check the commit
txn.commit();
txn = transactionService.getUserTransaction();
txn.begin();
assertEquals("The version should not advance from 1.1", "1.1", nodeService.getProperty(versionableDoc, ContentModel.PROP_VERSION_LABEL));
}
/**
* Putting a zero content file and update it
*
* Put an empty file
*
* Lock the file
*
* Put the contents
*
* Unlock the node
*/
@Test
public void testPutNoContentFileAndUpdate() throws Exception
{
String fileName = "file-" + GUID.generate();
NodeRef fileNoderef = null;
try
{
executeMethod(WebDAV.METHOD_PUT, fileName, new byte[0], null);
ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home//cm:" + fileName + "\"");
fileNoderef = resultSet.getNodeRef(0);
resultSet.close();
assertTrue("File should exist.", nodeService.exists(fileNoderef));
assertEquals("Filename is not correct", fileName, nodeService.getProperty(fileNoderef, ContentModel.PROP_NAME));
assertTrue("Expected return status is " + HttpServletResponse.SC_CREATED + ", but returned is " + response.getStatus(),
HttpServletResponse.SC_CREATED == response.getStatus());
byte[] updatedFile = IOUtils.toByteArray(fileFolderService.getReader(fileNoderef).getContentInputStream());
assertTrue("The content should be empty", updatedFile.length == 0);
}
catch (Exception e)
{
fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
try
{
executeMethod(WebDAV.METHOD_LOCK, fileName, davLockInfoFile, null);
assertEquals("File should be locked", LockStatus.LOCK_OWNER, lockService.getLockStatus(fileNoderef));
}
catch (Exception e)
{
fail("Failed to lock a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
// Construct IF HEADER
String lockToken = fileNoderef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + AuthenticationUtil.getAdminUserName();
String lockHeaderValue = "(<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">)";
HashMap headers = new HashMap();
headers.put(WebDAV.HEADER_IF, lockHeaderValue);
try
{
executeMethod(WebDAV.METHOD_PUT, fileName, testDataFile, headers);
assertTrue("File does not exist.", nodeService.exists(fileNoderef));
assertEquals("Filename is not correct", fileName, nodeService.getProperty(fileNoderef, ContentModel.PROP_NAME));
assertTrue("Expected return status is " + HttpServletResponse.SC_NO_CONTENT + ", but returned is " + response.getStatus(),
HttpServletResponse.SC_NO_CONTENT == response.getStatus());
assertTrue("File should have NO_CONTENT aspect", nodeService.hasAspect(fileNoderef, ContentModel.ASPECT_NO_CONTENT));
InputStream updatedFileIS = fileFolderService.getReader(fileNoderef).getContentInputStream();
byte[] updatedFile = IOUtils.toByteArray(updatedFileIS);
updatedFileIS.close();
assertTrue("The content has to be equal", ArrayUtils.isEquals(testDataFile, updatedFile));
}
catch (Exception e)
{
fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
headers = new HashMap();
headers.put(WebDAV.HEADER_LOCK_TOKEN, "<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">");
try
{
executeMethod(WebDAV.METHOD_UNLOCK, fileName, null, headers);
assertTrue("Expected return status is " + HttpServletResponse.SC_NO_CONTENT + ", but returned is " + response.getStatus(),
HttpServletResponse.SC_NO_CONTENT == response.getStatus());
assertFalse("File should not have NO_CONTENT aspect", nodeService.hasAspect(fileNoderef, ContentModel.ASPECT_NO_CONTENT));
assertEquals("File should be unlocked", LockStatus.NO_LOCK, lockService.getLockStatus(fileNoderef));
}
catch (Exception e)
{
fail("Failed to unlock a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
if (fileNoderef != null)
{
nodeService.deleteNode(fileNoderef);
}
}
/**
* Negative test to check that a temp file will be deleted from the repo if writing the content fails
*/
@Test
public void testPutNullContent() throws Exception
{
String fileName = "file-" + GUID.generate();
NodeRef fileNoderef = null;
// Lock the node
try
{
executeMethod(WebDAV.METHOD_LOCK, fileName, davLockInfoFile, null);
ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home//cm:" + fileName + "\"");
fileNoderef = resultSet.getNodeRef(0);
resultSet.close();
}
catch (Exception e)
{
fail("Failed to lock a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
txn.commit();
txn = transactionService.getUserTransaction();
txn.begin();
// Construct IF HEADER
String lockToken = fileNoderef.getId() + WebDAV.LOCK_TOKEN_SEPERATOR + AuthenticationUtil.getAdminUserName();
String lockHeaderValue = "(<" + WebDAV.OPAQUE_LOCK_TOKEN + lockToken + ">)";
HashMap headers = new HashMap();
headers.put(WebDAV.HEADER_IF, lockHeaderValue);
try
{
// setting a null content
executeMethod(WebDAV.METHOD_PUT, fileName, null, headers);
fail("The execution should fail.");
}
catch (WebDAVServerException wse)
{
if (nodeService.exists(fileNoderef))
{
nodeService.deleteNode(fileNoderef);
fail("File exist, but should not.");
}
}
catch (Exception e)
{
fail("Failed to upload a file: " + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
}
if (fileNoderef != null && nodeService.exists(fileNoderef))
{
nodeService.deleteNode(fileNoderef);
}
}
/**
* Executes WebDAV method for testing
*
* Sets content to request from a test file
*
* @param method Method to prepare, should be initialized (PUT, LOCK, UNLOCK are supported)
* @param fileName the name of the file set to the context, can be used with path, i.e. "path/to/file/fileName.txt"
* @param content If not null adds test content to the request
* @param headers to set to request, can be null
* @throws Exception
*/
private void executeMethod(String methodName, String fileName, byte[] content, Map headers) throws Exception
{
if (methodName == WebDAV.METHOD_PUT)
method = new PutMethod();
else if (methodName == WebDAV.METHOD_LOCK)
method = new LockMethod();
else if (methodName == WebDAV.METHOD_UNLOCK)
method = new UnlockMethod();
if (method != null)
{
request = new MockHttpServletRequest(methodName, "/alfresco/webdav/" + fileName);
response = new MockHttpServletResponse();
request.setServerPort(8080);
request.setServletPath("/webdav");
if (content != null)
{
request.setContent(content);
}
if (headers != null && !headers.isEmpty())
{
for (String key : headers.keySet())
{
request.addHeader(key, headers.get(key));
}
}
method.setDetails(request, response, webDAVHelper, companyHomeNodeRef);
method.execute();
}
}
}