View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.servlet;
15  
16  import java.io.BufferedReader;
17  import java.io.IOException;
18  import java.io.InputStreamReader;
19  
20  import jakarta.servlet.ServletInputStream;
21  import jakarta.servlet.http.HttpServletRequest;
22  import jakarta.servlet.http.HttpServletRequestWrapper;
23  
24  import static ch.qos.logback.access.AccessConstants.LB_INPUT_BUFFER;
25  
26  /**
27   * As the "tee" program on Unix, duplicate the request's input stream.
28   *
29   * @author Ceki Gülcü
30   */
31  class TeeHttpServletRequest extends HttpServletRequestWrapper {
32  
33      private TeeServletInputStream inStream;
34      private BufferedReader reader;
35      boolean postedParametersMode = false;
36  
37      TeeHttpServletRequest(HttpServletRequest request) {
38          super(request);
39          // we can't access the input stream and access the request parameters
40          // at the same time
41          if (Util.isFormUrlEncoded(request)) {
42              postedParametersMode = true;
43          } else {
44              inStream = new TeeServletInputStream(request);
45              // add the contents of the input buffer as an attribute of the request
46              request.setAttribute(LB_INPUT_BUFFER, inStream.getInputBuffer());
47              reader = new BufferedReader(new InputStreamReader(inStream));
48          }
49  
50      }
51  
52      byte[] getInputBuffer() {
53          if (postedParametersMode) {
54              throw new IllegalStateException("Call disallowed in postedParametersMode");
55          }
56          return inStream.getInputBuffer();
57      }
58  
59      @Override
60      public ServletInputStream getInputStream() throws IOException {
61          if (!postedParametersMode) {
62              return inStream;
63          } else {
64              return super.getInputStream();
65          }
66      }
67  
68      //
69  
70      @Override
71      public BufferedReader getReader() throws IOException {
72          if (!postedParametersMode) {
73              return reader;
74          } else {
75              return super.getReader();
76          }
77      }
78  
79      public boolean isPostedParametersMode() {
80          return postedParametersMode;
81      }
82  
83  }