Search in sources :

Example 11 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class LocalUserFilterTest method test_adds_custom_user_to_context.

@Test
public void test_adds_custom_user_to_context() throws Exception {
    LocalUserFilter localUserFilter = new LocalUserFilter(new PropertiesProvider(new HashMap<String, String>() {

        {
            put("defaultUserName", "foo");
        }
    }));
    Payload payload = localUserFilter.apply("url", context, nextFilter);
    assertThat(payload).isSameAs(next);
    verify(context).setCurrentUser(user.capture());
    assertThat(user.getValue().login()).isEqualTo("foo");
}
Also used : PropertiesProvider(org.icij.datashare.PropertiesProvider) HashMap(java.util.HashMap) Payload(net.codestory.http.payload.Payload) Test(org.junit.Test)

Example 12 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class LocalUserFilterTest method test_adds_local_user_to_context.

@Test
public void test_adds_local_user_to_context() throws Exception {
    LocalUserFilter localUserFilter = new LocalUserFilter(new PropertiesProvider());
    Payload payload = localUserFilter.apply("url", context, nextFilter);
    assertThat(payload).isSameAs(next);
    verify(context).setCurrentUser(user.capture());
    assertThat(user.getValue().login()).isEqualTo("local");
    assertThat(user.getValue().isInRole("local")).isTrue();
}
Also used : PropertiesProvider(org.icij.datashare.PropertiesProvider) Payload(net.codestory.http.payload.Payload) Test(org.junit.Test)

Example 13 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class BatchSearchResource method search.

/**
 * Creates a new batch search. This is a multipart form with 8 fields :
 * name, description, csvFile, published, fileTypes, paths, fuzziness, phrase_matches
 *
 * No matter the order. The name and csv file are mandatory else it will return 400 (bad request)
 * Csv file must have under 60 000 lines else it will return 413 (payload too large)
 * Queries with less than two characters are filtered
 *
 * To do so with bash you can create a text file like :
 * ```
 * --BOUNDARY
 * Content-Disposition: form-data; name="name"
 *
 * my batch search
 * --BOUNDARY
 * Content-Disposition: form-data; name="description"
 *
 * search description
 * --BOUNDARY
 * Content-Disposition: form-data; name="csvFile"; filename="search.csv"
 * Content-Type: text/csv
 *
 * Obama
 * skype
 * test
 * query three
 * --BOUNDARY--
 * Content-Disposition: form-data; name="published"
 *
 * true
 * --BOUNDARY--
 * ```
 * Then replace `\n` with `\r\n` with a sed like this:
 *
 * `sed -i 's/$/^M/g' ~/multipart.txt`
 *
 * Then make a curl request with this file :
 * ```
 * curl -i -XPOST localhost:8080/api/batch/search/prj -H 'Content-Type: multipart/form-data; boundary=BOUNDARY' --data-binary @/home/dev/multipart.txt
 * ```
 * @param projectId
 * @param context : the request body
 * @return 200 or 400 or 413
 */
@Post("/search/:project")
public Payload search(String projectId, Context context) throws Exception {
    List<Part> parts = context.parts();
    String name = fieldValue("name", parts);
    String csv = fieldValue("csvFile", parts);
    if (name == null || csv == null) {
        return badRequest();
    }
    String description = fieldValue("description", parts);
    boolean published = "true".equalsIgnoreCase(fieldValue("published", parts)) ? TRUE : FALSE;
    List<String> fileTypes = fieldValues("fileTypes", parts);
    List<String> paths = fieldValues("paths", parts);
    Optional<Part> fuzzinessPart = parts.stream().filter(p -> "fuzziness".equals(p.name())).findAny();
    int fuzziness = fuzzinessPart.isPresent() ? parseInt(fuzzinessPart.get().content()) : 0;
    Optional<Part> phraseMatchesPart = parts.stream().filter(p -> "phrase_matches".equals(p.name())).findAny();
    boolean phraseMatches = phraseMatchesPart.isPresent() ? parseBoolean(phraseMatchesPart.get().content()) : FALSE;
    LinkedHashSet<String> queries = getQueries(csv).stream().map(query -> (phraseMatches && query.contains("\"")) ? query : sanitizeDoubleQuotesInQuery(query)).collect(Collectors.toCollection(LinkedHashSet::new));
    if (queries.size() >= MAX_BATCH_SIZE)
        return new Payload(413);
    BatchSearch batchSearch = new BatchSearch(project(projectId), name, description, queries, (User) context.currentUser(), published, fileTypes, paths, fuzziness, phraseMatches);
    boolean isSaved = batchSearchRepository.save(batchSearch);
    if (isSaved)
        batchSearchQueue.put(batchSearch.uuid);
    return isSaved ? new Payload("application/json", batchSearch.uuid, 200) : badRequest();
}
Also used : java.util(java.util) Inject(com.google.inject.Inject) UnauthorizedException(net.codestory.http.errors.UnauthorizedException) SearchResult(org.icij.datashare.batch.SearchResult) JooqBatchSearchRepository(org.icij.datashare.db.JooqBatchSearchRepository) Boolean(java.lang.Boolean) User(org.icij.datashare.user.User) Project.project(org.icij.datashare.text.Project.project) DatashareUser(org.icij.datashare.session.DatashareUser) Part(net.codestory.http.Part) net.codestory.http.annotations(net.codestory.http.annotations) PropertiesProvider(org.icij.datashare.PropertiesProvider) Project(org.icij.datashare.text.Project) Context(net.codestory.http.Context) Payload(net.codestory.http.payload.Payload) CollectionUtils.asSet(org.icij.datashare.CollectionUtils.asSet) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) BatchSearchRecord(org.icij.datashare.batch.BatchSearchRecord) NotFoundException(net.codestory.http.errors.NotFoundException) BatchSearchRepository(org.icij.datashare.batch.BatchSearchRepository) Collectors(java.util.stream.Collectors) Integer.parseInt(java.lang.Integer.parseInt) String.format(java.lang.String.format) BatchSearch(org.icij.datashare.batch.BatchSearch) Arrays.stream(java.util.Arrays.stream) Singleton(com.google.inject.Singleton) BatchSearch(org.icij.datashare.batch.BatchSearch) Part(net.codestory.http.Part) Payload(net.codestory.http.payload.Payload)

Example 14 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class BatchSearchResource method getResultAsCsv.

/**
 * Retrieve the results of a batch search as a CSV file.
 *
 * The search request is by default all results of the batch search.
 *
 * @param batchId
 * @return 200 and the CSV file as attached file
 *
 * Example :
 * $(curl -i localhost:8080/api/batch/search/result/csv/f74432db-9ae8-401d-977c-5c44a124f2c8)
 */
@Get("/search/result/csv/:batchid")
public Payload getResultAsCsv(String batchId, Context context) {
    StringBuilder builder = new StringBuilder("\"query\", \"documentUrl\", \"documentId\",\"rootId\",\"contentType\",\"contentLength\",\"documentPath\",\"creationDate\",\"documentNumber\"\n");
    BatchSearch batchSearch = batchSearchRepository.get((User) context.currentUser(), batchId);
    String url = propertiesProvider.get("rootHost").orElse(context.header("Host"));
    getResultsOrThrowUnauthorized(batchId, (User) context.currentUser(), new BatchSearchRepository.WebQuery()).forEach(result -> builder.append("\"").append(result.query).append("\"").append(",").append("\"").append(docUrl(url, batchSearch.project, result.documentId, result.rootId)).append("\"").append(",").append("\"").append(result.documentId).append("\"").append(",").append("\"").append(result.rootId).append("\"").append(",").append("\"").append(result.contentType).append("\"").append(",").append("\"").append(result.contentLength).append("\"").append(",").append("\"").append(result.documentPath).append("\"").append(",").append("\"").append(result.creationDate).append("\"").append(",").append("\"").append(result.documentNumber).append("\"").append("\n"));
    return new Payload("text/csv", builder.toString()).withHeader("Content-Disposition", "attachment;filename=\"" + batchId + ".csv\"");
}
Also used : User(org.icij.datashare.user.User) DatashareUser(org.icij.datashare.session.DatashareUser) BatchSearch(org.icij.datashare.batch.BatchSearch) Payload(net.codestory.http.payload.Payload)

Example 15 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class YesCookieAuthFilterTest method test_adds_new_user_to_context.

@Test
public void test_adds_new_user_to_context() throws Exception {
    YesCookieAuthFilter filter = new YesCookieAuthFilter(new PropertiesProvider(new HashMap<String, String>() {

        {
            put("defaultProject", "demo");
            put("messageBusAddress", "redis://redis:6379");
        }
    }));
    Payload payload = filter.apply("url", context, nextFilter);
    assertThat(payload).isSameAs(next);
    verify(context).setCurrentUser(user.capture());
    assertThat(user.getValue().login()).isNotEmpty();
    assertThat(((DatashareUser) user.getValue()).getProjects()).containsExactly("demo");
    assertThat(user.getValue().isInRole("local")).isFalse();
}
Also used : PropertiesProvider(org.icij.datashare.PropertiesProvider) HashMap(java.util.HashMap) Payload(net.codestory.http.payload.Payload) 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