com.tinkerpop.rexster.servlet.RexsterStaticHttpHandler Maven / Gradle / Ivy
package com.tinkerpop.rexster.servlet;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.WriteHandler;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;
import org.glassfish.grizzly.http.server.StaticHttpHandler;
import org.glassfish.grizzly.http.server.io.NIOOutputStream;
import org.glassfish.grizzly.http.server.io.OutputBuffer;
import org.glassfish.grizzly.http.server.util.MimeType;
import org.glassfish.grizzly.http.util.HttpStatus;
import org.glassfish.grizzly.memory.MemoryManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
/**
* This class is a near replica of the StaticHttpHandler. The only difference is that it does not cache resources.
* Caching (at least in how it was implemented by Grizzly) caused problems in Dog House as mime-types for
* all resources were being returned as "text/html" even if they were clearly css/png/etc.
*
* Will drop this class when problem with StaticHttpHandler is resolved.
*
* As of 2.2.18 the issue described above seems resolved, but now an error exists where certain resources
* throw a PendingWriteQueueLimitExceededException. Modified this class to support that fix. According to
* the issue tracker for grizzly it seems that the issue is fixed, but it doesn't seem to work :/
*/
public class RexsterStaticHttpHandler extends StaticHttpHandler {
public RexsterStaticHttpHandler(String path) {
super(path);
}
@Override
protected boolean handle(final String uri,
final Request req,
final Response res) throws Exception {
boolean found = false;
final File[] fileFolders = docRoots.getArray();
if (fileFolders == null) {
return false;
}
File resource = null;
for (int i = 0; i < fileFolders.length; i++) {
final File webDir = fileFolders[i];
// local file
resource = new File(webDir, uri);
final boolean exists = resource.exists();
final boolean isDirectory = resource.isDirectory();
if (exists && isDirectory) {
final File f = new File(resource, "/index.html");
if (f.exists()) {
resource = f;
found = true;
break;
}
}
if (isDirectory || !exists) {
found = false;
} else {
found = true;
break;
}
}
if (!found) {
/*
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "File not found {0}", resource);
}
*/
return false;
}
// ********************************************
// * commented out this bit here for caching...keep an eye on grizzly for a fix
// ********************************************
// addToFileCache(req, resource);
// ********************************************
sendAFile(res, resource);
return true;
}
public static void sendAFile(final Response response, final File file)
throws IOException {
final String path = file.getPath();
final FileInputStream fis = new FileInputStream(file);
try {
response.setStatus(HttpStatus.OK_200);
String substr;
int dot = path.lastIndexOf('.');
if (dot < 0) {
substr = file.toString();
dot = substr.lastIndexOf('.');
} else {
substr = path;
}
if (dot > 0) {
String ext = substr.substring(dot + 1);
String ct = MimeType.get(ext);
if (ct != null) {
response.setContentType(ct);
}
} else {
response.setContentType(MimeType.get("html"));
}
response.setContentLengthLong(file.length());
final OutputStream outputStream = response.getOutputStream();
byte b[] = new byte[8192];
int rd;
while ((rd = fis.read(b)) > 0) {
outputStream.write(b, 0, rd);
}
outputStream.flush();
outputStream.close();
} finally {
try {
fis.close();
} catch (IOException ignore) {
}
}
}
}