private HttpResponse unzip() throws IOException { Map<String, List<String>> newHeaders = headers.entrySet().stream() .filter(e -> !e.getKey().equalsIgnoreCase("Content-Encoding")) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); if (data.length == 0) { return new HttpResponse(status, newHeaders); } else { return new HttpResponse(status, newHeaders, HttpUtils.gunzip(data)); } }
/** * Compress a byte array using GZIP's default compression level. */ static byte[] gzip(byte[] data) throws IOException { return gzip(data, Deflater.DEFAULT_COMPRESSION); }
/** * Extract a client name based on the uri host. See {@link #clientNameForHost(String)} * for more details. */ static String clientNameForURI(URI uri) { String host = uri.getHost(); return (host == null) ? DEFAULT : clientNameForHost(host); }
/** * Create a new PUT request builder. The client name will be selected based * on a prefix of the host name. * * @param uri * URI to use for the request. * @return * Builder for creating and executing a request. */ default HttpRequestBuilder put(URI uri) { return newRequest(HttpUtils.clientNameForURI(uri), uri).withMethod("PUT"); }
private String extract(String u) { return HttpUtils.clientNameForURI(URI.create(u)); }
/** * Create a new POST request builder. The client name will be selected based * on a prefix of the host name. * * @param uri * URI to use for the request. * @return * Builder for creating and executing a request. */ default HttpRequestBuilder post(URI uri) { return newRequest(HttpUtils.clientNameForURI(uri), uri).withMethod("POST"); }
/** * Compress a byte array using GZIP's default compression level. */ static byte[] gzip(byte[] data) throws IOException { return gzip(data, Deflater.DEFAULT_COMPRESSION); }
private HttpResponse unzip() throws IOException { Map<String, List<String>> newHeaders = headers.entrySet().stream() .filter(e -> !e.getKey().equalsIgnoreCase("Content-Encoding")) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); if (data.length == 0) { return new HttpResponse(status, newHeaders); } else { return new HttpResponse(status, newHeaders, HttpUtils.gunzip(data)); } }
/** * Extract a client name based on the uri host. See {@link #clientNameForHost(String)} * for more details. */ static String clientNameForURI(URI uri) { String host = uri.getHost(); return (host == null) ? DEFAULT : clientNameForHost(host); }
/** * Create a new GET request builder. The client name will be selected based * on a prefix of the host name. * * @param uri * URI to use for the request. * @return * Builder for creating and executing a request. */ default HttpRequestBuilder get(URI uri) { return newRequest(HttpUtils.clientNameForURI(uri), uri).withMethod("GET"); }
/** * Compress the request body using the specified compression level. * The content must have already been set on the builder. */ public HttpRequestBuilder compress(int level) throws IOException { addHeader("Content-Encoding", "gzip"); entity = HttpUtils.gzip(entity, level); return this; }
/** * Create a new POST request builder. The client name will be selected based * on a prefix of the host name. * * @param uri * URI to use for the request. * @return * Builder for creating and executing a request. */ default HttpRequestBuilder post(URI uri) { return newRequest(HttpUtils.clientNameForURI(uri), uri).withMethod("POST"); }
/** * Compress the request body using the specified compression level. * The content must have already been set on the builder. */ public HttpRequestBuilder compress(int level) throws IOException { addHeader("Content-Encoding", "gzip"); entity = HttpUtils.gzip(entity, level); return this; }
/** * Create a new GET request builder. The client name will be selected based * on a prefix of the host name. * * @param uri * URI to use for the request. * @return * Builder for creating and executing a request. */ default HttpRequestBuilder get(URI uri) { return newRequest(HttpUtils.clientNameForURI(uri), uri).withMethod("GET"); }
@Test public void decompressNoChange() throws IOException { Map<String, List<String>> headers = new HashMap<>(); headers.put("Content-Type", Collections.singletonList("application/json")); byte[] entity = HttpUtils.gzip("foo bar baz foo bar baz".getBytes(StandardCharsets.UTF_8)); HttpResponse res = new HttpResponse(200, headers, entity); Assertions.assertSame(res, res.decompress()); }
/** * Create a new PUT request builder. The client name will be selected based * on a prefix of the host name. * * @param uri * URI to use for the request. * @return * Builder for creating and executing a request. */ default HttpRequestBuilder put(URI uri) { return newRequest(HttpUtils.clientNameForURI(uri), uri).withMethod("PUT"); }
@Test public void decompress() throws IOException { Map<String, List<String>> headers = new HashMap<>(); headers.put("Content-Type", Collections.singletonList("application/json")); headers.put("Content-Encoding", Collections.singletonList("gzip")); byte[] entity = HttpUtils.gzip("foo bar baz foo bar baz".getBytes(StandardCharsets.UTF_8)); HttpResponse res = new HttpResponse(200, headers, entity); Assertions.assertEquals("foo bar baz foo bar baz", res.decompress().entityAsString()); Assertions.assertNull(res.decompress().header("content-encoding")); }
/** * Create a new DELETE request builder. The client name will be selected based * on a prefix of the host name. * * @param uri * URI to use for the request. * @return * Builder for creating and executing a request. */ default HttpRequestBuilder delete(URI uri) { return newRequest(HttpUtils.clientNameForURI(uri), uri).withMethod("DELETE"); } }