1 20 package org.enhydra.barracuda.core.util.dom; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.io.PrintWriter ; 25 import java.io.Writer ; 26 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.log4j.Logger; 30 import org.jivan.html.document.DocumentFactory; 31 import org.jivan.html.document.DocumentManager; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.html.HTMLDocument; 35 36 47 public class JivanDOMWriter implements DOMWriter { 48 49 protected static final Logger logger = Logger.getLogger(JivanDOMWriter.class.getName()); 50 51 protected boolean preventCaching = false; 53 54 protected String contentType = null; protected String contentDisposition = null; protected boolean leaveWriterOpen = false; protected int maxAge = 0; 59 60 63 public JivanDOMWriter() { 64 super(); 65 } 66 67 72 public void setContentType(String icontentType) { 73 contentType = icontentType; 74 } 75 76 79 public String getContentType() { 80 return contentType; 81 } 82 83 86 public void setContentDisposition(String icontentDisposition) { 87 contentDisposition = icontentDisposition; 88 } 89 90 93 public String getContentDisposition() { 94 return contentDisposition; 95 } 96 97 100 public void setLeaveWriterOpen(boolean val) { 101 leaveWriterOpen = val; 102 } 103 104 107 public boolean getLeaveWriterOpen() { 108 return leaveWriterOpen; 109 } 110 111 117 public void prepareResponse(HttpServletResponse resp) throws IOException { 118 if (logger.isDebugEnabled()) logger.debug("Setting content type:"+contentType+" content disposition:"+contentDisposition); 120 if (contentType!=null) resp.setContentType(contentType); 121 if (contentDisposition!=null) resp.setHeader("Content-Disposition", contentDisposition); 122 123 if (preventCaching) { 125 if (logger.isDebugEnabled()) logger.debug("updating resp hdr to prevent caching"); 127 resp.setHeader("Pragma","no-cache"); 128 resp.setHeader("Cache-Control","no-cache"); 129 resp.setDateHeader("Expires", System.currentTimeMillis()); 130 131 } else { 136 resp.setHeader("Cache-Control","max-age="+maxAge); 137 resp.setDateHeader("Last-Modified", System.currentTimeMillis()); 138 } 139 } 140 142 151 public void write(Node node, HttpServletResponse resp) throws IOException { 152 177 if (getContentType()==null) setContentType((node instanceof HTMLDocument) ? "text/html" : "text/xml"); 178 prepareResponse(resp); 179 181 write(node, resp.getWriter()); 183 } 184 185 188 public void write(Node node, OutputStream out) throws IOException { 189 if (node.getNodeType()==Node.DOCUMENT_NODE) { 190 DocumentFactory.getInstance().docManFor(node).serialize(out); 191 } else { 192 write(node, new PrintWriter (out)); 193 } 194 } 195 196 200 public void write(Node node, Writer writer) throws IOException { 201 switch(node.getNodeType()) { 202 case Node.DOCUMENT_NODE: 203 DocumentFactory.getInstance().docManFor(node).serialize(writer); 204 break; 205 case Node.ELEMENT_NODE: 206 DocumentManager man = DocumentFactory.getInstance().docManFor(node); 207 writer.write(man.serialize((Element )node, true)); 208 break; default: 210 throw new IllegalArgumentException ("Only Elements and Dcument can be serialized, not "+node); 211 } 212 if (!leaveWriterOpen) writer.close(); } 214 215 218 public int getMaxAge() { 219 return maxAge; 220 } 221 222 225 public void setMaxAge(int maxAge) { 226 this.maxAge = maxAge; 227 } 228 229 232 public boolean isPreventCaching() { 233 return preventCaching; 234 } 235 236 239 public void setPreventCaching(boolean preventCaching) { 240 this.preventCaching = preventCaching; 241 } 242 243 } | Popular Tags |