Search in sources :

Example 1 with Context

use of net.codestory.http.Context in project datashare by ICIJ.

the class ServerMode method configure.

@Override
protected void configure() {
    super.configure();
    String authUsersProviderClassName = propertiesProvider.get("authUsersProvider").orElse("org.icij.datashare.session.UsersInRedis");
    String batchQueueType = propertiesProvider.get("batchQueueType").orElse("org.icij.datashare.extract.MemoryBlockingQueue");
    bind(TaskManager.class).toInstance(new TaskManagerRedis(propertiesProvider, getBlockingQueue(propertiesProvider, batchQueueType, "ds:batchdownload:queue")));
    Class<? extends UsersWritable> authUsersProviderClass = UsersInRedis.class;
    try {
        authUsersProviderClass = (Class<? extends UsersWritable>) Class.forName(authUsersProviderClassName);
        logger.info("setting auth users provider to {}", authUsersProviderClass);
    } catch (ClassNotFoundException e) {
        logger.warn("\"{}\" auth users provider class not found. Setting provider to {}", authUsersProviderClassName, authUsersProviderClass);
    }
    bind(UsersWritable.class).to(authUsersProviderClass);
    bind(SessionIdStore.class).to(RedisSessionIdStore.class);
    bind(ApiKeyStore.class).to(ApiKeyStoreAdapter.class);
    String authFilterClassName = propertiesProvider.get("authFilter").orElse("");
    Class<? extends Filter> authFilterClass = OAuth2CookieFilter.class;
    if (!authFilterClassName.isEmpty()) {
        try {
            authFilterClass = (Class<? extends Filter>) Class.forName(authFilterClassName);
            logger.info("setting auth filter to {}", authFilterClass);
        } catch (ClassNotFoundException e) {
            logger.warn("\"{}\" auth filter class not found. Setting filter to {}", authFilterClassName, authFilterClass);
        }
    }
    bind(Filter.class).to(authFilterClass);
    if (authFilterClass.equals(BasicAuthAdaptorFilter.class)) {
        bind(ApiKeyFilter.class).toInstance(new ApiKeyFilter(null, apiKey -> null) {

            @Override
            public Payload apply(String uri, Context context, PayloadSupplier nextFilter) throws Exception {
                return nextFilter.get();
            }
        });
    }
    bind(StatusResource.class).asEagerSingleton();
    configurePersistence();
}
Also used : SessionIdStore(net.codestory.http.security.SessionIdStore) Routes(net.codestory.http.routes.Routes) Logger(org.slf4j.Logger) Properties(java.util.Properties) Context(net.codestory.http.Context) Payload(net.codestory.http.payload.Payload) LoggerFactory(org.slf4j.LoggerFactory) org.icij.datashare.web(org.icij.datashare.web) PayloadSupplier(net.codestory.http.filters.PayloadSupplier) TaskManager(org.icij.datashare.tasks.TaskManager) Map(java.util.Map) Filter(net.codestory.http.filters.Filter) TaskManagerRedis(org.icij.datashare.tasks.TaskManagerRedis) org.icij.datashare.session(org.icij.datashare.session) Context(net.codestory.http.Context) TaskManagerRedis(org.icij.datashare.tasks.TaskManagerRedis) TaskManager(org.icij.datashare.tasks.TaskManager) SessionIdStore(net.codestory.http.security.SessionIdStore) PayloadSupplier(net.codestory.http.filters.PayloadSupplier) Filter(net.codestory.http.filters.Filter) Payload(net.codestory.http.payload.Payload)

Example 2 with Context

use of net.codestory.http.Context 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)

Aggregations

Context (net.codestory.http.Context)2 Payload (net.codestory.http.payload.Payload)2 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 IOException (java.io.IOException)1 Boolean (java.lang.Boolean)1 Integer.parseInt (java.lang.Integer.parseInt)1 String.format (java.lang.String.format)1 java.util (java.util)1 Arrays.stream (java.util.Arrays.stream)1 Map (java.util.Map)1 Properties (java.util.Properties)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 Collectors (java.util.stream.Collectors)1 Part (net.codestory.http.Part)1 net.codestory.http.annotations (net.codestory.http.annotations)1 NotFoundException (net.codestory.http.errors.NotFoundException)1 UnauthorizedException (net.codestory.http.errors.UnauthorizedException)1 Filter (net.codestory.http.filters.Filter)1 PayloadSupplier (net.codestory.http.filters.PayloadSupplier)1