1 24 package org.riotfamily.cachius; 25 26 import java.io.File ; 27 import java.io.FileNotFoundException ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.OutputStream ; 31 import java.io.OutputStreamWriter ; 32 import java.io.UnsupportedEncodingException ; 33 import java.io.Writer ; 34 35 import javax.servlet.http.HttpServletRequest ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.riotfamily.cachius.support.SessionUtils; 40 import org.riotfamily.cachius.support.TokenFilterWriter; 41 import org.riotfamily.common.io.IOUtils; 42 import org.springframework.util.Assert; 43 44 47 public class ItemUpdater { 48 49 private static final String FILE_ENCODING = "UTF-8"; 50 51 private Log log = LogFactory.getLog(ItemUpdater.class); 52 53 private CacheItem cacheItem; 54 55 private HttpServletRequest request; 56 57 private File tempFile; 58 59 private boolean discard = false; 60 61 private Writer writer; 62 63 private OutputStream outputStream; 64 65 public ItemUpdater(CacheItem cacheItem, HttpServletRequest request) { 66 this.cacheItem = cacheItem; 67 this.request = request; 68 TaggingContext.openNestedContext(request); 69 } 70 71 protected File getTempFile() throws IOException { 72 if (tempFile == null) { 73 tempFile = cacheItem.createTempFile(); 74 } 75 Assert.notNull(tempFile); 76 return tempFile; 77 } 78 79 public Writer getWriter() throws UnsupportedEncodingException , 80 FileNotFoundException { 81 82 if (writer == null) { 83 if (outputStream != null) { 84 throw new IllegalStateException ("getWriter() must not be " 85 + "called after getOutputStream()!"); 86 } 87 try { 88 writer = new OutputStreamWriter ( 89 new FileOutputStream (getTempFile()), FILE_ENCODING); 90 91 if (cacheItem.isFilterSessionId()) { 92 writer = new TokenFilterWriter(request.getSession().getId(), 93 "${jsessionid}", writer); 94 } 95 } 96 catch (IOException e) { 97 discard(); 98 log.error(e); 99 } 100 } 101 return writer; 102 } 103 104 public OutputStream getOutputStream() throws FileNotFoundException { 105 if (outputStream == null) { 106 if (writer != null) { 107 throw new IllegalStateException ("getOutputStream() must not be " 108 + "called after getWriter()!"); 109 } 110 try { 111 outputStream = new FileOutputStream (getTempFile()); 112 } 113 catch (IOException e) { 114 discard(); 115 log.error(e); 116 } 117 } 118 return outputStream; 119 } 120 121 public void discard() { 122 discard = true; 123 } 124 125 public void setContentType(String contentType) { 126 cacheItem.setContentType(contentType); 127 } 128 129 public void updateCacheItem() { 130 cacheItem.setTags(TaggingContext.popTags(request)); 131 IOUtils.closeStream(outputStream); 132 IOUtils.closeWriter(writer); 133 if (!discard) { 134 if (tempFile == null) { 135 log.debug("No content for item " + cacheItem.getKey()); 136 return; 137 } 138 if (writer != null) { 139 if (SessionUtils.sessionStateChanged(request)) { 140 log.debug("Session state has changed during " + 141 "processing. Since possibly not all URLs " + 142 "are encoded the response is NOT cached."); 143 144 return; 145 } 146 } 147 cacheItem.update(tempFile, outputStream != null); 148 } 149 else { 150 IOUtils.delete(tempFile); 151 tempFile = null; 152 } 153 } 154 155 156 public void finalize() { 157 IOUtils.delete(tempFile); 158 } 159 } 160 | Popular Tags |