KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > JSPIncludeUtil


1 package info.magnolia.cms.util;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.PrintWriter JavaDoc;
6 import java.io.StringWriter JavaDoc;
7 import java.io.UnsupportedEncodingException JavaDoc;
8 import java.util.Locale JavaDoc;
9
10 import javax.servlet.ServletException JavaDoc;
11 import javax.servlet.ServletOutputStream JavaDoc;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
15
16 import org.apache.commons.lang.StringUtils;
17 import org.apache.taglibs.standard.resources.Resources;
18
19
20 /**
21  *
22  * Magnolia and its source-code is licensed under the LGPL.
23  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
24  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
25  * you are required to provide proper attribution to obinary.
26  * If you reproduce or distribute the document without making any substantive modifications to its content,
27  * please use the following attribution line:
28  *
29  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
30  *
31  */

32
33 /**
34  * We provide either a Writer or an OutputStream as requested. We actually have a true Writer and an OutputStream
35  * backing both, since we don't want to use a character encoding both ways (Writer -> OutputStream -> Writer). So we use
36  * no encoding at all (as none is relevant) when the target resource uses a Writer. And we decode the OutputStream's
37  * bytes using OUR tag's 'charEncoding' attribute, or ISO-8859-1 as the default. We thus ignore setLocale() and
38  * setContentType() in this wrapper. In other words, the target's asserted encoding is used to convert from a Writer to
39  * an OutputStream, which is typically the medium through with the target will communicate its ultimate response. Since
40  * we short-circuit that mechanism and read the target's characters directly if they're offered as such, we simply
41  * ignore the target's encoding assertion. The inner class ImportResponseWrapper is copied from the JSTL include tag
42  * @author philipp
43  */

44 public final class JSPIncludeUtil {
45
46     public static final String JavaDoc DEFAULT_ENCODING = "UTF-8"; //$NON-NLS-1$
47

48     /**
49      * Don't instantiate.
50      */

51     private JSPIncludeUtil() {
52         // unused
53
}
54
55     /** Wraps responses to allow us to retrieve results as Strings. */
56     private static class ImportResponseWrapper extends HttpServletResponseWrapper JavaDoc {
57
58         /** The Writer we convey. */
59         private StringWriter JavaDoc sw = new StringWriter JavaDoc();
60
61         /** A buffer, alternatively, to accumulate bytes. */
62         protected ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
63
64         /** A ServletOutputStream we convey, tied to this Writer. */
65         private ServletOutputStream JavaDoc sos = new ServletOutputStream JavaDoc() {
66
67             public void write(int b) throws IOException JavaDoc {
68                 bos.write(b);
69             }
70         };
71
72         /** 'True' if getWriter() was called; false otherwise. */
73         private boolean isWriterUsed;
74
75         /** 'True if getOutputStream() was called; false otherwise. */
76         private boolean isStreamUsed;
77
78         /** The HTTP status set by the target. */
79         private int status = 200;
80
81         /** Constructs a new ImportResponseWrapper. */
82         public ImportResponseWrapper(HttpServletResponse JavaDoc response) {
83             super(response);
84         }
85
86         /**
87          * Returns a Writer designed to buffer the output.
88          */

89         public PrintWriter JavaDoc getWriter() {
90             if (isStreamUsed) {
91                 throw new IllegalStateException JavaDoc(Resources.getMessage("IMPORT_ILLEGAL_STREAM")); //$NON-NLS-1$
92
}
93             isWriterUsed = true;
94             return new PrintWriter JavaDoc(sw);
95         }
96
97         /**
98          * Returns a ServletOutputStream designed to buffer the output.
99          */

100         public ServletOutputStream JavaDoc getOutputStream() {
101             if (isWriterUsed) {
102                 throw new IllegalStateException JavaDoc(Resources.getMessage("IMPORT_ILLEGAL_WRITER")); //$NON-NLS-1$
103
}
104             isStreamUsed = true;
105             return sos;
106         }
107
108         /**
109          * Has no effect.
110          */

111         public void setContentType(String JavaDoc x) {
112             // ignore
113
}
114
115         /** Has no effect. */
116         public void setLocale(Locale JavaDoc x) {
117             // ignore
118
}
119
120         public void setStatus(int status) {
121             this.status = status;
122         }
123
124         public int getStatus() {
125             return status;
126         }
127
128         /**
129          * Retrieves the buffered output, using the containing tag's 'charEncoding' attribute, or the tag's default
130          * encoding, <b>if necessary</b>.
131          */

132         // not simply toString() because we need to throw
133
// UnsupportedEncodingException
134
public String JavaDoc getString() throws UnsupportedEncodingException JavaDoc {
135             if (isWriterUsed) {
136                 return sw.toString();
137             }
138             else if (isStreamUsed) {
139                 return bos.toString(DEFAULT_ENCODING);
140             }
141             else {
142                 return StringUtils.EMPTY; // target didn't write anything
143
}
144         }
145     }
146
147     public static String JavaDoc get(String JavaDoc jsp, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
148         throws ServletException JavaDoc, IOException JavaDoc {
149         ImportResponseWrapper wrappedResponse = new ImportResponseWrapper(response);
150         request.getRequestDispatcher(jsp).include(request, wrappedResponse);
151         return wrappedResponse.getString();
152     }
153 }
154
Popular Tags