HTTP/2 Client Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HTTPのヘッダのContent-Dispositionがattachmentかinlineかを判別してダウンロードする HttpResponse.BodyHandler を作成するためのメソッド