@Override
public WOResponse handleRequest(WORequest request) {
int bufferSize = 16384;
WOApplication application = WOApplication.application();
application.awake();
try {
WOContext context = application.createContextForRequest(request);
WOResponse response = application.createResponseInContext(context);
String sessionIdKey = application.sessionIdKey();
String sessionId = (String) request.formValueForKey(sessionIdKey);
if (sessionId == null) {
sessionId = request.cookieValueForKey(sessionIdKey);
}
context._setRequestSessionID(sessionId);
if (context._requestSessionID() != null) {
application.restoreSessionWithID(sessionId, context);
}
try {
final WODynamicURL url = request._uriDecomposed();
final String requestPath = url.requestHandlerPath();
final Matcher idMatcher = Pattern.compile("^id/(\\d+)/").matcher(requestPath);
final Integer requestedAttachmentID;
String requestedWebPath;
final boolean requestedPathContainsAnAttachmentID = idMatcher.find();
if (requestedPathContainsAnAttachmentID) {
requestedAttachmentID = Integer.valueOf(idMatcher.group(1));
requestedWebPath = idMatcher.replaceFirst("/");
} else {
// MS: This is kind of goofy because we lookup by path, your web path needs to
// have a leading slash on it.
requestedWebPath = "/" + requestPath;
requestedAttachmentID = null;
}
try {
InputStream attachmentInputStream;
String mimeType;
String fileName;
long length;
String queryString = url.queryString();
boolean proxyAsAttachment = (queryString != null && queryString.contains("attachment=true"));
EOEditingContext editingContext = ERXEC.newEditingContext();
editingContext.lock();
try {
ERAttachment attachment = fetchAttachmentFor(editingContext, requestedAttachmentID, requestedWebPath);
if (_delegate != null && !_delegate.attachmentVisible(attachment, request, context)) {
throw new SecurityException("You are not allowed to view the requested attachment.");
}
mimeType = attachment.mimeType();
length = attachment.size().longValue();
fileName = attachment.originalFileName();
ERAttachmentProcessor<ERAttachment> attachmentProcessor = ERAttachmentProcessor.processorForType(attachment);
if (!proxyAsAttachment) {
proxyAsAttachment = attachmentProcessor.proxyAsAttachment(attachment);
}
InputStream rawAttachmentInputStream = attachmentProcessor.attachmentInputStream(attachment);
attachmentInputStream = new BufferedInputStream(rawAttachmentInputStream, bufferSize);
} finally {
editingContext.unlock();
}
response.setHeader(mimeType, "Content-Type");
response.setHeader(String.valueOf(length), "Content-Length");
if (proxyAsAttachment) {
response.setHeader("attachment; filename=\"" + fileName + "\"", "Content-Disposition");
}
response.setStatus(200);
response.setContentStream(attachmentInputStream, bufferSize, length);
}
catch (SecurityException e) {
NSLog.out.appendln(e);
response.setContent(e.getMessage());
response.setStatus(403);
}
catch (NoSuchElementException e) {
NSLog.out.appendln(e);
response.setContent(e.getMessage());
response.setStatus(404);
}
catch (FileNotFoundException e) {
NSLog.out.appendln(e);
response.setContent(e.getMessage());
response.setStatus(404);
}
catch (IOException e) {
NSLog.out.appendln(e);
response.setContent(e.getMessage());
response.setStatus(500);
}
return response;
}
finally {
if (context._requestSessionID() != null) {
WOApplication.application().saveSessionForContext(context);
}
}
}
finally {
application.sleep();
}
}