001: /*
002: * (C) Copyright 2000 - 2003 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package com.nabhinc.portal.container;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.OutputStream;
025: import java.io.OutputStreamWriter;
026: import java.io.PrintWriter;
027: import java.util.Locale;
028:
029: import javax.portlet.PortletURL;
030: import javax.portlet.RenderResponse;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: /**
035: *
036: *
037: * @author Padmanabh dabke
038: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
039: */
040: public class RenderResponseImpl extends PortletResponseImpl implements
041: RenderResponse {
042:
043: private String rriBaseURL = "";
044:
045: private boolean rriDetached = false;
046:
047: private boolean rriOutputStreamRequested = false;
048:
049: private int rriBufferSize = 1000;
050:
051: private String rriContentType = null;
052:
053: private String rriTitle = null;
054:
055: private PrintWriter rriWriter = null;
056:
057: private ByteArrayOutputStream rriOutputStream = null;
058:
059: private HttpServletRequest rriRequest = null;
060:
061: protected String rriClientMimeType = null;
062:
063: protected String rriSecureURLPrefix = null;
064:
065: /**
066: *
067: * @param req
068: * @param resp
069: * @param renderURL
070: * @param actionURL
071: * @param portletScope
072: * @param queryParams
073: */
074: public RenderResponseImpl(HttpServletRequest req,
075: HttpServletResponse resp, String clientMimeType) {
076: super (resp);
077: rriRequest = req;
078: rriClientMimeType = clientMimeType;
079: }
080:
081: /**
082: * @see javax.portlet.RenderResponse#getContentType()
083: */
084: public String getContentType() {
085: return rriContentType;
086: }
087:
088: /**
089: * @see javax.portlet.RenderResponse#createRenderURL()
090: */
091: public PortletURL createRenderURL() {
092: return new RenderURLImpl(priPortalInterface, rriRequest,
093: rriClientMimeType, rriBaseURL);
094: }
095:
096: /**
097: * @see javax.portlet.RenderResponse#createActionURL()
098: */
099: public PortletURL createActionURL() {
100: return new ActionURLImpl(priPortalInterface, rriRequest,
101: rriClientMimeType, rriBaseURL);
102: }
103:
104: /**
105: * @see javax.portlet.RenderResponse#getNamespace()
106: */
107: public String getNamespace() {
108: return priPortalInterface.getPortletWindowId();
109: }
110:
111: /**
112: * @see javax.portlet.RenderResponse#setTitle(java.lang.String)
113: */
114: public void setTitle(String title) {
115: rriTitle = title;
116:
117: }
118:
119: public String getTitle() {
120: return rriTitle;
121: }
122:
123: /**
124: * @see javax.portlet.RenderResponse#setContentType(java.lang.String)
125: */
126: public void setContentType(String type) {
127: rriContentType = type;
128:
129: }
130:
131: /**
132: * @see javax.portlet.RenderResponse#getCharacterEncoding()
133: */
134: public String getCharacterEncoding() {
135: return priResponse.getCharacterEncoding();
136: }
137:
138: /**
139: * @see javax.portlet.RenderResponse#getWriter()
140: */
141: public PrintWriter getWriter() throws IOException {
142: if (rriOutputStreamRequested)
143: throw new IllegalStateException(
144: "Portlet writer cannot be requested after requesting the output stream.");
145: rriOutputStream = new ByteArrayOutputStream(rriBufferSize);
146: rriWriter = new PrintWriter(new OutputStreamWriter(
147: rriOutputStream, "UTF-8"));
148: return rriWriter;
149: }
150:
151: /**
152: * @see javax.portlet.RenderResponse#getLocale()
153: */
154: public Locale getLocale() {
155: return priResponse.getLocale();
156: }
157:
158: /**
159: * @see javax.portlet.RenderResponse#setBufferSize(int)
160: */
161: public void setBufferSize(int size) {
162: //rriBufferSize = size;
163:
164: }
165:
166: /**
167: * @see javax.portlet.RenderResponse#getBufferSize()
168: */
169: public int getBufferSize() {
170: return Integer.MAX_VALUE;
171: }
172:
173: /**
174: * @see javax.portlet.RenderResponse#flushBuffer()
175: */
176: public void flushBuffer() throws IOException {
177: // we don't flush it untill all portlet content has been written
178:
179: }
180:
181: /**
182: * @see javax.portlet.RenderResponse#resetBuffer()
183: */
184: public void resetBuffer() {
185: rriOutputStream.reset();
186:
187: }
188:
189: /**
190: * @see javax.portlet.RenderResponse#isCommitted()
191: */
192: public boolean isCommitted() {
193: return false;
194: }
195:
196: /**
197: * @see javax.portlet.RenderResponse#reset()
198: */
199: public void reset() {
200: rriOutputStream.reset();
201: clearProperties();
202: }
203:
204: /**
205: * @see javax.portlet.RenderResponse#getPortletOutputStream()
206: */
207: public OutputStream getPortletOutputStream() throws IOException {
208: if (rriWriter != null)
209: throw new IllegalStateException(
210: "Portlet output stream cannot be requested after requesting the writer.");
211:
212: if (rriOutputStream == null) {
213: rriOutputStream = new ByteArrayOutputStream(rriBufferSize);
214: }
215:
216: rriOutputStreamRequested = true;
217: return rriOutputStream;
218: }
219:
220: /**
221: *
222: * @return
223: * @throws IOException
224: */
225: public String getContent() throws IOException {
226: if (rriWriter != null)
227: rriWriter.flush();
228: if (rriOutputStream == null) {
229: return null;
230: } else {
231: rriOutputStream.flush();
232: return rriOutputStream.toString("UTF-8");
233: }
234: }
235:
236: /**
237: *
238: */
239: public void setHttpServletResponse(HttpServletResponse resp) {
240: super .setHttpServletResponse(resp);
241: rriContentType = null;
242: rriOutputStream = null;
243: rriWriter = null;
244: rriOutputStreamRequested = false;
245: }
246:
247: /**
248: *
249: * @param req
250: */
251: public void setServletRequest(HttpServletRequest req) {
252: rriRequest = req;
253: }
254:
255: public String getBaseURL() {
256: return this .rriBaseURL;
257: }
258:
259: public void setBaseURL(String dMode) {
260: this .rriBaseURL = dMode;
261: }
262:
263: public boolean isDetached() {
264: return rriDetached;
265: }
266:
267: public void setDetached(boolean flag) {
268: rriDetached = flag;
269: }
270:
271: public void resetResponse() {
272: this .rriOutputStream = null;
273: this .rriWriter = null;
274: this .rriOutputStreamRequested = false;
275: clearProperties();
276: }
277:
278: }
|