001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core.http;
015:
016: import java.io.BufferedWriter;
017: import java.io.IOException;
018: import java.io.OutputStream;
019: import java.io.OutputStreamWriter;
020: import java.io.Writer;
021: import java.util.zip.GZIPOutputStream;
022: import javax.servlet.http.HttpServletRequest;
023: import org.itsnat.core.http.ItsNatHttpServletResponse;
024: import org.itsnat.core.http.ItsNatHttpServlet;
025: import org.itsnat.impl.core.ItsNatServletResponseImpl;
026: import javax.servlet.http.HttpServletResponse;
027: import org.itsnat.core.http.ItsNatHttpSession;
028:
029: /**
030: *
031: * @author jmarranz
032: */
033: public class ItsNatHttpServletResponseImpl extends
034: ItsNatServletResponseImpl implements ItsNatHttpServletResponse {
035: /**
036: * Creates a new instance of ItsNatHttpServletResponseImpl
037: */
038: public ItsNatHttpServletResponseImpl(
039: ItsNatHttpServletRequestImpl request,
040: HttpServletResponse response) {
041: super (request, response);
042: }
043:
044: public ItsNatHttpServletImpl getItsNatHttpServletImpl() {
045: return (ItsNatHttpServletImpl) getItsNatServletImpl();
046: }
047:
048: public ItsNatHttpServletRequestImpl getItsNatHttpServletRequestImpl() {
049: return (ItsNatHttpServletRequestImpl) request;
050: }
051:
052: public HttpServletResponse getHttpServletResponse() {
053: return (HttpServletResponse) response;
054: }
055:
056: public ItsNatHttpServlet getItsNatHttpServlet() {
057: return getItsNatHttpServletRequestImpl()
058: .getItsNatHttpServletImpl();
059: }
060:
061: public ItsNatHttpSession getItsNatHttpSession() {
062: return getItsNatHttpServletRequestImpl()
063: .getItsNatHttpSessionImpl();
064: }
065:
066: public void preProcess() {
067: HttpServletResponse response = getHttpServletResponse();
068: response.setHeader("Cache-Control",
069: "no-store,no-cache,must-revalidate"); //HTTP 1.1
070: response.setHeader("Pragma", "no-cache"); //HTTP 1.0
071: response.setDateHeader("Expires", 0); // Quizás también -1
072: // response.setHeader("Expires", "Thu, 01 Jan 1970 01:00:00 GMT"); // En el pasado
073: // response.addHeader("Expires", "Thu, 01 Jan 1970 01:00:00 GMT");
074: // response.setHeader("Last-Modified", "FRI, JUN 26 3020 23:59:59 GMT"); // En el futuro
075:
076: // Es absolutamente vital no cachear, sobre todo en FireFox porque soluciona el problema del back/forward
077: // que al volver ejecuta el evento load pero sin recargar la página en el servidor
078: // y el problema de que el evento unload no
079: // se ejecuta (por lo menos con listener asociado via addEventListener) y por tanto
080: // no podemos detectar cuando se deja la página.
081: // http://developer.mozilla.org/en/docs/Using_Firefox_1.5_caching
082: // http://www.jguru.com/faq/view.jsp?EID=377&page=2
083: // http://support.microsoft.com/kb/234067/EN-US/
084: // http://www.mnot.net/cache_docs/
085: // Problemas:
086: // http://support.microsoft.com/kb/q222064/
087: }
088:
089: public Writer getWriterGZip() throws IOException {
090: // http://www.javaworld.com/javaworld/jw-06-2004/jw-0628-performance.html?page=2
091:
092: // Si el navegador no soporta gzip se devuelve el Writer normal
093:
094: OutputStream out = null;
095:
096: ItsNatHttpServletRequestImpl itsNatRequest = getItsNatHttpServletRequestImpl();
097:
098: // MSIE 6 tiene problemas con gzip aunque en teoría lo soporta
099: // http://support.microsoft.com/kb/321722/EN-US/
100: // http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312496
101: // http://support.microsoft.com/default.aspx?scid=kb;en-us;823386&Product=ie600
102: // http://blog.joshuaeichorn.com/archives/2007/01/10/compressing-javascript-and-css/#comment-125483
103: // Aunque parece que están solucionados por lo menos enviando JavaScript
104: // como respuesta AJAX. FireFox no ha dado ningún problema
105:
106: HttpServletRequest request = itsNatRequest
107: .getHttpServletRequest();
108: HttpServletResponse response = getHttpServletResponse();
109:
110: String acceptEncoding = request.getHeader("Accept-Encoding");
111:
112: if (acceptEncoding != null) {
113: if (acceptEncoding.indexOf("gzip") != -1) {
114: response.setHeader("Content-Encoding", "gzip");
115: out = new GZIPOutputStream(response.getOutputStream());
116: }
117: /* // Ni FireFox ni MSIE por defecto aceptan "compress"
118: else if (acceptEncoding.indexOf("compress") != -1)
119: {
120: response.setHeader("Content-Encoding","compress");
121: out = new ZipOutputStream(response.getOutputStream());
122: }
123: */
124: }
125:
126: if (out != null) {
127: // Se supone que ya se definió antes en el response el acceptEncoding
128: String encoding = response.getCharacterEncoding();
129: return new BufferedWriter(new OutputStreamWriter(out,
130: encoding));
131: } else
132: return super.getWriter();
133: }
134: }
|