Search in sources :

Example 6 with QuotaExceededException

use of org.olat.core.util.vfs.QuotaExceededException in project OpenOLAT by OpenOLAT.

the class WebdavStatus method doPut.

/**
 * Process a PUT request for the specified resource.
 *
 * @param req The servlet request we are processing
 * @param resp The servlet response we are creating
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet-specified error occurs
 */
public void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (isLocked(req)) {
        resp.sendError(WebdavStatus.SC_LOCKED);
        return;
    }
    final String path = getRelativePath(req);
    final WebResourceRoot resources = getResources(req);
    if (!resources.canWrite(path)) {
        resp.sendError(WebdavStatus.SC_FORBIDDEN);
        return;
    }
    final WebResource resource = resources.getResource(path);
    Range range = parseContentRange(req, resp);
    InputStream resourceInputStream = null;
    try {
        // Assume just one range is specified for now
        if (range != null) {
            File contentFile = executePartialPut(req, range, path);
            resourceInputStream = new FileInputStream(contentFile);
        } else {
            resourceInputStream = req.getInputStream();
        }
        if (resources.write(path, resourceInputStream, true, null)) {
            if (resource.exists()) {
                resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
            } else {
                resp.setStatus(HttpServletResponse.SC_CREATED);
                PrintWriter writer = resp.getWriter();
                writer.append("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n").append("<html><head>\n").append("<title>201 Created</title>\n").append("</head><body>\n").append("<h1>Created</h1>\n").append("<p>Resource ").append(path).append(" created.</p>\n").append("</body></html>\n");
                resp.setContentType("text/html; charset=ISO-8859-1");
                String location = Settings.getServerContextPathURI() + path;
                resp.setHeader("Location", location);
            }
        } else {
            resp.sendError(HttpServletResponse.SC_CONFLICT);
        }
    } catch (QuotaExceededException e) {
        resp.sendError(WebdavStatus.SC_INSUFFICIENT_STORAGE);
    } finally {
        if (resourceInputStream != null) {
            try {
                resourceInputStream.close();
            } catch (IOException ioe) {
            // Ignore
            }
        }
    }
    // Removing any lock-null resource which would be present
    lockManager.removeLockNullResource(resource);
}
Also used : QuotaExceededException(org.olat.core.util.vfs.QuotaExceededException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter)

Example 7 with QuotaExceededException

use of org.olat.core.util.vfs.QuotaExceededException in project OpenOLAT by OpenOLAT.

the class VFSResourceRoot method copyVFS.

private void copyVFS(VFSLeaf file, InputStream is) throws IOException {
    // Try to get Quota
    long quotaLeft = -1;
    boolean withQuotaCheck = false;
    VFSContainer parentContainer = file.getParentContainer();
    if (parentContainer != null) {
        quotaLeft = VFSManager.getQuotaLeftKB(parentContainer);
        if (quotaLeft != Quota.UNLIMITED) {
            // convert from kB
            quotaLeft = quotaLeft * 1024;
            withQuotaCheck = true;
        } else {
            withQuotaCheck = false;
        }
    }
    // Open os
    OutputStream os = null;
    byte[] buffer = new byte[BUFFER_SIZE];
    int len = -1;
    boolean quotaExceeded = false;
    try {
        os = file.getOutputStream(false);
        while (true) {
            len = is.read(buffer);
            if (len == -1)
                break;
            if (withQuotaCheck) {
                // re-calculate quota and check
                quotaLeft = quotaLeft - len;
                if (quotaLeft < 0) {
                    log.info("Quota exceeded: " + file);
                    quotaExceeded = true;
                    break;
                }
            }
            os.write(buffer, 0, len);
        }
        if (quotaExceeded) {
            IOUtils.closeQuietly(os);
            file.delete();
            throw new QuotaExceededException("");
        }
    } catch (IOException e) {
        // close first, in order to be able to delete any reamins of the file
        IOUtils.closeQuietly(os);
        file.delete();
        throw e;
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(is);
    }
}
Also used : QuotaExceededException(org.olat.core.util.vfs.QuotaExceededException) VFSContainer(org.olat.core.util.vfs.VFSContainer) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 8 with QuotaExceededException

use of org.olat.core.util.vfs.QuotaExceededException in project openolat by klemens.

the class WebdavStatus method copyResource.

/**
 * Copy a collection.
 *
 * @param errorList Hashtable containing the list of errors which occurred
 * during the copy operation
 * @param source Path of the resource to be copied
 * @param dest Destination path
 */
private boolean copyResource(HttpServletRequest req, Hashtable<String, Integer> errorList, String source, String dest, boolean moved) {
    if (log.isDebug())
        log.debug("Copy: " + source + " To: " + dest);
    WebResourceRoot resources = getResources(req);
    WebResource sourceResource = resources.getResource(source);
    if (sourceResource.isDirectory()) {
        if (!resources.mkdir(dest)) {
            WebResource destResource = resources.getResource(dest);
            if (!destResource.isDirectory()) {
                errorList.put(dest, new Integer(WebdavStatus.SC_CONFLICT));
                return false;
            }
        }
        Collection<VFSItem> entries = resources.list(source);
        for (VFSItem entry : entries) {
            String childDest = dest;
            if (!childDest.equals("/")) {
                childDest += "/";
            }
            childDest += entry.getName();
            String childSrc = source;
            if (!childSrc.equals("/")) {
                childSrc += "/";
            }
            childSrc += entry.getName();
            copyResource(req, errorList, childSrc, childDest, moved);
        }
    } else if (sourceResource.isFile()) {
        WebResource destResource = resources.getResource(dest);
        if (!destResource.exists() && !destResource.getPath().endsWith("/")) {
            int lastSlash = destResource.getPath().lastIndexOf('/');
            if (lastSlash > 0) {
                String parent = destResource.getPath().substring(0, lastSlash);
                WebResource parentResource = resources.getResource(parent);
                if (!parentResource.isDirectory()) {
                    errorList.put(source, new Integer(WebdavStatus.SC_CONFLICT));
                    return false;
                }
            }
        }
        WebResource movedFrom = moved ? sourceResource : null;
        try {
            if (!resources.write(dest, sourceResource.getInputStream(), false, movedFrom)) {
                errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
                return false;
            }
        } catch (QuotaExceededException e) {
            errorList.put(source, new Integer(WebdavStatus.SC_INSUFFICIENT_STORAGE));
            return false;
        }
    } else {
        errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
        return false;
    }
    return true;
}
Also used : QuotaExceededException(org.olat.core.util.vfs.QuotaExceededException) VFSItem(org.olat.core.util.vfs.VFSItem)

Aggregations

QuotaExceededException (org.olat.core.util.vfs.QuotaExceededException)8 IOException (java.io.IOException)6 VFSContainer (org.olat.core.util.vfs.VFSContainer)4 VFSItem (org.olat.core.util.vfs.VFSItem)4 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 PrintWriter (java.io.PrintWriter)2 RandomAccessFile (java.io.RandomAccessFile)2 MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)2 MetaTagged (org.olat.core.commons.modules.bc.meta.tagged.MetaTagged)2 SubscriptionContext (org.olat.core.commons.services.notifications.SubscriptionContext)2 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)2 VFSSecurityCallback (org.olat.core.util.vfs.callbacks.VFSSecurityCallback)2 Versionable (org.olat.core.util.vfs.version.Versionable)2