Search in sources :

Example 1 with ConflictException

use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.

the class WebDataDirResource method delete.

@Override
public void delete() throws NotAuthorizedException, ConflictException, BadRequestException {
    // Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "delete() for {0}", getPath());
    if (getPath().isRoot()) {
        throw new ConflictException(this, "Cannot delete root");
    }
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            getCatalogue().remove(getLogicalData(), getPrincipal(), connection);
            connection.commit();
            connection.close();
        } catch (SQLException e) {
            Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e);
            if (connection != null && !connection.isClosed()) {
                connection.rollback();
                connection.close();
            }
            throw new BadRequestException(this, e.getMessage());
        }
    } catch (SQLException e1) {
        Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e1);
        throw new BadRequestException(this, e1.getMessage());
    }
}
Also used : ConflictException(io.milton.http.exceptions.ConflictException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) BadRequestException(io.milton.http.exceptions.BadRequestException)

Example 2 with ConflictException

use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.

the class WebDataDirResource method createCollection.

@Override
public CollectionResource createCollection(String newName) throws NotAuthorizedException, ConflictException, BadRequestException {
    Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "createCollection {0} in {1}", new Object[] { newName, getPath() });
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            Path newCollectionPath = Path.path(getPath(), newName);
            Long newFolderEntryId = getCatalogue().getLogicalDataUidByParentRefAndName(getLogicalData().getUid(), newName, connection);
            if (newFolderEntryId != null) {
                throw new ConflictException(this, newName);
            } else {
                // collection does not exists, create a new one
                // newCollectionPath, Constants.LOGICAL_FOLDER,
                LogicalData newFolderEntry = new LogicalData();
                newFolderEntry.setType(Constants.LOGICAL_FOLDER);
                newFolderEntry.setParentRef(getLogicalData().getUid());
                newFolderEntry.setName(newName);
                newFolderEntry.setCreateDate(System.currentTimeMillis());
                newFolderEntry.setModifiedDate(System.currentTimeMillis());
                newFolderEntry.setLastAccessDate(System.currentTimeMillis());
                newFolderEntry.setTtlSec(getLogicalData().getTtlSec());
                newFolderEntry.setOwner(getPrincipal().getUserId());
                getCatalogue().setPermissions(getCatalogue().registerDirLogicalData(newFolderEntry, connection).getUid(), new Permissions(getPrincipal(), getPermissions()), connection);
                newFolderEntry = inheritProperties(newFolderEntry, connection);
                connection.commit();
                connection.close();
                WebDataDirResource res = new WebDataDirResource(newFolderEntry, newCollectionPath, getCatalogue(), authList);
                return res;
            }
        } catch (SQLException e) {
            Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e);
            if (connection != null && !connection.isClosed()) {
                connection.rollback();
                connection.close();
            }
            throw new BadRequestException(this, e.getMessage());
        }
    } catch (SQLException e1) {
        Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e1);
        throw new BadRequestException(this, e1.getMessage());
    }
}
Also used : Path(io.milton.common.Path) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) ConflictException(io.milton.http.exceptions.ConflictException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Permissions(nl.uva.cs.lobcder.auth.Permissions) BadRequestException(io.milton.http.exceptions.BadRequestException)

Example 3 with ConflictException

use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.

the class WebDataFileResource method moveTo.

@Override
public void moveTo(CollectionResource collectionResource, String name) throws ConflictException, NotAuthorizedException, BadRequestException {
    WebDataDirResource toWDDR = (WebDataDirResource) collectionResource;
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "moveTo(''{0}'', ''{1}'') for {2}", new Object[] { toWDDR.getPath(), name, getPath() });
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            Permissions destPerm = getCatalogue().getPermissions(toWDDR.getLogicalData().getUid(), toWDDR.getLogicalData().getOwner(), connection);
            LogicalData parentLD = getCatalogue().getLogicalDataByUid(getLogicalData().getParentRef());
            Permissions parentPerm = getCatalogue().getPermissions(parentLD.getUid(), parentLD.getOwner());
            if (!(getPrincipal().canWrite(destPerm) && getPrincipal().canWrite(parentPerm))) {
                throw new NotAuthorizedException(this);
            }
            getCatalogue().moveEntry(getLogicalData(), toWDDR.getLogicalData(), name, connection);
            connection.commit();
        } catch (Exception e) {
            Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
            connection.rollback();
            throw new BadRequestException(this, e.getMessage());
        }
    } catch (SQLException e) {
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
        throw new BadRequestException(this, e.getMessage());
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Permissions(nl.uva.cs.lobcder.auth.Permissions) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotFoundException(io.milton.http.exceptions.NotFoundException)

Example 4 with ConflictException

use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.

the class FckQuickUploaderResource method processFileUpload.

private void processFileUpload(FileItem f, Map<String, String> params) throws BadRequestException, NotAuthorizedException {
    CollectionResource target = null;
    if (wrappedResource == null) {
        throw new BadRequestException(this, "collection not found");
    }
    target = (CollectionResource) wrappedResource.child("uploads");
    if (target == null) {
        try {
            if (wrappedResource instanceof MakeCollectionableResource) {
                MakeCollectionableResource mk = (MakeCollectionableResource) wrappedResource;
                target = mk.createCollection("uploads");
            } else {
                throw new BadRequestException(target, "Cant create subfolder");
            }
        } catch (ConflictException ex) {
            throw new RuntimeException(ex);
        } catch (NotAuthorizedException ex) {
            throw new RuntimeException(ex);
        } catch (BadRequestException ex) {
            throw new RuntimeException(ex);
        }
    }
    String name = FileUtils.sanitiseName(f.getName());
    log.debug("processFileUpload: " + name);
    boolean isFirst = true;
    String newName = null;
    while (target.child(name) != null) {
        name = FileUtils.incrementFileName(name, isFirst);
        newName = name;
        isFirst = false;
    }
    long size = f.getSize();
    try {
        if (target instanceof PutableResource) {
            PutableResource putable = (PutableResource) target;
            Resource newRes = putable.createNew(name, f.getInputStream(), size, null);
            if (newRes != null) {
                log.trace("created: " + newRes.getName() + " of type: " + newRes.getClass());
            } else {
                log.trace("createNew returned null");
            }
        } else {
            throw new BadRequestException(target, "Does not implement PutableResource");
        }
    } catch (ConflictException ex) {
        throw new RuntimeException(ex);
    } catch (NotAuthorizedException ex) {
        throw new RuntimeException(ex);
    } catch (BadRequestException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    try {
        if (newName != null) {
            // we renamed the file
            uploadResponseOk(name);
        } else {
            uploadResponseOk();
        }
    } catch (Throwable ex) {
        log.error("Exception saving new file", ex);
        uploadResponseFailed(ex.getMessage());
    }
}
Also used : CollectionResource(io.milton.resource.CollectionResource) ConflictException(io.milton.http.exceptions.ConflictException) PutableResource(io.milton.resource.PutableResource) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource) Resource(io.milton.resource.Resource) CollectionResource(io.milton.resource.CollectionResource) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) IOException(java.io.IOException) BadRequestException(io.milton.http.exceptions.BadRequestException) MakeCollectionableResource(io.milton.resource.MakeCollectionableResource) PutableResource(io.milton.resource.PutableResource)

Example 5 with ConflictException

use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.

the class PutJsonResource method getName.

/**
 * We dont return anything, so best not use json
 *
 * @param accepts
 * @return
 */
// @Override
// public String getContentType(String accepts) {
// return "text/html";
// }
private String getName(String filename, Map<String, String> parameters) throws ConflictException, NotAuthorizedException, BadRequestException {
    String initialName = filename;
    if (parameters.containsKey(PARAM_NAME)) {
        initialName = parameters.get(PARAM_NAME);
    }
    boolean nonBlankName = initialName != null && initialName.trim().length() > 0;
    boolean autoname = (parameters.get(PARAM_AUTONAME) != null);
    boolean overwrite = (parameters.get(PARAM_OVERWRITE) != null);
    if (nonBlankName) {
        Resource child = wrapped.child(initialName);
        if (child == null) {
            log.trace("no existing file with that name");
            return initialName;
        } else {
            if (overwrite) {
                log.trace("file exists, and overwrite parameters is set, so allow overwrite: " + initialName);
                return initialName;
            } else {
                if (!autoname) {
                    log.warn("Conflict: Can't create resource with name " + initialName + " because it already exists. To rename automatically use request parameter: " + PARAM_AUTONAME + ", or to overwrite use " + PARAM_OVERWRITE);
                    throw new ConflictException(this);
                } else {
                    log.trace("file exists and autoname is set, so will find acceptable name");
                }
            }
        }
    } else {
        initialName = getDateAsName("upload");
        log.trace("no name given in request");
    }
    return findAcceptableName(initialName);
}
Also used : ConflictException(io.milton.http.exceptions.ConflictException) DeletableResource(io.milton.resource.DeletableResource) PutableResource(io.milton.resource.PutableResource) ReplaceableResource(io.milton.resource.ReplaceableResource) Resource(io.milton.resource.Resource) PostableResource(io.milton.resource.PostableResource)

Aggregations

ConflictException (io.milton.http.exceptions.ConflictException)11 BadRequestException (io.milton.http.exceptions.BadRequestException)9 Resource (io.milton.resource.Resource)6 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)5 IOException (java.io.IOException)5 CollectionResource (io.milton.resource.CollectionResource)4 PostableResource (io.milton.resource.PostableResource)4 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 Path (io.milton.common.Path)3 DeletableResource (io.milton.resource.DeletableResource)3 PutableResource (io.milton.resource.PutableResource)3 ReplaceableResource (io.milton.resource.ReplaceableResource)3 NotFoundException (io.milton.http.exceptions.NotFoundException)2 MoveableResource (io.milton.resource.MoveableResource)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URISyntaxException (java.net.URISyntaxException)2 UnknownHostException (java.net.UnknownHostException)2 Permissions (nl.uva.cs.lobcder.auth.Permissions)2 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)2