001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.util;
014
015import java.io.ByteArrayOutputStream;
016import java.io.IOException;
017
018import jakarta.servlet.ServletOutputStream;
019import jakarta.servlet.WriteListener;
020import jakarta.servlet.http.*;
021
022/**
023 * Wraps an {@link HttpServletResponse} and caches the output stream in a separate buffer for debugging purposes.
024 *
025 * <h5 class='section'>See Also:</h5><ul>
026 * </ul>
027 */
028public class CachingHttpServletResponse extends HttpServletResponseWrapper {
029
030   final ByteArrayOutputStream baos = new ByteArrayOutputStream();
031   final ServletOutputStream os;
032
033   /**
034    * Wraps the specified response inside a {@link CachingHttpServletResponse} if it isn't already.
035    *
036    * @param res The response to wrap.
037    * @return The wrapped request.
038    * @throws IOException Thrown by underlying content stream.
039    */
040   public static CachingHttpServletResponse wrap(HttpServletResponse res) throws IOException {
041      if (res instanceof CachingHttpServletResponse)
042         return (CachingHttpServletResponse)res;
043      return new CachingHttpServletResponse(res);
044   }
045
046   /**
047    * Constructor.
048    *
049    * @param res The wrapped servlet response.
050    * @throws IOException Thrown by underlying stream.
051    */
052   protected CachingHttpServletResponse(HttpServletResponse res) throws IOException {
053      super(res);
054      os = res.getOutputStream();
055   }
056
057   @Override
058   public ServletOutputStream getOutputStream() throws IOException {
059      return new ServletOutputStream() {
060
061         @Override
062         public boolean isReady() {
063            return os.isReady();
064         }
065
066         @Override
067         public void setWriteListener(WriteListener writeListener) {
068            os.setWriteListener(writeListener);
069         }
070
071         @Override
072         public void write(int b) throws IOException {
073            baos.write(b);
074            os.write(b);
075         }
076
077         @Override
078         public void flush() throws IOException {
079            os.flush();
080         }
081
082         @Override
083         public void close() throws IOException {
084            os.close();
085         }
086      };
087   }
088
089   /**
090    * Returns the content of the servlet response without consuming the stream.
091    *
092    * @return The content of the response.
093    */
094   public byte[] getContent() {
095      return baos.toByteArray();
096   }
097}