use of net.codestory.http.payload.Payload in project datashare by ICIJ.
the class DocumentResource method getPayload.
private Payload getPayload(Document doc, String index, boolean inline, boolean filterMetadata) throws IOException {
try (InputStream from = new SourceExtractor(filterMetadata).getSource(project(index), doc)) {
String contentType = ofNullable(doc.getContentType()).orElse(ContentTypes.get(doc.getPath().toFile().getName()));
Payload payload = new Payload(contentType, InputStreams.readBytes(from));
String fileName = doc.isRootDocument() ? doc.getName() : doc.getId().substring(0, 10) + "." + FileExtension.get(contentType);
return inline ? payload : payload.withHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
} catch (FileNotFoundException fnf) {
return Payload.notFound();
}
}
use of net.codestory.http.payload.Payload in project datashare by ICIJ.
the class YesCookieAuthFilter method otherUri.
@Override
protected Payload otherUri(String uri, Context context, PayloadSupplier nextFilter) throws Exception {
Payload payload = super.otherUri(uri, context, nextFilter);
if (payload.code() == 401) {
User user = createUser(NameGenerator.generate());
context.setCurrentUser(user);
return nextFilter.get().withCookie(this.authCookie(this.buildCookie(user, "/")));
}
return payload;
}
use of net.codestory.http.payload.Payload in project datashare by ICIJ.
the class ApiKeyFilter method apply.
@Override
public Payload apply(String uri, Context context, PayloadSupplier nextFilter) throws Exception {
if (context.cookies().get(dsCookieName()) != null) {
return nextFilter.get();
}
String apiKey = readApiKeyInHeader(context);
if (apiKey != null) {
String login = apiKeyStore.getLogin(apiKey);
if (login != null) {
User user = users.find(login);
context.setCurrentUser(user);
return nextFilter.get().withHeader(CACHE_CONTROL, "must-revalidate");
}
}
return new Payload(UNAUTHORIZED);
}
use of net.codestory.http.payload.Payload in project datashare by ICIJ.
the class IndexWaiterFilterTest method test_wait_for_index.
@Test
public void test_wait_for_index() throws Exception {
IndexWaiterFilter indexWaiterFilter = new IndexWaiterFilter(esClient).waitForIndexAsync();
Payload payload = indexWaiterFilter.apply("/", context, nextFilter);
assertThat(payload.rawContentType()).isEqualTo("text/html");
assertThat((String) payload.rawContent()).contains("waiting for Datashare to be up...");
indexWaiterFilter.executor.awaitTermination(2, SECONDS);
assertThat(indexWaiterFilter.apply("/", context, nextFilter)).isSameAs(next);
}
Aggregations