use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.
the class MiltonFtpFile method createOutputStream.
@Override
public OutputStream createOutputStream(long offset) throws IOException {
log.debug("createOutputStream: " + offset);
final BufferingOutputStream out = new BufferingOutputStream(50000);
if (r instanceof ReplaceableResource) {
log.debug("resource is replaceable");
final ReplaceableResource rr = (ReplaceableResource) r;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
rr.replaceContent(out.getInputStream(), out.getSize());
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
} catch (ConflictException ex) {
throw new RuntimeException(ex);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
}
}
};
out.setOnClose(runnable);
return out;
} else {
CollectionResource col;
try {
col = getParent();
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
}
if (col == null) {
throw new IOException("parent not found");
} else if (col instanceof PutableResource) {
final PutableResource putableResource = (PutableResource) col;
final String newName = path.getName();
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
putableResource.createNew(newName, out.getInputStream(), out.getSize(), null);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (ConflictException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
};
out.setOnClose(runnable);
return out;
} else {
throw new IOException("folder doesnt support PUT, and the resource is not replaceable");
}
}
}
use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.
the class WebDataFileResource method delete.
@Override
public void delete() throws NotAuthorizedException, BadRequestException, ConflictException {
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "delete() file {0}", getPath());
try (Connection connection = getCatalogue().getConnection()) {
try {
getCatalogue().remove(getLogicalData(), getPrincipal(), 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 MoveHandler method processExistingResource.
@Override
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException {
MoveableResource r = (MoveableResource) resource;
Dest dest = Utils.getDecodedDestination(request.getDestinationHeader());
Resource rDest = manager.getResourceFactory().getResource(dest.host, dest.url);
log.debug("process: moving from: " + r.getName() + " -> " + dest.url + " with name: " + dest.name);
if (rDest == null) {
log.debug("process: destination parent does not exist: " + dest);
responseHandler.respondConflict(resource, response, request, "Destination parent does not exist: " + dest);
} else if (!(rDest instanceof CollectionResource)) {
log.debug("process: destination exists but is not a collection");
responseHandler.respondConflict(resource, response, request, "Destination exists but is not a collection: " + dest);
} else {
boolean wasDeleted = false;
CollectionResource colDest = (CollectionResource) rDest;
// check if the dest exists
Resource rExisting = colDest.child(dest.name);
if (rExisting != null) {
// check for overwrite header
if (!canOverwrite(request)) {
log.info("destination resource exists, and overwrite header is not set. dest name: " + dest.name + " dest folder: " + colDest.getName());
responseHandler.respondPreconditionFailed(request, response, rExisting);
return;
} else {
if (deleteExistingBeforeMove) {
if (rExisting instanceof DeletableResource) {
log.debug("deleting existing resource");
DeletableResource drExisting = (DeletableResource) rExisting;
if (deleteHelper.isLockedOut(request, drExisting)) {
log.debug("destination resource exists but is locked");
responseHandler.respondLocked(request, response, drExisting);
return;
}
log.debug("deleting pre-existing destination resource");
deleteHelper.delete(drExisting, manager.getEventManager());
wasDeleted = true;
} else {
log.warn("destination exists, and overwrite header is set, but destination is not a DeletableResource");
responseHandler.respondConflict(resource, response, request, "A resource exists at the destination, and it cannot be deleted");
return;
}
}
}
}
log.debug("process: moving resource to: " + rDest.getName());
try {
if (!handlerHelper.checkAuthorisation(manager, colDest, request, request.getMethod(), request.getAuthorization())) {
responseHandler.respondUnauthorised(colDest, response, request);
return;
}
manager.getEventManager().fireEvent(new MoveEvent(resource, colDest, dest.name));
r.moveTo(colDest, dest.name);
// See http://www.ettrema.com:8080/browse/MIL-87
if (wasDeleted) {
responseHandler.respondNoContent(resource, response, request);
} else {
responseHandler.respondCreated(resource, response, request);
}
} catch (ConflictException ex) {
log.warn("conflict", ex);
responseHandler.respondConflict(resource, response, request, dest.toString());
}
}
log.debug("process: finished");
}
use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.
the class MoveJsonResource method processForm.
@Override
public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws BadRequestException, NotAuthorizedException {
String dest = parameters.get("destination");
if (dest == null) {
throw new BadRequestException(this, "The destination parameter is null");
} else if (dest.trim().length() == 0) {
throw new BadRequestException(this, "The destination parameter is empty");
}
Path pDest = Path.path(dest);
if (pDest == null) {
throw new BadRequestException(this, "Couldnt parse the destination header, returned null from: " + dest);
}
String parentPath = "/";
if (pDest.getParent() != null) {
parentPath = pDest.getParent().toString();
}
Resource rDestParent = resourceFactory.getResource(host, parentPath);
if (rDestParent == null)
throw new BadRequestException(wrapped, "The destination parent does not exist");
if (rDestParent instanceof CollectionResource) {
CollectionResource colDestParent = (CollectionResource) rDestParent;
if (colDestParent.child(pDest.getName()) == null) {
try {
wrapped.moveTo(colDestParent, pDest.getName());
} catch (ConflictException ex) {
log.warn("Exception copying to: " + pDest.getName(), ex);
throw new BadRequestException(rDestParent, "conflict: " + ex.getMessage());
}
return null;
} else {
log.warn("destination already exists: " + pDest.getName() + " in folder: " + colDestParent.getName());
throw new BadRequestException(rDestParent, "File already exists");
}
} else {
throw new BadRequestException(wrapped, "The destination parent is not a collection resource");
}
}
use of io.milton.http.exceptions.ConflictException in project lobcder by skoulouzis.
the class PutJsonResource method processForm.
@Override
public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws ConflictException, NotAuthorizedException, BadRequestException {
log.info("processForm: " + wrapped.getClass());
if (files.isEmpty()) {
log.debug("no files uploaded");
return null;
}
newFiles = new ArrayList<NewFile>();
for (FileItem file : files.values()) {
NewFile nf = new NewFile();
String ua = HttpManager.request().getUserAgentHeader();
String f = Utils.truncateFileName(ua, file.getName());
nf.setOriginalName(f);
nf.setContentType(file.getContentType());
nf.setLength(file.getSize());
newFiles.add(nf);
String newName = getName(f, parameters);
log.info("creating resource: " + newName + " size: " + file.getSize());
InputStream in = null;
Resource newResource;
try {
in = file.getInputStream();
Resource existing = wrapped.child(newName);
if (existing != null) {
if (existing instanceof ReplaceableResource) {
log.trace("existing resource is replaceable, so replace content");
ReplaceableResource rr = (ReplaceableResource) existing;
rr.replaceContent(in, null);
log.trace("completed POST processing for file. Updated: " + existing.getName());
eventManager.fireEvent(new PutEvent(rr));
newResource = rr;
} else {
log.trace("existing resource is not replaceable, will be deleted");
if (existing instanceof DeletableResource) {
DeletableResource dr = (DeletableResource) existing;
dr.delete();
newResource = wrapped.createNew(newName, in, file.getSize(), file.getContentType());
log.trace("completed POST processing for file. Deleted, then created: " + newResource.getName());
eventManager.fireEvent(new PutEvent(newResource));
} else {
throw new BadRequestException(existing, "existing resource could not be deleted, is not deletable");
}
}
} else {
newResource = wrapped.createNew(newName, in, file.getSize(), file.getContentType());
log.info("completed POST processing for file. Created: " + newResource.getName());
eventManager.fireEvent(new PutEvent(newResource));
}
String newHref = buildNewHref(href, newResource.getName());
nf.setHref(newHref);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
} catch (ConflictException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException("Exception creating resource", ex);
} finally {
FileUtils.close(in);
}
}
log.trace("completed all POST processing");
return null;
}
Aggregations