You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by "Carmona Perez, David" <DP...@fcc.es> on 2003/12/15 18:46:29 UTC

gzip compression

Hi all,

Does someone how can I make some action that activates compressing output data using GZip?  Gzip compression is an HTTP 1.1 standard feature.

Ideally it would be great if it could be like an action, so that it's easy to activate it on or off.
I have tried this code:

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.zip.GZIPOutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.acting.AbstractAction;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.environment.http.HttpEnvironment;
import org.apache.cocoon.environment.http.HttpResponse;

public class CompressAction extends AbstractAction {
	class StreamGzip extends ServletOutputStream {
		public StreamGzip(OutputStream out) throws IOException {
			gzip = new GZIPOutputStream(out);
		}
		/** @see java.io.OutputStream#write(int) */
		public void write(int b) throws IOException {
			gzip.write(b);
		}
		/** @see java.io.OutputStream#close() */
		public void close() throws IOException {
			gzip.close();
		}
		/** @see java.io.OutputStream#flush() */
		public void flush() throws IOException {
			gzip.flush();
		}
		private GZIPOutputStream gzip;
	}
	class GzipResponse extends HttpResponse {
		GzipResponse(HttpServletResponse resp) throws IOException {
			super(resp);
			os = new StreamGzip(resp.getOutputStream());
		}
		public ServletOutputStream getOutputStream() throws IOException {
			return os;
		}
		StreamGzip os;
	}
	public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception {
		Request pet = ObjectModelHelper.getRequest(objectModel);
		String accept = pet.getHeader("Accept-Encoding");
		if (accept != null && accept.indexOf("gzip") >= 0) {
			GzipResponse resp = new GzipResponse((HttpServletResponse)objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT));
			resp.setHeader("Content-Encoding", "gzip");
			objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, resp);
			return EMPTY_MAP;
		}
		return null;
	}
}

But it doesn't work, because the HttpEnvironment holds also holds a reference to the outputstream.
Which is the cleanest way to perform this?
Any suggestion will be greatly appreciated.


--------
David


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: gzip compression

Posted by Nicolas Toper <nt...@jouve.fr>.
Apache could do that automatically...

Otherwise, I'd use Tomcat and a filter servlet to do it: you could find some
all ready to be used on the Web...
Le Lundi 15 Décembre 2003 18:46, Carmona Perez, David a écrit :
> Hi all,
>
> Does someone how can I make some action that activates compressing output
> data using GZip?  Gzip compression is an HTTP 1.1 standard feature.
>
> Ideally it would be great if it could be like an action, so that it's easy
> to activate it on or off. I have tried this code:
>
> import java.io.IOException;
> import java.io.OutputStream;
> import java.util.Map;
> import java.util.zip.GZIPOutputStream;
>
> import javax.servlet.ServletOutputStream;
> import javax.servlet.http.HttpServletResponse;
>
> import org.apache.avalon.framework.parameters.Parameters;
> import org.apache.cocoon.acting.AbstractAction;
> import org.apache.cocoon.environment.ObjectModelHelper;
> import org.apache.cocoon.environment.Redirector;
> import org.apache.cocoon.environment.Request;
> import org.apache.cocoon.environment.SourceResolver;
> import org.apache.cocoon.environment.http.HttpEnvironment;
> import org.apache.cocoon.environment.http.HttpResponse;
>
> public class CompressAction extends AbstractAction {
> 	class StreamGzip extends ServletOutputStream {
> 		public StreamGzip(OutputStream out) throws IOException {
> 			gzip = new GZIPOutputStream(out);
> 		}
> 		/** @see java.io.OutputStream#write(int) */
> 		public void write(int b) throws IOException {
> 			gzip.write(b);
> 		}
> 		/** @see java.io.OutputStream#close() */
> 		public void close() throws IOException {
> 			gzip.close();
> 		}
> 		/** @see java.io.OutputStream#flush() */
> 		public void flush() throws IOException {
> 			gzip.flush();
> 		}
> 		private GZIPOutputStream gzip;
> 	}
> 	class GzipResponse extends HttpResponse {
> 		GzipResponse(HttpServletResponse resp) throws IOException {
> 			super(resp);
> 			os = new StreamGzip(resp.getOutputStream());
> 		}
> 		public ServletOutputStream getOutputStream() throws IOException {
> 			return os;
> 		}
> 		StreamGzip os;
> 	}
> 	public Map act(Redirector redirector, SourceResolver resolver, Map
> objectModel, String source, Parameters parameters) throws Exception {
> Request pet = ObjectModelHelper.getRequest(objectModel);
> 		String accept = pet.getHeader("Accept-Encoding");
> 		if (accept != null && accept.indexOf("gzip") >= 0) {
> 			GzipResponse resp = new
> GzipResponse((HttpServletResponse)objectModel.get(HttpEnvironment.HTTP_RESP
>ONSE_OBJECT)); resp.setHeader("Content-Encoding", "gzip");
> 			objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, resp);
> 			return EMPTY_MAP;
> 		}
> 		return null;
> 	}
> }
>
> But it doesn't work, because the HttpEnvironment holds also holds a
> reference to the outputstream. Which is the cleanest way to perform this?
> Any suggestion will be greatly appreciated.
>
>
> --------
> David
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: gzip compression

Posted by Upayavira <uv...@upaya.co.uk>.
If you use Apache in front of your servlet container, you could use 
mod_deflate, I believe. Really, HTTP compression of pages is not a job 
for Cocoon as such, but for the servlet container or front end server.

Upayavira

Carmona Perez, David wrote:

>Hi all,
>
>Does someone how can I make some action that activates compressing output data using GZip?  Gzip compression is an HTTP 1.1 standard feature.
>
>Ideally it would be great if it could be like an action, so that it's easy to activate it on or off.
>I have tried this code:
>
>import java.io.IOException;
>import java.io.OutputStream;
>import java.util.Map;
>import java.util.zip.GZIPOutputStream;
>
>import javax.servlet.ServletOutputStream;
>import javax.servlet.http.HttpServletResponse;
>
>import org.apache.avalon.framework.parameters.Parameters;
>import org.apache.cocoon.acting.AbstractAction;
>import org.apache.cocoon.environment.ObjectModelHelper;
>import org.apache.cocoon.environment.Redirector;
>import org.apache.cocoon.environment.Request;
>import org.apache.cocoon.environment.SourceResolver;
>import org.apache.cocoon.environment.http.HttpEnvironment;
>import org.apache.cocoon.environment.http.HttpResponse;
>
>public class CompressAction extends AbstractAction {
>	class StreamGzip extends ServletOutputStream {
>		public StreamGzip(OutputStream out) throws IOException {
>			gzip = new GZIPOutputStream(out);
>		}
>		/** @see java.io.OutputStream#write(int) */
>		public void write(int b) throws IOException {
>			gzip.write(b);
>		}
>		/** @see java.io.OutputStream#close() */
>		public void close() throws IOException {
>			gzip.close();
>		}
>		/** @see java.io.OutputStream#flush() */
>		public void flush() throws IOException {
>			gzip.flush();
>		}
>		private GZIPOutputStream gzip;
>	}
>	class GzipResponse extends HttpResponse {
>		GzipResponse(HttpServletResponse resp) throws IOException {
>			super(resp);
>			os = new StreamGzip(resp.getOutputStream());
>		}
>		public ServletOutputStream getOutputStream() throws IOException {
>			return os;
>		}
>		StreamGzip os;
>	}
>	public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception {
>		Request pet = ObjectModelHelper.getRequest(objectModel);
>		String accept = pet.getHeader("Accept-Encoding");
>		if (accept != null && accept.indexOf("gzip") >= 0) {
>			GzipResponse resp = new GzipResponse((HttpServletResponse)objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT));
>			resp.setHeader("Content-Encoding", "gzip");
>			objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, resp);
>			return EMPTY_MAP;
>		}
>		return null;
>	}
>}
>
>But it doesn't work, because the HttpEnvironment holds also holds a reference to the outputstream.
>Which is the cleanest way to perform this?
>Any suggestion will be greatly appreciated.
>
>
>--------
>David
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>For additional commands, e-mail: users-help@cocoon.apache.org
>
>
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org