You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2010/01/19 14:18:09 UTC

svn commit: r900766 - in /labs/magma/trunk/foundation-website/src: main/java/org/apache/magma/website/ test/java/org/apache/magma/website/

Author: simoneg
Date: Tue Jan 19 13:18:08 2010
New Revision: 900766

URL: http://svn.apache.org/viewvc?rev=900766&view=rev
Log:
Producer have now access to write custom headers

Modified:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/StreamProducer.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/DefaultExpectations.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java?rev=900766&r1=900765&r2=900766&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java (original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Dispatch.java Tue Jan 19 13:18:08 2010
@@ -30,6 +30,7 @@
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Method;
 import java.net.URLDecoder;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -194,16 +195,25 @@
 		}
 	}
 	
+	public void setHeaders(Producer producer, HttpServletResponse resp) {
+		Map<String, Object> headers = producer.headers();
+		if (headers == null || headers.size() == 0) return;
+		for (Map.Entry<String, Object> entry : headers.entrySet()) {
+			Object value = entry.getValue();
+			if (value instanceof CharSequence) {
+				resp.setHeader(entry.getKey(), value.toString());
+			} else if (value instanceof Date) {
+				resp.setDateHeader(entry.getKey(), ((Date)value).getTime());
+			} else if (value instanceof Number) {
+				resp.setIntHeader(entry.getKey(), ((Number)value).intValue());
+			} else {
+				// TODO warn wrong header type
+			}
+		}		
+	}
+	
 	public void handleAJAX(Producer producer, HttpServletRequest req, HttpServletResponse resp) {
-		long expiry = producer.getExpires();
-		if (expiry == -1) expiry = 0;
-		resp.setDateHeader("Expires", System.currentTimeMillis() + expiry);
-		String contentType = producer.getMimeType();
-		if (contentType == null) contentType = "application/octet-stream";
-		if (contentType.startsWith("text")) {
-			contentType += "; charset=utf-8";
-		}
-		resp.setContentType(contentType);
+		setHeaders(producer, resp);
 		OutputStream str = Cycle.get().getRewriting();
 		if (producer instanceof HtmlProducer) {
 			try {
@@ -229,8 +239,6 @@
 	}
 
 	public void handleHTML(HtmlProducer producer, HttpServletRequest req, HttpServletResponse resp) {
-		resp.setContentType("text/html; charset=utf-8");
-
 		// Try to determine if current request is considered a main or not
 		HttpSession session = req.getSession();
 		// Will set this to true when the right template has been found
@@ -292,6 +300,10 @@
 			}
 		}
 		// Finally produce the output with the right template
+		// TODO aggregate some headers here?
+		//setHeaders(producer, resp);
+		resp.setHeader("Content-Type", "text/html; charset=utf-8");
+		
 		t.produce(Cycle.get().getRewriting());
 		
 		if (t.wasMain() && producer.isRepeatable()) {
@@ -316,15 +328,7 @@
 	}
 
 	public void handleRAW(Producer producer, HttpServletRequest req, HttpServletResponse resp) {
-		long expiry = producer.getExpires();
-		if (expiry == -1) expiry = 0;
-		resp.setDateHeader("Expires", System.currentTimeMillis() + expiry);
-		String contentType = producer.getMimeType();
-		if (contentType == null) contentType = "application/octet-stream";
-		if (contentType.startsWith("text")) {
-			contentType += "; charset=utf-8";
-		}
-		resp.setContentType(contentType);
+		setHeaders(producer, resp);
 		try {
 			producer.produce(resp.getOutputStream());
 		} catch (IOException e) {

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java?rev=900766&r1=900765&r2=900766&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java (original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/Producer.java Tue Jan 19 13:18:08 2010
@@ -19,6 +19,9 @@
 
 import java.io.OutputStream;
 import java.lang.reflect.Method;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.apache.magma.basics.context.ContextOwner;
 import org.apache.magma.basics.context.RunningContext;
@@ -35,6 +38,39 @@
 	EXPIRES_1_WEEK = EXPIRES_TOMORROW * 7,
 	EXPIRES_NEVER = EXPIRES_TOMORROW * 1095;
 	
+    public final static String 
+        HEADER_CONNECTION= "Connection",
+        HEADER_CACHE_CONTROL= "Cache-Control",
+        HEADER_DATE= "Date",
+        HEADER_PRAGMA= "Pragma",
+        HEADER_PROXY_CONNECTION = "Proxy-Connection",
+        HEADER_TRAILER= "Trailer",
+        HEADER_TRANSFER_ENCODING= "Transfer-Encoding",
+        HEADER_UPGRADE= "Upgrade",
+        HEADER_VIA= "Via",
+        HEADER_WARNING= "Warning",
+        HEADER_ALLOW= "Allow",
+        HEADER_CONTENT_ENCODING= "Content-Encoding",
+        HEADER_CONTENT_LANGUAGE= "Content-Language",
+        HEADER_CONTENT_LENGTH= "Content-Length",
+        HEADER_CONTENT_LOCATION= "Content-Location",
+        HEADER_CONTENT_MD5= "Content-MD5",
+        HEADER_CONTENT_RANGE= "Content-Range",
+        HEADER_CONTENT_TYPE= "Content-Type",
+        HEADER_CONTENT_DISPOSITION = "Content-Disposition",
+        HEADER_EXPIRES= "Expires",
+        HEADER_LAST_MODIFIED= "Last-Modified",
+        HEADER_ACCEPT_RANGES= "Accept-Ranges",
+        HEADER_AGE= "Age",
+        HEADER_ETAG= "ETag",
+        HEADER_LOCATION= "Location",
+        HEADER_PROXY_AUTHENTICATE= "Proxy-Authenticate",
+        HEADER_RETRY_AFTER= "Retry-After",
+        HEADER_SERVER= "Server",
+        HEADER_SERVLET_ENGINE= "Servlet-Engine",
+        HEADER_VARY= "Vary";
+
+	
 	protected long expires = EXPIRES_UNSET;
 	
 	public long getExpires() {
@@ -53,6 +89,18 @@
 		return RunningContext.get().getPreviousHandler();
 	}
 	
+	public Map<String, Object> headers() {
+		Map<String, Object> ret = new HashMap<String, Object>();
+		String contentType = getMimeType();
+		if (contentType == null) contentType = "application/octet-stream";
+		if (contentType.startsWith("text") && contentType.indexOf(';') == -1) {
+			contentType += "; charset=utf-8";
+		}		
+		ret.put(HEADER_CONTENT_TYPE, contentType);
+		ret.put(HEADER_EXPIRES, new Date(System.currentTimeMillis() + getExpires()));
+		return ret;
+	}
+	
 	public abstract void produce(OutputStream stream);	
 	
 	public abstract String getMimeType();

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/StreamProducer.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/StreamProducer.java?rev=900766&r1=900765&r2=900766&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/StreamProducer.java (original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/StreamProducer.java Tue Jan 19 13:18:08 2010
@@ -46,6 +46,7 @@
 	protected InputStream stream = null;
 	protected String mimeType = null;
 	protected String fileName = null;
+	protected boolean forDownload = false;
 
 	public StreamProducer(InputStream stream) {
 		this.stream = stream;
@@ -54,6 +55,11 @@
 		this.stream = stream;
 		this.fileName = name;
 	}
+	public StreamProducer(String name, boolean forDownload, InputStream stream) {
+		this.stream = stream;
+		this.fileName = name;
+		this.forDownload = forDownload;
+	}
 	protected StreamProducer() {}
 	
 	@Override
@@ -70,6 +76,22 @@
 		return null;
 	}
 	
+	@Override
+	public Map<String, Object> headers() {
+		Map<String, Object> headers = super.headers();
+		String cdv = "";
+		if (forDownload) {
+			cdv = "attachment";
+			if (this.fileName != null) {
+				cdv += "; filename=" + this.fileName;
+			}
+		} else {
+			cdv = "inline";
+		}
+		headers.put(HEADER_CONTENT_DISPOSITION, cdv);
+		return headers;
+	}
+	
 	protected String getMimeForExtension(String ext) {
 		String mime = mimes.get(ext);
 		if (mime == null) {

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/DefaultExpectations.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/DefaultExpectations.java?rev=900766&r1=900765&r2=900766&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/DefaultExpectations.java (original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/DefaultExpectations.java Tue Jan 19 13:18:08 2010
@@ -79,7 +79,7 @@
 		atMost(1).of(resp).setHeader("User-Agent", "Mozilla");
 		atMost(1).of(resp).addCookie(with(any(Cookie.class)));
 		atMost(1).of(req).setAttribute("user", person);
-		atMost(1).of(resp).setContentType("text/html; charset=utf-8");
+		atMost(1).of(resp).setHeader("Content-Type","text/html; charset=utf-8");			
 		allowing(resp).getOutputStream(); will(returnValue(out));
 	}
 	
@@ -102,7 +102,7 @@
 		atMost(1).of(session).getAttribute("__magma_lasttemplate"); will(returnValue(null));			
 		allowing(session).setAttribute(with(equal("__magma_lasttemplate")), with(any(Class.class))); 			
 		allowing(session).setAttribute("__magma_lastmain", path);
-		one(resp).setContentType("text/html; charset=utf-8");			
+		one(resp).setHeader("Content-Type","text/html; charset=utf-8");			
 		allowing(resp).getOutputStream(); will(returnValue(out));
 	}
 	

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java?rev=900766&r1=900765&r2=900766&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java (original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/WebHandlerCycleTest.java Tue Jan 19 13:18:08 2010
@@ -276,7 +276,8 @@
 	@Test
 	public void fetchingResource() throws Exception {
 		DefaultExpectations def = new DefaultExpectations() {{
-			one(resp).setContentType(with(allOf(containsString("text/plain"), containsString("charset=utf-8"))));
+			one(resp).setHeader(with(equal("Content-Type")), with(allOf(containsString("text/plain"), containsString("charset=utf-8"))));
+			one(resp).setHeader(with(equal("Content-Disposition")), with(equal("inline")));
 			one(resp).setDateHeader(with(equal("Expires")), with(any(long.class)));
 		}};
 		def.setupDefaults("/test/resource.txt");



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org