(HttpURLConnection connection, Request request)
| 92 | } |
| 93 | |
| 94 | Response convertResponse(HttpURLConnection connection, Request request) throws IOException { |
| 95 | int status = connection.getResponseCode(); |
| 96 | String reason = connection.getResponseMessage(); |
| 97 | |
| 98 | if (status < 0) { |
| 99 | throw new IOException( |
| 100 | format( |
| 101 | "Invalid status(%s) executing %s %s", |
| 102 | status, connection.getRequestMethod(), connection.getURL())); |
| 103 | } |
| 104 | |
| 105 | Map<String, Collection<String>> headers = new TreeMap<>(CASE_INSENSITIVE_ORDER); |
| 106 | for (Map.Entry<String, List<String>> field : connection.getHeaderFields().entrySet()) { |
| 107 | // response message |
| 108 | if (field.getKey() != null) { |
| 109 | headers.put(field.getKey(), field.getValue()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | Integer length = connection.getContentLength(); |
| 114 | if (length == -1) { |
| 115 | length = null; |
| 116 | } |
| 117 | InputStream stream; |
| 118 | if (status >= 400) { |
| 119 | stream = connection.getErrorStream(); |
| 120 | } else { |
| 121 | stream = connection.getInputStream(); |
| 122 | } |
| 123 | if (stream != null && this.isGzip(headers.get(CONTENT_ENCODING))) { |
| 124 | stream = new GZIPInputStream(stream); |
| 125 | } else if (stream != null && this.isDeflate(headers.get(CONTENT_ENCODING))) { |
| 126 | stream = new InflaterInputStream(stream); |
| 127 | } |
| 128 | return Response.builder() |
| 129 | .status(status) |
| 130 | .reason(reason) |
| 131 | .headers(headers) |
| 132 | .request(request) |
| 133 | .body(stream, length) |
| 134 | .build(); |
| 135 | } |
| 136 | |
| 137 | public HttpURLConnection getConnection(final URL url) throws IOException { |
| 138 | return (HttpURLConnection) url.openConnection(); |
no test coverage detected