Search in sources :

Example 1 with JDBCatalogue

use of nl.uva.cs.lobcder.catalogue.JDBCatalogue 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)

Example 2 with JDBCatalogue

use of nl.uva.cs.lobcder.catalogue.JDBCatalogue in project lobcder by skoulouzis.

the class RegisterSRMSites method initVFS.

private static void initVFS() throws VlException, MalformedURLException, NamingException, Exception {
    vfsClient = new VFSClient();
    VRSContext context = vfsClient.getVRSContext();
    BdiiService bdii = context.getBdiiService();
    srms = bdii.getSRMv22SAsforVO("biomed");
    // 
    debug("srms: " + context.getConfigManager().getBdiiHost());
    for (StorageArea inf : srms) {
        debug("srms: " + inf.getVOStorageLocation());
    }
    JDBCatalogue cat = new JDBCatalogue();
    String resourceURI = "";
    Credential credentials = new Credential();
// cat.registerStorageSite(resourceURI, credentials, -1, -1, -1, -1, null);
}
Also used : VRSContext(nl.uva.vlet.vrs.VRSContext) VFSClient(nl.uva.vlet.vfs.VFSClient) Credential(nl.uva.cs.lobcder.resources.Credential) JDBCatalogue(nl.uva.cs.lobcder.catalogue.JDBCatalogue) StorageArea(nl.uva.vlet.util.bdii.StorageArea) BdiiService(nl.uva.vlet.util.bdii.BdiiService)

Aggregations

JDBCatalogue (nl.uva.cs.lobcder.catalogue.JDBCatalogue)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 MyPrincipal (nl.uva.cs.lobcder.auth.MyPrincipal)1 Permissions (nl.uva.cs.lobcder.auth.Permissions)1 Credential (nl.uva.cs.lobcder.resources.Credential)1 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)1 PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)1 BdiiService (nl.uva.vlet.util.bdii.BdiiService)1 StorageArea (nl.uva.vlet.util.bdii.StorageArea)1 VFSClient (nl.uva.vlet.vfs.VFSClient)1