use of com.pspace.ifs.ksan.gw.api.S3Request in project ksan by infinistor.
the class GWHandler method doHandle.
public final void doHandle(Request baseRequest, HttpServletRequest request, HttpServletResponse response, InputStream is) throws GWException {
long requestSize = 0L;
String method = request.getMethod();
requestSize = method.length();
String uri = request.getRequestURI();
requestSize += uri.length();
long startTime = System.currentTimeMillis();
logger.info(GWConstants.LOG_GWHANDLER_PREURI, uri);
uri = removeDuplicateRoot(uri);
logger.info(GWConstants.LOG_GWHANDLER_URI, uri);
logger.info(GWConstants.LOG_GWHANDLER_CLIENT_ADDRESS, request.getRemoteAddr());
logger.info(GWConstants.LOG_GWHANDLER_CLIENT_HOST, request.getRemoteHost());
logger.info(GWConstants.LOG_GWHANDLER_METHOD, method);
for (String parameter : Collections.list(request.getParameterNames())) {
logger.info(GWConstants.LOG_GWHANDLER_PARAMETER, parameter, Strings.nullToEmpty(request.getParameter(parameter)));
requestSize += parameter.length();
if (!Strings.isNullOrEmpty(request.getParameter(parameter))) {
requestSize += request.getParameter(parameter).length();
}
}
for (String headerName : Collections.list(request.getHeaderNames())) {
for (String headerValue : Collections.list(request.getHeaders(headerName))) {
logger.info(GWConstants.LOG_GWHANDLER_HEADER, headerName, Strings.nullToEmpty(headerValue));
requestSize += headerName.length();
if (!Strings.isNullOrEmpty(headerValue)) {
requestSize += headerValue.length();
}
}
}
// make request id
String requestID = UUID.randomUUID().toString().substring(24).toUpperCase();
String[] path = uri.split(GWConstants.SLASH, 3);
try {
for (int i = 0; i < path.length; i++) {
path[i] = URLDecoder.decode(path[i], GWConstants.CHARSET_UTF_8);
logger.info(GWConstants.LOG_GWHANDLER_PATH, i, path[i]);
}
} catch (UnsupportedEncodingException e) {
PrintStack.logging(logger, e);
throw new GWException(GWErrorCode.BAD_REQUEST, null);
}
String pathCategory = GWConstants.EMPTY_STRING;
if (uri.equals(GWConstants.SLASH)) {
pathCategory = GWConstants.CATEGORY_ROOT;
} else if (path.length <= 2 || path[2].isEmpty()) {
pathCategory = GWConstants.CATEGORY_BUCKET;
} else {
pathCategory = GWConstants.CATEGORY_OBJECT;
}
S3Parameter s3Parameter = new S3Parameter();
s3Parameter.setRequestSize(requestSize);
s3Parameter.setRequestID(requestID);
s3Parameter.setRequest(request);
s3Parameter.setResponse(response);
s3Parameter.setInputStream(is);
if (!Strings.isNullOrEmpty(path[1])) {
s3Parameter.setBucketName(path[1]);
}
if (path.length == 3 && !Strings.isNullOrEmpty(path[2])) {
s3Parameter.setObjectName(path[2]);
}
s3Parameter.setMethod(method);
s3Parameter.setStartTime(startTime);
s3Parameter.setPathCategory(pathCategory);
s3Parameter.setMaxFileSize(maxFileSize);
s3Parameter.setMaxTimeSkew(maxTimeSkew);
s3Parameter.setRemoteHost(request.getRemoteHost());
s3Parameter.setRequestURI(request.getRequestURI());
s3Parameter.setReferer(request.getHeader(HttpHeaders.REFERER));
s3Parameter.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
s3Parameter.setAuthorization(request.getHeader(HttpHeaders.AUTHORIZATION));
s3Parameter.setxAmzAlgorithm(request.getParameter(GWConstants.X_AMZ_ALGORITHM));
s3Parameter.setHostName(request.getHeader(HttpHeaders.HOST));
s3Parameter.setHostID(request.getHeader(GWConstants.X_AMZ_ID_2));
s3Parameter.setRemoteAddr(!Strings.isNullOrEmpty(request.getHeader(GWConstants.X_FORWARDED_FOR)) ? request.getHeader(GWConstants.X_FORWARDED_FOR) : request.getRemoteAddr());
if (request.getHeader(HttpHeaders.AUTHORIZATION) == null && request.getParameter(GWConstants.X_AMZ_ALGORITHM) == null && request.getParameter(GWConstants.AWS_ACCESS_KEY_ID) == null) {
if (s3Parameter.getPathCategory().equals(GWConstants.CATEGORY_ROOT)) {
throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
}
S3Signing s3signing = new S3Signing(s3Parameter);
s3Parameter = s3signing.publicvalidation();
s3Parameter.setPublicAccess(true);
} else {
S3Signing s3signing = new S3Signing(s3Parameter);
s3Parameter = s3signing.validation();
s3Parameter.setPublicAccess(false);
}
logger.info(GWConstants.LOG_GWHANDLER_MOTHOD_CATEGORY, s3Parameter.getMethod(), s3Parameter.getPathCategory());
S3Request s3Request = s3RequestFactory.createS3Request(s3Parameter);
s3Request.process();
s3Parameter.setStatusCode(response.getStatus());
AsyncHandler.s3logging(s3Parameter);
}
Aggregations