Search in sources :

Example 31 with Resource

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 );
    }
}
Also used : LockableResource(io.milton.resource.LockableResource) Resource(io.milton.resource.Resource)

Example 32 with Resource

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);
        }
    }
}
Also used : GetableResource(io.milton.resource.GetableResource) Resource(io.milton.resource.Resource) BadRequestException(io.milton.http.exceptions.BadRequestException) GetableResource(io.milton.resource.GetableResource) NotFoundException(io.milton.http.exceptions.NotFoundException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Example 33 with Resource

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");
    }
}
Also used : Path(io.milton.common.Path) CollectionResource(io.milton.resource.CollectionResource) ConflictException(io.milton.http.exceptions.ConflictException) CopyableResource(io.milton.resource.CopyableResource) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource) PostableResource(io.milton.resource.PostableResource) BadRequestException(io.milton.http.exceptions.BadRequestException)

Example 34 with 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;
}
Also used : CollectionResource(io.milton.resource.CollectionResource) GetableResource(io.milton.resource.GetableResource) CollectionResource(io.milton.resource.CollectionResource) Resource(io.milton.resource.Resource)

Example 35 with Resource

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;
            }
        }
    }
}
Also used : DiscretePrincipal(io.milton.principal.DiscretePrincipal) Resource(io.milton.resource.Resource) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Aggregations

Resource (io.milton.resource.Resource)37 CollectionResource (io.milton.resource.CollectionResource)23 BadRequestException (io.milton.http.exceptions.BadRequestException)11 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)10 Path (io.milton.common.Path)9 MakeCollectionableResource (io.milton.resource.MakeCollectionableResource)8 PutableResource (io.milton.resource.PutableResource)8 GetableResource (io.milton.resource.GetableResource)7 ConflictException (io.milton.http.exceptions.ConflictException)6 ReplaceableResource (io.milton.resource.ReplaceableResource)6 DeletableResource (io.milton.resource.DeletableResource)5 PostableResource (io.milton.resource.PostableResource)4 IOException (java.io.IOException)4 PutEvent (io.milton.event.PutEvent)3 LockableResource (io.milton.resource.LockableResource)3 PropFindableResource (io.milton.resource.PropFindableResource)3 NewFolderEvent (io.milton.event.NewFolderEvent)2 CopyableResource (io.milton.resource.CopyableResource)2 LockingCollectionResource (io.milton.resource.LockingCollectionResource)2 MoveableResource (io.milton.resource.MoveableResource)2