use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.
the class WebDAVHelper method determineSiteId.
public String determineSiteId(NodeRef rootNodeRef, String path) {
SiteService siteService = getServiceRegistry().getSiteService();
String siteId;
try {
FileInfo fileInfo = getNodeForPath(rootNodeRef, path);
siteId = siteService.getSiteShortName(fileInfo.getNodeRef());
if (siteId == null) {
throw new RuntimeException("Node is not contained by a site: " + path);
}
} catch (Exception error) {
siteId = EMPTY_SITE_ID;
}
return siteId;
}
use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.
the class AbstractBulkFileSystemImportWebScript method convertPathToNodeRef.
protected NodeRef convertPathToNodeRef(String targetPath) throws FileNotFoundException {
NodeRef result = null;
NodeRef companyHome = repository.getCompanyHome();
String cleanTargetPath = targetPath.replaceAll("/+", "/");
if (cleanTargetPath.startsWith(COMPANY_HOME_PATH))
cleanTargetPath = cleanTargetPath.substring(COMPANY_HOME_PATH.length());
if (cleanTargetPath.startsWith("/"))
cleanTargetPath = cleanTargetPath.substring(1);
if (cleanTargetPath.endsWith("/"))
cleanTargetPath = cleanTargetPath.substring(0, cleanTargetPath.length() - 1);
if (cleanTargetPath.length() == 0)
result = companyHome;
else {
FileInfo info = fileFolderService.resolveNamePath(companyHome, Arrays.asList(cleanTargetPath.split("/")));
if (info == null)
throw new WebScriptException("could not determine NodeRef for path :'" + cleanTargetPath + "'");
result = info.getNodeRef();
}
return (result);
}
use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.
the class GetMethod method generateDirectoryListing.
/**
* Generates a HTML representation of the contents of the path represented by the given node
*
* @param fileInfo the file to use
*/
private void generateDirectoryListing(FileInfo fileInfo) {
MimetypeService mimeTypeService = getMimetypeService();
NodeService nodeService = getNodeService();
Writer writer = null;
try {
writer = m_response.getWriter();
boolean wasLink = false;
if (fileInfo.isLink()) {
fileInfo = getFileFolderService().getFileInfo(fileInfo.getLinkNodeRef());
wasLink = true;
}
// Get the list of child nodes for the parent node
List<FileInfo> childNodeInfos = getDAVHelper().getChildren(fileInfo);
// Send back the start of the HTML
writer.write("<html><head><title>");
writer.write(WebDAVHelper.encodeHTML(I18NUtil.getMessage("webdav.repository_title")));
writer.write("</title>");
writer.write("<style>");
writer.write("body { font-family: Arial, Helvetica; font-size: 12pt; background-color: white; }\n");
writer.write("table { font-family: Arial, Helvetica; font-size: 12pt; background-color: white; }\n");
writer.write(".listingTable { border: solid black 1px; }\n");
writer.write(".textCommand { font-family: verdana; font-size: 10pt; }\n");
writer.write(".textLocation { font-family: verdana; font-size: 11pt; font-weight: bold; color: #2a568f; }\n");
writer.write(".textData { font-family: verdana; font-size: 10pt; }\n");
writer.write(".tableHeading { font-family: verdana; font-size: 10pt; font-weight: bold; color: white; background-color: #2a568f; }\n");
writer.write(".rowOdd { background-color: #eeeeee; }\n");
writer.write(".rowEven { background-color: #dddddd; }\n");
writer.write("</style></head>\n");
writer.flush();
// Send back the table heading
writer.write("<body>\n");
writer.write("<table cellspacing='2' cellpadding='3' border='0' width='100%'>\n");
writer.write("<tr><td colspan='4' class='textLocation'>");
writer.write(WebDAVHelper.encodeHTML(I18NUtil.getMessage("webdav.directory_listing")));
writer.write(' ');
writer.write(WebDAVHelper.encodeHTML(getPath()));
writer.write("</td></tr>\n");
writer.write("<tr><td height='10' colspan='4'></td></tr></table>");
writer.write("<table cellspacing='2' cellpadding='3' border='0' width='100%' class='listingTable'>\n");
writer.write("<tr><td class='tableHeading' width='*'>");
writer.write(WebDAVHelper.encodeHTML(I18NUtil.getMessage("webdav.column.name")));
writer.write("</td>");
writer.write("<td class='tableHeading' width='10%'>");
writer.write(WebDAVHelper.encodeHTML(I18NUtil.getMessage("webdav.column.size")));
writer.write("</td>");
writer.write("<td class='tableHeading' width='20%'>");
writer.write(WebDAVHelper.encodeHTML(I18NUtil.getMessage("webdav.column.type")));
writer.write("</td>");
writer.write("<td class='tableHeading' width='25%'>");
writer.write(WebDAVHelper.encodeHTML(I18NUtil.getMessage("webdav.column.modifieddate")));
writer.write("</td>");
writer.write("</tr>\n");
// Get the URL for the root path
String rootURL = getURLForPath(m_request, getPath(), true);
if (rootURL.endsWith(WebDAVHelper.PathSeperator) == false) {
rootURL = rootURL + WebDAVHelper.PathSeperator;
}
if (wasLink) {
Path pathToNode = nodeService.getPath(fileInfo.getNodeRef());
if (pathToNode.size() > 2) {
pathToNode = pathToNode.subPath(2, pathToNode.size() - 1);
}
rootURL = getURLForPath(m_request, pathToNode.toDisplayPath(nodeService, getPermissionService()), true);
if (rootURL.endsWith(WebDAVHelper.PathSeperator) == false) {
rootURL = rootURL + WebDAVHelper.PathSeperator;
}
rootURL = rootURL + WebDAVHelper.encodeURL(fileInfo.getName(), m_userAgent) + WebDAVHelper.PathSeperator;
}
// Start with a link to the parent folder so we can navigate back up, unless we are at the root level
if (!getDAVHelper().isRootPath(getPath(), getServletPath())) {
writer.write("<tr class='rowOdd'>");
writer.write("<td colspan='4' class='textData'><a href=\"");
// Strip the last folder from the path
String parentFolderUrl = parentFolder(rootURL);
writer.write(parentFolderUrl);
writer.write("\">");
writer.write("[");
writer.write(WebDAVHelper.encodeHTML(I18NUtil.getMessage("webdav.column.navigate_up")));
writer.write("]</a>");
writer.write("</tr>\n");
}
// Send back what we have generated so far
writer.flush();
int rowId = 0;
for (FileInfo childNodeInfo : childNodeInfos) {
// Output the details for the current node
writer.write("<tr class='");
if ((rowId++ & 1) == 1) {
writer.write("rowOdd");
} else {
writer.write("rowEven");
}
writer.write("'><td class='textData'><a href=\"");
writer.write(rootURL);
// name field
String fname = childNodeInfo.getName();
writer.write(WebDAVHelper.encodeURL(fname, m_userAgent));
writer.write("\">");
writer.write(WebDAVHelper.encodeHTML(fname));
writer.write("</a>");
// size field
writer.write("</td><td class='textData'>");
ContentData contentData = null;
if (!childNodeInfo.isFolder()) {
Serializable contentPropertyName = nodeService.getProperty(childNodeInfo.getNodeRef(), ContentModel.PROP_CONTENT_PROPERTY_NAME);
QName contentPropertyQName = DefaultTypeConverter.INSTANCE.convert(QName.class, contentPropertyName);
if (null == contentPropertyQName) {
contentPropertyQName = ContentModel.PROP_CONTENT;
}
Serializable contentProperty = nodeService.getProperty(childNodeInfo.getNodeRef(), contentPropertyQName);
if (contentProperty instanceof ContentData) {
contentData = (ContentData) contentProperty;
}
}
if (childNodeInfo.isFolder()) {
writer.write(" ");
} else {
if (null != contentData) {
writer.write(formatSize(Long.toString(contentData.getSize())));
} else {
writer.write(" ");
}
}
writer.write("</td><td class='textData'>");
// mimetype field
if (childNodeInfo.isFolder()) {
writer.write(" ");
} else {
String mimetype = " ";
if (null != contentData) {
mimetype = contentData.getMimetype();
String displayType = mimeTypeService.getDisplaysByMimetype().get(mimetype);
if (displayType != null) {
mimetype = displayType;
}
}
writer.write(mimetype);
}
writer.write("</td><td class='textData'>");
// modified date field
Date modifiedDate = childNodeInfo.getModifiedDate();
if (modifiedDate != null) {
writer.write(WebDAV.formatHeaderDate(DefaultTypeConverter.INSTANCE.longValue(modifiedDate)));
} else {
writer.write(" ");
}
writer.write("</td></tr>\n");
// flush every few rows
if ((rowId & 15) == 0) {
writer.flush();
}
}
writer.write("</table></body></html>");
} catch (Throwable e) {
logger.error(e);
if (writer != null) {
try {
writer.write("</table><table><tr><td style='color:red'>");
writer.write(WebDAVHelper.encodeHTML(I18NUtil.getMessage("webdav.err.dir")));
writer.write("</td></tr></table></body></html>");
writer.flush();
} catch (IOException ioe) {
}
}
}
}
use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.
the class LockMethod method attemptLock.
/**
* The main lock implementation method.
*
* @throws WebDAVServerException
* @throws Exception
*/
protected void attemptLock() throws WebDAVServerException, Exception {
FileFolderService fileFolderService = getFileFolderService();
final String path = getPath();
NodeRef rootNodeRef = getRootNodeRef();
// Get the active user
final String userName = getDAVHelper().getAuthenticationService().getCurrentUserName();
if (logger.isDebugEnabled()) {
logger.debug("Locking node: \n" + " user: " + userName + "\n" + " path: " + path);
}
FileInfo lockNodeInfo = null;
try {
// Check if the path exists
lockNodeInfo = getNodeForPath(getRootNodeRef(), getPath());
} catch (FileNotFoundException e) {
if (m_conditions != null) {
// MNT-12303 fix, check whether this is a refresh lock request
for (Condition condition : m_conditions) {
List<String> lockTolensMatch = condition.getLockTokensMatch();
List<String> etagsMatch = condition.getETagsMatch();
if (m_request.getContentLength() == -1 && (lockTolensMatch != null && !lockTolensMatch.isEmpty()) || (etagsMatch != null && !etagsMatch.isEmpty())) {
// so there is nothing to refresh. Return 403 Forbidden as original SharePoint Server.
throw new WebDAVServerException(HttpServletResponse.SC_FORBIDDEN);
}
}
}
// need to create it
String[] splitPath = getDAVHelper().splitPath(path);
// check
if (splitPath[1].length() == 0) {
throw new WebDAVServerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
FileInfo dirInfo = null;
List<String> dirPathElements = getDAVHelper().splitAllPaths(splitPath[0]);
if (dirPathElements.size() == 0) {
// if there are no path elements we are at the root so get the root node
dirInfo = fileFolderService.getFileInfo(getRootNodeRef());
} else {
// make sure folder structure is present
dirInfo = FileFolderUtil.makeFolders(fileFolderService, rootNodeRef, dirPathElements, ContentModel.TYPE_FOLDER);
}
if (dirInfo == null) {
throw new WebDAVServerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
// create the file
lockNodeInfo = createNode(dirInfo.getNodeRef(), splitPath[1], ContentModel.TYPE_CONTENT);
// ALF-10309 fix, mark created node with webdavNoContent aspect, we assume that save operation
// is performed by client, webdavNoContent aspect normally removed in put method unless there
// is a cancel before the PUT request takes place
int lockTimeout = getLockTimeout();
if (lockTimeout > 0 && !getNodeService().hasAspect(lockNodeInfo.getNodeRef(), ContentModel.ASPECT_WEBDAV_NO_CONTENT)) {
final NodeRef nodeRef = lockNodeInfo.getNodeRef();
getNodeService().addAspect(nodeRef, ContentModel.ASPECT_WEBDAV_NO_CONTENT, null);
// Remove node after the timeout (MS Office 2003 requests 3 minutes) if the PUT or UNLOCK has not taken place
timer.schedule(new TimerTask() {
@Override
public void run() {
// run as current user
AuthenticationUtil.runAs(new RunAsWork<Void>() {
@Override
public Void doWork() throws Exception {
try {
if (getNodeService().hasAspect(nodeRef, ContentModel.ASPECT_WEBDAV_NO_CONTENT)) {
getTransactionService().getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<String>() {
public String execute() throws Throwable {
getNodeService().deleteNode(nodeRef);
if (logger.isDebugEnabled()) {
logger.debug("Timer DELETE " + path);
}
return null;
}
}, false, true);
} else if (logger.isDebugEnabled()) {
logger.debug("Timer IGNORE " + path);
}
} catch (InvalidNodeRefException e) {
// Might get this if the node is deleted. If so just ignore.
if (logger.isDebugEnabled()) {
logger.debug("Timer DOES NOT EXIST " + path);
}
}
return null;
}
}, userName);
}
}, lockTimeout * 1000);
if (logger.isDebugEnabled()) {
logger.debug("Timer START in " + lockTimeout + " seconds " + path);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Created new node for lock: \n" + " path: " + path + "\n" + " node: " + lockNodeInfo);
}
m_response.setStatus(HttpServletResponse.SC_CREATED);
}
// Check if this is a new lock or a lock refresh
if (hasLockToken()) {
lockInfo = checkNode(lockNodeInfo);
if (!lockInfo.isLocked() && m_request.getContentLength() == -1) {
// see http://www.ics.uci.edu/~ejw/authoring/protocol/rfc2518.html#rfc.section.7.8
throw new WebDAVServerException(HttpServletResponse.SC_BAD_REQUEST);
}
// If a request body is not defined and "If" header is sent we have createExclusive as false,
// but we need to check a previous LOCK was an exclusive. I.e. get the property for node. It
// is already has got in a checkNode method, so we need just get a scope from lockInfo.
// This particular case was raised as ALF-4008.
this.createExclusive = WebDAV.XML_EXCLUSIVE.equals(this.lockInfo.getScope());
// Refresh an existing lock
refreshLock(lockNodeInfo, userName);
} else {
lockInfo = checkNode(lockNodeInfo, true, createExclusive);
// Create a new lock
createLock(lockNodeInfo, userName);
}
m_response.setHeader(WebDAV.HEADER_LOCK_TOKEN, "<" + WebDAV.makeLockToken(lockNodeInfo.getNodeRef(), userName) + ">");
m_response.setHeader(WebDAV.HEADER_CONTENT_TYPE, WebDAV.XML_CONTENT_TYPE);
// We either created a new lock or refreshed an existing lock, send back the lock details
generateResponse(lockNodeInfo, userName);
}
use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.
the class MkcolMethod method executeImpl.
/**
* Execute the request
*
* @exception WebDAVServerException
*/
protected void executeImpl() throws WebDAVServerException, Exception {
FileFolderService fileFolderService = getFileFolderService();
// see if it exists
try {
getDAVHelper().getNodeForPath(getRootNodeRef(), getPath());
// already exists
throw new WebDAVServerException(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
} catch (FileNotFoundException e) {
// it doesn't exist
}
// Trim the last path component and check if the parent path exists
String parentPath = getPath();
int lastPos = parentPath.lastIndexOf(WebDAVHelper.PathSeperator);
NodeRef parentNodeRef = null;
if (lastPos == 0) {
// Create new folder at root
parentPath = WebDAVHelper.PathSeperator;
parentNodeRef = getRootNodeRef();
} else if (lastPos != -1) {
// Trim the last path component
parentPath = parentPath.substring(0, lastPos + 1);
try {
FileInfo parentFileInfo = getDAVHelper().getNodeForPath(getRootNodeRef(), parentPath);
parentNodeRef = parentFileInfo.getNodeRef();
} catch (FileNotFoundException e) {
// parent path is missing
throw new WebDAVServerException(HttpServletResponse.SC_CONFLICT);
}
} else {
// Looks like a bad path
throw new WebDAVServerException(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}
// Get the new folder name
String folderName = getPath().substring(lastPos + 1);
// Create the new folder node
FileInfo fileInfo = fileFolderService.create(parentNodeRef, folderName, ContentModel.TYPE_FOLDER);
// Don't post activity data for hidden folder
if (!fileInfo.isHidden()) {
postActivity(fileInfo);
}
// Return a success status
m_response.setStatus(HttpServletResponse.SC_CREATED);
}
Aggregations