use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.
the class WebDataDirResource method delete.
@Override
public void delete() throws NotAuthorizedException, ConflictException, BadRequestException {
// Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "delete() for {0}", getPath());
if (getPath().isRoot()) {
throw new ConflictException(this, "Cannot delete root");
}
try (Connection connection = getCatalogue().getConnection()) {
try {
getCatalogue().remove(getLogicalData(), getPrincipal(), connection);
connection.commit();
connection.close();
} catch (SQLException e) {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e);
if (connection != null && !connection.isClosed()) {
connection.rollback();
connection.close();
}
throw new BadRequestException(this, e.getMessage());
}
} catch (SQLException e1) {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e1);
throw new BadRequestException(this, e1.getMessage());
}
}
use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.
the class WebDataDirResource method createCollection.
@Override
public CollectionResource createCollection(String newName) throws NotAuthorizedException, ConflictException, BadRequestException {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "createCollection {0} in {1}", new Object[] { newName, getPath() });
try (Connection connection = getCatalogue().getConnection()) {
try {
Path newCollectionPath = Path.path(getPath(), newName);
Long newFolderEntryId = getCatalogue().getLogicalDataUidByParentRefAndName(getLogicalData().getUid(), newName, connection);
if (newFolderEntryId != null) {
throw new ConflictException(this, newName);
} else {
// collection does not exists, create a new one
// newCollectionPath, Constants.LOGICAL_FOLDER,
LogicalData newFolderEntry = new LogicalData();
newFolderEntry.setType(Constants.LOGICAL_FOLDER);
newFolderEntry.setParentRef(getLogicalData().getUid());
newFolderEntry.setName(newName);
newFolderEntry.setCreateDate(System.currentTimeMillis());
newFolderEntry.setModifiedDate(System.currentTimeMillis());
newFolderEntry.setLastAccessDate(System.currentTimeMillis());
newFolderEntry.setTtlSec(getLogicalData().getTtlSec());
newFolderEntry.setOwner(getPrincipal().getUserId());
getCatalogue().setPermissions(getCatalogue().registerDirLogicalData(newFolderEntry, connection).getUid(), new Permissions(getPrincipal(), getPermissions()), connection);
newFolderEntry = inheritProperties(newFolderEntry, connection);
connection.commit();
connection.close();
WebDataDirResource res = new WebDataDirResource(newFolderEntry, newCollectionPath, getCatalogue(), authList);
return res;
}
} catch (SQLException e) {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e);
if (connection != null && !connection.isClosed()) {
connection.rollback();
connection.close();
}
throw new BadRequestException(this, e.getMessage());
}
} catch (SQLException e1) {
Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e1);
throw new BadRequestException(this, e1.getMessage());
}
}
use of io.milton.http.exceptions.ConflictException 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.ConflictException in project lobcder by skoulouzis.
the class FckQuickUploaderResource method processFileUpload.
private void processFileUpload(FileItem f, Map<String, String> params) throws BadRequestException, NotAuthorizedException {
CollectionResource target = null;
if (wrappedResource == null) {
throw new BadRequestException(this, "collection not found");
}
target = (CollectionResource) wrappedResource.child("uploads");
if (target == null) {
try {
if (wrappedResource instanceof MakeCollectionableResource) {
MakeCollectionableResource mk = (MakeCollectionableResource) wrappedResource;
target = mk.createCollection("uploads");
} else {
throw new BadRequestException(target, "Cant create subfolder");
}
} catch (ConflictException ex) {
throw new RuntimeException(ex);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
}
}
String name = FileUtils.sanitiseName(f.getName());
log.debug("processFileUpload: " + name);
boolean isFirst = true;
String newName = null;
while (target.child(name) != null) {
name = FileUtils.incrementFileName(name, isFirst);
newName = name;
isFirst = false;
}
long size = f.getSize();
try {
if (target instanceof PutableResource) {
PutableResource putable = (PutableResource) target;
Resource newRes = putable.createNew(name, f.getInputStream(), size, null);
if (newRes != null) {
log.trace("created: " + newRes.getName() + " of type: " + newRes.getClass());
} else {
log.trace("createNew returned null");
}
} else {
throw new BadRequestException(target, "Does not implement PutableResource");
}
} catch (ConflictException ex) {
throw new RuntimeException(ex);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
try {
if (newName != null) {
// we renamed the file
uploadResponseOk(name);
} else {
uploadResponseOk();
}
} catch (Throwable ex) {
log.error("Exception saving new file", ex);
uploadResponseFailed(ex.getMessage());
}
}
use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.
the class PutJsonResource method getName.
/**
* We dont return anything, so best not use json
*
* @param accepts
* @return
*/
// @Override
// public String getContentType(String accepts) {
// return "text/html";
// }
private String getName(String filename, Map<String, String> parameters) throws ConflictException, NotAuthorizedException, BadRequestException {
String initialName = filename;
if (parameters.containsKey(PARAM_NAME)) {
initialName = parameters.get(PARAM_NAME);
}
boolean nonBlankName = initialName != null && initialName.trim().length() > 0;
boolean autoname = (parameters.get(PARAM_AUTONAME) != null);
boolean overwrite = (parameters.get(PARAM_OVERWRITE) != null);
if (nonBlankName) {
Resource child = wrapped.child(initialName);
if (child == null) {
log.trace("no existing file with that name");
return initialName;
} else {
if (overwrite) {
log.trace("file exists, and overwrite parameters is set, so allow overwrite: " + initialName);
return initialName;
} else {
if (!autoname) {
log.warn("Conflict: Can't create resource with name " + initialName + " because it already exists. To rename automatically use request parameter: " + PARAM_AUTONAME + ", or to overwrite use " + PARAM_OVERWRITE);
throw new ConflictException(this);
} else {
log.trace("file exists and autoname is set, so will find acceptable name");
}
}
}
} else {
initialName = getDateAsName("upload");
log.trace("no name given in request");
}
return findAcceptableName(initialName);
}
Aggregations