Search in sources :

Example 36 with BadRequestException

use of io.milton.http.exceptions.BadRequestException in project lobcder by skoulouzis.

the class PutHelper method parseContentRange.

/**
 * Largly copied from tomcat
 *
 * See the spec
 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
 *
 * @param r
 * @param request
 * @return
 * @throws IOException
 * @throws BadRequestException - if the range header is invalid
 */
public Range parseContentRange(Resource r, Request request) throws IOException, BadRequestException {
    // Retrieving the content-range header (if any is specified
    String rangeHeader = request.getContentRangeHeader();
    if (rangeHeader == null) {
        return null;
    }
    // bytes is the only range unit supported
    if (!rangeHeader.startsWith("bytes")) {
        log.warn("Invalid range header, does not start with 'bytes': " + rangeHeader);
        throw new BadRequestException(r);
    }
    rangeHeader = rangeHeader.substring(6).trim();
    int dashPos = rangeHeader.indexOf('-');
    int slashPos = rangeHeader.indexOf('/');
    if (dashPos == -1) {
        log.warn("Invalid range header, dash not found: " + rangeHeader);
        throw new BadRequestException(r);
    }
    if (slashPos == -1) {
        log.warn("Invalid range header, slash not found: " + rangeHeader);
        throw new BadRequestException(r);
    }
    String s;
    long start;
    s = rangeHeader.substring(0, dashPos);
    try {
        start = Long.parseLong(s);
    } catch (NumberFormatException e) {
        log.warn("Invalid range header, start is not a valid number: " + s + " Raw header:" + rangeHeader);
        throw new BadRequestException(r);
    }
    long finish;
    s = rangeHeader.substring(dashPos + 1, slashPos);
    try {
        finish = Long.parseLong(s);
    } catch (NumberFormatException e) {
        log.warn("Invalid range header, finish is not a valid number: " + s + " Raw header:" + rangeHeader);
        throw new BadRequestException(r);
    }
    Range range = new Range(start, finish);
    if (!validate(range)) {
        throw new BadRequestException(r);
    }
    return range;
}
Also used : BadRequestException(io.milton.http.exceptions.BadRequestException) Range(io.milton.http.Range)

Example 37 with BadRequestException

use of io.milton.http.exceptions.BadRequestException 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

BadRequestException (io.milton.http.exceptions.BadRequestException)37 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)25 IOException (java.io.IOException)15 Resource (io.milton.resource.Resource)11 ConflictException (io.milton.http.exceptions.ConflictException)10 SQLException (java.sql.SQLException)10 Connection (java.sql.Connection)9 Path (io.milton.common.Path)6 NotFoundException (io.milton.http.exceptions.NotFoundException)6 Permissions (nl.uva.cs.lobcder.auth.Permissions)6 CollectionResource (io.milton.resource.CollectionResource)5 URISyntaxException (java.net.URISyntaxException)5 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)5 ReplaceableResource (io.milton.resource.ReplaceableResource)4 BufferingOutputStream (io.milton.common.BufferingOutputStream)3 Range (io.milton.http.Range)3 PreConditionFailedException (io.milton.http.exceptions.PreConditionFailedException)3 GetableResource (io.milton.resource.GetableResource)3 PostableResource (io.milton.resource.PostableResource)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3