KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > portlet > MockRenderResponse


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.mock.web.portlet;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.OutputStreamWriter JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.util.Locale JavaDoc;
27
28 import javax.portlet.PortalContext;
29 import javax.portlet.PortletURL;
30 import javax.portlet.RenderResponse;
31
32 import org.springframework.web.util.WebUtils;
33
34 /**
35  * Mock implementation of the {@link javax.portlet.RenderResponse} interface.
36  *
37  * @author John A. Lewis
38  * @author Juergen Hoeller
39  * @since 2.0
40  */

41 public class MockRenderResponse extends MockPortletResponse implements RenderResponse {
42
43     private String JavaDoc contentType;
44
45     private String JavaDoc namespace = "MockPortlet";
46
47     private String JavaDoc title;
48
49     private String JavaDoc characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
50
51     private PrintWriter JavaDoc writer;
52
53     private Locale JavaDoc locale = Locale.getDefault();
54
55     private int bufferSize = 4096;
56
57     private final ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
58
59     private boolean committed;
60
61     private String JavaDoc includedUrl;
62
63
64     /**
65      * Create a new MockRenderResponse with a default {@link MockPortalContext}.
66      * @see MockPortalContext
67      */

68     public MockRenderResponse() {
69         super();
70     }
71
72     /**
73      * Create a new MockRenderResponse.
74      * @param portalContext the PortalContext defining the supported
75      * PortletModes and WindowStates
76      */

77     public MockRenderResponse(PortalContext portalContext) {
78         super(portalContext);
79     }
80
81
82     //---------------------------------------------------------------------
83
// RenderResponse methods
84
//---------------------------------------------------------------------
85

86     public String JavaDoc getContentType() {
87         return this.contentType;
88     }
89
90     public PortletURL createRenderURL() {
91         PortletURL url = new MockPortletURL(getPortalContext(), MockPortletURL.URL_TYPE_RENDER);
92         return url;
93     }
94
95     public PortletURL createActionURL() {
96         PortletURL url = new MockPortletURL(getPortalContext(), MockPortletURL.URL_TYPE_ACTION);
97         return url;
98     }
99
100     public String JavaDoc getNamespace() {
101         return this.namespace;
102     }
103
104     public void setTitle(String JavaDoc title) {
105         this.title = title;
106     }
107
108     public String JavaDoc getTitle() {
109         return title;
110     }
111
112     public void setContentType(String JavaDoc contentType) {
113         this.contentType = contentType;
114     }
115
116     public void setCharacterEncoding(String JavaDoc characterEncoding) {
117         this.characterEncoding = characterEncoding;
118     }
119
120     public String JavaDoc getCharacterEncoding() {
121         return this.characterEncoding;
122     }
123
124     public PrintWriter JavaDoc getWriter() throws UnsupportedEncodingException JavaDoc {
125         if (this.writer == null) {
126             Writer JavaDoc targetWriter = (this.characterEncoding != null
127                     ? new OutputStreamWriter JavaDoc(this.outputStream, this.characterEncoding)
128                     : new OutputStreamWriter JavaDoc(this.outputStream));
129             this.writer = new PrintWriter JavaDoc(targetWriter);
130         }
131         return this.writer;
132     }
133
134     public byte[] getContentAsByteArray() {
135         flushBuffer();
136         return this.outputStream.toByteArray();
137     }
138
139     public String JavaDoc getContentAsString() throws UnsupportedEncodingException JavaDoc {
140         flushBuffer();
141         return (this.characterEncoding != null)
142                 ? this.outputStream.toString(this.characterEncoding)
143                 : this.outputStream.toString();
144     }
145
146     public void setLocale(Locale JavaDoc locale) {
147         this.locale = locale;
148     }
149
150     public Locale JavaDoc getLocale() {
151         return this.locale;
152     }
153
154     public void setBufferSize(int bufferSize) {
155         this.bufferSize = bufferSize;
156     }
157
158     public int getBufferSize() {
159         return this.bufferSize;
160     }
161
162     public void flushBuffer() {
163         if (this.writer != null) {
164             this.writer.flush();
165         }
166         if (this.outputStream != null) {
167             try {
168                 this.outputStream.flush();
169             }
170             catch (IOException JavaDoc ex) {
171                 throw new IllegalStateException JavaDoc("Could not flush OutputStream: " + ex.getMessage());
172             }
173         }
174         this.committed = true;
175     }
176
177     public void resetBuffer() {
178         if (this.committed) {
179             throw new IllegalStateException JavaDoc("Cannot reset buffer - response is already committed");
180         }
181         this.outputStream.reset();
182     }
183
184     public void setCommitted(boolean committed) {
185         this.committed = committed;
186     }
187
188     public boolean isCommitted() {
189         return this.committed;
190     }
191
192     public void reset() {
193         resetBuffer();
194         this.characterEncoding = null;
195         this.contentType = null;
196         this.locale = null;
197     }
198
199     public OutputStream JavaDoc getPortletOutputStream() throws IOException JavaDoc {
200         return this.outputStream;
201     }
202
203
204     //---------------------------------------------------------------------
205
// Methods for MockPortletRequestDispatcher
206
//---------------------------------------------------------------------
207

208     public void setIncludedUrl(String JavaDoc includedUrl) {
209         this.includedUrl = includedUrl;
210     }
211
212     public String JavaDoc getIncludedUrl() {
213         return includedUrl;
214     }
215
216 }
217
Popular Tags