Skip to content

Instantly share code, notes, and snippets.

@skrb
Created August 19, 2017 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skrb/4d0106e04ac61d2ee9e97fdd10722727 to your computer and use it in GitHub Desktop.
Save skrb/4d0106e04ac61d2ee9e97fdd10722727 to your computer and use it in GitHub Desktop.
HTTP/2 Client Sample
public class HttpResponseUtils {
public static HttpResponse.BodyHandler<Path> createHandler(Path path) {
return (status, headers) -> {
return headers.firstValue("Content-Disposition")
.map(header -> {
if (header.startsWith("attachment;")) {
return Arrays.stream(header.split(";"))
.map(item -> item.trim())
.filter(item -> item.startsWith("filename="))
.findFirst()
.map(f -> {
int index = f.indexOf("=") + 1;
Path newPath = path.getParent().resolve(f.substring(index));
return HttpResponse.BodyProcessor.asFile(newPath);
}).orElseThrow(() -> new UncheckedIOException(new IOException("No Filename")));
} else if (header.startsWith("inline")){
return HttpResponse.BodyProcessor.asFile(path);
} else {
throw new UncheckedIOException(new IOException("No Attachment"));
}
}).orElse(HttpResponse.BodyProcessor.asFile(path));
};
}
}
@skrb
Copy link
Author

skrb commented Aug 19, 2017

HTTPのヘッダのContent-Dispositionがattachmentかinlineかを判別してダウンロードする HttpResponse.BodyHandler を作成するためのメソッド

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment