001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.mock.web.portlet;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import java.io.OutputStreamWriter;
023: import java.io.PrintWriter;
024: import java.io.UnsupportedEncodingException;
025: import java.io.Writer;
026: import java.util.Locale;
027:
028: import javax.portlet.PortalContext;
029: import javax.portlet.PortletURL;
030: import javax.portlet.RenderResponse;
031:
032: import org.springframework.web.util.WebUtils;
033:
034: /**
035: * Mock implementation of the {@link javax.portlet.RenderResponse} interface.
036: *
037: * @author John A. Lewis
038: * @author Juergen Hoeller
039: * @since 2.0
040: */
041: public class MockRenderResponse extends MockPortletResponse implements
042: RenderResponse {
043:
044: private String contentType;
045:
046: private String namespace = "MockPortlet";
047:
048: private String title;
049:
050: private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
051:
052: private PrintWriter writer;
053:
054: private Locale locale = Locale.getDefault();
055:
056: private int bufferSize = 4096;
057:
058: private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
059:
060: private boolean committed;
061:
062: private String includedUrl;
063:
064: /**
065: * Create a new MockRenderResponse with a default {@link MockPortalContext}.
066: * @see MockPortalContext
067: */
068: public MockRenderResponse() {
069: super ();
070: }
071:
072: /**
073: * Create a new MockRenderResponse.
074: * @param portalContext the PortalContext defining the supported
075: * PortletModes and WindowStates
076: */
077: public MockRenderResponse(PortalContext portalContext) {
078: super (portalContext);
079: }
080:
081: //---------------------------------------------------------------------
082: // RenderResponse methods
083: //---------------------------------------------------------------------
084:
085: public String getContentType() {
086: return this .contentType;
087: }
088:
089: public PortletURL createRenderURL() {
090: PortletURL url = new MockPortletURL(getPortalContext(),
091: MockPortletURL.URL_TYPE_RENDER);
092: return url;
093: }
094:
095: public PortletURL createActionURL() {
096: PortletURL url = new MockPortletURL(getPortalContext(),
097: MockPortletURL.URL_TYPE_ACTION);
098: return url;
099: }
100:
101: public String getNamespace() {
102: return this .namespace;
103: }
104:
105: public void setTitle(String title) {
106: this .title = title;
107: }
108:
109: public String getTitle() {
110: return title;
111: }
112:
113: public void setContentType(String contentType) {
114: this .contentType = contentType;
115: }
116:
117: public void setCharacterEncoding(String characterEncoding) {
118: this .characterEncoding = characterEncoding;
119: }
120:
121: public String getCharacterEncoding() {
122: return this .characterEncoding;
123: }
124:
125: public PrintWriter getWriter() throws UnsupportedEncodingException {
126: if (this .writer == null) {
127: Writer targetWriter = (this .characterEncoding != null ? new OutputStreamWriter(
128: this .outputStream, this .characterEncoding)
129: : new OutputStreamWriter(this .outputStream));
130: this .writer = new PrintWriter(targetWriter);
131: }
132: return this .writer;
133: }
134:
135: public byte[] getContentAsByteArray() {
136: flushBuffer();
137: return this .outputStream.toByteArray();
138: }
139:
140: public String getContentAsString()
141: throws UnsupportedEncodingException {
142: flushBuffer();
143: return (this .characterEncoding != null) ? this .outputStream
144: .toString(this .characterEncoding) : this .outputStream
145: .toString();
146: }
147:
148: public void setLocale(Locale locale) {
149: this .locale = locale;
150: }
151:
152: public Locale getLocale() {
153: return this .locale;
154: }
155:
156: public void setBufferSize(int bufferSize) {
157: this .bufferSize = bufferSize;
158: }
159:
160: public int getBufferSize() {
161: return this .bufferSize;
162: }
163:
164: public void flushBuffer() {
165: if (this .writer != null) {
166: this .writer.flush();
167: }
168: if (this .outputStream != null) {
169: try {
170: this .outputStream.flush();
171: } catch (IOException ex) {
172: throw new IllegalStateException(
173: "Could not flush OutputStream: "
174: + ex.getMessage());
175: }
176: }
177: this .committed = true;
178: }
179:
180: public void resetBuffer() {
181: if (this .committed) {
182: throw new IllegalStateException(
183: "Cannot reset buffer - response is already committed");
184: }
185: this .outputStream.reset();
186: }
187:
188: public void setCommitted(boolean committed) {
189: this .committed = committed;
190: }
191:
192: public boolean isCommitted() {
193: return this .committed;
194: }
195:
196: public void reset() {
197: resetBuffer();
198: this .characterEncoding = null;
199: this .contentType = null;
200: this .locale = null;
201: }
202:
203: public OutputStream getPortletOutputStream() throws IOException {
204: return this .outputStream;
205: }
206:
207: //---------------------------------------------------------------------
208: // Methods for MockPortletRequestDispatcher
209: //---------------------------------------------------------------------
210:
211: public void setIncludedUrl(String includedUrl) {
212: this .includedUrl = includedUrl;
213: }
214:
215: public String getIncludedUrl() {
216: return includedUrl;
217: }
218:
219: }
|