use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.
the class WebDataDirResource method sendContent.
@Override
public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotAuthorizedException, BadRequestException {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "sendContent({0}) for {1}", new Object[] { contentType, getPath() });
try (Connection connection = getCatalogue().getConnection()) {
try (PrintStream ps = new PrintStream(out)) {
HtmlCanvas html = new HtmlCanvas();
html.table(border("1")).tr().th().content("Name").th().content("Size").th().content("Modification Date").th().content("Creation Date").th().content("Owner").th().content("Content Type").th().content("Type").th().content("Is Supervised").th().content("Uid");
String ref;
for (LogicalData ld : getCatalogue().getChildrenByParentRef(getLogicalData().getUid(), connection)) {
if (ld.isFolder()) {
ref = "../dav" + getPath() + "/" + ld.getName();
// if (ld.getUid() != 1) {
// } else {
// }
} else {
ref = "../dav" + getPath() + "/" + ld.getName();
}
html._tr().tr().td().a(href(ref)).img(src("").alt(ld.getName()))._a()._td().td().content(String.valueOf(ld.getLength())).td().content(new Date(ld.getModifiedDate()).toString()).td().content(new Date(ld.getCreateDate()).toString()).td().content(ld.getOwner()).td().content(ld.getContentTypesAsString()).td().content(ld.getType()).td().content(ld.getSupervised().toString()).td().content(ld.getUid().toString());
}
html._tr()._table();
ps.println(html.toHtml());
getCatalogue().addViewForRes(getLogicalData().getUid(), connection);
connection.commit();
connection.close();
} catch (Exception e) {
if (connection != null && !connection.isClosed()) {
connection.rollback();
connection.close();
}
throw e;
}
} catch (Exception e) {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e);
throw new BadRequestException(this);
}
}
use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.
the class WebDataFileResource method copyTo.
@Override
public void copyTo(CollectionResource collectionResource, String name) throws NotAuthorizedException, BadRequestException, ConflictException {
WebDataDirResource toWDDR = (WebDataDirResource) collectionResource;
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "copyTo(''{0}'', ''{1}'') for {2}", new Object[] { toWDDR.getPath(), name, getPath() });
try (Connection connection = getCatalogue().getConnection()) {
try {
Permissions newParentPerm = getCatalogue().getPermissions(toWDDR.getLogicalData().getUid(), toWDDR.getLogicalData().getOwner(), connection);
if (!getPrincipal().canWrite(newParentPerm)) {
throw new NotAuthorizedException(this);
}
getCatalogue().copyFile(getLogicalData(), toWDDR.getLogicalData(), name, getPrincipal(), connection);
connection.commit();
} catch (SQLException | NotAuthorizedException 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 io.milton.http.exceptions.NotAuthorizedException 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 io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.
the class WebDataResource method checkRedirect.
@Override
public String checkRedirect(Request request) throws NotAuthorizedException, BadRequestException {
switch(request.getMethod()) {
case PUT:
case POST:
if (!redirectPosts) {
return null;
}
String redirect = null;
try {
if (!canRedirect(request)) {
return null;
}
Map<Long, Pair<WebDataFileResource, Long>> resources = createResouses(request);
// lockResources(resources);
Map<String, Pair<Long, Collection<Long>>> storageMap = getStorageMap(resources);
StringBuilder sb = new StringBuilder();
Set<String> keys = storageMap.keySet();
for (String k : keys) {
sb.append("file_name=").append(k).append("/");
Pair pair = storageMap.get(k);
Long fileUid = (Long) pair.getLeft();
sb.append("file_uid=").append(fileUid).append("/");
Long pdriGroupUid = resources.get(fileUid).getRight();
sb.append("pdrigroup_uid=").append(pdriGroupUid).append("/");
Collection<Long> ssids = (Collection<Long>) pair.getRight();
for (Long ssid : ssids) {
sb.append("ss_id=").append(ssid).append("/");
}
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
String folder = request.getAbsolutePath();
if (!folder.endsWith("/")) {
folder += "/";
}
redirect = "http://localhost:8080/lobcder-worker" + folder + "?" + sb.toString();
} catch (Exception ex) {
Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, ex);
}
return redirect;
default:
return null;
}
// return null;
}
use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.
the class WebDataResource method unlock.
@Override
public void unlock(String token) throws NotAuthorizedException, PreConditionFailedException {
try (Connection connection = getCatalogue().getConnection()) {
try {
String tokenID = getLogicalData().getLockTokenID();
if (tokenID == null || tokenID.length() <= 0) {
return;
} else {
if (tokenID.startsWith("<") && tokenID.endsWith(">") && !token.startsWith("<") && !token.endsWith(">")) {
StringBuilder sb = new StringBuilder();
sb.append("<").append(token).append(">");
token = sb.toString();
}
if (!tokenID.startsWith("<") && !tokenID.endsWith(">") && token.startsWith("<") && token.endsWith(">")) {
token = token.replaceFirst("<", "");
token = token.replaceFirst(">", "");
}
if (!tokenID.equals(token)) {
throw new PreConditionFailedException(this);
}
}
getCatalogue().setLockTokenID(getLogicalData().getUid(), null, connection);
connection.commit();
getLogicalData().setLockTokenID(null);
getLogicalData().setLockScope(null);
getLogicalData().setLockType(null);
getLogicalData().setLockedByUser(null);
getLogicalData().setLockDepth(null);
getLogicalData().setLockTimeout(null);
} catch (Exception ex) {
Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, ex);
connection.rollback();
throw new PreConditionFailedException(this);
}
} catch (SQLException e) {
Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, e);
throw new PreConditionFailedException(this);
}
}
Aggregations