下面列出了java.net.http.HttpRequest.Builder#header ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Build the body for the request builder. Handling compression type of the request.
*
* @param builder Request Builder
* @param algoliaRequest HttpClient agnostic Algolia's request
*/
private BodyPublisher buildRequestBody(
@Nonnull Builder builder, @Nonnull HttpRequest algoliaRequest) {
if (algoliaRequest.getBody() == null) {
return java.net.http.HttpRequest.BodyPublishers.noBody();
}
if (algoliaRequest.canCompress()) {
builder.header(Defaults.CONTENT_ENCODING_HEADER, Defaults.CONTENT_ENCODING_GZIP);
} else {
builder.header(Defaults.CONTENT_TYPE_HEADER, Defaults.APPLICATION_JSON);
}
return BodyPublishers.ofInputStream(algoliaRequest::getBody);
}
private HttpRequest.Builder createRequest(String path) {
Builder result = HttpRequest.newBuilder().uri(URI.create(hostname + path));
result.timeout(Duration.ofMinutes(1L));
result.header("User-Agent", R2Cloud.getVersion() + " [email protected]");
result.header("Authorization", config.getProperty("r2cloud.apiKey"));
return result;
}
private void downloadAndSaveTo(String url, Path dst) throws IOException {
Path tempPath = dst.getParent().resolve(dst.getFileName() + ".tmp").normalize();
if (Files.exists(tempPath) && !Util.deleteDirectory(tempPath)) {
throw new RuntimeException("unable to delete tmp directory: " + tempPath);
}
Files.createDirectories(tempPath);
Builder result = HttpRequest.newBuilder().uri(URI.create(url));
result.timeout(Duration.ofMillis(TIMEOUT));
result.header("User-Agent", R2Cloud.getVersion() + " [email protected]");
HttpRequest request = result.build();
try {
HttpResponse<InputStream> response = httpclient.send(request, BodyHandlers.ofInputStream());
if (response.statusCode() != 200) {
throw new IOException("invalid status code: " + response.statusCode());
}
Optional<String> contentType = response.headers().firstValue("Content-Type");
if (contentType.isEmpty() || !contentType.get().equals("application/zip")) {
throw new IOException("Content-Type is empty or unsupported: " + contentType);
}
try (ZipInputStream zis = new ZipInputStream(response.body())) {
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
Path destFile = tempPath.resolve(zipEntry.getName()).normalize();
if (!destFile.startsWith(tempPath)) {
throw new IOException("invalid archive. zip slip detected: " + destFile);
}
if (zipEntry.isDirectory()) {
Files.createDirectories(destFile);
continue;
}
if (!Files.exists(destFile.getParent())) {
Files.createDirectories(destFile.getParent());
}
Files.copy(zis, destFile, StandardCopyOption.REPLACE_EXISTING);
}
Files.move(tempPath, dst, StandardCopyOption.REPLACE_EXISTING);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
/**
* Builds a friendly Headers for Java's HTTPClient
*
* @param builder Request Builder
* @param headers HttpClient agnostic Algolia's headers
*/
private void buildHeaders(@Nonnull Builder builder, @Nonnull Map<String, String> headers) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
}