use of io.milton.http.exceptions.BadRequestException 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.BadRequestException 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;
}
use of io.milton.http.exceptions.BadRequestException in project lobcder by skoulouzis.
the class ReportHandler method processExistingResource.
@Override
public void processExistingResource(HttpManager manager, Request request, Response response, Resource resource) throws NotAuthorizedException, BadRequestException, ConflictException {
try {
org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
org.jdom.Document doc = builder.build(request.getInputStream());
String reportName = doc.getRootElement().getName();
Report r = reports.get(reportName);
if (r == null) {
log.error("report not known: " + reportName);
throw new BadRequestException(resource);
} else {
log.trace("process report: " + reportName + " with : " + r.getClass());
String xml = r.process(request.getHostHeader(), request.getAbsolutePath(), resource, doc);
response.setStatus(Response.Status.SC_MULTI_STATUS);
response.setContentTypeHeader("text/xml");
response.setEntity(new ByteArrayEntity(xml.getBytes("UTF-8")));
}
} catch (JDOMException ex) {
java.util.logging.Logger.getLogger(ReportHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (ReadingException ex) {
throw new RuntimeException(ex);
} catch (WritingException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
use of io.milton.http.exceptions.BadRequestException in project lobcder by skoulouzis.
the class LoginResponseHandler method attemptRespondLoginPage.
private void attemptRespondLoginPage(Request request, Resource resource, Response response) throws RuntimeException {
Resource rLogin;
try {
rLogin = resourceFactory.getResource(request.getHostHeader(), loginPage);
} catch (NotAuthorizedException e) {
throw new RuntimeException(e);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
}
if (rLogin == null || !(rLogin instanceof GetableResource)) {
log.info("Couldnt find login resource: " + request.getHostHeader() + loginPage + " with resource factory: " + resourceFactory.getClass());
wrapped.respondUnauthorised(resource, response, request);
} else {
log.trace("respond with 200 to suppress login prompt, using resource: " + rLogin.getName() + " - " + rLogin.getClass());
try {
// set request attribute so rendering knows it authorisation failed, or authentication is required
Auth auth = request.getAuthorization();
if (auth != null && auth.getTag() != null) {
// no authentication was attempted,
request.getAttributes().put("authReason", "notPermitted");
} else {
request.getAttributes().put("authReason", "required");
}
GetableResource gr = (GetableResource) rLogin;
wrapped.respondContent(gr, response, request, null);
} catch (NotAuthorizedException ex) {
throw new RuntimeException(ex);
} catch (BadRequestException ex) {
throw new RuntimeException(ex);
} catch (NotFoundException ex) {
throw new RuntimeException(ex);
}
}
}
use of io.milton.http.exceptions.BadRequestException in project lobcder by skoulouzis.
the class CopyJsonResource method processForm.
public String processForm(Map<String, String> parameters, Map<String, FileItem> files) throws BadRequestException, NotAuthorizedException {
String dest = parameters.get("destination");
Path pDest = Path.path(dest);
Resource rDestParent = resourceFactory.getResource(host, pDest.getParent().toString());
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.copyTo(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());
throw new BadRequestException(rDestParent, "File already exists");
}
} else {
throw new BadRequestException(wrapped, "The destination parent is not a collection resource");
}
}
Aggregations