001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 void flushBuffer() throws IOException {
048                    if (_servletOutputStream != null) {
049                            _unsyncByteArrayOutputStream.flush();
050                    }
051                    else if (_printWriter != null) {
052                            _unsyncStringWriter.flush();
053                    }
054            }
055    
056            public ServletOutputStream getOutputStream() {
057                    if (_printWriter != null) {
058                            throw new IllegalStateException(
059                                    "Cannot obtain OutputStream because Writer is already in use");
060                    }
061    
062                    if (_servletOutputStream == null) {
063                            _unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
064                            _servletOutputStream = new PipingServletOutputStream(
065                                    _unsyncByteArrayOutputStream);
066                    }
067    
068                    return _servletOutputStream;
069            }
070    
071            public String getString() {
072                    if (_string != null) {
073                            return _string;
074                    }
075    
076                    if (_servletOutputStream != null) {
077                            try {
078                                    _string = _unsyncByteArrayOutputStream.toString(
079                                            StringPool.UTF8);
080                            }
081                            catch (UnsupportedEncodingException uee) {
082                                    _log.error(uee, uee);
083    
084                                    _string = StringPool.BLANK;
085                            }
086                    }
087                    else if (_printWriter != null) {
088                            _string = _unsyncStringWriter.toString();
089                    }
090                    else {
091                            _string = StringPool.BLANK;
092                    }
093    
094                    return _string;
095            }
096    
097            public UnsyncByteArrayOutputStream getUnsyncByteArrayOutputStream() {
098                    return _unsyncByteArrayOutputStream;
099            }
100    
101            public PrintWriter getWriter() {
102                    if (_servletOutputStream != null) {
103                            throw new IllegalStateException(
104                                    "Cannot obtain Writer because OutputStream is already in use");
105                    }
106    
107                    if (_printWriter == null) {
108                            _unsyncStringWriter = new UnsyncStringWriter();
109                            _printWriter = new UnsyncPrintWriter(_unsyncStringWriter);
110                    }
111    
112                    return _printWriter;
113            }
114    
115            public boolean isCalledGetOutputStream() {
116                    if (_servletOutputStream != null) {
117                            return true;
118                    }
119                    else {
120                            return false;
121                    }
122            }
123    
124            public boolean isCalledGetWriter() {
125                    if (_printWriter != null) {
126                            return true;
127                    }
128                    else {
129                            return false;
130                    }
131            }
132    
133            public void recycle() {
134                    _string = null;
135    
136                    setStatus(SC_OK);
137    
138                    resetBuffer();
139            }
140    
141            public void resetBuffer() {
142                    if (_servletOutputStream != null) {
143                            _unsyncByteArrayOutputStream.reset();
144                    }
145                    else if (_printWriter != null) {
146                            _unsyncStringWriter.reset();
147                    }
148            }
149    
150            public void setBufferSize(int bufferSize) {
151                    _bufferSize = bufferSize;
152            }
153    
154            public void setLocale(Locale locale) {
155            }
156    
157            public void setString(String string) {
158                    _string = string;
159            }
160    
161            private static Log _log = LogFactoryUtil.getLog(
162                    StringServletResponse.class);
163    
164            private int _bufferSize;
165            private PrintWriter _printWriter;
166            private ServletOutputStream _servletOutputStream;
167            private String _string;
168            private UnsyncByteArrayOutputStream _unsyncByteArrayOutputStream;
169            private UnsyncStringWriter _unsyncStringWriter;
170    
171    }