1 /*
2  * Copyright 2008-2019 by Emeric Vernat
3  *
4  *     This file is part of Java Melody.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.bull.javamelody.internal.web;
19
20 import java.io.IOException;
21
22 import javax.servlet.ServletOutputStream;
23 import javax.servlet.http.HttpServletResponse;
24
25 /**
26  * Implémentation de {@link FilterServletResponseWrapper} qui fonctionne avec le {@link CounterResponseStream},
27  * pour calculer la taille du flux de réponse.
28  * @author Emeric Vernat
29  */

30 public class CounterServletResponseWrapper extends FilterServletResponseWrapper {
31     /**
32      * Constructeur qui crée un adapteur de HttpServletResponse wrappant la response spécifiée.
33      * @param response HttpServletResponse
34      */

35     public CounterServletResponseWrapper(HttpServletResponse response) {
36         super(response);
37         assert response != null;
38     }
39
40     /**
41      * Retourne la taille en octets du flux écrit dans la réponse.
42      * @return long
43      */

44     public long getDataLength() {
45         return getCounterResponseStream() == null ? 0 : getCounterResponseStream().getDataLength();
46     }
47
48     /** {@inheritDoc} */
49     @Override
50     public void reset() {
51         super.reset();
52         resetStream();
53     }
54
55     /** {@inheritDoc} */
56     @Override
57     public void resetBuffer() {
58         super.resetBuffer();
59         resetStream();
60     }
61
62     private void resetStream() {
63         if (getCounterResponseStream() != null) {
64             getCounterResponseStream().reset();
65         }
66     }
67
68     private CounterResponseStream getCounterResponseStream() {
69         return (CounterResponseStream) getStream();
70     }
71
72     /** {@inheritDoc} */
73     @Override
74     public ServletOutputStream createOutputStream() throws IOException {
75         return new CounterResponseStream(getHttpServletResponse());
76     }
77 }
78