@@ -115,27 +115,19 @@ public Downloader withBaseDir(final Path override) {
115115
116116 @ Override
117117 public CompletableFuture <ResolutionResult <String >> readString (final URI source , final String relativePath ) {
118- return this .download (source , this . baseDirectory . resolve ( relativePath ) , path -> {
118+ return this .download (source , relativePath , path -> {
119119 final HttpResponse .BodyHandler <String > reader = HttpResponse .BodyHandlers .ofString (StandardCharsets .UTF_8 );
120- if (this .writeToDisk ) {
121- return JdkHttpClientDownloader .downloading (reader , path );
122- } else {
123- return reader ;
124- }
120+ return this .writeToDisk ? JdkHttpClientDownloader .downloading (reader , path ) : reader ;
125121 }, this ::readTextAsync );
126122 }
127123
128124 @ Override
129125 public CompletableFuture <ResolutionResult <String >> readStringAndValidate (
130126 final URI source , final String relativePath , final HashAlgorithm algorithm , final String hash
131127 ) {
132- return this .downloadValidating (source , this . baseDirectory . resolve ( relativePath ) , algorithm , hash , path -> {
128+ return this .downloadValidating (source , relativePath , algorithm , hash , path -> {
133129 final HttpResponse .BodyHandler <String > reader = HttpResponse .BodyHandlers .ofString (StandardCharsets .UTF_8 );
134- if (this .writeToDisk ) {
135- return JdkHttpClientDownloader .downloading (reader , path );
136- } else {
137- return reader ;
138- }
130+ return this .writeToDisk ? JdkHttpClientDownloader .downloading (reader , path ) : reader ;
139131 }, this ::readTextAsync );
140132 }
141133
@@ -145,27 +137,19 @@ private CompletableFuture<String> readTextAsync(final Path path) {
145137
146138 @ Override
147139 public CompletableFuture <ResolutionResult <byte []>> readBytes (final URI source , final String relativePath ) {
148- return this .download (source , this . baseDirectory . resolve ( relativePath ) , path -> {
140+ return this .download (source , relativePath , path -> {
149141 final HttpResponse .BodyHandler <byte []> reader = HttpResponse .BodyHandlers .ofByteArray ();
150- if (this .writeToDisk ) {
151- return JdkHttpClientDownloader .downloading (reader , path );
152- } else {
153- return reader ;
154- }
142+ return this .writeToDisk ? JdkHttpClientDownloader .downloading (reader , path ) : reader ;
155143 }, this ::readBytesAsync );
156144 }
157145
158146 @ Override
159147 public CompletableFuture <ResolutionResult <byte []>> readBytesAndValidate (
160148 final URI source , final String relativePath , final HashAlgorithm algorithm , final String hash
161149 ) {
162- return this .downloadValidating (source , this . baseDirectory . resolve ( relativePath ) , algorithm , hash , path -> {
150+ return this .downloadValidating (source , relativePath , algorithm , hash , path -> {
163151 final HttpResponse .BodyHandler <byte []> reader = HttpResponse .BodyHandlers .ofByteArray ();
164- if (this .writeToDisk ) {
165- return JdkHttpClientDownloader .downloading (reader , path );
166- } else {
167- return reader ;
168- }
152+ return this .writeToDisk ? JdkHttpClientDownloader .downloading (reader , path ) : reader ;
169153 }, this ::readBytesAsync );
170154 }
171155
@@ -175,75 +159,52 @@ private CompletableFuture<byte[]> readBytesAsync(final Path path) {
175159
176160 @ Override
177161 public CompletableFuture <ResolutionResult <Path >> download (final URI source , final String destination ) {
178- return this .download (
179- source ,
180- this .baseDirectory .resolve (destination ),
181- JdkHttpClientDownloader ::downloading ,
182- CompletableFuture ::completedFuture
183- );
162+ return this .download (source , destination , JdkHttpClientDownloader ::downloading , CompletableFuture ::completedFuture );
184163 }
185164
186165 @ Override
187166 public CompletableFuture <ResolutionResult <Path >> downloadAndValidate (
188167 final URI source , final String destination , final HashAlgorithm algorithm , final String hash
189168 ) {
190- return this .downloadValidating (
191- source ,
192- this .baseDirectory .resolve (destination ),
193- algorithm ,
194- hash ,
195- JdkHttpClientDownloader ::downloading ,
196- CompletableFuture ::completedFuture
197- );
169+ return this .downloadValidating (source , destination , algorithm , hash , JdkHttpClientDownloader ::downloading , CompletableFuture ::completedFuture );
198170 }
199171
200172 // Shared logic
201173
202174 private <T > CompletableFuture <ResolutionResult <T >> download (
203175 final URI source ,
204- final Path destination ,
176+ final String destination ,
205177 final Function <Path , HttpResponse .BodyHandler <T >> responseConsumer ,
206178 final Function <Path , CompletableFuture <T >> existingHandler
207179 ) {
208- final BasicFileAttributes destAttributes = FileUtils .fileAttributesIfExists (destination );
180+ final Path path = this .baseDirectory .resolve (destination );
181+ final BasicFileAttributes destAttributes = FileUtils .fileAttributesIfExists (path );
209182 if (this .resolveMode != ResolveMode .REMOTE_ONLY && (destAttributes != null && destAttributes .isRegularFile ())) { // TODO: check etag?
210183 // Check every 24 hours
211184 if (this .resolveMode == ResolveMode .LOCAL_ONLY
212185 || System .currentTimeMillis () - destAttributes .lastModifiedTime ().toMillis () < JdkHttpClientDownloader .CACHE_TIMEOUT_SECONDS * 1000 ) {
213- return existingHandler .apply (destination ).thenApply (result -> ResolutionResult .result (result , true ));
186+ return existingHandler .apply (path ).thenApply (result -> ResolutionResult .result (result , true ));
214187 }
215188 }
216189
217190 if (this .resolveMode == ResolveMode .LOCAL_ONLY ) {
218- // No value in cache and we aren't able to resolve, so return a not found
191+ // No value in cache, and we aren't able to resolve, so return a not found
219192 return CompletableFuture .completedFuture (ResolutionResult .notFound ());
220193 }
221194
222- return this .client .sendAsync (
223- this .makeRequest (source , null ), // todo: etag
224- responseConsumer .apply (destination )
225- ).thenApply (message -> {
226- switch (message .statusCode ()) {
227- case 404 :
228- return ResolutionResult .notFound ();
229- case 200 :
230- return ResolutionResult .result (message .body (), false );
231- default :
232- throw new CompletionException (new HttpErrorResponseException (source , message .statusCode (), String .valueOf (message .statusCode ())));
233- }
234- });
195+ return this .sendRequest (source , null , responseConsumer .apply (path )); // todo: etag
235196 }
236197
237198 private <T > CompletableFuture <ResolutionResult <T >> downloadValidating (
238199 final URI source ,
239- final Path destination ,
200+ final String destination ,
240201 final HashAlgorithm algorithm ,
241202 final String expectedHash ,
242203 final Function <Path , HttpResponse .BodyHandler <T >> responseConsumer ,
243204 final Function <Path , CompletableFuture <T >> existingHandler
244205 ) {
245- final Path path = destination ;
246- if (path . toFile (). isFile ( )) {
206+ final Path path = this . baseDirectory . resolve ( destination ) ;
207+ if (Files . isRegularFile ( path )) {
247208 // Validate that the file matches the path, only download if it doesn't.
248209 try {
249210 if (algorithm .validate (expectedHash , path )) {
@@ -262,33 +223,30 @@ private <T> CompletableFuture<ResolutionResult<T>> downloadValidating(
262223 }
263224
264225 if (this .resolveMode == ResolveMode .LOCAL_ONLY ) {
265- // No value in cache and we aren't able to resolve, so return a not found
226+ // No value in cache, and we aren't able to resolve, so return a not found
266227 return CompletableFuture .completedFuture (ResolutionResult .notFound ());
267228 }
268229
269- return this .client .sendAsync (
270- this .makeRequest (source , null ),
271- JdkHttpClientDownloader .validating (responseConsumer .apply (path ), algorithm , expectedHash )
272- ).thenApply (message -> {
230+ return this .sendRequest (source , null , JdkHttpClientDownloader .validating (responseConsumer .apply (path ), algorithm , expectedHash ));
231+ }
232+
233+ private <T > CompletableFuture <ResolutionResult <T >> sendRequest (final URI uri , final @ Nullable String etag , final HttpResponse .BodyHandler <T > bodyHandler ) {
234+ final HttpRequest .Builder requestBuilder = HttpRequest .newBuilder ().GET ().uri (uri );
235+ if (etag != null ) {
236+ requestBuilder .header (HttpConstants .HEADER_IF_NONE_MATCH , etag );
237+ }
238+ return this .client .sendAsync (requestBuilder .build (), bodyHandler ).thenApply (message -> {
273239 switch (message .statusCode ()) {
274240 case HttpConstants .STATUS_NOT_FOUND :
275241 return ResolutionResult .notFound ();
276242 case HttpConstants .STATUS_OK :
277243 return ResolutionResult .result (message .body (), false ); // Known invalid, hash does not match expected.
278244 default :
279- throw new CompletionException (new HttpErrorResponseException (source , message .statusCode (), message .toString ()));
245+ throw new CompletionException (new HttpErrorResponseException (uri , message .statusCode (), message .toString ()));
280246 }
281247 });
282248 }
283249
284- private HttpRequest makeRequest (final URI uri , final @ Nullable String etag ) {
285- final var requestBuilder = HttpRequest .newBuilder ().GET ().uri (uri );
286- if (etag != null ) {
287- requestBuilder .header (HttpConstants .HEADER_IF_NONE_MATCH , etag );
288- }
289- return requestBuilder .build ();
290- }
291-
292250 @ Override
293251 public void close () throws IOException {
294252 // nothing needed, the client just relies on the executor
0 commit comments