001    /**
002     * Copyright (c) 2000-2010 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.io.unsync.UnsyncPrintWriter;
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            public void setContentLength(int length) {
046                    super.setContentLength(length);
047    
048                    _contentLength = length;
049            }
050    
051            public String getContentType() {
052                    return _contentType;
053            }
054    
055            public void setContentType(String type) {
056                    super.setContentType(type);
057    
058                    _contentType = type;
059            }
060    
061            public ServletOutputStream getOutputStream() {
062                    return new GenericServletOutputStream(_ubaos);
063            }
064    
065            public PrintWriter getWriter() {
066                    return new UnsyncPrintWriter(getOutputStream(), true);
067            }
068    
069            private int _contentLength;
070            private String _contentType;
071            private UnsyncByteArrayOutputStream _ubaos;
072    
073    }