Search in sources :

Example 6 with ConflictException

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

the class MiltonFtpFile method createOutputStream.

@Override
public OutputStream createOutputStream(long offset) throws IOException {
    log.debug("createOutputStream: " + offset);
    final BufferingOutputStream out = new BufferingOutputStream(50000);
    if (r instanceof ReplaceableResource) {
        log.debug("resource is replaceable");
        final ReplaceableResource rr = (ReplaceableResource) r;
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    rr.replaceContent(out.getInputStream(), out.getSize());
                } catch (BadRequestException ex) {
                    throw new RuntimeException(ex);
                } catch (ConflictException ex) {
                    throw new RuntimeException(ex);
                } catch (NotAuthorizedException ex) {
                    throw new RuntimeException(ex);
                }
            }
        };
        out.setOnClose(runnable);
        return out;
    } else {
        CollectionResource col;
        try {
            col = getParent();
        } catch (NotAuthorizedException ex) {
            throw new RuntimeException(ex);
        } catch (BadRequestException ex) {
            throw new RuntimeException(ex);
        }
        if (col == null) {
            throw new IOException("parent not found");
        } else if (col instanceof PutableResource) {
            final PutableResource putableResource = (PutableResource) col;
            final String newName = path.getName();
            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    try {
                        putableResource.createNew(newName, out.getInputStream(), out.getSize(), null);
                    } catch (BadRequestException ex) {
                        throw new RuntimeException(ex);
                    } catch (NotAuthorizedException ex) {
                        throw new RuntimeException(ex);
                    } catch (ConflictException ex) {
                        throw new RuntimeException(ex);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            };
            out.setOnClose(runnable);
            return out;
        } else {
            throw new IOException("folder doesnt support PUT, and the resource is not replaceable");
        }
    }
}
Also used : ConflictException(io.milton.http.exceptions.ConflictException) BufferingOutputStream(io.milton.common.BufferingOutputStream) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) IOException(java.io.IOException) ReplaceableResource(io.milton.resource.ReplaceableResource)

Example 7 with ConflictException

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

the class WebDataFileResource method delete.

@Override
public void delete() throws NotAuthorizedException, BadRequestException, ConflictException {
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "delete() file {0}", getPath());
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            getCatalogue().remove(getLogicalData(), getPrincipal(), 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 : SQLException(java.sql.SQLException) Connection(java.sql.Connection) BadRequestException(io.milton.http.exceptions.BadRequestException) 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 8 with ConflictException

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

the class MoveHandler method processExistingResource.

@Override
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException {
    MoveableResource r = (MoveableResource) resource;
    Dest dest = Utils.getDecodedDestination(request.getDestinationHeader());
    Resource rDest = manager.getResourceFactory().getResource(dest.host, dest.url);
    log.debug("process: moving from: " + r.getName() + " -> " + dest.url + " with name: " + dest.name);
    if (rDest == null) {
        log.debug("process: destination parent does not exist: " + dest);
        responseHandler.respondConflict(resource, response, request, "Destination parent does not exist: " + dest);
    } else if (!(rDest instanceof CollectionResource)) {
        log.debug("process: destination exists but is not a collection");
        responseHandler.respondConflict(resource, response, request, "Destination exists but is not a collection: " + dest);
    } else {
        boolean wasDeleted = false;
        CollectionResource colDest = (CollectionResource) rDest;
        // check if the dest exists
        Resource rExisting = colDest.child(dest.name);
        if (rExisting != null) {
            // check for overwrite header
            if (!canOverwrite(request)) {
                log.info("destination resource exists, and overwrite header is not set. dest name: " + dest.name + " dest folder: " + colDest.getName());
                responseHandler.respondPreconditionFailed(request, response, rExisting);
                return;
            } else {
                if (deleteExistingBeforeMove) {
                    if (rExisting instanceof DeletableResource) {
                        log.debug("deleting existing resource");
                        DeletableResource drExisting = (DeletableResource) rExisting;
                        if (deleteHelper.isLockedOut(request, drExisting)) {
                            log.debug("destination resource exists but is locked");
                            responseHandler.respondLocked(request, response, drExisting);
                            return;
                        }
                        log.debug("deleting pre-existing destination resource");
                        deleteHelper.delete(drExisting, manager.getEventManager());
                        wasDeleted = true;
                    } else {
                        log.warn("destination exists, and overwrite header is set, but destination is not a DeletableResource");
                        responseHandler.respondConflict(resource, response, request, "A resource exists at the destination, and it cannot be deleted");
                        return;
                    }
                }
            }
        }
        log.debug("process: moving resource to: " + rDest.getName());
        try {
            if (!handlerHelper.checkAuthorisation(manager, colDest, request, request.getMethod(), request.getAuthorization())) {
                responseHandler.respondUnauthorised(colDest, response, request);
                return;
            }
            manager.getEventManager().fireEvent(new MoveEvent(resource, colDest, dest.name));
            r.moveTo(colDest, dest.name);
            // See http://www.ettrema.com:8080/browse/MIL-87
            if (wasDeleted) {
                responseHandler.respondNoContent(resource, response, request);
            } else {
                responseHandler.respondCreated(resource, response, request);
            }
        } catch (ConflictException ex) {
            log.warn("conflict", ex);
            responseHandler.respondConflict(resource, response, request, dest.toString());
        }
    }
    log.debug("process: finished");
}
Also used : CollectionResource(io.milton.resource.CollectionResource) MoveableResource(io.milton.resource.MoveableResource) ConflictException(io.milton.http.exceptions.ConflictException) DeletableResource(io.milton.resource.DeletableResource) Resource(io.milton.resource.Resource) CollectionResource(io.milton.resource.CollectionResource) MoveableResource(io.milton.resource.MoveableResource) DeletableResource(io.milton.resource.DeletableResource) MoveEvent(io.milton.event.MoveEvent)

Example 9 with ConflictException

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

the class MoveJsonResource method processForm.

@Override
public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws BadRequestException, NotAuthorizedException {
    String dest = parameters.get("destination");
    if (dest == null) {
        throw new BadRequestException(this, "The destination parameter is null");
    } else if (dest.trim().length() == 0) {
        throw new BadRequestException(this, "The destination parameter is empty");
    }
    Path pDest = Path.path(dest);
    if (pDest == null) {
        throw new BadRequestException(this, "Couldnt parse the destination header, returned null from: " + dest);
    }
    String parentPath = "/";
    if (pDest.getParent() != null) {
        parentPath = pDest.getParent().toString();
    }
    Resource rDestParent = resourceFactory.getResource(host, parentPath);
    if (rDestParent == null)
        throw new BadRequestException(wrapped, "The destination parent does not exist");
    if (rDestParent instanceof CollectionResource) {
        CollectionResource colDestParent = (CollectionResource) rDestParent;
        if (colDestParent.child(pDest.getName()) == null) {
            try {
                wrapped.moveTo(colDestParent, pDest.getName());
            } catch (ConflictException ex) {
                log.warn("Exception copying to: " + pDest.getName(), ex);
                throw new BadRequestException(rDestParent, "conflict: " + ex.getMessage());
            }
            return null;
        } else {
            log.warn("destination already exists: " + pDest.getName() + " in folder: " + colDestParent.getName());
            throw new BadRequestException(rDestParent, "File already exists");
        }
    } else {
        throw new BadRequestException(wrapped, "The destination parent is not a collection resource");
    }
}
Also used : Path(io.milton.common.Path) CollectionResource(io.milton.resource.CollectionResource) ConflictException(io.milton.http.exceptions.ConflictException) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource) PostableResource(io.milton.resource.PostableResource) MoveableResource(io.milton.resource.MoveableResource) BadRequestException(io.milton.http.exceptions.BadRequestException)

Example 10 with ConflictException

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

the class PutJsonResource method processForm.

@Override
public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws ConflictException, NotAuthorizedException, BadRequestException {
    log.info("processForm: " + wrapped.getClass());
    if (files.isEmpty()) {
        log.debug("no files uploaded");
        return null;
    }
    newFiles = new ArrayList<NewFile>();
    for (FileItem file : files.values()) {
        NewFile nf = new NewFile();
        String ua = HttpManager.request().getUserAgentHeader();
        String f = Utils.truncateFileName(ua, file.getName());
        nf.setOriginalName(f);
        nf.setContentType(file.getContentType());
        nf.setLength(file.getSize());
        newFiles.add(nf);
        String newName = getName(f, parameters);
        log.info("creating resource: " + newName + " size: " + file.getSize());
        InputStream in = null;
        Resource newResource;
        try {
            in = file.getInputStream();
            Resource existing = wrapped.child(newName);
            if (existing != null) {
                if (existing instanceof ReplaceableResource) {
                    log.trace("existing resource is replaceable, so replace content");
                    ReplaceableResource rr = (ReplaceableResource) existing;
                    rr.replaceContent(in, null);
                    log.trace("completed POST processing for file. Updated: " + existing.getName());
                    eventManager.fireEvent(new PutEvent(rr));
                    newResource = rr;
                } else {
                    log.trace("existing resource is not replaceable, will be deleted");
                    if (existing instanceof DeletableResource) {
                        DeletableResource dr = (DeletableResource) existing;
                        dr.delete();
                        newResource = wrapped.createNew(newName, in, file.getSize(), file.getContentType());
                        log.trace("completed POST processing for file. Deleted, then created: " + newResource.getName());
                        eventManager.fireEvent(new PutEvent(newResource));
                    } else {
                        throw new BadRequestException(existing, "existing resource could not be deleted, is not deletable");
                    }
                }
            } else {
                newResource = wrapped.createNew(newName, in, file.getSize(), file.getContentType());
                log.info("completed POST processing for file. Created: " + newResource.getName());
                eventManager.fireEvent(new PutEvent(newResource));
            }
            String newHref = buildNewHref(href, newResource.getName());
            nf.setHref(newHref);
        } catch (NotAuthorizedException ex) {
            throw new RuntimeException(ex);
        } catch (BadRequestException ex) {
            throw new RuntimeException(ex);
        } catch (ConflictException ex) {
            throw new RuntimeException(ex);
        } catch (IOException ex) {
            throw new RuntimeException("Exception creating resource", ex);
        } finally {
            FileUtils.close(in);
        }
    }
    log.trace("completed all POST processing");
    return null;
}
Also used : ConflictException(io.milton.http.exceptions.ConflictException) InputStream(java.io.InputStream) PutEvent(io.milton.event.PutEvent) 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) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) IOException(java.io.IOException) ReplaceableResource(io.milton.resource.ReplaceableResource) BadRequestException(io.milton.http.exceptions.BadRequestException) DeletableResource(io.milton.resource.DeletableResource)

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