Search in sources :

Example 56 with RequestScope

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

the class ResourceIT method testSpecialCharacterLikeQueryHQL.

@ParameterizedTest
@MethodSource("queryProviderHQL")
public void testSpecialCharacterLikeQueryHQL(FilterPredicate filterPredicate, int noOfRecords) throws Exception {
    DataStoreTransaction tx = dataStore.beginReadTransaction();
    RequestScope scope = mock(RequestScope.class);
    EntityDictionary dictionary = EntityDictionary.builder().build();
    dictionary.bindEntity(Book.class);
    when(scope.getDictionary()).thenReturn(dictionary);
    PaginationImpl pagination = mock(PaginationImpl.class);
    when(pagination.returnPageTotals()).thenReturn(true);
    tx.loadObjects(EntityProjection.builder().type(Book.class).filterExpression(filterPredicate).pagination(pagination).build(), scope);
    tx.commit(scope);
    tx.close();
    verify(pagination).setPageTotals((long) noOfRecords);
}
Also used : PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 57 with RequestScope

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

the class Elide method get.

/**
 * Handle GET.
 *
 * @param baseUrlEndPoint base URL with prefix endpoint
 * @param path the path
 * @param queryParams the query params
 * @param requestHeaders the request headers
 * @param opaqueUser the opaque user
 * @param apiVersion the API version
 * @param requestId the request ID
 * @return Elide response object
 */
public ElideResponse get(String baseUrlEndPoint, String path, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
    if (elideSettings.isStrictQueryParams()) {
        try {
            verifyQueryParams(queryParams);
        } catch (BadRequestException e) {
            return buildErrorResponse(e, false);
        }
    }
    return handleRequest(true, opaqueUser, dataStore::beginReadTransaction, requestId, (tx, user) -> {
        JsonApiDocument jsonApiDoc = new JsonApiDocument();
        RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
        requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
        BaseVisitor visitor = new GetVisitor(requestScope);
        return visit(path, requestScope, visitor);
    });
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) BaseVisitor(com.yahoo.elide.jsonapi.parser.BaseVisitor) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) GetVisitor(com.yahoo.elide.jsonapi.parser.GetVisitor) EntityProjectionMaker(com.yahoo.elide.jsonapi.EntityProjectionMaker) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) RequestScope(com.yahoo.elide.core.RequestScope)

Example 58 with RequestScope

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

the class Elide method patch.

/**
 * Handle PATCH.
 *
 * @param baseUrlEndPoint base URL with prefix endpoint
 * @param contentType the content type
 * @param accept the accept
 * @param path the path
 * @param jsonApiDocument the json api document
 * @param queryParams the query params
 * @param requestHeaders the request headers
 * @param opaqueUser the opaque user
 * @param apiVersion the API version
 * @param requestId the request ID
 * @return Elide response object
 */
public ElideResponse patch(String baseUrlEndPoint, String contentType, String accept, String path, String jsonApiDocument, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
    Handler<DataStoreTransaction, User, HandlerResult> handler;
    if (JsonApiPatch.isPatchExtension(contentType) && JsonApiPatch.isPatchExtension(accept)) {
        handler = (tx, user) -> {
            PatchRequestScope requestScope = new PatchRequestScope(baseUrlEndPoint, path, apiVersion, tx, user, requestId, queryParams, requestHeaders, elideSettings);
            try {
                Supplier<Pair<Integer, JsonNode>> responder = JsonApiPatch.processJsonPatch(dataStore, path, jsonApiDocument, requestScope);
                return new HandlerResult(requestScope, responder);
            } catch (RuntimeException e) {
                return new HandlerResult(requestScope, e);
            }
        };
    } else {
        handler = (tx, user) -> {
            JsonApiDocument jsonApiDoc = mapper.readJsonApiDocument(jsonApiDocument);
            RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
            requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
            BaseVisitor visitor = new PatchVisitor(requestScope);
            return visit(path, requestScope, visitor);
        };
    }
    return handleRequest(false, opaqueUser, dataStore::beginTransaction, requestId, handler);
}
Also used : PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) User(com.yahoo.elide.core.security.User) JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) PatchVisitor(com.yahoo.elide.jsonapi.parser.PatchVisitor) EntityProjectionMaker(com.yahoo.elide.jsonapi.EntityProjectionMaker) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) RequestScope(com.yahoo.elide.core.RequestScope) BaseVisitor(com.yahoo.elide.jsonapi.parser.BaseVisitor) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Pair(org.apache.commons.lang3.tuple.Pair)

Example 59 with RequestScope

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

the class Elide method post.

/**
 * Handle POST.
 *
 * @param baseUrlEndPoint base URL with prefix endpoint
 * @param path the path
 * @param jsonApiDocument the json api document
 * @param queryParams the query params
 * @param requestHeaders the request headers
 * @param opaqueUser the opaque user
 * @param apiVersion the API version
 * @param requestId the request ID
 * @return Elide response object
 */
public ElideResponse post(String baseUrlEndPoint, String path, String jsonApiDocument, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
    return handleRequest(false, opaqueUser, dataStore::beginTransaction, requestId, (tx, user) -> {
        JsonApiDocument jsonApiDoc = mapper.readJsonApiDocument(jsonApiDocument);
        RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
        requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
        BaseVisitor visitor = new PostVisitor(requestScope);
        return visit(path, requestScope, visitor);
    });
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) BaseVisitor(com.yahoo.elide.jsonapi.parser.BaseVisitor) EntityProjectionMaker(com.yahoo.elide.jsonapi.EntityProjectionMaker) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) RequestScope(com.yahoo.elide.core.RequestScope) PostVisitor(com.yahoo.elide.jsonapi.parser.PostVisitor)

Example 60 with RequestScope

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

the class Elide method delete.

/**
 * Handle DELETE.
 *
 * @param baseUrlEndPoint base URL with prefix endpoint
 * @param path the path
 * @param jsonApiDocument the json api document
 * @param queryParams the query params
 * @param requestHeaders the request headers
 * @param opaqueUser the opaque user
 * @param apiVersion the API version
 * @param requestId the request ID
 * @return Elide response object
 */
public ElideResponse delete(String baseUrlEndPoint, String path, String jsonApiDocument, MultivaluedMap<String, String> queryParams, Map<String, List<String>> requestHeaders, User opaqueUser, String apiVersion, UUID requestId) {
    return handleRequest(false, opaqueUser, dataStore::beginTransaction, requestId, (tx, user) -> {
        JsonApiDocument jsonApiDoc = StringUtils.isEmpty(jsonApiDocument) ? new JsonApiDocument() : mapper.readJsonApiDocument(jsonApiDocument);
        RequestScope requestScope = new RequestScope(baseUrlEndPoint, path, apiVersion, jsonApiDoc, tx, user, queryParams, requestHeaders, requestId, elideSettings);
        requestScope.setEntityProjection(new EntityProjectionMaker(elideSettings.getDictionary(), requestScope).parsePath(path));
        BaseVisitor visitor = new DeleteVisitor(requestScope);
        return visit(path, requestScope, visitor);
    });
}
Also used : JsonApiDocument(com.yahoo.elide.jsonapi.models.JsonApiDocument) BaseVisitor(com.yahoo.elide.jsonapi.parser.BaseVisitor) EntityProjectionMaker(com.yahoo.elide.jsonapi.EntityProjectionMaker) PatchRequestScope(com.yahoo.elide.jsonapi.extensions.PatchRequestScope) RequestScope(com.yahoo.elide.core.RequestScope) DeleteVisitor(com.yahoo.elide.jsonapi.parser.DeleteVisitor)

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