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;
}
}
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;
}
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();
}
}
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;
}
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();
}
Aggregations