Search in sources :

Example 31 with BadRequestException

use of io.milton.http.exceptions.BadRequestException 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 32 with BadRequestException

use of io.milton.http.exceptions.BadRequestException 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)

Example 33 with BadRequestException

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

the class ReportHandler method processExistingResource.

@Override
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException {
    try {
        org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
        org.jdom.Document doc = builder.build(request.getInputStream());
        String reportName = doc.getRootElement().getName();
        Report r = reports.get(reportName);
        if (r == null) {
            log.error("report not known: " + reportName);
            throw new BadRequestException(resource);
        } else {
            log.trace("process report: " + reportName + " with : " + r.getClass());
            String xml = r.process(request.getHostHeader(), request.getAbsolutePath(), resource, doc);
            response.setStatus(Response.Status.SC_MULTI_STATUS);
            response.setContentTypeHeader("text/xml");
            response.setEntity(new ByteArrayEntity(xml.getBytes("UTF-8")));
        }
    } catch (JDOMException ex) {
        java.util.logging.Logger.getLogger(ReportHandler.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ReadingException ex) {
        throw new RuntimeException(ex);
    } catch (WritingException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : IOException(java.io.IOException) JDOMException(org.jdom.JDOMException) ReadingException(io.milton.common.ReadingException) ByteArrayEntity(io.milton.http.entity.ByteArrayEntity) BadRequestException(io.milton.http.exceptions.BadRequestException) WritingException(io.milton.common.WritingException)

Example 34 with BadRequestException

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

the class LoginResponseHandler method attemptRespondLoginPage.

private void attemptRespondLoginPage(Request request, Resource resource, Response response) throws RuntimeException {
    Resource rLogin;
    try {
        rLogin = resourceFactory.getResource(request.getHostHeader(), loginPage);
    } catch (NotAuthorizedException e) {
        throw new RuntimeException(e);
    } catch (BadRequestException ex) {
        throw new RuntimeException(ex);
    }
    if (rLogin == null || !(rLogin instanceof GetableResource)) {
        log.info("Couldnt find login resource: " + request.getHostHeader() + loginPage + " with resource factory: " + resourceFactory.getClass());
        wrapped.respondUnauthorised(resource, response, request);
    } else {
        log.trace("respond with 200 to suppress login prompt, using resource: " + rLogin.getName() + " - " + rLogin.getClass());
        try {
            // set request attribute so rendering knows it authorisation failed, or authentication is required
            Auth auth = request.getAuthorization();
            if (auth != null && auth.getTag() != null) {
                // no authentication was attempted,
                request.getAttributes().put("authReason", "notPermitted");
            } else {
                request.getAttributes().put("authReason", "required");
            }
            GetableResource gr = (GetableResource) rLogin;
            wrapped.respondContent(gr, response, request, null);
        } catch (NotAuthorizedException ex) {
            throw new RuntimeException(ex);
        } catch (BadRequestException ex) {
            throw new RuntimeException(ex);
        } catch (NotFoundException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : GetableResource(io.milton.resource.GetableResource) Resource(io.milton.resource.Resource) BadRequestException(io.milton.http.exceptions.BadRequestException) GetableResource(io.milton.resource.GetableResource) NotFoundException(io.milton.http.exceptions.NotFoundException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Example 35 with BadRequestException

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

the class CopyJsonResource method processForm.

public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws BadRequestException, NotAuthorizedException {
    String dest = parameters.get("destination");
    Path pDest = Path.path(dest);
    Resource rDestParent = resourceFactory.getResource(host, pDest.getParent().toString());
    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.copyTo(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());
            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) CopyableResource(io.milton.resource.CopyableResource) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource) PostableResource(io.milton.resource.PostableResource) BadRequestException(io.milton.http.exceptions.BadRequestException)

Aggregations

BadRequestException (io.milton.http.exceptions.BadRequestException)37 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)25 IOException (java.io.IOException)15 Resource (io.milton.resource.Resource)11 ConflictException (io.milton.http.exceptions.ConflictException)10 SQLException (java.sql.SQLException)10 Connection (java.sql.Connection)9 Path (io.milton.common.Path)6 NotFoundException (io.milton.http.exceptions.NotFoundException)6 Permissions (nl.uva.cs.lobcder.auth.Permissions)6 CollectionResource (io.milton.resource.CollectionResource)5 URISyntaxException (java.net.URISyntaxException)5 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)5 ReplaceableResource (io.milton.resource.ReplaceableResource)4 BufferingOutputStream (io.milton.common.BufferingOutputStream)3 Range (io.milton.http.Range)3 PreConditionFailedException (io.milton.http.exceptions.PreConditionFailedException)3 GetableResource (io.milton.resource.GetableResource)3 PostableResource (io.milton.resource.PostableResource)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3