}
@Override
public WOResponse handleRequest(WORequest request) {
if(!ERSelenium.testsEnabled()) {
return new ERXResponse(ERXHttpStatusCodes.STATUS_FORBIDDEN);
}
NSArray pathElements = request.requestHandlerPathArray();
StringBuilder builder = new StringBuilder();
Iterator iter = pathElements.iterator();
while (iter.hasNext()) {
builder.append(iter.next());
if (iter.hasNext())
builder.append('/');
}
String filePath = builder.toString();
log.debug("Processing file '" + filePath + "'");
/*
* Synchronization mistakes are possible here, but not fatal at all.
* At the worst case the file will be read 2-or-more times instead of 1 (if process 1
* checks that the file is not cached and process 2 does the same check before
* process 1 has updated the cache).
*/
CachedFile cachedFile;
synchronized (_cache) {
cachedFile = (CachedFile)_cache.objectForKey(filePath);
}
if (cachedFile == null) {
cachedFile = new CachedFile();
URL fileUrl = WOApplication.application().resourceManager().pathURLForResourceNamed(filePath, "ERSelenium", null);
if (fileUrl == null) {
throw new RuntimeException("Can't find specified resource ('" + filePath + "')");
}
cachedFile.mimeType = WOApplication.application().resourceManager().contentTypeForResourceNamed(filePath);
if (cachedFile.mimeType == null) {
throw new RuntimeException("Can't determine resource mime type ('" + filePath + "')");
}
try {
cachedFile.data = new NSData(ERXFileUtilities.bytesFromInputStream(fileUrl.openStream()));
} catch (Exception e) {
throw new RuntimeException("Error reading file '" + fileUrl.getPath() + "'", e);
}
synchronized (_cache) {
_cache.setObjectForKey(cachedFile, filePath);
}
}
ERXResponse response = new ERXResponse();
response.setHeader(cachedFile.mimeType, "content-type");
response.setContent(cachedFile.data);
NSNotificationCenter.defaultCenter().postNotification(WORequestHandler.DidHandleRequestNotification, response);
return response;
}