use of io.milton.resource.Resource in project lobcder by skoulouzis.
the class UnlockHandler method process.
@Override
public void process(HttpManager httpManager, Request request, Response response) throws ConflictException, NotAuthorizedException, BadRequestException {
// resourceHandlerHelper.process(httpManager, request, response, this);
String host = request.getHostHeader();
String url = HttpManager.decodeUrl(request.getAbsolutePath());
// Find a resource if it exists
Resource r = httpManager.getResourceFactory().getResource(host, url);
if (r != null) {
processExistingResource(httpManager, request, response, r);
} else {
// log.debug( "lock target doesnt exist, attempting lock null.." );
// processNonExistingResource( httpManager, request, response, host, url );
}
}
use of io.milton.resource.Resource 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.resource.Resource 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");
}
}
use of io.milton.resource.Resource in project lobcder by skoulouzis.
the class PutHelper method findNearestParent.
public CollectionResource findNearestParent(HttpManager manager, String host, Path path) throws NotAuthorizedException, ConflictException, BadRequestException {
if (path == null) {
return null;
}
Resource thisResource = manager.getResourceFactory().getResource(host, path.toString());
if (thisResource != null) {
if (thisResource instanceof CollectionResource) {
return (CollectionResource) thisResource;
} else {
log.warn("parent is not a collection: " + path);
return null;
}
}
CollectionResource parent = findNearestParent(manager, host, path.getParent());
return parent;
}
use of io.milton.resource.Resource in project lobcder by skoulouzis.
the class CookieAuthenticationHandler method authenticate.
@Override
public Object authenticate(Resource resource, Request request) {
// If there is a delegating handler which supports the request then we MUST use it
// This would have been selected in the supports method
AuthenticationHandler delegateHandler = (AuthenticationHandler) request.getAttributes().get(HANDLER_ATT_NAME);
if (delegateHandler != null) {
if (log.isTraceEnabled()) {
log.trace("authenticate: use delegateHandler: " + delegateHandler);
}
// Attempt to authenticate against wrapped handler
// If successful generate a signed cookie and put into a request attribute
log.info("use handler: " + delegateHandler);
Object tag = delegateHandler.authenticate(resource, request);
if (tag != null) {
if (tag instanceof DiscretePrincipal) {
DiscretePrincipal p = (DiscretePrincipal) tag;
setLoginCookies(p, request);
log.trace("authentication passed by delegated handler, persisted userUrl to cookie");
} else {
log.warn("auth.tag is not a " + DiscretePrincipal.class + ", is: " + tag);
}
return tag;
} else {
log.info("Login failed by delegated handler: " + delegateHandler.getClass());
return null;
}
} else {
log.info("no delegating handler");
// via a cookie, or this is an anonymous request
if (isLogout(request)) {
log.trace("authenticate: is logout");
return null;
} else {
String userUrl = getUserUrl(request);
log.info("userurl: " + userUrl);
if (userUrl == null) {
log.trace("authenticate: no userUrl in request or cookie, nothing to di");
// no token in request, so is anonymous
return null;
} else {
if (log.isTraceEnabled()) {
log.trace("authenticate: userUrl=" + userUrl);
}
// we found a userUrl
String host = request.getHostHeader();
Resource r;
try {
r = principalResourceFactory.getResource(host, userUrl);
log.info("found current user: " + r);
} catch (NotAuthorizedException ex) {
log.error("Couldnt check userUrl in cookie", ex);
r = null;
} catch (BadRequestException ex) {
log.error("Couldnt check userUrl in cookie", ex);
r = null;
}
if (r == null) {
log.warn("User not found host: " + host + " userUrl: " + userUrl + " with resourcefactory: " + principalResourceFactory);
clearCookieValue(HttpManager.response());
} else {
// which case we need to set cookies
if (request.getParams() != null && request.getParams().containsKey(cookieUserUrlValue)) {
if (r instanceof DiscretePrincipal) {
DiscretePrincipal dp = (DiscretePrincipal) r;
setLoginCookies(dp, request);
} else {
log.warn("Found user from request, but user object is not expected type. Should be " + DiscretePrincipal.class + " but is " + r.getClass());
}
} else {
log.trace("Do not set cookies, because token did not come from request variable");
}
}
return r;
}
}
}
}
Aggregations