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;
}
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;
}
}
}
}
Aggregations