Search in sources :

Example 1 with PDRIDescr

use of nl.uva.cs.lobcder.resources.PDRIDescr in project lobcder by skoulouzis.

the class JDBCatalogue method getPdriStorageSiteID.

public List<PDRIDescr> getPdriStorageSiteID(Long StorageSiteId, @Nonnull Connection connection) throws SQLException {
    List<PDRIDescr> res = getFromPDRIDescrCache(StorageSiteId);
    if (res != null) {
        return res;
    }
    res = new ArrayList<>();
    long pdriId = -1;
    try (PreparedStatement ps = connection.prepareStatement("" + "SELECT fileName, storageSiteRef, storage_site_table.resourceUri, " + "username, password, isEncrypted, encryptionKey, pdri_table.pdriId, pdriGroupRef, storage_site_table.isCache " + "FROM pdri_table " + "JOIN storage_site_table ON storageSiteRef = storageSiteId " + "JOIN credential_table ON credentialRef = credintialId " + "WHERE storageSiteRef = ? ")) {
        ps.setLong(1, StorageSiteId);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String fileName = rs.getString(1);
            long ssID = rs.getLong(2);
            String resourceURI = rs.getString(3);
            String uName = rs.getString(4);
            String passwd = rs.getString(5);
            boolean encrypt = rs.getBoolean(6);
            long key = rs.getLong(7);
            pdriId = rs.getLong(8);
            boolean isCache = rs.getBoolean(9);
            // if (resourceURI.startsWith("lfc") || resourceURI.startsWith("srm")
            // || resourceURI.startsWith("gftp")) {
            // try {
            // passwd = getProxyAsBase64String();
            // } catch (FileNotFoundException ex) {
            // Logger.getLogger(JDBCatalogue.class.getName()).log(Level.SEVERE, null, ex);
            // } catch (IOException ex) {
            // Logger.getLogger(JDBCatalogue.class.getName()).log(Level.SEVERE, null, ex);
            // }
            // }
            long groupId = rs.getLong(9);
            res.add(new PDRIDescr(fileName, ssID, resourceURI, uName, passwd, encrypt, BigInteger.valueOf(key), groupId, (pdriId), isCache));
        }
        if (pdriId > -1) {
            putToPDRIDescrCache(pdriId, res);
        }
        return res;
    }
}
Also used : PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr)

Example 2 with PDRIDescr

use of nl.uva.cs.lobcder.resources.PDRIDescr in project lobcder by skoulouzis.

the class WebDataFileResource method transferer.

private PDRI transferer(List<PDRIDescr> pdris, OutputStream out, int tryCount, boolean doCircularStreamBufferTransferer) throws IOException, NotFoundException {
    InputStream in = null;
    PDRI pdri = null;
    try {
        PDRIDescr pdriDescr = selectBestPDRI(pdris);
        pdri = PDRIFactory.getFactory().createInstance(pdriDescr, false);
        if (pdri != null) {
            in = pdri.getData();
            WebDataFileResource.log.log(Level.FINE, "sendContent() for {0}--------- {1}", new Object[] { getPath(), pdri.getFileName() });
            if (!pdri.getEncrypted()) {
                if (doCircularStreamBufferTransferer) {
                    CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((Constants.BUF_SIZE), in, out);
                    cBuff.startTransfer((long) -1);
                } else {
                    int read;
                    byte[] copyBuffer = new byte[Constants.BUF_SIZE];
                    while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
                        out.write(copyBuffer, 0, read);
                    }
                }
            } else {
                DesEncrypter encrypter = new DesEncrypter(pdri.getKeyInt());
                encrypter.decrypt(in, out);
            }
        } else {
            sleepTime = 5;
            throw new NotFoundException("Physical resource not found");
        }
    } catch (Exception ex) {
        if (ex instanceof NotFoundException) {
            throw (NotFoundException) ex;
        }
        if (ex.getMessage().contains("Resource not found")) {
            throw new NotFoundException(ex.getMessage());
        }
        try {
            sleepTime = sleepTime + 20;
            Thread.sleep(sleepTime);
            if (ex instanceof nl.uva.vlet.exception.VlInterruptedException && ++tryCount < Constants.RECONNECT_NTRY) {
                transferer(pdris, out, tryCount, false);
            } else if (++tryCount < Constants.RECONNECT_NTRY) {
                transferer(pdris, out, tryCount, true);
            } else {
                transferer(pdris, out, 0, true);
            }
        } catch (InterruptedException ex1) {
            sleepTime = 5;
            throw new IOException(ex1);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    sleepTime = 5;
    return pdri;
}
Also used : CircularStreamBufferTransferer(nl.uva.vlet.io.CircularStreamBufferTransferer) InputStream(java.io.InputStream) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) NotFoundException(io.milton.http.exceptions.NotFoundException) DesEncrypter(nl.uva.cs.lobcder.util.DesEncrypter) IOException(java.io.IOException) PDRI(nl.uva.cs.lobcder.resources.PDRI) 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 3 with PDRIDescr

use of nl.uva.cs.lobcder.resources.PDRIDescr in project lobcder by skoulouzis.

the class ItemBean method main.

// / test
public static void main(String[] args) {
    try {
        DatatypeFactory df = DatatypeFactory.newInstance();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        ItemBean ldb = new ItemBean();
        ldb.setUid(123L);
        ldb.setCreateDate(df.newXMLGregorianCalendar(gregorianCalendar));
        ldb.setType(Type.FILE);
        ldb.setName("Name");
        ldb.setParentRef(45L);
        Permissions p = new Permissions();
        p.setOwner("Dima");
        p.getRead().add("group_read1");
        p.getRead().add("group_read2");
        p.getWrite().add("group_write1");
        ldb.setPermissions(p);
        ldb.setParentRef(46L);
        ldb.addContentType("application/xml");
        ldb.setPath("/qwa/qwe");
        List<PDRIDescr> pris = new ArrayList<>();
        // pdril.getPdriList().add(new PdriBean("name", "url", "username", "password", false, null, false));
        // pdril.getPdriList().add(new PdriBean("name2","url", "username", "password", false, null, false));
        // ldb.setPdriList(pdril);
        LockInfo lockInfo = new LockInfo(LockInfo.LockScope.EXCLUSIVE, LockInfo.LockType.WRITE, "user", LockInfo.LockDepth.INFINITY);
        // LockTokenBean lockToken = new LockTokenBean();
        // lockToken.setLocktoken("token-name");
        // lockToken.setLockInfo(lockInfo);
        // lockToken.setLockedUntil(df.newXMLGregorianCalendar(gregorianCalendar));
        // ldb.setLock(lockToken);
        DriBean dri = new DriBean();
        dri.setStatus(DriBean.Status.unavailable);
        ldb.setDri(dri);
        JAXBContext context = JAXBContext.newInstance(ItemBean.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(ldb, System.err);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) DatatypeFactory(javax.xml.datatype.DatatypeFactory) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Permissions(nl.uva.cs.lobcder.auth.Permissions) LockInfo(io.milton.http.LockInfo) JAXBContext(javax.xml.bind.JAXBContext)

Example 4 with PDRIDescr

use of nl.uva.cs.lobcder.resources.PDRIDescr in project lobcder by skoulouzis.

the class Catalogue method removeUnreachablePDRIs.

private LogicalDataWrapped removeUnreachablePDRIs(LogicalDataWrapped logicalData, String fileUID) throws IOException, URISyntaxException {
    List<PDRIDescr> pdris = logicalData.getPdriList();
    if (pdris != null) {
        List<PDRIDescr> removeIt = new ArrayList<>();
        for (PDRIDescr p : pdris) {
            URI uri = new URI(p.getResourceUrl());
            String pdriHost = uri.getHost();
            String pdriScheme = uri.getScheme();
            if (pdriHost == null || pdriHost.equals("localhost") || pdriHost.startsWith("127.0.")) {
                removeIt.add(p);
            } else if (pdriScheme.equals("file") && !isPDRIOnWorker(new URI(p.getResourceUrl()))) {
                removeIt.add(p);
            }
        }
        if (!removeIt.isEmpty()) {
            pdris.removeAll(removeIt);
            if (pdris.isEmpty()) {
                Logger.getLogger(Catalogue.class.getName()).log(Level.SEVERE, "PDRIS from master is either empty or contains unreachable files");
                Catalogue.logicalDataCache.remove(fileUID);
                throw new IOException("PDRIS from master is either empty or contains unreachable files");
            }
            Catalogue.logicalDataCache.put(fileUID, logicalData);
        }
    }
    return logicalData;
}
Also used : PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URI(java.net.URI)

Example 5 with PDRIDescr

use of nl.uva.cs.lobcder.resources.PDRIDescr in project lobcder by skoulouzis.

the class Archive method getZip.

/**
 * Generates a zip archive of folder
 *
 * @param path the folder name
 * @return the stream of the archive
 */
@GET
@Path("/getzip/{name:.+}")
public Response getZip(@PathParam("name") String path) {
    class Folder {

        private String path;

        private LogicalData logicalData;

        private Folder(String path, LogicalData logicalData) {
            this.path = path;
            this.logicalData = logicalData;
        }

        /**
         * @return the path
         */
        public String getPath() {
            return path;
        }

        /**
         * @param path the path to set
         */
        public void setPath(String path) {
            this.path = path;
        }

        /**
         * @return the logicalData
         */
        public LogicalData getLogicalData() {
            return logicalData;
        }

        /**
         * @param logicalData the logicalData to set
         */
        public void setLogicalData(LogicalData logicalData) {
            this.logicalData = logicalData;
        }
    }
    final String rootPath;
    if (path.endsWith("/")) {
        rootPath = path.substring(0, path.length() - 1);
    } else {
        rootPath = path;
    }
    int index = rootPath.lastIndexOf('/');
    final String rootName;
    if (index != -1) {
        rootName = rootPath.substring(index + 1);
    } else {
        rootName = rootPath;
    }
    if (rootName.isEmpty()) {
        throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE);
    }
    final MyPrincipal principal = (MyPrincipal) request.getAttribute("myprincipal");
    final JDBCatalogue catalogue = getCatalogue();
    StreamingOutput result = new StreamingOutput() {

        @Override
        public void write(OutputStream out) throws IOException, WebApplicationException {
            Stack<Folder> folders;
            try (Connection connection = catalogue.getConnection()) {
                LogicalData rootElement = catalogue.getLogicalDataByPath(io.milton.common.Path.path(rootPath), connection);
                if (rootElement == null) {
                    throw new WebApplicationException(Response.Status.NOT_FOUND);
                }
                try (ZipOutputStream zip = new ZipOutputStream(out)) {
                    ZipEntry ze;
                    folders = new Stack<>();
                    Permissions p = catalogue.getPermissions(rootElement.getUid(), rootElement.getOwner(), connection);
                    if (principal.canRead(p)) {
                        if (rootElement.isFolder()) {
                            folders.add(new Folder(rootName, rootElement));
                        } else {
                            ze = new ZipEntry(rootName);
                            zip.putNextEntry(ze);
                            List<PDRIDescr> pdris = catalogue.getPdriDescrByGroupId(rootElement.getPdriGroupId());
                            copyStream(pdris, zip);
                            zip.closeEntry();
                            getCatalogue().addViewForRes(rootElement.getUid());
                        }
                    }
                    while (!folders.isEmpty()) {
                        Folder folder = folders.pop();
                        ze = new ZipEntry(folder.getPath() + "/");
                        ze.setTime(folder.getLogicalData().getModifiedDate());
                        zip.putNextEntry(ze);
                        getCatalogue().addViewForRes(folder.getLogicalData().getUid());
                        for (LogicalData ld : catalogue.getChildrenByParentRef(folder.getLogicalData().getUid(), connection)) {
                            Permissions entry_p = catalogue.getPermissions(ld.getUid(), ld.getOwner(), connection);
                            if (principal.canRead(entry_p)) {
                                if (ld.isFolder()) {
                                    folders.push(new Folder(folder.getPath() + "/" + ld.getName(), ld));
                                } else {
                                    ze = new ZipEntry(folder.getPath() + "/" + ld.getName());
                                    ze.setTime(ld.getModifiedDate());
                                    zip.putNextEntry(ze);
                                    copyStream(catalogue.getPdriDescrByGroupId(ld.getPdriGroupId()), zip);
                                    zip.closeEntry();
                                    getCatalogue().addViewForRes(ld.getUid());
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                if (e instanceof WebApplicationException) {
                    throw (WebApplicationException) e;
                } else {
                    throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
                }
            }
        }
    };
    Response.ResponseBuilder response = Response.ok(result, "application/zip");
    return response.header("Content-Disposition", "attachment; filename=" + rootName + ".zip").build();
}
Also used : JDBCatalogue(nl.uva.cs.lobcder.catalogue.JDBCatalogue) WebApplicationException(javax.ws.rs.WebApplicationException) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipEntry(java.util.zip.ZipEntry) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) Connection(java.sql.Connection) StreamingOutput(javax.ws.rs.core.StreamingOutput) SQLException(java.sql.SQLException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Response(javax.ws.rs.core.Response) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) ZipOutputStream(java.util.zip.ZipOutputStream) Permissions(nl.uva.cs.lobcder.auth.Permissions) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)22 IOException (java.io.IOException)8 PDRI (nl.uva.cs.lobcder.resources.PDRI)7 SQLException (java.sql.SQLException)6 Permissions (nl.uva.cs.lobcder.auth.Permissions)5 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 UnknownHostException (java.net.UnknownHostException)4 ArrayList (java.util.ArrayList)4 LogicalDataWrapped (nl.uva.cs.lobcder.rest.wrappers.LogicalDataWrapped)4 BadRequestException (io.milton.http.exceptions.BadRequestException)3 NotFoundException (io.milton.http.exceptions.NotFoundException)3 InputStream (java.io.InputStream)3 Path (javax.ws.rs.Path)3 MyPrincipal (nl.uva.cs.lobcder.auth.MyPrincipal)3 ConflictException (io.milton.http.exceptions.ConflictException)2 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 SocketException (java.net.SocketException)2