Search in sources :

Example 21 with Path

use of io.milton.common.Path in project lobcder by skoulouzis.

the class WebDataDirResource method createAndLock.

/**
 * This means to just lock the name Not to create the resource.
 *
 * @param name
 * @param timeout
 * @param lockInfo
 * @return
 * @throws NotAuthorizedException
 */
@Override
public LockToken createAndLock(String name, LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException {
    try (Connection connection = getCatalogue().getConnection()) {
        Path newPath = Path.path(getPath(), name);
        // If the resource exists
        LogicalData fileLogicalData = getCatalogue().getLogicalDataByPath(newPath, connection);
        if (fileLogicalData != null) {
            throw new PreConditionFailedException(new WebDataFileResource(fileLogicalData, Path.path(getPath(), name), getCatalogue(), authList));
        }
        LockToken lockToken = new LockToken(UUID.randomUUID().toString(), lockInfo, timeout);
        return lockToken;
    } catch (SQLException | PreConditionFailedException ex) {
        Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, ex);
        if (ex instanceof PreConditionFailedException) {
            throw new RuntimeException(ex);
        }
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}
Also used : Path(io.milton.common.Path) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 22 with Path

use of io.milton.common.Path in project lobcder by skoulouzis.

the class LockHandler method processNonExistingResource.

private void processNonExistingResource(HttpManager manager, Request request, Response response, String host, String url) throws NotAuthorizedException, BadRequestException {
    String name;
    Path parentPath = Path.path(url);
    name = parentPath.getName();
    parentPath = parentPath.getParent();
    url = parentPath.toString();
    Resource r = manager.getResourceFactory().getResource(host, url);
    if (r != null) {
        if (!handlerHelper.checkAuthorisation(manager, r, request)) {
            responseHandler.respondUnauthorised(r, response, request);
            return;
        } else {
            processCreateAndLock(manager, request, response, r, name);
        }
    } else {
        // log.debug("couldnt find parent to execute lock-null, returning not found");
        // respondNotFound(response,request);
        response.setStatus(Status.SC_CONFLICT);
    }
}
Also used : Path(io.milton.common.Path) LockingCollectionResource(io.milton.resource.LockingCollectionResource) Resource(io.milton.resource.Resource) LockableResource(io.milton.resource.LockableResource)

Example 23 with Path

use of io.milton.common.Path 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 24 with Path

use of io.milton.common.Path 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)

Example 25 with Path

use of io.milton.common.Path in project lobcder by skoulouzis.

the class ClassPathResourceFactory method getResource.

@Override
public Resource getResource(String host, String path) {
    Path p = Path.path(path);
    if (basePath != null) {
        if (p.getFirst().equals(basePath)) {
            p = p.getStripFirst();
        } else {
            return null;
        }
    }
    // try to locate a resource with the given path
    InputStream content = this.getClass().getResourceAsStream(p.toString());
    if (content == null) {
        return null;
    } else {
        log.trace("return class path resource");
        return new ClassPathResource(host, p, content);
    }
}
Also used : Path(io.milton.common.Path) InputStream(java.io.InputStream)

Aggregations

Path (io.milton.common.Path)25 Resource (io.milton.resource.Resource)9 CollectionResource (io.milton.resource.CollectionResource)8 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)7 BadRequestException (io.milton.http.exceptions.BadRequestException)6 SQLException (java.sql.SQLException)5 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)4 Connection (java.sql.Connection)4 ConflictException (io.milton.http.exceptions.ConflictException)3 MakeCollectionableResource (io.milton.resource.MakeCollectionableResource)3 File (java.io.File)3 Permissions (nl.uva.cs.lobcder.auth.Permissions)3 GetableResource (io.milton.resource.GetableResource)2 PostableResource (io.milton.resource.PostableResource)2 PutableResource (io.milton.resource.PutableResource)2 ReplaceableResource (io.milton.resource.ReplaceableResource)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 PDRI (nl.uva.cs.lobcder.resources.PDRI)2