001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
019    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    
024    import java.io.IOException;
025    import java.io.PrintWriter;
026    import java.io.UnsupportedEncodingException;
027    
028    import java.util.Locale;
029    
030    import javax.servlet.ServletOutputStream;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Shuyang Zhou
036     */
037    public class StringServletResponse extends HeaderCacheServletResponse {
038    
039            public StringServletResponse(HttpServletResponse response) {
040                    super(response);
041            }
042    
043            public int getBufferSize() {
044                    return _bufferSize;
045            }
046    
047            public String getContentType() {
048                    return _contentType;
049            }
050    
051            public void flushBuffer() throws IOException {
052                    if (_servletOutputStream != null) {
053                            _unsyncByteArrayOutputStream.flush();
054                    }
055                    else if (_printWriter != null) {
056                            _unsyncStringWriter.flush();
057                    }
058            }
059    
060            public ServletOutputStream getOutputStream() {
061                    if (_printWriter != null) {
062                            throw new IllegalStateException(
063                                    "Cannot obtain OutputStream because Writer is already in use");
064                    }
065    
066                    if (_servletOutputStream == null) {
067                            _unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
068                            _servletOutputStream = new PipingServletOutputStream(
069                                    _unsyncByteArrayOutputStream);
070                    }
071    
072                    return _servletOutputStream;
073            }
074    
075            public int getStatus() {
076                    return _status;
077            }
078    
079            public String getString() {
080                    if (_string != null) {
081                            return _string;
082                    }
083    
084                    if (_servletOutputStream != null) {
085                            try {
086                                    _string = _unsyncByteArrayOutputStream.toString(
087                                            StringPool.UTF8);
088                            }
089                            catch (UnsupportedEncodingException uee) {
090                                    _log.error(uee, uee);
091    
092                                    _string = StringPool.BLANK;
093                            }
094                    }
095                    else if (_printWriter != null) {
096                            _string = _unsyncStringWriter.toString();
097                    }
098                    else {
099                            _string = StringPool.BLANK;
100                    }
101    
102                    return _string;
103            }
104    
105            public UnsyncByteArrayOutputStream getUnsyncByteArrayOutputStream() {
106                    return _unsyncByteArrayOutputStream;
107            }
108    
109            public PrintWriter getWriter() {
110                    if (_servletOutputStream != null) {
111                            throw new IllegalStateException(
112                                    "Cannot obtain Writer because OutputStream is already in use");
113                    }
114    
115                    if (_printWriter == null) {
116                            _unsyncStringWriter = new UnsyncStringWriter();
117                            _printWriter = new UnsyncPrintWriter(_unsyncStringWriter);
118                    }
119    
120                    return _printWriter;
121            }
122    
123            public boolean isCalledGetOutputStream() {
124                    if (_servletOutputStream != null) {
125                            return true;
126                    }
127                    else {
128                            return false;
129                    }
130            }
131    
132            public void recycle() {
133                    _status = SC_OK;
134                    _string = null;
135    
136                    resetBuffer();
137            }
138    
139            public void resetBuffer() {
140                    if (_servletOutputStream != null) {
141                            _unsyncByteArrayOutputStream.reset();
142                    }
143                    else if (_printWriter != null) {
144                            _unsyncStringWriter.reset();
145                    }
146            }
147    
148            public void sendError(int status) throws IOException {
149                    _status = status;
150    
151                    super.sendError(status);
152            }
153    
154            public void sendError(int status, String msg) throws IOException {
155                    _status = status;
156    
157                    super.sendError(status, msg);
158            }
159    
160            public void setBufferSize(int bufferSize) {
161                    _bufferSize = bufferSize;
162            }
163    
164            public void setContentType(String contentType) {
165                    _contentType = contentType;
166    
167                    super.setContentType(contentType);
168            }
169    
170            public void setLocale(Locale locale) {
171            }
172    
173            public void setStatus(int status) {
174                    _status = status;
175    
176                    super.setStatus(_status);
177            }
178    
179            public void setString(String string) {
180                    _string = string;
181            }
182    
183            private static Log _log = LogFactoryUtil.getLog(
184                    StringServletResponse.class);
185    
186            private int _bufferSize;
187            private String _contentType;
188            private PrintWriter _printWriter;
189            private ServletOutputStream _servletOutputStream;
190            private int _status = SC_OK;
191            private String _string;
192            private UnsyncByteArrayOutputStream _unsyncByteArrayOutputStream;
193            private UnsyncStringWriter _unsyncStringWriter;
194    
195    }