Search in sources :

Example 1 with Path

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

the class JDBCatalogue method getLogicalDataByName.

public List<LogicalData> getLogicalDataByName(Path fileName, Connection connection) throws SQLException, UnsupportedEncodingException {
    Path decodedLogicalFileName = Path.path(java.net.URLDecoder.decode(fileName.toString(), "UTF-8"));
    try (PreparedStatement ps = connection.prepareStatement("SELECT uid, parentRef, " + "ownerId, datatype, ldName, createDate, modifiedDate, ldLength, " + "contentTypesStr, pdriGroupRef, isSupervised, checksum, " + "lastValidationDate, lockTokenID, lockScope, lockType, " + "lockedByUser, lockDepth, lockTimeout, description, " + "locationPreference, status" + // + ", resourceUri "
    "FROM ldata_table " + // + "JOIN storage_site_table ON storageSiteId = storageSiteRef"
    "WHERE ldata_table.ldName like '" + decodedLogicalFileName.toString() + "'")) {
        // ps.setString(1, fileName.toString());
        ResultSet rs = ps.executeQuery();
        List<LogicalData> results = new ArrayList<>();
        while (rs.next()) {
            LogicalData res = new LogicalData();
            res.setUid(rs.getLong(1));
            res.setParentRef(rs.getLong(2));
            res.setOwner(rs.getString(3));
            res.setType(rs.getString(4));
            res.setName(rs.getString(5));
            res.setCreateDate(rs.getTimestamp(6).getTime());
            res.setModifiedDate(rs.getTimestamp(7).getTime());
            res.setLength(rs.getLong(8));
            res.setContentTypesAsString(rs.getString(9));
            res.setPdriGroupId(rs.getLong(10));
            res.setSupervised(rs.getBoolean(11));
            res.setChecksum(rs.getString(12));
            res.setLastValidationDate(rs.getLong(13));
            res.setLockTokenID(rs.getString(14));
            res.setLockScope(rs.getString(15));
            res.setLockType(rs.getString(16));
            res.setLockedByUser(rs.getString(17));
            res.setLockDepth(rs.getString(18));
            res.setLockTimeout(rs.getLong(19));
            res.setDescription(rs.getString(20));
            // res.setDataLocationPreference(rs.getString(21));
            res.setStatus(rs.getString(22));
            // Array locationPerf = rs.getArray(23);
            // res.setDataLocationPreferences(getDataLocationPreferace(connection, res.getUid()));
            results.add(res);
        }
        return results;
    }
}
Also used : Path(io.milton.common.Path) LogicalData(nl.uva.cs.lobcder.resources.LogicalData)

Example 2 with Path

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

the class GraphPopulator method run.

@Override
public void run() {
    try (Connection connection = datasource.getConnection()) {
        LogicalData root = getLogicalDataByUid(Long.valueOf(1), connection);
        ArrayList<Path> nodes = getNodes(Path.root, root, connection, null);
        // String msg = "";
        // for (Path d : nodes) {
        // msg += d + "\n";
        // }
        // log.log(Level.INFO, "Nodes: {0}", msg);
        Graph graph = populateGraph(nodes);
        ArrayList<Vertex> transitions = getTransitions(connection, nodes);
        graph = addEdges(graph, transitions);
        this.graph = graph;
    } catch (MalformedURLException | SQLException ex) {
        Logger.getLogger(GraphPopulator.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : Path(io.milton.common.Path) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) MalformedURLException(java.net.MalformedURLException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 3 with Path

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

the class TestUtils method test.

public void test() throws NotAuthorizedException, BadRequestException {
    ColRes col1 = new ColRes("col1");
    ColRes col2 = new ColRes("col2");
    col1.children.put("col2", col2);
    Res page = new Res("page");
    col2.children.put("page", page);
    Path path = Path.path("col2/page");
    Resource r = Utils.findChild(col1, path);
    assertEquals(page, r);
}
Also used : Path(io.milton.common.Path) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource)

Example 4 with Path

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

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

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