Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #929 implement a utility class to save large downloads to a file #12292

Closed
Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.client;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.CompletableFuture;

import org.eclipse.jetty.client.Response.Listener;
import org.eclipse.jetty.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Implementation of {@link Response.ContentListener} that produces an {@link Path}
* that allows applications to save a file from a response {@link Response}
* like {@code curl <url> -o file.bin} does.
* <p>
* Typical usage is:
* <pre>{@code httpClient.newRequest(host, port)
* .send(new PathResponseListener(Path.of("/tmp/file.bin"));
*
* var request = httpClient.newRequest(host, port);
* CompletableFuture<Path> completable = PathResponseListener.write(request, Path.of("/tmp/file.bin"), rewriteExistingFile);
* }</pre>
*/
public class PathResponseListener extends CompletableFuture<Path> implements Listener
{
private static final Logger LOG = LoggerFactory.getLogger(InputStreamResponseListener.class);

private final Path path;
private final FileChannel fileChannel;

public PathResponseListener(Path path, boolean overwrite) throws IOException
{
this.path = path;

// Throws the exception if file can't be overwritten
// otherwise truncate it.
if (Files.exists(path) && !overwrite)
{
throw new FileAlreadyExistsException("File can't be overwritten");
}

fileChannel = FileChannel.open(this.path, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
}

@Override
public void onHeaders(Response response)
{
if (response.getStatus() != HttpStatus.OK_200)
{
response.abort(new HttpResponseException(String.format("HTTP status code of response %d", response.getStatus()), response));
}
}

@Override
public void onContent(Response response, ByteBuffer content)
{
try
{
var bytesWritten = fileChannel.write(content);
if (LOG.isDebugEnabled())
LOG.debug("%d bytes written", bytesWritten);
}
catch (IOException e)
{
response.abort(e);
}
}

@Override
public void onComplete(Result result)
arsenalzp marked this conversation as resolved.
Show resolved Hide resolved
{
try
{
if (result.isFailed())
{
if (LOG.isDebugEnabled())
LOG.debug("Result failure", result.getFailure());
completeExceptionally(result.getFailure());
return;
}

this.complete(this.path);
}
finally
{
try
{
fileChannel.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

}

/**
* Writes a file into {@link Path}.
*
* @param request to a server
* @param path to write a file
* @param overwrite true overwrites a file, otherwise fails
* @return {@code CompletableFuture<Path>}
*/
public static CompletableFuture<Path> write(Request request, Path path, boolean overwrite)
arsenalzp marked this conversation as resolved.
Show resolved Hide resolved
{
PathResponseListener l = null;
try
{
l = new PathResponseListener(path, overwrite);
request.send(l);
}
catch (Throwable e)
{
l.completeExceptionally(e);
}
return l;
}
}
Loading