LCOV - code coverage report
Current view: top level - httpd - HttpServletResponseRecorder.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 16 23 69.6 %
Date: 2022-11-19 15:00:39 Functions: 5 8 62.5 %

          Line data    Source code
       1             : // Copyright (C) 2016 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.httpd;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import java.io.IOException;
      19             : import java.util.HashMap;
      20             : import java.util.Map;
      21             : import javax.servlet.http.HttpServletResponse;
      22             : import javax.servlet.http.HttpServletResponseWrapper;
      23             : 
      24             : /**
      25             :  * HttpServletResponse wrapper to allow response status code override.
      26             :  *
      27             :  * <p>Differently from the normal HttpServletResponse, this class allows multiple filters to
      28             :  * override the response http status code.
      29             :  */
      30             : public class HttpServletResponseRecorder extends HttpServletResponseWrapper {
      31           2 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      32             :   private static final String LOCATION_HEADER = "Location";
      33             : 
      34             :   private int status;
      35           2 :   private String statusMsg = "";
      36           2 :   private Map<String, String> headers = new HashMap<>();
      37             : 
      38             :   /**
      39             :    * Constructs a response recorder wrapping the given response.
      40             :    *
      41             :    * @param response the response to be wrapped
      42             :    */
      43             :   public HttpServletResponseRecorder(HttpServletResponse response) {
      44           2 :     super(response);
      45           2 :   }
      46             : 
      47             :   @Override
      48             :   public void sendError(int sc) throws IOException {
      49           0 :     this.status = sc;
      50           0 :   }
      51             : 
      52             :   @Override
      53             :   public void sendError(int sc, String msg) throws IOException {
      54           0 :     this.status = sc;
      55           0 :     this.statusMsg = msg;
      56           0 :   }
      57             : 
      58             :   @Override
      59             :   public void sendRedirect(String location) throws IOException {
      60           2 :     this.status = SC_MOVED_TEMPORARILY;
      61           2 :     setHeader(LOCATION_HEADER, location);
      62           2 :   }
      63             : 
      64             :   @Override
      65             :   public void setHeader(String name, String value) {
      66           2 :     super.setHeader(name, value);
      67           2 :     headers.put(name, value);
      68           2 :   }
      69             : 
      70             :   @SuppressWarnings({"all", "MissingOverride"})
      71             :   // @Override is omitted for backwards compatibility with servlet-api 2.5
      72             :   // TODO: Remove @SuppressWarnings and add @Override when Google upgrades
      73             :   //       to servlet-api 3.1
      74             :   public int getStatus() {
      75           0 :     return status;
      76             :   }
      77             : 
      78             :   void play() throws IOException {
      79           2 :     if (status != 0) {
      80           2 :       logger.atFine().log("Replaying %s %s", status, statusMsg);
      81             : 
      82           2 :       if (status == SC_MOVED_TEMPORARILY) {
      83           2 :         super.sendRedirect(headers.get(LOCATION_HEADER));
      84             :       } else {
      85           0 :         super.sendError(status, statusMsg);
      86             :       }
      87             :     }
      88           2 :   }
      89             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750