Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

OkHttpResponse.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarqube.ws.client;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.Reader;
  24. import java.util.Optional;
  25. import okhttp3.Response;
  26. import okhttp3.ResponseBody;
  27. class OkHttpResponse extends BaseResponse {
  28. private final Response okResponse;
  29. OkHttpResponse(Response okResponse) {
  30. this.okResponse = okResponse;
  31. }
  32. @Override
  33. public int code() {
  34. return okResponse.code();
  35. }
  36. @Override
  37. public String requestUrl() {
  38. return okResponse.request().url().toString();
  39. }
  40. @Override
  41. public String contentType() {
  42. return okResponse.header("Content-Type");
  43. }
  44. @Override
  45. public Optional<String> header(String name) {
  46. return Optional.ofNullable(okResponse.header(name));
  47. }
  48. /**
  49. * Get stream of bytes
  50. */
  51. @Override
  52. public InputStream contentStream() {
  53. return okResponse.body().byteStream();
  54. }
  55. /**
  56. * Get stream of characters, decoded with the charset
  57. * of the Content-Type header. If that header is either absent or lacks a
  58. * charset, this will attempt to decode the response body as UTF-8.
  59. */
  60. @Override
  61. public Reader contentReader() {
  62. return okResponse.body().charStream();
  63. }
  64. /**
  65. * Get body content as a String. This response will be automatically closed.
  66. */
  67. @Override
  68. public String content() {
  69. try (ResponseBody body = okResponse.body()) {
  70. return body.string();
  71. } catch (IOException e) {
  72. throw fail(e);
  73. }
  74. }
  75. private RuntimeException fail(Exception e) {
  76. throw new IllegalStateException("Fail to read response of " + requestUrl(), e);
  77. }
  78. /**
  79. * Equivalent to closing contentReader or contentStream.
  80. */
  81. @Override
  82. public void close() {
  83. okResponse.close();
  84. }
  85. }