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