Search in sources :

Example 1 with MyPrincipal

use of nl.uva.cs.lobcder.auth.MyPrincipal in project lobcder by skoulouzis.

the class PathReservationService method getXml.

@Path("get_workers/")
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public List<WorkerStatus> getXml() throws MalformedURLException {
    // rest/reservation/get_workers/?id=all
    MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
    MultivaluedMap<String, String> queryParameters = info.getQueryParameters();
    if (mp.getRoles().contains("planner") || mp.isAdmin() && queryParameters != null && !queryParameters.isEmpty()) {
        String workerID = queryParameters.getFirst("id");
        ArrayList<WorkerStatus> workersStatus = new ArrayList<>();
        workers = PropertiesHelper.getWorkers();
        for (String s : workers) {
            WorkerStatus ws = new WorkerStatus();
            ws.setHostName(new URL(s).getHost());
            ws.setStatus("READY");
            workersStatus.add(ws);
        }
        return workersStatus;
    }
    return null;
}
Also used : MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) WorkerStatus(nl.uva.cs.lobcder.rest.wrappers.WorkerStatus) ArrayList(java.util.ArrayList) URL(java.net.URL) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with MyPrincipal

use of nl.uva.cs.lobcder.auth.MyPrincipal in project lobcder by skoulouzis.

the class PathReservationService method getWorkersState.

@Path("workers/")
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public List<WorkerStatus> getWorkersState() {
    // rest/reservation/workers/?host=kscvdfv&host=sp2&host=192.168.1.1
    MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
    if (mp.getRoles().contains("planner") || mp.isAdmin()) {
        MultivaluedMap<String, String> queryParameters = info.getQueryParameters();
        List<String> queryWorkers = queryParameters.get("host");
        List<WorkerStatus> workersStatus = new ArrayList<>();
        workers = PropertiesHelper.getWorkers();
        for (String worker : queryWorkers) {
            WorkerStatus ws = new WorkerStatus();
            ws.setStatus("UNKNOWN");
            for (String w : workers) {
                if (w.contains(worker)) {
                    ws.setStatus("READY");
                    break;
                }
            }
            ws.setHostName(worker);
            workersStatus.add(ws);
        }
        return workersStatus;
    }
    return null;
}
Also used : MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) WorkerStatus(nl.uva.cs.lobcder.rest.wrappers.WorkerStatus) ArrayList(java.util.ArrayList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with MyPrincipal

use of nl.uva.cs.lobcder.auth.MyPrincipal in project lobcder by skoulouzis.

the class StorageSitesService method delete.

@Path("delete/")
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public void delete(JAXBElement<StorageSiteWrapperList> jbSites) throws SQLException {
    StorageSiteWrapperList sitesWL = jbSites.getValue();
    MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
    if (sitesWL != null && sitesWL.getSites() != null && sitesWL.getSites().size() > 0 && mp.isAdmin()) {
        List<Long> ids = new ArrayList<>();
        for (StorageSiteWrapper ssw : sitesWL.getSites()) {
            ids.add(ssw.getStorageSiteId());
        // if(ssw.isSaveFilesOnDelete()){
        // getCatalogue().getPdriStorageSiteID(ssw.getStorageSiteId(), null);
        // }
        }
        try (Connection connection = getCatalogue().getConnection()) {
            getCatalogue().deleteStorageSites(ids, connection);
            connection.commit();
        }
    }
}
Also used : StorageSiteWrapper(nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapper) StorageSiteWrapperList(nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapperList) MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 4 with MyPrincipal

use of nl.uva.cs.lobcder.auth.MyPrincipal 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 5 with MyPrincipal

use of nl.uva.cs.lobcder.auth.MyPrincipal in project lobcder by skoulouzis.

the class DRIDataResource method getLastValidationDateDate.

/**
 * Gets lastvalidationdate property for a resource in text format
 *
 * @param uid the resource's id
 * @return the date last validated in text format
 */
@Path("{uid}/lastValidationDate/")
@GET
@Produces({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML })
public String getLastValidationDateDate(@PathParam("uid") Long uid) {
    try (Connection cn = catalogue.getConnection()) {
        LogicalData res = catalogue.getLogicalDataByUid(uid, cn);
        if (res == null) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
        Permissions p = catalogue.getPermissions(uid, res.getOwner(), cn);
        if (!mp.canRead(p)) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }
        return new Date(res.getLastValidationDate() * 1000).toString();
    } catch (SQLException ex) {
        Logger.getLogger(DRIDataResource.class.getName()).log(Level.SEVERE, null, ex);
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Permissions(nl.uva.cs.lobcder.auth.Permissions) Date(java.util.Date)

Aggregations

MyPrincipal (nl.uva.cs.lobcder.auth.MyPrincipal)37 Connection (java.sql.Connection)23 SQLException (java.sql.SQLException)22 Permissions (nl.uva.cs.lobcder.auth.Permissions)20 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)20 Path (javax.ws.rs.Path)11 ArrayList (java.util.ArrayList)8 GET (javax.ws.rs.GET)8 Produces (javax.ws.rs.Produces)7 WebApplicationException (javax.ws.rs.WebApplicationException)5 PreparedStatement (java.sql.PreparedStatement)4 Stack (java.util.Stack)4 ResultSet (java.sql.ResultSet)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 Consumes (javax.ws.rs.Consumes)3 PUT (javax.ws.rs.PUT)3 JAXBElement (javax.xml.bind.JAXBElement)3 QName (javax.xml.namespace.QName)3 PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)3