KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > cachius > ItemUpdater


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.cachius;
25
26 import java.io.File JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.OutputStreamWriter JavaDoc;
32 import java.io.UnsupportedEncodingException JavaDoc;
33 import java.io.Writer JavaDoc;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
45  * Class that updates a CacheItem after request processing has finished.
46  */

47 public class ItemUpdater {
48
49     private static final String JavaDoc FILE_ENCODING = "UTF-8";
50
51     private Log log = LogFactory.getLog(ItemUpdater.class);
52
53     private CacheItem cacheItem;
54
55     private HttpServletRequest JavaDoc request;
56
57     private File JavaDoc tempFile;
58
59     private boolean discard = false;
60
61     private Writer JavaDoc writer;
62
63     private OutputStream JavaDoc outputStream;
64
65     public ItemUpdater(CacheItem cacheItem, HttpServletRequest JavaDoc request) {
66         this.cacheItem = cacheItem;
67         this.request = request;
68         TaggingContext.openNestedContext(request);
69     }
70
71     protected File JavaDoc getTempFile() throws IOException JavaDoc {
72         if (tempFile == null) {
73             tempFile = cacheItem.createTempFile();
74         }
75         Assert.notNull(tempFile);
76         return tempFile;
77     }
78
79     public Writer JavaDoc getWriter() throws UnsupportedEncodingException JavaDoc,
80             FileNotFoundException JavaDoc {
81
82         if (writer == null) {
83             if (outputStream != null) {
84                 throw new IllegalStateException JavaDoc("getWriter() must not be "
85                         + "called after getOutputStream()!");
86             }
87             try {
88                 writer = new OutputStreamWriter JavaDoc(
89                         new FileOutputStream JavaDoc(getTempFile()), FILE_ENCODING);
90
91                 if (cacheItem.isFilterSessionId()) {
92                     writer = new TokenFilterWriter(request.getSession().getId(),
93                             "${jsessionid}", writer);
94                 }
95             }
96             catch (IOException JavaDoc e) {
97                 discard();
98                 log.error(e);
99             }
100         }
101         return writer;
102     }
103
104     public OutputStream JavaDoc getOutputStream() throws FileNotFoundException JavaDoc {
105         if (outputStream == null) {
106             if (writer != null) {
107                 throw new IllegalStateException JavaDoc("getOutputStream() must not be "
108                         + "called after getWriter()!");
109             }
110             try {
111                 outputStream = new FileOutputStream JavaDoc(getTempFile());
112             }
113             catch (IOException JavaDoc 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 JavaDoc 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