use of nl.uva.cs.lobcder.resources.LogicalData in project lobcder by skoulouzis.
the class WebDataFileResource method moveTo.
@Override
public void moveTo(CollectionResource collectionResource, String name) throws ConflictException, NotAuthorizedException, BadRequestException {
WebDataDirResource toWDDR = (WebDataDirResource) collectionResource;
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "moveTo(''{0}'', ''{1}'') for {2}", new Object[] { toWDDR.getPath(), name, getPath() });
try (Connection connection = getCatalogue().getConnection()) {
try {
Permissions destPerm = getCatalogue().getPermissions(toWDDR.getLogicalData().getUid(), toWDDR.getLogicalData().getOwner(), connection);
LogicalData parentLD = getCatalogue().getLogicalDataByUid(getLogicalData().getParentRef());
Permissions parentPerm = getCatalogue().getPermissions(parentLD.getUid(), parentLD.getOwner());
if (!(getPrincipal().canWrite(destPerm) && getPrincipal().canWrite(parentPerm))) {
throw new NotAuthorizedException(this);
}
getCatalogue().moveEntry(getLogicalData(), toWDDR.getLogicalData(), name, connection);
connection.commit();
} catch (Exception e) {
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
connection.rollback();
throw new BadRequestException(this, e.getMessage());
}
} catch (SQLException e) {
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
throw new BadRequestException(this, e.getMessage());
}
}
use of nl.uva.cs.lobcder.resources.LogicalData in project lobcder by skoulouzis.
the class WebDataResourceFactory method getResource.
@Override
public Resource getResource(String host, String strPath) {
if (strPath.equals("/login.html")) {
return null;
}
Path ldri = Path.path(strPath);
String first;
do {
first = ldri.getFirst();
ldri = ldri.getStripFirst();
} while (!first.equals("dav"));
// try (Connection cn = catalogue.getConnection()) {
try {
Logger.getLogger(WebDataResourceFactory.class.getName()).log(Level.FINER, "getResource: strPath: {0} path: {1} ldri: {2}" + "\n" + "\tgetResource: host: {3} path: {4}", new Object[] { strPath, Path.path(strPath), ldri, host, ldri });
// LogicalData entry = catalogue.getLogicalDataByPath(ldri, cn);
LogicalData entry = getCatalogue().getLogicalDataByPath(ldri);
if (entry == null) {
return null;
}
if (entry.getType().equals(Constants.LOGICAL_FOLDER)) {
return new WebDataDirResource(entry, ldri, getCatalogue(), getAuthList());
}
if (entry.getType().equals(Constants.LOGICAL_FILE)) {
return new WebDataFileResource(entry, ldri, getCatalogue(), getAuthList());
}
attempts = 0;
// return null;
} catch (SQLException ex) {
if (attempts <= Constants.RECONNECT_NTRY) {
attempts++;
getResource(host, strPath);
} else {
Logger.getLogger(WebDataResourceFactory.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(WebDataResourceFactory.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
use of nl.uva.cs.lobcder.resources.LogicalData in project lobcder by skoulouzis.
the class Items method queryLogicalData.
private List<LogicalDataWrapped> queryLogicalData(@Nonnull MyPrincipal mp, @Nonnull Connection cn) throws Exception {
MultivaluedMap<String, String> queryParameters = info.getQueryParameters();
boolean addFlag = true;
String rootPath = (queryParameters.containsKey("path") && queryParameters.get("path").iterator().hasNext()) ? queryParameters.get("path").iterator().next() : "/";
if (!rootPath.equals("/") && rootPath.endsWith("/")) {
rootPath = rootPath.substring(0, rootPath.length() - 1);
}
int rowLimit;
try {
rowLimit = (queryParameters.containsKey("limit") && queryParameters.get("limit").iterator().hasNext()) ? Integer.valueOf(queryParameters.get("limit").iterator().next()).intValue() : defaultRowLimit;
} catch (Throwable th) {
rowLimit = defaultRowLimit;
}
LogicalData ld = getCatalogue().getLogicalDataByPath(io.milton.common.Path.path(rootPath), cn);
List<LogicalDataWrapped> logicalDataWrappedList = new ArrayList<>();
if (ld == null || rowLimit < 1) {
return logicalDataWrappedList;
}
Permissions p = getCatalogue().getPermissions(ld.getUid(), ld.getOwner(), cn);
if (mp.canRead(p)) {
try (PreparedStatement ps1 = cn.prepareStatement("SELECT uid, parentRef, " + "ownerId, datatype, ldName, createDate, modifiedDate, ldLength, " + "contentTypesStr, pdriGroupRef, isSupervised, checksum, lastValidationDate, " + "lockTokenID, lockScope, lockType, lockedByUser, lockDepth, lockTimeout, " + "description, locationPreference, status " + "FROM ldata_table WHERE (parentRef = ?) " + "AND (? OR (isSupervised = ?)) " + "AND (? OR (createDate BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?))) " + "AND (? OR (createDate >= FROM_UNIXTIME(?))) " + "AND (? OR (createDate <= FROM_UNIXTIME(?))) " + "AND (? OR (modifiedDate BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?))) " + "AND (? OR (modifiedDate >= FROM_UNIXTIME(?))) " + "AND (? OR (modifiedDate <= FROM_UNIXTIME(?))) " + "AND (? OR (ldName LIKE CONCAT('%', ? , '%')))" + "LIMIT ?");
PreparedStatement ps2 = cn.prepareStatement("SELECT uid, ownerId, " + "ldName FROM ldata_table WHERE parentRef = ? AND datatype = '" + Constants.LOGICAL_FOLDER + "'")) {
{
if (queryParameters.containsKey("name") && queryParameters.get("name").iterator().hasNext()) {
String name = queryParameters.get("name").iterator().next();
ps1.setBoolean(18, false);
ps1.setString(19, name);
addFlag &= ld.getName().contains(name);
} else {
ps1.setBoolean(18, true);
ps1.setString(19, "");
}
if (queryParameters.containsKey("cStartDate") && queryParameters.get("cStartDate").iterator().hasNext() && queryParameters.containsKey("cEndDate") && queryParameters.get("cEndDate").iterator().hasNext()) {
long cStartDate = Long.valueOf(queryParameters.get("cStartDate").iterator().next());
long cEndDate = Long.valueOf(queryParameters.get("cEndDate").iterator().next());
ps1.setBoolean(4, false);
ps1.setBoolean(7, true);
ps1.setBoolean(9, true);
ps1.setLong(5, cStartDate);
ps1.setLong(6, cEndDate);
ps1.setLong(8, 0);
ps1.setLong(10, 0);
addFlag &= (ld.getCreateDate() >= cStartDate * 1000) && (ld.getCreateDate() <= cEndDate * 1000);
} else if (queryParameters.containsKey("cStartDate") && queryParameters.get("cStartDate").iterator().hasNext()) {
long cStartDate = Long.valueOf(queryParameters.get("cStartDate").iterator().next());
ps1.setBoolean(4, true);
ps1.setBoolean(7, false);
ps1.setBoolean(9, true);
ps1.setLong(5, 0);
ps1.setLong(6, 0);
ps1.setLong(8, cStartDate);
ps1.setLong(10, 0);
addFlag &= (ld.getCreateDate() >= cStartDate * 1000);
} else if (queryParameters.containsKey("cEndDate") && queryParameters.get("cEndDate").iterator().hasNext()) {
long cEndDate = Long.valueOf(queryParameters.get("cEndDate").iterator().next());
ps1.setBoolean(4, true);
ps1.setBoolean(7, true);
ps1.setBoolean(9, false);
ps1.setLong(5, 0);
ps1.setLong(6, 0);
ps1.setLong(8, 0);
ps1.setLong(10, cEndDate);
addFlag &= (ld.getCreateDate() <= cEndDate * 1000);
} else {
ps1.setBoolean(4, true);
ps1.setBoolean(7, true);
ps1.setBoolean(9, true);
ps1.setLong(5, 0);
ps1.setLong(6, 0);
ps1.setLong(8, 0);
ps1.setLong(10, 0);
}
if (queryParameters.containsKey("mStartDate") && queryParameters.get("mStartDate").iterator().hasNext() && queryParameters.containsKey("mEndDate") && queryParameters.get("mEndDate").iterator().hasNext()) {
long mStartDate = Long.valueOf(queryParameters.get("mStartDate").iterator().next());
long mEndDate = Long.valueOf(queryParameters.get("mEndDate").iterator().next());
ps1.setBoolean(11, false);
ps1.setBoolean(14, true);
ps1.setBoolean(16, true);
ps1.setLong(12, mStartDate);
ps1.setLong(13, mEndDate);
ps1.setLong(15, 0);
ps1.setLong(17, 0);
addFlag &= (ld.getModifiedDate() >= mStartDate * 1000) && (ld.getModifiedDate() <= mEndDate * 1000);
} else if (queryParameters.containsKey("mStartDate") && queryParameters.get("mStartDate").iterator().hasNext()) {
long mStartDate = Long.valueOf(queryParameters.get("mStartDate").iterator().next());
ps1.setBoolean(11, true);
ps1.setBoolean(14, false);
ps1.setBoolean(16, true);
ps1.setLong(12, 0);
ps1.setLong(13, 0);
ps1.setLong(15, mStartDate);
ps1.setLong(17, 0);
addFlag &= (ld.getModifiedDate() >= mStartDate * 1000);
} else if (queryParameters.containsKey("mEndDate") && queryParameters.get("mEndDate").iterator().hasNext()) {
long mEndDate = Long.valueOf(queryParameters.get("mEndDate").iterator().next());
ps1.setBoolean(11, true);
ps1.setBoolean(14, true);
ps1.setBoolean(16, false);
ps1.setLong(12, 0);
ps1.setLong(13, 0);
ps1.setLong(15, 0);
ps1.setLong(17, mEndDate);
addFlag &= (ld.getModifiedDate() <= mEndDate * 1000);
} else {
ps1.setBoolean(11, true);
ps1.setBoolean(14, true);
ps1.setBoolean(16, true);
ps1.setLong(12, 0);
ps1.setLong(13, 0);
ps1.setLong(15, 0);
ps1.setLong(17, 0);
}
if (queryParameters.containsKey("isSupervised") && queryParameters.get("isSupervised").iterator().hasNext()) {
boolean isSupervised = Boolean.valueOf(queryParameters.get("isSupervised").iterator().next());
ps1.setBoolean(2, false);
ps1.setBoolean(3, isSupervised);
addFlag &= (ld.getSupervised() == isSupervised);
} else {
ps1.setBoolean(2, true);
ps1.setBoolean(3, true);
}
if (addFlag) {
LogicalDataWrapped ldw = new LogicalDataWrapped();
ldw.setGlobalID(getCatalogue().getGlobalID(ld.getUid(), cn));
ldw.setLogicalData(ld);
ldw.setPath(rootPath);
ldw.setPermissions(p);
ldw.setUid(ld.getUid());
List<PDRIDescr> pdriDescr = getCatalogue().getPdriDescrByGroupId(ld.getPdriGroupId(), cn);
if (mp.isAdmin() && pdriDescr != null) {
for (PDRIDescr pdri : pdriDescr) {
if (pdri.getResourceUrl().startsWith("lfc") || pdri.getResourceUrl().startsWith("srm") || pdri.getResourceUrl().startsWith("gftp")) {
pdriDescr.remove(pdri);
GridHelper.initGridProxy(pdri.getUsername(), pdri.getPassword(), null, false);
pdri.setPassword(GridHelper.getProxyAsBase64String());
pdriDescr.add(pdri);
}
}
} else if (pdriDescr != null) {
for (PDRIDescr pdri : pdriDescr) {
pdriDescr.remove(pdri);
pdri.setPassword(null);
pdri.setUsername(null);
pdri.setKey(null);
pdri.setId(null);
pdri.setPdriGroupRef(null);
pdri.setStorageSiteId(null);
pdriDescr.add(pdri);
}
}
ldw.setPdriList(pdriDescr);
logicalDataWrappedList.add(ldw);
rowLimit--;
}
if (rowLimit != 0) {
logicalDataWrappedList.addAll(queryLogicalData(new MyData(ld.getUid(), rootPath.equals("/") ? "" : rootPath), rowLimit, ps1, ps2, mp, cn));
}
}
}
}
return logicalDataWrappedList;
}
use of nl.uva.cs.lobcder.resources.LogicalData in project lobcder by skoulouzis.
the class Items method queryLogicalData.
private List<LogicalDataWrapped> queryLogicalData(MyData myData, int limit, PreparedStatement ps1, PreparedStatement ps2, MyPrincipal mp, Connection cn) throws Exception {
List<LogicalDataWrapped> ldwl = new LinkedList<>();
Queue<MyData> dirs = new LinkedList<>();
dirs.offer(myData);
MyData dir;
while ((dir = dirs.poll()) != null) {
ps1.setLong(1, dir.getUid());
ps1.setInt(20, limit + 1);
try (ResultSet resultSet = ps1.executeQuery()) {
while (resultSet.next()) {
Long uid = resultSet.getLong(1);
String datatype = resultSet.getString(4);
String ldName = resultSet.getString(5);
String owner = resultSet.getString(3);
Permissions p = getCatalogue().getPermissions(uid, owner, cn);
if (mp.canRead(p) && uid != 1) {
LogicalData logicalData = new LogicalData();
logicalData.setUid(uid);
logicalData.setParentRef(dir.getUid());
logicalData.setOwner(owner);
logicalData.setType(datatype);
logicalData.setName(ldName);
logicalData.setCreateDate(resultSet.getTimestamp(6).getTime());
logicalData.setModifiedDate(resultSet.getTimestamp(7).getTime());
logicalData.setLength(resultSet.getLong(8));
logicalData.setContentTypesAsString(resultSet.getString(9));
logicalData.setPdriGroupId(resultSet.getLong(10));
logicalData.setSupervised(resultSet.getBoolean(11));
logicalData.setChecksum(resultSet.getString(12));
logicalData.setLastValidationDate(resultSet.getLong(13));
logicalData.setLockTokenID(resultSet.getString(14));
logicalData.setLockScope(resultSet.getString(15));
logicalData.setLockType(resultSet.getString(16));
logicalData.setLockedByUser(resultSet.getString(17));
logicalData.setLockDepth(resultSet.getString(18));
logicalData.setLockTimeout(resultSet.getLong(19));
logicalData.setDescription(resultSet.getString(20));
// logicalData.setDataLocationPreference(resultSet.getString(21));
logicalData.setStatus(resultSet.getString(22));
LogicalDataWrapped ldw = new LogicalDataWrapped();
ldw.setGlobalID(getCatalogue().getGlobalID(uid, cn));
ldw.setLogicalData(logicalData);
ldw.setPermissions(p);
ldw.setPath(dir.getPath().concat("/").concat(logicalData.getName()));
if (!logicalData.isFolder() && mp.isAdmin()) {
List<PDRIDescr> pdriDescr = getCatalogue().getPdriDescrByGroupId(logicalData.getPdriGroupId(), cn);
for (PDRIDescr pdri : pdriDescr) {
if (pdri.getResourceUrl().startsWith("lfc") || pdri.getResourceUrl().startsWith("srm") || pdri.getResourceUrl().startsWith("gftp")) {
pdriDescr.remove(pdri);
GridHelper.initGridProxy(pdri.getUsername(), pdri.getPassword(), null, false);
pdri.setPassword(GridHelper.getProxyAsBase64String());
pdriDescr.add(pdri);
}
}
ldw.setPdriList(pdriDescr);
}
ldwl.add(ldw);
limit--;
}
if (limit == 0) {
break;
}
}
}
if (limit != 0) {
ps2.setLong(1, dir.getUid());
try (ResultSet resultSet = ps2.executeQuery()) {
while (resultSet.next()) {
Long myUid = resultSet.getLong(1);
String myOwner = resultSet.getString(2);
String myPath = dir.getPath().concat("/").concat(resultSet.getString(3));
Permissions p = getCatalogue().getPermissions(myUid, myOwner, cn);
if (mp.canRead(p) && myUid != 1) {
dirs.offer(new MyData(myUid, myPath));
}
}
}
} else {
break;
}
}
return ldwl;
}
use of nl.uva.cs.lobcder.resources.LogicalData in project lobcder by skoulouzis.
the class PermissionsResource method addPermissionsRecursive.
@Path("recursive/{uid}/")
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public UIDS addPermissionsRecursive(@PathParam("uid") Long uid_p, @DefaultValue("False") @QueryParam("getall") Boolean getall, JAXBElement<Permissions> jbPermissions) {
UIDS result = new UIDS();
try (Connection connection = catalogue.getConnection()) {
try {
Permissions permissions = jbPermissions.getValue();
MyPrincipal principal = (MyPrincipal) request.getAttribute("myprincipal");
LogicalData ld = catalogue.getLogicalDataByUid(uid_p, connection);
Stack<Long> folders = new Stack<>();
ArrayList<Long> elements = new ArrayList<>();
ArrayList<Long> changeOwner = new ArrayList<>();
Permissions p = catalogue.getPermissions(ld.getUid(), ld.getOwner(), connection);
if (ld.isFolder() && principal.canRead(p)) {
folders.add(ld.getUid());
}
if (principal.canWrite(p)) {
elements.add(ld.getUid());
if (permissions.getOwner() != null && !ld.getOwner().equals(permissions.getOwner())) {
changeOwner.add(ld.getUid());
}
}
try (PreparedStatement ps = connection.prepareStatement("SELECT uid, ownerId, datatype FROM ldata_table WHERE parentRef = ?")) {
while (!folders.isEmpty()) {
Long curUid = folders.pop();
ps.setLong(1, curUid);
try (ResultSet resultSet = ps.executeQuery()) {
while (resultSet.next()) {
Long entry_uid = resultSet.getLong(1);
String entry_owner = resultSet.getString(2);
String entry_datatype = resultSet.getString(3);
Permissions entry_p = catalogue.getPermissions(entry_uid, entry_owner, connection);
if (entry_datatype.equals(Constants.LOGICAL_FOLDER) && principal.canRead(entry_p)) {
folders.push(entry_uid);
}
if (principal.canWrite(entry_p)) {
elements.add(entry_uid);
if (permissions.getOwner() != null && !entry_owner.equals(permissions.getOwner())) {
changeOwner.add(entry_uid);
}
}
}
}
}
}
try (PreparedStatement ps = connection.prepareStatement("SELECT permType, roleName, ldUidRef, id FROM permission_table WHERE permission_table.ldUidRef = ?", java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE)) {
for (Long uid : elements) {
ps.setLong(1, uid);
ResultSet rs = ps.executeQuery();
Set<String> read = new HashSet<>(permissions.getRead());
Set<String> write = new HashSet<>(permissions.getWrite());
while (rs.next()) {
String permType = rs.getString(1);
String roleName = rs.getString(2);
if (permType.equals("read")) {
read.remove(roleName);
} else if (permType.equals("write")) {
write.remove(roleName);
}
}
for (String role : read) {
rs.moveToInsertRow();
rs.updateString(1, "read");
rs.updateString(2, role);
rs.updateLong(3, uid);
rs.insertRow();
}
for (String role : write) {
rs.moveToInsertRow();
rs.updateString(1, "write");
rs.updateString(2, role);
rs.updateLong(3, uid);
rs.insertRow();
}
if (getall || !read.isEmpty() || !write.isEmpty()) {
String myuid = catalogue.getGlobalID(uid, connection);
if (myuid != null) {
result.uids.add(myuid);
}
}
}
}
if (permissions.getOwner() != null && !permissions.getOwner().isEmpty()) {
try (PreparedStatement ps = connection.prepareStatement("SELECT ownerId, uid from ldata_table WHERE uid = ?", java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE)) {
for (Long uid : changeOwner) {
ps.setLong(1, uid);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
rs.updateString(1, permissions.getOwner());
rs.updateRow();
if (!getall) {
result.uids.add(catalogue.getGlobalID(uid, connection));
}
}
}
}
}
connection.commit();
return result;
} catch (SQLException ex) {
Logger.getLogger(PermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
connection.rollback();
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
} catch (SQLException ex) {
Logger.getLogger(PermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
}
Aggregations