?? staticrequesthandler.java
字號:
package freecs.external;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import freecs.Server;
import freecs.content.ContentContainer;
import freecs.interfaces.IRequest;
public class StaticRequestHandler extends AbstractRequestHandler {
private HashMap fileCache = new HashMap();
public StaticRequestHandler(String handlerName) {
super(handlerName);
}
/**
* class rendering information on server internals
*/
public void handle(IRequest req, ContentContainer c) {
String file = req.getAction();
file = file.substring(file.substring(1).indexOf("/") + 2);
Object cached = fileCache.get(file);
if (cached != null) {
FileProperties fp = (FileProperties) cached;
if (!fp.f.exists()) {
c.setTemplate("not_found");
return;
} else if (fp.f.lastModified() == fp.lastModified) {
try {
c.setContent(fp.content);
} catch (Exception e) {
throw new AccessForbiddenException(true);
}
c.setContentType(fp.contentType);
return;
}
}
if (file.indexOf("/") > -1 && !file.endsWith(".class"))
throw new AccessForbiddenException (true);
File f = new File (Server.BASE_PATH + "/static/" + file);
if (!f.exists()) {
c.setTemplate("not_found");
return;
}
String contentType;
if (file.toLowerCase().endsWith (".gif"))
contentType="image/gif";
else if (file.toLowerCase().endsWith (".jpg")
|| file.toLowerCase().endsWith (".jpeg"))
contentType="image/jpeg";
else if (file.toLowerCase().endsWith (".class"))
contentType="application/x-java-applet";
else
throw new AccessForbiddenException (true);
try {
InputStream is = new FileInputStream (f);
byte[] cntnt = new byte[(int) f.length()];
for (int i = 0; i<cntnt.length; i++)
cntnt[i] = (byte) is.read();
fileCache.put(file, new FileProperties(f, cntnt, contentType));
c.setContent(cntnt);
} catch (Exception e) {
Server.debug ("StaticRequestHandler", "exception during reading file " + file, e, Server.MSG_ERROR, Server.LVL_MINOR);
c.setTemplate("techerror");
return;
}
c.setContentType(contentType);
}
class FileProperties {
long lastModified = -1;
File f;
byte[] content;
String contentType;
FileProperties (File f, byte[] content, String contentType) {
this.f = f;
this.content = content;
this.contentType = contentType;
lastModified = f.lastModified();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -