001    /**
002     * Copyright (c) 2000-2012 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.util.servlet;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.util.UnsyncPrintWriterPool;
019    
020    import java.io.PrintWriter;
021    
022    import javax.servlet.ServletOutputStream;
023    import javax.servlet.http.HttpServletResponse;
024    import javax.servlet.http.HttpServletResponseWrapper;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class GenericServletResponse extends HttpServletResponseWrapper {
030    
031            public GenericServletResponse(HttpServletResponse response) {
032                    super(response);
033    
034                    _ubaos = new UnsyncByteArrayOutputStream();
035            }
036    
037            public byte[] getData() {
038                    return _ubaos.toByteArray();
039            }
040    
041            public int getContentLength() {
042                    return _contentLength;
043            }
044    
045            @Override
046            public void setContentLength(int length) {
047                    super.setContentLength(length);
048    
049                    _contentLength = length;
050            }
051    
052            @Override
053            public String getContentType() {
054                    return _contentType;
055            }
056    
057            @Override
058            public void setContentType(String type) {
059                    super.setContentType(type);
060    
061                    _contentType = type;
062            }
063    
064            @Override
065            public ServletOutputStream getOutputStream() {
066                    return new GenericServletOutputStream(_ubaos);
067            }
068    
069            @Override
070            public PrintWriter getWriter() {
071                    return UnsyncPrintWriterPool.borrow(getOutputStream());
072            }
073    
074            private int _contentLength;
075            private String _contentType;
076            private UnsyncByteArrayOutputStream _ubaos;
077    
078    }