HttpResponseWrapper.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.core.monitor.rs.server.api.impl.utils;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;

  23. import javax.servlet.ServletOutputStream;
  24. import javax.servlet.WriteListener;
  25. import javax.servlet.http.HttpServletResponse;

  26. import org.openspcoop2.utils.transport.http.WrappedHttpServletResponse;

  27. /**
  28.  * HttpResponseWrapper
  29.  *
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  *
  33.  */
  34. public class HttpResponseWrapper extends WrappedHttpServletResponse  {

  35.     private ResponseOutputStream responseOutputStream;
  36.    
  37.     public HttpResponseWrapper(HttpServletResponse httpServletResponse) {
  38.         super(httpServletResponse);
  39.         this.responseOutputStream = new ResponseOutputStream();
  40.     }
  41.    
  42.     @Override
  43.     public ServletOutputStream getOutputStream() throws IOException {
  44.         return this.responseOutputStream;
  45.     }
  46.    
  47.     public byte[] toByteArray() {
  48.         return this.responseOutputStream.toByteArray();
  49.     }
  50.    
  51. }

  52. class ResponseOutputStream extends ServletOutputStream{

  53.     private ByteArrayOutputStream bout = new ByteArrayOutputStream();
  54.    
  55.     public byte[] toByteArray() {
  56.         return this.bout.toByteArray();
  57.     }
  58.    
  59.     @Override
  60.     public void write(byte[] b) throws IOException {
  61.         this.bout.write(b);
  62.     }

  63.     @Override
  64.     public void write(byte[] b, int off, int len) throws IOException {
  65.         this.bout.write(b, off, len);
  66.     }

  67.     @Override
  68.     public void flush() throws IOException {
  69.         this.bout.flush();
  70.     }

  71.     @Override
  72.     public void close() throws IOException {
  73.         this.bout.close();
  74.     }

  75.     @Override
  76.     public boolean isReady() {
  77.         return true;
  78.     }

  79.     @Override
  80.     public void setWriteListener(WriteListener arg0) {
  81.        
  82.     }

  83.     @Override
  84.     public void write(int b) throws IOException {
  85.         this.bout.write(b);
  86.     }
  87.    
  88. }