cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicolas Toper <nto...@jouve.fr>
Subject Re: gzip compression
Date Mon, 15 Dec 2003 17:56:14 GMT
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


Mime
View raw message