LCOV - code coverage report
Current view: top level - acceptance - HttpResponse.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 20 23 87.0 %
Date: 2022-11-19 15:00:39 Functions: 8 9 88.9 %

          Line data    Source code
       1             : // Copyright (C) 2014 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.acceptance;
      16             : 
      17             : import static com.google.common.collect.ImmutableList.toImmutableList;
      18             : import static java.nio.charset.StandardCharsets.UTF_8;
      19             : import static java.util.Objects.requireNonNull;
      20             : 
      21             : import com.google.common.collect.ImmutableList;
      22             : import com.google.gerrit.common.Nullable;
      23             : import java.io.IOException;
      24             : import java.io.InputStreamReader;
      25             : import java.io.Reader;
      26             : import java.nio.ByteBuffer;
      27             : import java.util.Arrays;
      28             : import org.apache.http.Header;
      29             : import org.eclipse.jgit.util.IO;
      30             : import org.eclipse.jgit.util.RawParseUtils;
      31             : 
      32             : public class HttpResponse {
      33             : 
      34             :   protected org.apache.http.HttpResponse response;
      35             :   protected Reader reader;
      36             : 
      37          29 :   HttpResponse(org.apache.http.HttpResponse response) {
      38          29 :     this.response = response;
      39          29 :   }
      40             : 
      41             :   public Reader getReader() throws IllegalStateException, IOException {
      42           0 :     if (reader == null && response.getEntity() != null) {
      43           0 :       reader = new InputStreamReader(response.getEntity().getContent(), UTF_8);
      44             :     }
      45           0 :     return reader;
      46             :   }
      47             : 
      48             :   public void consume() throws IllegalStateException, IOException {
      49           8 :     Reader reader = getReader();
      50           8 :     if (reader != null) {
      51           8 :       while (reader.read() != -1) {}
      52             :     }
      53           8 :   }
      54             : 
      55             :   public int getStatusCode() {
      56          28 :     return response.getStatusLine().getStatusCode();
      57             :   }
      58             : 
      59             :   public String getContentType() {
      60           1 :     return getHeader("X-FYI-Content-Type");
      61             :   }
      62             : 
      63             :   @Nullable
      64             :   public String getHeader(String name) {
      65           3 :     Header hdr = response.getFirstHeader(name);
      66           3 :     return hdr != null ? hdr.getValue() : null;
      67             :   }
      68             : 
      69             :   public ImmutableList<String> getHeaders(String name) {
      70           1 :     return Arrays.asList(response.getHeaders(name)).stream()
      71           1 :         .map(Header::getValue)
      72           1 :         .collect(toImmutableList());
      73             :   }
      74             : 
      75             :   public boolean hasContent() {
      76           2 :     requireNonNull(response, "Response is not initialized.");
      77           2 :     return response.getEntity() != null;
      78             :   }
      79             : 
      80             :   public String getEntityContent() throws IOException {
      81          10 :     requireNonNull(response, "Response is not initialized.");
      82          10 :     requireNonNull(response.getEntity(), "Response.Entity is not initialized.");
      83          10 :     ByteBuffer buf = IO.readWholeStream(response.getEntity().getContent(), 1024);
      84          10 :     return RawParseUtils.decode(buf.array(), buf.arrayOffset(), buf.limit()).trim();
      85             :   }
      86             : }

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