Search in sources :

Example 1 with BadRequestException

use of io.milton.http.exceptions.BadRequestException in project polymap4-core by Polymap4.

the class WebDavFilePostable method processForm.

public String processForm(Map<String, String> params, Map<String, FileItem> files) throws BadRequestException, NotAuthorizedException, ConflictException {
    log.info("params: " + params);
    log.info("files: " + files);
    try {
        return node.processForm(params, files);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (org.polymap.service.fs.spi.BadRequestException e) {
        log.warn("", e);
        throw new BadRequestException(this, e.getLocalizedMessage());
    } catch (org.polymap.service.fs.spi.NotAuthorizedException e) {
        log.warn("", e);
        throw new NotAuthorizedException(this);
    }
}
Also used : BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Example 2 with BadRequestException

use of io.milton.http.exceptions.BadRequestException in project polymap4-core by Polymap4.

the class WebDavFileResource method sendContent.

public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotAuthorizedException, BadRequestException {
    try {
        org.polymap.service.fs.spi.Range fsRange = range != null ? new org.polymap.service.fs.spi.Range(range.getStart(), range.getFinish()) : null;
        delegate().sendContent(out, fsRange, params, contentType);
    } catch (IOException e) {
        throw e;
    } catch (org.polymap.service.fs.spi.BadRequestException e) {
        throw new BadRequestException(this, e.getMessage());
    }
}
Also used : BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException)

Example 3 with BadRequestException

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

the class WebDataDirResource method copyTo.

@Override
public void copyTo(CollectionResource toCollection, String name) throws NotAuthorizedException, BadRequestException, ConflictException {
    WebDataDirResource toWDDR = (WebDataDirResource) toCollection;
    Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "copyTo({0}, ''{1}'') for {2}", new Object[] { toWDDR.getPath(), name, getPath() });
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            Permissions newParentPerm = getCatalogue().getPermissions(toWDDR.getLogicalData().getUid(), toWDDR.getLogicalData().getOwner(), connection);
            if (!getPrincipal().canWrite(newParentPerm)) {
                throw new NotAuthorizedException(this);
            }
            getCatalogue().copyFolder(getLogicalData(), toWDDR.getLogicalData(), name, 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 : 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)

Example 4 with BadRequestException

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

the class WebDataDirResource method createNew.

@Override
public Resource createNew(String newName, InputStream inputStream, Long length, String contentType) throws IOException, ConflictException, NotAuthorizedException, BadRequestException, InternalError {
    Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "createNew. for {0}\n\t newName:\t{1}\n\t length:\t{2}\n\t contentType:\t{3}", new Object[] { getPath(), newName, length, contentType });
    LogicalData fileLogicalData;
    // List<PDRIDescr> pdriDescrList;
    WebDataFileResource resource;
    PDRI pdri;
    double start = System.currentTimeMillis();
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            // Long uid = getCatalogue().getLogicalDataUidByParentRefAndName(getLogicalData().getUid(), newName, connection);
            Path newPath = Path.path(getPath(), newName);
            fileLogicalData = getCatalogue().getLogicalDataByPath(newPath, connection);
            if (contentType == null || contentType.equals("application/octet-stream")) {
                contentType = mimeTypeMap.get(FilenameUtils.getExtension(newName));
            }
            if (fileLogicalData != null) {
                // Resource exists, update
                // throw new ConflictException(this, newName);
                Permissions p = getCatalogue().getPermissions(fileLogicalData.getUid(), fileLogicalData.getOwner(), connection);
                if (!getPrincipal().canWrite(p)) {
                    throw new NotAuthorizedException(this);
                }
                fileLogicalData.setLength(length);
                fileLogicalData.setModifiedDate(System.currentTimeMillis());
                fileLogicalData.setLastAccessDate(fileLogicalData.getModifiedDate());
                if (contentType == null) {
                    contentType = mimeTypeMap.get(FilenameUtils.getExtension(newName));
                }
                fileLogicalData.addContentType(contentType);
                resource = new WebDataFileResource(fileLogicalData, Path.path(getPath(), newName), getCatalogue(), authList);
                // LockToken tocken = resource.getCurrentLock();
                // System.err.println("tokenId: "+tocken.tokenId+" lockedByUser: "+tocken.info.lockedByUser+" timeout: "+tocken.timeout.toString()+" getOtherSeconds: "+tocken.timeout.getOtherSeconds()+" getSeconds: "+tocken.timeout.getSeconds());
                // if (tocken != null) {
                // if (tocken.getFrom().after(new Date(System.currentTimeMillis()))) {
                // throw new ConflictException(resource, "The resource is locked");
                // }
                // }
                // Create new
                pdri = createPDRI(fileLogicalData.getLength(), newName, connection);
                pdri.setLength(length);
                pdri.putData(inputStream);
                fileLogicalData = getCatalogue().updateLogicalDataAndPdri(fileLogicalData, pdri, connection);
                // fileLogicalData = inheritProperties(fileLogicalData, connection);
                connection.commit();
                connection.close();
            // String md5 = pdri.getStringChecksum();
            // if (md5 != null) {
            // fileLogicalData.setChecksum(md5);
            // }
            // return new WebDataFileResource(fileLogicalData, Path.path(getPath(), newName), getCatalogue(), authList);
            } else {
                // Resource does not exists, create a new one
                // new need write prmissions for current collection
                fileLogicalData = new LogicalData();
                fileLogicalData.setName(newName);
                fileLogicalData.setParentRef(getLogicalData().getUid());
                fileLogicalData.setType(Constants.LOGICAL_FILE);
                fileLogicalData.setOwner(getPrincipal().getUserId());
                fileLogicalData.setLength(length);
                fileLogicalData.setCreateDate(System.currentTimeMillis());
                fileLogicalData.setModifiedDate(System.currentTimeMillis());
                fileLogicalData.setLastAccessDate(System.currentTimeMillis());
                fileLogicalData.setTtlSec(getLogicalData().getTtlSec());
                fileLogicalData.addContentType(contentType);
                pdri = createPDRI(length, newName, connection);
                pdri.setLength(length);
                pdri.putData(inputStream);
                // String md5 = pdri.getStringChecksum();
                // if (md5 != null) {
                // fileLogicalData.setChecksum(md5);
                // }
                fileLogicalData = getCatalogue().associateLogicalDataAndPdri(fileLogicalData, pdri, connection);
                getCatalogue().setPermissions(fileLogicalData.getUid(), new Permissions(getPrincipal(), getPermissions()), connection);
                // fileLogicalData = inheritProperties(fileLogicalData, connection);
                getCatalogue().setPreferencesOn(fileLogicalData.getUid(), getLogicalData().getUid(), connection);
                List<String> pref = getLogicalData().getDataLocationPreferences();
                fileLogicalData.setDataLocationPreferences(pref);
                connection.commit();
                connection.close();
                resource = new WebDataFileResource(fileLogicalData, Path.path(getPath(), newName), getCatalogue(), authList);
            // return new WebDataFileResource(fileLogicalData, Path.path(getPath(), newName), getCatalogue(), authList);
            }
        } catch (NoSuchAlgorithmException ex) {
            if (connection != null && !connection.isClosed()) {
                connection.rollback();
                connection.close();
            }
            Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, ex);
            throw new InternalError(ex.getMessage());
        } 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 | IOException e1) {
        Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e1);
        throw new BadRequestException(this, e1.getMessage());
    // throw new InternalError(e1.getMessage());
    }
    double elapsed = System.currentTimeMillis() - start;
    double speed = ((resource.getLogicalData().getLength() * 8.0) * 1000.0) / (elapsed * 1000.0);
    String msg = null;
    try {
        Stats stats = new Stats();
        stats.setSource(fromAddress);
        stats.setDestination(pdri.getHost());
        stats.setSpeed(speed);
        stats.setSize(resource.getLogicalData().getLength());
        if (!pdri.isCahce()) {
            getCatalogue().setSpeed(stats);
        }
        msg = "Source: " + fromAddress + " Destination: " + new URI(pdri.getURI()).getScheme() + "://" + pdri.getHost() + " Rx_Speed: " + speed + " Kbites/sec Rx_Size: " + (resource.getLogicalData().getLength()) + " bytes Elapsed_Time: " + elapsed + " ms";
    } catch (URISyntaxException | SQLException ex) {
        Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, ex);
    }
    Logger.getLogger(WebDataDirResource.class.getName()).log(Level.INFO, msg);
    return resource;
}
Also used : Path(io.milton.common.Path) SQLException(java.sql.SQLException) Connection(java.sql.Connection) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) PDRI(nl.uva.cs.lobcder.resources.PDRI) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Permissions(nl.uva.cs.lobcder.auth.Permissions) Stats(nl.uva.cs.lobcder.rest.wrappers.Stats) BadRequestException(io.milton.http.exceptions.BadRequestException)

Example 5 with BadRequestException

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

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