1 12 13 17 package com.openedit.generators; 18 19 import java.io.InputStream ; 20 import java.io.InputStreamReader ; 21 22 import javax.servlet.http.HttpServletResponse ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import com.openedit.Generator; 28 import com.openedit.OpenEditException; 29 import com.openedit.WebPageRequest; 30 import com.openedit.error.ContentNotAvailableException; 31 import com.openedit.page.Page; 32 import com.openedit.util.FileUtils; 33 34 39 public class FileGenerator extends BaseGenerator implements Generator 40 { 41 private static Log log = LogFactory.getLog(FileGenerator.class); 42 43 public void generate( WebPageRequest inContext, Page inPage, Output inOut ) throws OpenEditException 44 { 45 if( !inPage.exists() ) 46 { 47 String vir = inPage.get("virtual"); 48 if ( !Boolean.parseBoolean(vir) ) 49 { 50 log.info("Missing: " +inPage.getPath()); 51 throw new ContentNotAvailableException("Missing: " +inPage.getPath(),inPage.getPath()); 52 } 53 else 54 { 55 return; } 57 } 58 HttpServletResponse res = inContext.getResponse(); 59 if ( res != null && inContext.getContentPage() == inPage ) 61 { 62 res.setDateHeader("Last-Modified",inPage.getLastModified().getTime()); 63 long now = System.currentTimeMillis(); 64 boolean cache = true; 65 String nocache = inContext.getRequestParameter("cache"); 66 if( nocache != null ) 67 { 68 cache = Boolean.parseBoolean(nocache); 69 } 70 else 71 { 72 cache = inPage.getLastModified().getTime() + (1000 * 60 * 60 * 24 ) < now; 75 } 76 77 if( cache ) 78 { 79 res.setDateHeader("Expires", now + (1000 * 60 * 60 * 24 )); } 81 else 82 { 83 res.setDateHeader("Expires", now - (1000 * 60 * 60 * 24)); } 85 if ( inPage.isBinary() ) 87 { int len = (int)inPage.getContentItem().getLength(); 89 if ( len != -1) 90 { 91 res.setContentLength(len); 92 } 93 } 94 } 95 InputStream in = inPage.getInputStream(); 96 97 try 98 { 99 if ( inPage.isBinary() ) 100 { 101 getOutputFiller().fill(in, inOut.getStream()); 102 } 103 else 104 { 105 InputStreamReader reader = null; 106 if ( inPage.getCharacterEncoding() != null ) 107 { 108 reader = new InputStreamReader ( in, inPage.getCharacterEncoding() ); 109 } 110 else 111 { 112 reader = new InputStreamReader ( in ); 113 } 114 getOutputFiller().fill(reader, inOut.getWriter()); 115 } 116 } 117 catch ( Exception eof ) 118 { 119 if( ignoreError(eof)) 120 { 121 return; 123 } 124 throw new OpenEditException(eof); 125 } 126 finally 127 { 128 FileUtils.safeClose(in); 129 } 130 131 } 132 public boolean canGenerate(WebPageRequest inReq) 133 { 134 return true; 135 } 136 } 137 | Popular Tags |