Search in sources :

Example 76 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class ConfigDataStoreTest method updateFile.

protected ConfigFile updateFile(String configRoot, ConfigDataStore store) {
    Supplier<String> contentProvider = () -> "{            \n" + "  tables: [{     \n" + "      name: Test2\n" + "      table: test\n" + "      schema: test\n" + "      measures : [\n" + "         {\n" + "          name : measure\n" + "          type : INTEGER\n" + "          definition: 'MAX({{$measure}})'\n" + "         }\n" + "      ]      \n" + "      dimensions : [\n" + "         {\n" + "           name : dimension\n" + "           type : TEXT\n" + "           definition : '{{$dimension}}'\n" + "         }\n" + "      ]\n" + "  }]\n" + "}";
    ConfigFile updatedFile = ConfigFile.builder().type(TABLE).contentProvider(contentProvider).path("models/tables/test.hjson").build();
    ConfigDataStoreTransaction tx = store.beginTransaction();
    RequestScope scope = mock(RequestScope.class);
    tx.save(updatedFile, scope);
    tx.flush(scope);
    tx.commit(scope);
    return updatedFile;
}
Also used : ConfigFile(com.yahoo.elide.modelconfig.store.models.ConfigFile) RequestScope(com.yahoo.elide.core.RequestScope)

Example 77 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class ConfigDataStoreTest method testDelete.

@Test
public void testDelete(@TempDir Path configPath) {
    String configRoot = configPath.toFile().getPath();
    Validator validator = new DynamicConfigValidator(DefaultClassScanner.getInstance(), configRoot);
    ConfigDataStore store = new ConfigDataStore(configRoot, validator);
    ConfigFile newFile = createFile("test", store, false);
    ConfigDataStoreTransaction tx = store.beginTransaction();
    RequestScope scope = mock(RequestScope.class);
    tx.delete(newFile, scope);
    tx.flush(scope);
    tx.commit(scope);
    ConfigDataStoreTransaction readTx = store.beginReadTransaction();
    ConfigFile loaded = readTx.loadObject(EntityProjection.builder().type(ClassType.of(ConfigFile.class)).build(), toId("models/tables/test.hjson", NO_VERSION), scope);
    assertNull(loaded);
}
Also used : ConfigFile(com.yahoo.elide.modelconfig.store.models.ConfigFile) RequestScope(com.yahoo.elide.core.RequestScope) DynamicConfigValidator(com.yahoo.elide.modelconfig.validator.DynamicConfigValidator) Validator(com.yahoo.elide.modelconfig.validator.Validator) DynamicConfigValidator(com.yahoo.elide.modelconfig.validator.DynamicConfigValidator) Test(org.junit.jupiter.api.Test)

Example 78 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class ConfigDataStoreTest method createFile.

protected ConfigFile createFile(String tableName, ConfigDataStore store, boolean validateOnly) {
    Supplier<String> contentProvider = () -> String.format("{            \n" + "  tables: [{     \n" + "      name: %s\n" + "      table: test\n" + "      schema: test\n" + "      measures : [\n" + "         {\n" + "          name : measure\n" + "          type : INTEGER\n" + "          definition: 'MAX({{$measure}})'\n" + "         }\n" + "      ]      \n" + "      dimensions : [\n" + "         {\n" + "           name : dimension\n" + "           type : TEXT\n" + "           definition : '{{$dimension}}'\n" + "         }\n" + "      ]\n" + "  }]\n" + "}", tableName);
    ConfigFile newFile = ConfigFile.builder().type(TABLE).contentProvider(contentProvider).path(String.format("models/tables/%s.hjson", tableName)).build();
    ConfigDataStoreTransaction tx = store.beginTransaction();
    RequestScope scope = mock(RequestScope.class);
    if (validateOnly) {
        when(scope.getRequestHeaderByName(eq(VALIDATE_ONLY_HEADER))).thenReturn("true");
    }
    tx.createObject(newFile, scope);
    tx.save(newFile, scope);
    tx.flush(scope);
    tx.commit(scope);
    return newFile;
}
Also used : ConfigFile(com.yahoo.elide.modelconfig.store.models.ConfigFile) RequestScope(com.yahoo.elide.core.RequestScope)

Example 79 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class JSONAPITableExportOperation method getRequestScope.

@Override
public RequestScope getRequestScope(TableExport export, RequestScope scope, DataStoreTransaction tx, Map<String, List<String>> additionalRequestHeaders) {
    UUID requestId = UUID.fromString(export.getRequestId());
    User user = scope.getUser();
    String apiVersion = scope.getApiVersion();
    URIBuilder uri;
    try {
        uri = new URIBuilder(export.getQuery());
    } catch (URISyntaxException e) {
        throw new BadRequestException(e.getMessage());
    }
    MultivaluedMap<String, String> queryParams = JSONAPIAsyncQueryOperation.getQueryParams(uri);
    // Call with additionalHeader alone
    if (scope.getRequestHeaders().isEmpty()) {
        return new RequestScope("", JSONAPIAsyncQueryOperation.getPath(uri), apiVersion, null, tx, user, queryParams, additionalRequestHeaders, requestId, getService().getElide().getElideSettings());
    }
    // Combine additionalRequestHeaders and existing scope's request headers
    Map<String, List<String>> finalRequestHeaders = new HashMap<String, List<String>>();
    scope.getRequestHeaders().forEach((entry, value) -> finalRequestHeaders.put(entry, value));
    // additionalRequestHeaders will override any headers in scope.getRequestHeaders()
    additionalRequestHeaders.forEach((entry, value) -> finalRequestHeaders.put(entry, value));
    return new RequestScope("", JSONAPIAsyncQueryOperation.getPath(uri), apiVersion, null, tx, user, queryParams, scope.getRequestHeaders(), requestId, getService().getElide().getElideSettings());
}
Also used : User(com.yahoo.elide.core.security.User) HashMap(java.util.HashMap) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) List(java.util.List) URISyntaxException(java.net.URISyntaxException) UUID(java.util.UUID) RequestScope(com.yahoo.elide.core.RequestScope) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 80 with RequestScope

use of com.yahoo.elide.core.RequestScope in project elide by yahoo.

the class TableExportOperation method call.

@Override
public AsyncAPIResult call() {
    log.debug("TableExport Object from request: {}", exportObj);
    Elide elide = service.getElide();
    TableExportResult exportResult = new TableExportResult();
    UUID requestId = UUID.fromString(exportObj.getRequestId());
    try (DataStoreTransaction tx = elide.getDataStore().beginTransaction()) {
        // Do Not Cache Export Results
        Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
        requestHeaders.put("bypasscache", new ArrayList<String>(Arrays.asList("true")));
        RequestScope requestScope = getRequestScope(exportObj, scope, tx, requestHeaders);
        Collection<EntityProjection> projections = getProjections(exportObj, requestScope);
        validateProjections(projections);
        EntityProjection projection = projections.iterator().next();
        Observable<PersistentResource> observableResults = Observable.empty();
        elide.getTransactionRegistry().addRunningTransaction(requestId, tx);
        // TODO - we need to add the baseUrlEndpoint to the queryObject.
        // TODO - Can we have projectionInfo as null?
        requestScope.setEntityProjection(projection);
        if (projection != null) {
            projection.setPagination(null);
            observableResults = PersistentResource.loadRecords(projection, Collections.emptyList(), requestScope);
        }
        Observable<String> results = Observable.empty();
        String preResult = formatter.preFormat(projection, exportObj);
        results = observableResults.map(resource -> {
            this.recordNumber++;
            return formatter.format(resource, recordNumber);
        });
        String postResult = formatter.postFormat(projection, exportObj);
        // Stitch together Pre-Formatted, Formatted, Post-Formatted results of Formatter in single observable.
        Observable<String> interimResults = concatStringWithObservable(preResult, results, true);
        Observable<String> finalResults = concatStringWithObservable(postResult, interimResults, false);
        TableExportResult result = storeResults(exportObj, engine, finalResults);
        if (result != null && result.getMessage() != null) {
            throw new IllegalStateException(result.getMessage());
        }
        exportResult.setUrl(new URL(generateDownloadURL(exportObj, scope)));
        exportResult.setRecordCount(recordNumber);
        tx.flush(requestScope);
        elide.getAuditLogger().commit();
        tx.commit(requestScope);
    } catch (BadRequestException e) {
        exportResult.setMessage(e.getMessage());
    } catch (MalformedURLException e) {
        exportResult.setMessage("Download url generation failure.");
    } catch (IOException e) {
        log.error("IOException during TableExport", e);
        exportResult.setMessage(e.getMessage());
    } catch (Exception e) {
        exportResult.setMessage(e.getMessage());
    } finally {
        // Follows same flow as GraphQL. The query may result in failure but request was successfully processed.
        exportResult.setHttpStatus(200);
        exportResult.setCompletedOn(new Date());
        elide.getTransactionRegistry().removeRunningTransaction(requestId);
        elide.getAuditLogger().clear();
    }
    return exportResult;
}
Also used : Arrays(java.util.Arrays) Getter(lombok.Getter) ResultStorageEngine(com.yahoo.elide.async.service.storageengine.ResultStorageEngine) AsyncAPIResult(com.yahoo.elide.async.models.AsyncAPIResult) URL(java.net.URL) Date(java.util.Date) SingleRootProjectionValidator(com.yahoo.elide.async.export.validator.SingleRootProjectionValidator) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) Validator(com.yahoo.elide.async.export.validator.Validator) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) Observable(io.reactivex.Observable) AsyncAPI(com.yahoo.elide.async.models.AsyncAPI) TableExport(com.yahoo.elide.async.models.TableExport) RequestScope(com.yahoo.elide.core.RequestScope) Elide(com.yahoo.elide.Elide) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) FileExtensionType(com.yahoo.elide.async.models.FileExtensionType) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) EntityProjection(com.yahoo.elide.core.request.EntityProjection) IOException(java.io.IOException) TableExportFormatter(com.yahoo.elide.async.export.formatter.TableExportFormatter) UUID(java.util.UUID) TableExportResult(com.yahoo.elide.async.models.TableExportResult) AsyncExecutorService(com.yahoo.elide.async.service.AsyncExecutorService) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Collections(java.util.Collections) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) IOException(java.io.IOException) RequestScope(com.yahoo.elide.core.RequestScope) TableExportResult(com.yahoo.elide.async.models.TableExportResult) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Date(java.util.Date) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) Elide(com.yahoo.elide.Elide) UUID(java.util.UUID)

Aggregations

RequestScope (com.yahoo.elide.core.RequestScope)132 Test (org.junit.jupiter.api.Test)99 PersistentResource (com.yahoo.elide.core.PersistentResource)63 TestRequestScope (com.yahoo.elide.core.TestRequestScope)28 Include (com.yahoo.elide.annotation.Include)27 Entity (javax.persistence.Entity)27 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)26 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)23 ReadPermission (com.yahoo.elide.annotation.ReadPermission)22 EntityProjection (com.yahoo.elide.core.request.EntityProjection)22 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)22 Book (example.Book)19 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)17 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)15 HashSet (java.util.HashSet)15 Publisher (example.Publisher)14 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)12 Author (example.Author)10 Editor (example.Editor)10 Collection (java.util.Collection)10