public interface HierarchyItem
Defines methods common to all WebDAV folders and files.
getCreated()
and getModified()
methods must return Universal Coordinated Time (UTC).
getProperties(com.ithit.webdav.server.Property[])
and updateProperties(com.ithit.webdav.server.Property[], com.ithit.webdav.server.Property[])
are called when WebDAV client is reading, adding,
updating or deleting custom properties.
This interface also provides methods for managing hierarchy: moving, copying and deleting WebDAV items.
See copyTo(com.ithit.webdav.server.Folder, java.lang.String, boolean)
, moveTo(com.ithit.webdav.server.Folder, java.lang.String)
and delete()
methods.
Your file items must implement File
interface, folder items - Folder
interface.
Modifier and Type | Method and Description |
---|---|
void |
copyTo(Folder folder,
String destName,
boolean deep)
Creates a copy of this item with a new name in the destination folder.
|
void |
delete()
Deletes this item.
|
long |
getCreated()
Gets the creation date of the item in repository expressed as the coordinated universal time (UTC).
|
long |
getModified()
Gets the last modification date of the item in repository expressed as the coordinated universal time (UTC).
|
String |
getName()
Gets the name of the item in repository.
|
String |
getPath()
Unique item path in the repository relative to storage root.
|
List<Property> |
getProperties(Property[] props)
Gets values of all properties or selected properties for this item.
|
List<Property> |
getPropertyNames()
Gets names of all properties for this item.
|
void |
moveTo(Folder folder,
String destName)
Moves this item to the destination folder under a new name.
|
void |
updateProperties(Property[] setProps,
Property[] delProps)
Adds, modifies and removes properties for this item.
|
String getName() throws ServerException
ServerException
- In case of an error.long getCreated() throws ServerException
ServerException
- In case of an error.long getModified() throws ServerException
ServerException
- In case of an error.String getPath() throws ServerException
The URL returned by this method is relative to storage root. If your server root is located at http://webdavserver.com/myserver/ and the item URL is http://webdavserver.com/myserver/myfolder/myitem.doc this property implementation must return myfolder/myitem.doc. To calculate the entire item URL the engine will call DavRequest.getContextPath()
property and attach it to url returned by this property.
Examples:
ServerException
- In case of an error.List<Property> getProperties(Property[] props) throws ServerException
props
- null
to get all properties.
ServerException
- In case of an error.
@Override public List<Property> getProperties(Property[] props) throws ServerException { List<Property> l = getPropertyNames(); List<Property> result; if (props == null) { return l; } Set<String> propNames = Arrays.stream(props).map(Property::getName).collect(Collectors.toSet()); result = l.stream().filter(x -> propNames.contains(x.getName())).collect(Collectors.toList()); Property snippet = Arrays.stream(props).filter(x -> propNames.contains(SNIPPET)).findFirst().orElse(null); if (snippet != null && this instanceof FileImpl) { result.add(Property.create(snippet.getNamespace(), snippet.getName(), ((FileImpl) this).getSnippet())); } return result; }
List<Property> getPropertyNames() throws ServerException
Most WebDAV clients never request list of property names, so your implementation can just throw ServerException
with WebDavStatus.NOT_ALLOWED
status.
ServerException
- In case of an error.
@Override public List<Property> getPropertyNames() throws ServerException { if (ExtendedAttributesExtension.hasExtendedAttribute(getFullPath().toString(), PROPERTIES_ATTRIBUTE)) { String propJson = ExtendedAttributesExtension.getExtendedAttribute(getFullPath().toString(), PROPERTIES_ATTRIBUTE); return SerializationUtils.deserializeList(Property.class, propJson); } return new LinkedList<>(); }
void updateProperties(Property[] setProps, Property[] delProps) throws LockedException, MultistatusException, ServerException
setProps
- Array of properties to be set.delProps
- Array of properties to be removed. Property.value
field is ignored.
Specifying the removal of a property that does not exist is not an error.LockedException
- this item was locked and client did not provide lock token.MultistatusException
- If update fails for a property, this exception shall be thrown and contain
result of the operation for each property.
Status for each property can be one of following:
WebDavStatus.OK
- the property was successfully updated or deleted.
WebDavStatus.CONFLICT
- the client has provided a value whose semantics are not appropriate for the property,
this includes trying to set read-only properties.
WebDavStatus.FAILED_DEPENDENCY
- indicates this action would have succeeded if it were not for the conflict
with updating/removing some other property.
ServerException
- In case of other error.
@Override public void updateProperties(Property[] setProps, Property[] delProps) throws LockedException, MultistatusException, ServerException { ensureHasToken(); for (final Property prop : setProps) { String basicAttributeNS = "urn:schemas-microsoft-com:"; // Microsoft Mini-redirector may update file creation date, modification date and access time passing properties: // <Win32CreationTime xmlns="urn:schemas-microsoft-com:">Thu, 28 Mar 2013 20:15:34 GMT</Win32CreationTime> // <Win32LastModifiedTime xmlns="urn:schemas-microsoft-com:">Thu, 28 Mar 2013 20:36:24 GMT</Win32LastModifiedTime> // <Win32LastAccessTime xmlns="urn:schemas-microsoft-com:">Thu, 28 Mar 2013 20:36:24 GMT</Win32LastAccessTime> // In this case update creation and modified date in your storage or do not save this properties at all, otherwise // Windows Explorer will display creation and modification date from this props and it will differ from the values // in the Created and Modified fields in your storage // String basicAttributeNS = "urn:schemas-microsoft-com:"; if (prop.getNamespace().equals(basicAttributeNS)) { updateBasicProperties(prop.getXmlValueRaw(), prop.getName()); } else { properties = getProperties(); Property existingProp = properties.stream().filter(x -> x.getName().equals(prop.getName())).findFirst().orElse(null); if (existingProp != null) { existingProp.setXmlValueRaw(prop.getXmlValueRaw()); } else { properties.add(prop); } } } properties = getProperties(); Set<String> propNamesToDel = Arrays.stream(delProps).map(Property::getName).collect(Collectors.toSet()); properties = properties.stream() .filter(e -> !propNamesToDel.contains(e.getName())) .collect(Collectors.toList()); ExtendedAttributesExtension.setExtendedAttribute(getFullPath().toString(), PROPERTIES_ATTRIBUTE, SerializationUtils.serialize(properties)); getEngine().getWebSocketServer().notifyUpdated(getPath(), getWebSocketID()); }
void copyTo(Folder folder, String destName, boolean deep) throws LockedException, ConflictException, MultistatusException, ServerException
If error occurred while copying items located in a subtree, the server
should try to continue copy operation and copy all other items. In this case
you must throw MultistatusException
that contain separate response for
every item that was successfully copied or failed to copy.
A CopyTo method invocation must not copy any locks active on the source item. However, if this method copies the item into a folder that has a deep lock, then the destination item must be added to the lock.
folder
- Destination folder.destName
- Name of the destination item.deep
- Indicates whether to copy entire subtree.LockedException
- - the destination item was locked and client did not provide lock token.ConflictException
- - destination folder does not exist.MultistatusException
- - errors has occured during processing of the subtree.
Every item that has been eithre successfully copied or failed to copy must be present in exception with corresponding status.ServerException
- - In case of other error.
@Override public void copyTo(Folder folder, String destName, boolean deep) throws LockedException, MultistatusException, ServerException { ((FolderImpl) folder).ensureHasToken(); String relUrl = HierarchyItemImpl.decodeAndConvertToPath(folder.getPath()); String destinationFolder = Paths.get(getRootFolder(), relUrl).toString(); if (isRecursive(relUrl)) { throw new ServerException("Cannot copy to subfolder", WebDavStatus.FORBIDDEN); } if (!Files.exists(Paths.get(destinationFolder))) throw new ServerException(); try { Path sourcePath = this.getFullPath(); Path destinationFullPath = Paths.get(destinationFolder, destName); FileUtils.copyDirectory(sourcePath.toFile(), destinationFullPath.toFile()); addIndex(destinationFullPath, folder.getPath() + destName, destName); } catch (IOException e) { throw new ServerException(e); } setName(destName); getEngine().getWebSocketServer().notifyCreated(folder.getPath() + encode(destName), getWebSocketID()); }
void moveTo(Folder folder, String destName) throws LockedException, ConflictException, MultistatusException, ServerException
folder
- Destination folder.destName
- Name of the destination item.LockedException
- - the source or the destination item was locked and client did not provide lock token.ConflictException
- - destination folder does not exist.MultistatusException
- - errors has occured during processing of the subtree. Every processed item must have corresponding response added
with corresponding status.ServerException
- - in case of another error.
@Override public void moveTo(Folder folder, String destName) throws LockedException, ConflictException, MultistatusException, ServerException { ensureHasToken(); ((FolderImpl) folder).ensureHasToken(); String destinationFolder = Paths.get(getRootFolder(), HierarchyItemImpl.decodeAndConvertToPath(folder.getPath())).toString(); if (!Files.exists(Paths.get(destinationFolder))) throw new ConflictException(); Path sourcePath = this.getFullPath(); Path destinationFullPath = Paths.get(destinationFolder, destName); try { removeIndex(getFullPath(), this); Files.move(sourcePath, destinationFullPath, StandardCopyOption.REPLACE_EXISTING); addIndex(destinationFullPath, folder.getPath() + destName, destName); } catch (IOException e) { throw new ServerException(e); } setName(destName); getEngine().getWebSocketServer().notifyMoved(getPath(), folder.getPath() + encode(destName), getWebSocketID()); }
void delete() throws LockedException, MultistatusException, ServerException
LockedException
- - this item or its parent was locked and client did not provide lock token.MultistatusException
- - errors has occured during processing of the subtree. Every processed item must have corresponding response added
to the exception with corresponding status.ServerException
- - in case of another error.
@Override public void delete() throws LockedException, MultistatusException, ServerException { ensureHasToken(); try { removeIndex(getFullPath(), this); FileUtils.deleteDirectory(getFullPath().toFile()); } catch (IOException e) { throw new ServerException(e); } getEngine().getWebSocketServer().notifyDeleted(getPath(), getWebSocketID()); }
Copyright © ITHit. All Rights Reserved.