001    /**
002     * Copyright (c) 2000-2013 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 int getContentLength() {
038                    return _contentLength;
039            }
040    
041            @Override
042            public String getContentType() {
043                    return _contentType;
044            }
045    
046            public byte[] getData() {
047                    return _ubaos.toByteArray();
048            }
049    
050            @Override
051            public ServletOutputStream getOutputStream() {
052                    return new GenericServletOutputStream(_ubaos);
053            }
054    
055            @Override
056            public PrintWriter getWriter() {
057                    return UnsyncPrintWriterPool.borrow(getOutputStream());
058            }
059    
060            @Override
061            public void setContentLength(int length) {
062                    super.setContentLength(length);
063    
064                    _contentLength = length;
065            }
066    
067            @Override
068            public void setContentType(String type) {
069                    super.setContentType(type);
070    
071                    _contentType = type;
072            }
073    
074            private int _contentLength;
075            private String _contentType;
076            private UnsyncByteArrayOutputStream _ubaos;
077    
078    }