StaticResourcesAdapterpublic class StaticResourcesAdapter extends Object implements org.apache.coyote.AdapterSimple HTTP based Web Server. Part of this class is from Tomcat sandbox code
from Costin Manolache. |
Fields Summary |
---|
static Properties | contentTypes | private String | rootFolder | private File | rootFolderF | private ConcurrentHashMap | cache |
Constructors Summary |
---|
public StaticResourcesAdapter()
rootFolder = SelectorThread.getWebAppRootPath();
rootFolderF = new File(rootFolder);
try {
rootFolder = rootFolderF.getCanonicalPath();
SelectorThread.logger().log(Level.INFO, "Servicing page from: "
+ rootFolder);
} catch (IOException e) {
}
|
Methods Summary |
---|
public void | afterService(org.apache.coyote.Request req, org.apache.coyote.Response res)
// Recycle the wrapper request and response
req.recycle();
res.recycle();
| public void | fireAdapterEvent(java.lang.String string, java.lang.Object object)
| public java.lang.String | getContentType(java.lang.String ext)
return contentTypes.getProperty( ext, "text/plain" );
| public java.lang.String | getRootFolder()
return rootFolder;
| static void | initContentTypes()
initContentTypes();
contentTypes.put("html", "text/html");
contentTypes.put("txt", "text/plain");
| public void | service(org.apache.coyote.Request req, org.apache.coyote.Response res)
MessageBytes mb = req.requestURI();
ByteChunk requestURI = mb.getByteChunk();
String uri = req.requestURI().toString();
if (uri.indexOf("..") >= 0) {
res.setStatus(404);
return;
}
// local file
File resource = cache.get(uri);
if (resource == null){
resource = new File(rootFolderF, uri);
cache.put(uri,resource);
}
if (!resource.getCanonicalPath().startsWith(rootFolder)) {
res.setStatus(404);
return;
}
if (resource.isDirectory()) {
resource = new File(resource, "index.html");
cache.put(uri,resource);
}
if (!resource.exists()) {
SelectorThread.logger().log(Level.INFO,"File not found " + resource);
res.setStatus(404);
return;
}
res.setStatus(200);
int dot=uri.lastIndexOf(".");
if( dot > 0 ) {
String ext=uri.substring(dot+1);
String ct=getContentType(ext);
if( ct!=null) {
res.setContentType(ct);
}
}
res.setContentLength((int)resource.length());
res.sendHeaders();
/* Workaround Linux NIO bug
* 6427312: (fc) FileChannel.transferTo() throws IOException "system call interrupted"
* 5103988: (fc) FileChannel.transferTo should return -1 for EAGAIN instead throws IOException
* 6253145: (fc) FileChannel.transferTo on Linux fails when going beyond 2GB boundary
* 6470086: (fc) FileChannel.transferTo(2147483647, 1, channel) cause "Value too large" exception
*/
FileInputStream fis = new FileInputStream(resource);
byte b[] = new byte[8192];
ByteChunk chunk = new ByteChunk();
int rd = 0;
while ((rd = fis.read(b)) > 0) {
chunk.setBytes(b, 0, rd);
res.doWrite(chunk);
}
try{
req.action( ActionCode.ACTION_POST_REQUEST , null);
}catch (Throwable t) {
t.printStackTrace();
}
res.finish();
|
|