📄 fileobjectdavresource.java
字号:
/**
* <p>
* Return the MIME Content-Type of this {@link DAVResource}.
* </p>
*
* <p>
* If the {@link #isCollection()} method returns <b>true</b> this method
* always returns <code>text/html</code>.
* </p>
*
* @return a {@link String} instance or <b>null</b> if this resource does
* not exist.
*/
public String getContentType() {
if (this.isNull())
return null;
if (this.isCollection())
return GET.COLLECTION_MIME_TYPE;
String mime = DAVUtilities.getMimeType(this.getDisplayName());
return mime == null ? "application/octet-stream" : mime;
}
/**
* <p>
* Return the MIME Content-Length of this {@link DAVResource}.
* </p>
*
* @return a {@link Long} instance or <b>null</b> if this resource does not
* exist or is a collection.
*/
public Long getContentLength() {
if (this.isNull() || this.isCollection())
return null;
try {
return new Long(getFile().getContent().getSize());
} catch (IOException e) {
log.error("Failed to get content length.", e);
return null;
}
}
/**
* <p>
* Return the last modification date of this {@link DAVResource}.
* </p>
*
* @return a {@link String} instance or <b>null</b> if this resource does
* not exist.
*/
public Date getLastModified() {
if (this.isNull())
return null;
try {
return new Date(getFile().getContent().getLastModifiedTime());
} catch (IOException e) {
log.error("Failed to get last modified date of resource.", e);
return null;
}
}
/**
* <p>
* Return a {@link String} representing the Entity Tag of this
* {@link DAVResource} as described by the <a
* href="http://www.rfc-editor.org/rfc/rfc2616.txt">HTTP RFC</a>.
* </p>
*
* @return a {@link String} instance or <b>null</b> if this resource does
* not exist.
*/
public String getEntityTag() {
if (this.isNull())
return null;
String path = this.getRelativePath();
return DAVUtilities.getETAG(path, this.getLastModified());
}
/**
* <p>
* Delete this resource.
* </p>
*
* @throws IOException
*
* @throws DAVException If for any reason this resource cannot be deleted.
*/
public void delete() throws DAVMultiStatus, IOException {
if (this.isNull())
throw new DAVException(404, "Not found", this);
if (((AbstractNetworkPlaceMount) getMount()).isReadOnly()) {
throw new DAVException(DAVStatus.SC_FORBIDDEN, "You cannot delete this file because the the mount is readonly!");
}
if (((AbstractNetworkPlaceMount) getMount()).getNetworkPlace().isNoDelete()) {
throw new DAVException(500, "This resource cannot be deleted because the system policy does not allow deletion.");
}
if (this.isResource()) {
try {
if (!getFile().delete()) {
throw new DAVException(403, "Can't delete resource '" + getRelativePath() + "'", this);
} else {
this.getMount().getStore().getRepository().notify(this, DAVListener.RESOURCE_REMOVED);
}
} catch (IOException e) {
log.error("Failed to delete resource.", e);
throw new DAVException(403, "Can't delete resource. " + e.getMessage(), this);
}
} else if (this.isMount()) {
throw new DAVException(403, "Can't delete resource '" + getRelativePath() + "' as it is the root for the mount point "
+ this.getMount().getMountString(), this);
} else if (this.isCollection()) {
DAVMultiStatus multistatus = new DAVMultiStatus();
Iterator children = this.getChildren();
while (children.hasNext())
try {
((DAVResource) children.next()).delete();
} catch (DAVException exception) {
multistatus.merge(exception);
}
if (multistatus.size() > 0)
throw multistatus;
try {
if (!getFile().delete()) {
throw new DAVException(403, "Can't delete collection", this);
} else {
this.getMount().getStore().getRepository().notify(this, DAVListener.COLLECTION_REMOVED);
}
} catch (IOException e) {
log.error("Failed to delete resource.", e);
throw new DAVException(403, "Can't delete collection " + getRelativePath() + ". " + e.getMessage(), this);
}
}
}
/**
* <p>
* Copy this resource to the specified destination.
* </p>
*
* @throws IOException
*
* @throws DAVException If for any reason this resource cannot be deleted.
*/
public void copy(DAVResource dest, boolean overwrite, boolean recursive) throws DAVMultiStatus, IOException {
/*
* NOTE: Since the COPY operation relies on other operation defined in
* this class (and in DAVOutputStream for resources) rather than on
* files temselves, notifications are sent elsewhere, not here.
*/
if (this.isNull())
throw new DAVException(404, "Not found", this);
/* Check if the destination exists and delete if possible */
if (!dest.isNull()) {
if (!overwrite) {
String msg = "Not overwriting existing destination";
throw new DAVException(412, msg, dest);
}
dest.delete();
}
/* Copy a single resource (destination is null as we deleted it) */
if (this.isResource()) {
DAVInputStream in = this.read();
DAVOutputStream out = dest.write();
byte buffer[] = new byte[4096];
int k = -1;
while ((k = in.read(buffer)) != -1)
out.write(buffer, 0, k);
out.close();
}
/* Copy the collection and all nested members */
if (this.isCollection()) {
dest.makeCollection();
if (!recursive)
return;
DAVMultiStatus multistatus = new DAVMultiStatus();
Iterator children = this.getChildren();
while (children.hasNext())
try {
FileObjectDAVResource childResource = (FileObjectDAVResource) children.next();
try {
FileObject child = ((FileObjectDAVResource) dest).getFile().resolveFile(
childResource.getFile().getName().getBaseName());
FileObjectDAVResource target = new FileObjectDAVResource(this.getMount(), child, transaction, this, this
.getMount().getMountString());
childResource.copy(target, overwrite, recursive);
} catch (IOException e) {
throw new DAVException(403, "Could not resolve child.", e);
}
} catch (DAVException exception) {
multistatus.merge(exception);
}
if (multistatus.size() > 0)
throw multistatus;
}
}
/**
* <p>
* Move this resource to the specified destination.
* </p>
*
* @throws IOException
*
* @throws DAVException If for any reason this resource cannot be moved.
*/
public void move(DAVResource dest, boolean overwrite) throws DAVMultiStatus, IOException {
/*
* NOTE: Since the COPY operation relies on other operation defined in
* this class (and in DAVOutputStream for resources) rather than on
* files temselves, notifications are sent elsewhere, not here.
*/
if (this.isNull())
throw new DAVException(404, "Not found", this);
/* Check read only */
if (((AbstractNetworkPlaceMount) getMount()).isReadOnly()) {
throw new DAVException(DAVStatus.SC_FORBIDDEN, "You cannot move this file because the the mount is readonly!");
}
/* Check if the destination exists and delete if possible */
if (!dest.isNull()) {
if (!overwrite) {
String msg = "Not overwriting existing destination";
throw new DAVException(412, msg, dest);
}
dest.delete();
}
/* If the file system supports then move then just do it */
if (getFile().canRenameTo(dest.getFile())) {
getFile().moveTo(dest.getFile());
return;
}
/* Otherwise copy */
copy(dest, overwrite, true);
}
/**
* <p>
* Create a collection identified by this {@link DAVResource}.
* </p>
*
* <p>
* This resource must be {@link #isNull() non-null} and its
* {@link #getParent() parent} must be accessible and be a
* {@link #isCollection() collection}.
* </p>
*
* @throws DAVException If for any reason a collection identified by this
* resource cannot be created.
*/
public void makeCollection() {
DAVResource parent = this.getParent();
if (!this.isNull())
throw new DAVException(405, "Resource exists", this);
if (parent.isNull())
throw new DAVException(409, "Parent does not not exist", this);
if (!parent.isCollection())
throw new DAVException(403, "Parent not a collection", this);
/* Check read only */
if (((AbstractNetworkPlaceMount) getMount()).isReadOnly()) {
throw new DAVException(DAVStatus.SC_FORBIDDEN, "You cannot create a folder here because the the mount is readonly!");
}
try {
getFile().createFolder();
this.getMount().getStore().getRepository().notify(this, DAVListener.COLLECTION_CREATED);
} catch (IOException e) {
throw new DAVException(507, "Can't create collection", this);
}
}
/**
* <p>
* Return an {@link InputStream} reading the resource.
* </p>
*
* @return a <b>non-null</b> {@link InputStream} instance.
* @throws DAVException
*/
public DAVInputStream read() {
if (this.isNull())
throw new DAVException(404, "Not found", this);
if (this.isCollection())
throw new DAVException(403, "Resource is collection", this);
return new DAVInputStream(this);
}
/**
* <p>
* Return a {@link DAVOutputStream} writing to this {@link DAVResource}
* instance.
* </p>
*
* @return a <b>non-null</b> {@link DAVOutputStream} instance.
* @throws DAVException
*/
public DAVOutputStream write() {
if (this.isCollection())
throw new DAVException(409, "Can't write a collection", this);
if (((AbstractNetworkPlaceMount) getMount()).isReadOnly()) {
throw new DAVException(DAVStatus.SC_FORBIDDEN, "You cannot create a write here because the the mount is readonly!");
}
return new DAVOutputStream(this);
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.vfs.webdav.DAVResource#start(com.sslexplorer.vfs.webdav.DAVTransaction)
*/
public void start(DAVTransaction transaction) {
this.transaction = transaction;
}
/**
*
*/
public void stop() {
transaction = null;
}
/*
* (non-Javadoc)
*
* @see com.sslexplorer.vfs.webdav.DAVResource#getFullURI()
*/
public URI getFullURI() {
DAVMount mount = getMount();
URI uri = URI
.create(mount == null ? ("/" + getRelativeURI()) : ("/"
+ DAVUtilities.stripTrailingSlash(DAVUtilities.encodePath(mount.getMountString(), true))
+ "/" + DAVUtilities.stripLeadingSlash(DAVUtilities.encodePath(getRelativePath(), true))));
return uri;
}
public DAVTransaction getTransaction() {
return transaction;
}
public String getFullPath() {
DAVMount mount = getMount();
return mount == null ? ("/" + getRelativeURI()) : ("/" + mount.getMountString() + "/" + getRelativePath());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -