01: package com.opensymphony.sitemesh.webapp;
02:
03: import com.opensymphony.module.sitemesh.filter.PageResponseWrapper;
04: import com.opensymphony.module.sitemesh.PageParserSelector;
05: import com.opensymphony.module.sitemesh.PageParser;
06: import com.opensymphony.sitemesh.ContentProcessor;
07: import com.opensymphony.sitemesh.Content;
08:
09: import javax.servlet.http.HttpServletResponseWrapper;
10: import javax.servlet.http.HttpServletResponse;
11: import java.io.IOException;
12:
13: /**
14: * @author Joe Walnes
15: * @since SiteMesh 3
16: */
17: public class ContentBufferingResponse extends
18: HttpServletResponseWrapper {
19:
20: // TODO: Temporary SM3 migration implementation! Wraps SM2 PageResponseWrapper. This class is an evil stopgap.
21: // Eventually PageResponseWrapper will go away and the functionality will be rolled into this class.
22:
23: private final PageResponseWrapper pageResponseWrapper;
24: private final ContentProcessor contentProcessor;
25: private final SiteMeshWebAppContext webAppContext;
26:
27: public ContentBufferingResponse(HttpServletResponse response,
28: final ContentProcessor contentProcessor,
29: final SiteMeshWebAppContext webAppContext) {
30: super (new PageResponseWrapper(response,
31: new PageParserSelector() {
32: public boolean shouldParsePage(String contentType) {
33: return contentProcessor.handles(contentType);
34: }
35:
36: public PageParser getPageParser(String contentType) {
37: // Migration: Not actually needed by PageResponseWrapper, so long as getPage() isn't called.
38: return null;
39: }
40: }) {
41: public void setContentType(String contentType) {
42: webAppContext.setContentType(contentType);
43: super .setContentType(contentType);
44: }
45: });
46: this .contentProcessor = contentProcessor;
47: this .webAppContext = webAppContext;
48: pageResponseWrapper = (PageResponseWrapper) getResponse();
49: }
50:
51: public boolean isUsingStream() {
52: return pageResponseWrapper.isUsingStream();
53: }
54:
55: public Content getContent() throws IOException {
56: char[] data = pageResponseWrapper.getContents();
57: if (data != null) {
58: return contentProcessor.build(data, webAppContext);
59: } else {
60: return null;
61: }
62: }
63: }
|