boolean serveContent = content;
// Identify the requested resource path
String sessionId = extractSessionId(request);
RepositoryHttpEndpointImpl elem = repoHttpManager
.getHttpRepoItemElem(sessionId);
if (elem == null) {
if (debug > 0) {
log("Resource with sessionId '" + sessionId + "' not found");
}
response.sendError(SC_NOT_FOUND, request.getRequestURI());
return;
}
elem.fireStartedEventIfFirstTime();
RepositoryItem repositoryItem = elem.getRepositoryItem();
RepositoryItemAttributes attributes = repositoryItem.getAttributes();
if (debug > 0) {
if (serveContent) {
log("Serving resource with sessionId '"
+ sessionId
+ "' headers and data. This resource corresponds to repository item '"
+ repositoryItem.getId() + "'");
} else {
log("Serving resource with sessionId '"
+ sessionId
+ "' headers only. This resource corresponds to repository item '"
+ repositoryItem.getId() + "'");
}
}
boolean malformedRequest = response.getStatus() >= SC_BAD_REQUEST;
if (!malformedRequest && !checkIfHeaders(request, response, attributes)) {
return;
}
String contentType = getContentType(elem, attributes);
List<Range> ranges = null;
if (!malformedRequest) {
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("ETag", attributes.getETag());
response.setHeader("Last-Modified",
attributes.getLastModifiedHttp());
ranges = parseRange(request, response, attributes);
}
long contentLength = attributes.getContentLength();
// Special case for zero length files, which would cause a
// (silent) ISE when setting the output buffer size
if (contentLength == 0L) {
serveContent = false;
}
// Check to see if a Filter, Valve of wrapper has written some content.
// If it has, disable range requests and setting of a content length
// since neither can be done reliably.
boolean contentWritten = response.isCommitted();
if (contentWritten) {
ranges = FULL;
}
boolean noRanges = (ranges == null || ranges.isEmpty());
if (malformedRequest
|| (noRanges && request.getHeader("Range") == null)
|| ranges == FULL) {
setContentType(response, contentType);
if (contentLength >= 0) {
// Don't set a content length if something else has already
// written to the response.
if (!contentWritten) {
setContentLength(response, contentLength);
}
}
// Copy the input stream to our output stream (if requested)
if (serveContent) {
copy(elem, response);
}
} else {
if (noRanges) {
return;
}
// Partial content response.
response.setStatus(SC_PARTIAL_CONTENT);
if (ranges.size() == 1) {
Range range = ranges.get(0);
response.addHeader("Content-Range", "bytes " + range.start
+ "-" + range.end + "/" + range.length);
long length = range.end - range.start + 1;
setContentLength(response, length);
setContentType(response, contentType);
if (serveContent) {
copy(elem, response, range);
}
} else {
response.setContentType("multipart/byteranges; boundary="
+ MIME_SEPARATION);
if (serveContent) {
copy(elem, response, ranges, contentType);
}
}
}
elem.stopInTimeout();
}