mcardle 2005/10/09 18:45:43 CEST
Modified files:
src/net/sf/j2ep/responsehandlers ResponseHandlerBase.java
src/org/jahia/esi FragmentCache.java
Log:
marchouille
Revision Changes Path
1.4 +91 -10
esi_server/src/net/sf/j2ep/responsehandlers/ResponseHandlerBase.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/esi_server/src/net/sf/j2ep/responsehandlers/ResponseHandlerBase.java.diff?r1=1.3&r2=1.4&f=h
1.4 +7 -5 esi_server/src/org/jahia/esi/FragmentCache.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/esi_server/src/org/jahia/esi/FragmentCache.java.diff?r1=1.3&r2=1.4&f=h
Index: ResponseHandlerBase.java
===================================================================
RCS file:
/home/cvs/repository/esi_server/src/net/sf/j2ep/responsehandlers/ResponseHandlerBase.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ResponseHandlerBase.java 9 Oct 2005 10:34:11 -0000 1.3
+++ ResponseHandlerBase.java 9 Oct 2005 16:45:42 -0000 1.4
@@ -22,12 +22,8 @@
import java.io.ByteArrayOutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Locale;
import javax.servlet.ServletResponse;
-import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
@@ -47,8 +43,6 @@
import org.jahia.esi.CacheAdminstrator;
import org.jahia.esi.FragmentCache;
import org.jahia.esi.CacheObject;
-import org.jahia.esi.UrlCacheObject;
-import org.jahia.esi.ContentCacheObject;
/**
@@ -108,6 +102,9 @@
*
* @see
net.sf.j2ep.model.ResponseHandler#process(javax.servlet.http.HttpServletResponse)
*/
+
+ static int CACHEABLE_CONTENT_MAX_SIZE_BYTES = 100000;
+
public void process(HttpServletResponse clientResponse,
HttpServletRequest clientRequest) throws IOException, MethodNotAllowedException
{
String cacheKey =
CacheAdminstrator.getInstance().generateEntryKey(this.requestHandler);
@@ -119,25 +116,63 @@
else log.debug(" MISS on "+cacheKey);
+
//Case 1: Passthrough (post etc...)
if (!this.cacheMethod) {
- log.debug(" * Case 1: Passthrough (post etc...) *");
+ log.debug(" * Case 1: Passthrough (post etc...) *
url:"+cacheKey);
beginServerRequest();
setHeaders(clientResponse);
//TODO: set cookies here
clientResponse.setStatus(getStatusCode());
+
if (!method.getName().equals("HEAD")) //since there is no
content for HEAD requests
sendStreamToClientPassthrough(clientResponse);
}
//Case 2: Not in cache => Passthrough with cache storing (gif png
etc...)
else if (!fragmentCache.contains(cacheKey)) {
- log.debug(" * Case 2: Not in cache => Passthrough with cache
storing (gif png etc...) *");
+ log.debug(" * Case 2: Not in cache => Passthrough with cache
storing (gif png etc...) * url:"+cacheKey);
beginServerRequest();
setHeaders(clientResponse);
//TODO: set cookies here
clientResponse.setStatus(getStatusCode());
- if (!method.getName().equals("HEAD")) //since there is no
content for HEAD requests
- sendStreamToClientPassthroughAndCache(clientResponse,
cacheKey);
+
+ int contentLength = -1;
+ try {
+ String contentLengthStr =
method.getResponseHeader("Content-Length").getValue().toLowerCase();
+ contentLength = Integer.parseInt(contentLengthStr);
+ } catch (Exception ex) {
+ log.debug(" NO VALID CONTENT LENGTH on
["+method.getResponseHeader("Content-Length")+"] url:"+cacheKey+" ex: "+ex);
+ }
+
+ String contentType = "";
+ try {
+ contentType =
method.getResponseHeader("Content-type").getValue().toLowerCase();
+ } catch (Exception ex) {
+ log.debug(" NO VALID CONTENT TYPE on
["+method.getResponseHeader("Content-type")+"] url:"+cacheKey+" ex: "+ex);
+ }
+
+ //only cache ASCII content or content which is under the limited
size
+ if( contentType.indexOf("text/")>-1
+ ||
+ (contentType.indexOf("text/")==-1 &&
+ contentLength !=-1 &&
contentLength<CACHEABLE_CONTENT_MAX_SIZE_BYTES)
+ ) {
+ /*(contentType.indexOf("html")>-1
+ || contentType.indexOf("plain")>-1
+ || contentType.indexOf("text")>-1
+ || contentType.indexOf("css")>-1
+ || contentType.indexOf("javascript")>-1)*/
+ if (!method.getName().equals("HEAD")) {//since there is no
content for HEAD requests
+ log.debug("detected cacheable content
contentType["+contentType+"] contentLength["+contentLength+"] url:"+cacheKey);
+ sendStreamToClientPassthroughAndCache(clientResponse,
cacheKey);
+ }
+ }
+ //not cacheable content
+ else {
+ log.debug("detected NON-cacheable content, so just using
passthrough : contentType["+contentType +"] contentLength["+contentLength+"]
url:"+cacheKey);
+ sendStreamToClientPassthrough(clientResponse);
+ }
+
}
//Case 3: in cache => return from cache
else {
@@ -150,6 +185,7 @@
setHeaders(clientResponse);
//TODO: set cookies here
clientResponse.setStatus(getStatusCode());
+
if (!method.getName().equals("HEAD")) //since there is no
content for HEAD requests
sendStreamToClientFromCache(clientResponse, cacheKey,
cachedByteContent);
@@ -315,8 +351,37 @@
if (!((HttpMethodBase) method).isAborted()) {
+
+ String ifnonematch = "";
+ try {
+ if (method.getRequestHeader("if-none-match").getValue()
!=null) {
+ log.debug("removed if-none-match
["+method.getRequestHeader("if-none-match")+"] ");
+ method.removeRequestHeader("if-none-match");
+ }
+
+ } catch (Exception ex) {
+ log.debug(" no if-none-match
["+method.getResponseHeader("if-none-match")+"] ex: "+ex);
+ }
+ String ifmodifiedsince = "";
+ try {
+ if (method.getRequestHeader("if-modified-since").getValue()
!=null) {
+ log.debug("removed if-modified-since
["+method.getRequestHeader("if-modified-since")+"] ");
+ method.removeRequestHeader("if-modified-since");
+ }
+
+ } catch (Exception ex) {
+ log.debug(" no if-modified-since
["+method.getResponseHeader("if-modified-since")+"] ex: "+ex);
+ }
+
ProxyFilter.getHttpClient().executeMethod(method);
+ printRequestHeaders();
+ printResponseHeaders();
+
+
+
+
+
if (method.getStatusCode() == 405) {
Header allow = method.getResponseHeader("allow");
String value = allow.getValue();
@@ -328,5 +393,21 @@
else log.info("||||||||||||||||||||||||||||||||||||||| NOT
executing httpClient.executeMethod(newMethod)");
}
+ public void printRequestHeaders() {
+ Header[] headers = method.getRequestHeaders();
+ log.debug("HTTP Request Headers\n");
+ for (int i=0; i<headers.length; i++){
+ log.debug("REQU " + headers[i]);
+ }
+ }
+ public void printResponseHeaders() {
+
+ Header[] headers2 = method.getResponseHeaders();
+ log.debug("HTTP Response Headers\n");
+ for (int i=0; i<headers2.length; i++){
+ log.debug("RESP " + headers2[i]);
+ }
+ }
+
}
Index: FragmentCache.java
===================================================================
RCS file:
/home/cvs/repository/esi_server/src/org/jahia/esi/FragmentCache.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- FragmentCache.java 9 Oct 2005 10:34:11 -0000 1.3
+++ FragmentCache.java 9 Oct 2005 16:45:43 -0000 1.4
@@ -49,7 +49,7 @@
}
- public void add(String urlKey, HttpMethod method, byte[] byteContent) {
+ public synchronized void add(String urlKey, HttpMethod method, byte[]
byteContent) {
String byteContentHash = getMD5Hash(byteContent);
UrlCacheObject newUrlObj = null;
@@ -86,8 +86,7 @@
UrlCacheObject urlObj = (UrlCacheObject) hashCache.get(urlKey);
if ( urlObj!=null ) {
-
- //sanity check: Shouldn't have to do this check, only for debug
+ //sanity check: Shouldn't have to do this check, only for debug
if (contentCache.containsKey(urlObj.getContentHash())) {
log.debug("Request/Hash Cache Hit: request ["+urlKey +"]
already in cache");
urlObj.incHitCount(); //SHOULD REALLY BE
CALLED from code we know for sure is doing a user cache access
@@ -143,9 +142,12 @@
} catch (Exception e) {
log.error(" Error generating Content Hash");
}
-
return null;
- //return MD5.asHex(content).toString();
+ }
+
+ public String getFastMD5Hash(byte[] content) {
+ //seems to double size of original content...
+ return MD5.asHex(content).toString();
}
/*public String generateEntryKey(NoodleData noodleData) {