Search in sources :

Example 16 with Payload

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();
    }
}
Also used : InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Payload(net.codestory.http.payload.Payload) SourceExtractor(org.icij.datashare.text.indexing.elasticsearch.SourceExtractor)

Example 17 with Payload

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;
}
Also used : User(net.codestory.http.security.User) Payload(net.codestory.http.payload.Payload)

Example 18 with 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);
}
Also used : User(net.codestory.http.security.User) Payload(net.codestory.http.payload.Payload)

Example 19 with Payload

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);
}
Also used : Payload(net.codestory.http.payload.Payload) IndexWaiterFilter(org.icij.datashare.web.IndexWaiterFilter) Test(org.junit.Test)

Aggregations

Payload (net.codestory.http.payload.Payload)19 Test (org.junit.Test)11 PropertiesProvider (org.icij.datashare.PropertiesProvider)5 HashMap (java.util.HashMap)2 Context (net.codestory.http.Context)2 User (net.codestory.http.security.User)2 BatchSearch (org.icij.datashare.batch.BatchSearch)2 DatashareUser (org.icij.datashare.session.DatashareUser)2 User (org.icij.datashare.user.User)2 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Boolean (java.lang.Boolean)1 Integer.parseInt (java.lang.Integer.parseInt)1 String.format (java.lang.String.format)1 Path (java.nio.file.Path)1 java.util (java.util)1