java.net.http.HttpRequest.Builder Java Examples
The following examples show how to use
java.net.http.HttpRequest.Builder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RestClient.java From Mastering-Microservices-with-Java-Third-Edition with MIT License | 5 votes |
public Builder requestBuilder(URI uri, Optional<Map<String, String>> additionalHeaders) { Builder builder = HttpRequest.newBuilder() .uri(uri) .timeout(Duration.ofMinutes(1)) .header("Content-Type", "application/json"); if (additionalHeaders.isPresent()) { additionalHeaders.get().forEach((k, v) -> builder.header(k, v)); } return builder; }
Example #2
Source File: RestClient.java From Mastering-Microservices-with-Java-Third-Edition with MIT License | 5 votes |
public Builder requestBuilder(URI uri, Optional<Map<String, String>> additionalHeaders) { Builder builder = HttpRequest.newBuilder() .uri(uri) .timeout(Duration.ofMinutes(1)) .header("Content-Type", "application/json"); if (additionalHeaders.isPresent()) { additionalHeaders.get().forEach((k, v) -> builder.header(k, v)); } return builder; }
Example #3
Source File: RestClient.java From Mastering-Microservices-with-Java-Third-Edition with MIT License | 5 votes |
public Builder requestBuilder(URI uri, Optional<Map<String, String>> additionalHeaders) { Builder builder = HttpRequest.newBuilder() .uri(uri) .timeout(Duration.ofMinutes(1)) .header("Content-Type", "application/json"); if (additionalHeaders.isPresent()) { additionalHeaders.get().forEach((k, v) -> builder.header(k, v)); } return builder; }
Example #4
Source File: R2ServerClient.java From r2cloud with Apache License 2.0 | 5 votes |
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; }
Example #5
Source File: JavaNetHttpRequester.java From algoliasearch-client-java-2 with MIT License | 5 votes |
/** * Builds a friendly URI Object for Java.net HTTP Client * * @param builder Request Builder * @param url HttpClient agnostic Algolia's URL */ private void buildURI(@Nonnull Builder builder, @Nonnull URL url) { try { builder.uri(url.toURI()); } catch (URISyntaxException e) { throw new AlgoliaRuntimeException(e); } }
Example #6
Source File: R2ServerClient.java From r2cloud with Apache License 2.0 | 4 votes |
private HttpRequest.Builder createJsonRequest(String path, JsonValue json) { return createRequest(path).header("Content-Type", "application/json").POST(BodyPublishers.ofString(json.toString(), StandardCharsets.UTF_8)); }
Example #7
Source File: OreKitDataClient.java From r2cloud with Apache License 2.0 | 4 votes |
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); } }
Example #8
Source File: RestClient.java From r2cloud with Apache License 2.0 | 4 votes |
private HttpRequest.Builder createJsonPost(String path, JsonObject obj) { return createAuthRequest(path).header("Content-Type", "application/json").POST(BodyPublishers.ofString(obj.toString(), StandardCharsets.UTF_8)); }
Example #9
Source File: RestClient.java From r2cloud with Apache License 2.0 | 4 votes |
private HttpRequest.Builder createAuthRequest(String path) { return createDefaultRequest(path).header("Authorization", "Bearer " + accessToken); }
Example #10
Source File: RestClient.java From r2cloud with Apache License 2.0 | 4 votes |
private HttpRequest.Builder createDefaultRequest(String path) { return HttpRequest.newBuilder().uri(URI.create(baseUrl + path)).timeout(Duration.ofMinutes(1L)).header("User-Agent", "r2cloud/0.1 [email protected]"); }
Example #11
Source File: JavaNetHttpRequester.java From algoliasearch-client-java-2 with MIT License | 2 votes |
/** * 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()); } }
Example #12
Source File: JavaNetHttpRequester.java From algoliasearch-client-java-2 with MIT License | -1 votes |
/** * Builds an http request from an AlgoliaRequest object * * @param algoliaRequest The Algolia request object */ private java.net.http.HttpRequest buildRequest(@Nonnull HttpRequest algoliaRequest) { java.net.http.HttpRequest.Builder builder = java.net.http.HttpRequest.newBuilder(); buildHeaders(builder, algoliaRequest.getHeaders()); buildURI(builder, algoliaRequest.getUri()); builder.timeout(Duration.ofMillis(algoliaRequest.getTimeout())); BodyPublisher body = buildRequestBody(builder, algoliaRequest); builder.method(algoliaRequest.getMethod().toString(), body); return builder.build(); }
Example #13
Source File: JavaNetHttpRequester.java From algoliasearch-client-java-2 with MIT License | -1 votes |
/** * 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); }