Search in sources :

Example 16 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.

the class RestLiValidationFilter method onResponse.

public CompletableFuture<Void> onResponse(final FilterRequestContext requestContext, final FilterResponseContext responseContext) {
    Class<?> resourceClass = requestContext.getFilterResourceModel().getResourceClass();
    ResourceMethod method = requestContext.getMethodType();
    RestLiDataValidator validator = new RestLiDataValidator(resourceClass.getAnnotations(), requestContext.getFilterResourceModel().getValueClass(), method);
    RestLiResponseData responseData = responseContext.getResponseData();
    MaskTree projectionMask = requestContext.getProjectionMask();
    if (responseData.isErrorResponse()) {
        return CompletableFuture.completedFuture(null);
    }
    switch(method) {
        case GET:
            validateSingleResponse(validator, responseData.getRecordResponseEnvelope().getRecord(), projectionMask);
            break;
        case CREATE:
            if (requestContext.getCustomAnnotations().containsKey("returnEntity")) {
                validateSingleResponse(validator, responseData.getRecordResponseEnvelope().getRecord(), projectionMask);
            }
            break;
        case GET_ALL:
        case FINDER:
            validateCollectionResponse(validator, responseData.getCollectionResponseEnvelope().getCollectionResponse(), projectionMask);
            break;
        case BATCH_GET:
            validateBatchResponse(validator, responseData.getBatchResponseEnvelope().getBatchResponseMap(), projectionMask);
            break;
        case BATCH_CREATE:
            if (requestContext.getCustomAnnotations().containsKey("returnEntity")) {
                validateCreateCollectionResponse(validator, responseData.getBatchCreateResponseEnvelope().getCreateResponses(), projectionMask);
            }
            break;
    }
    return CompletableFuture.completedFuture(null);
}
Also used : RestLiDataValidator(com.linkedin.restli.common.validation.RestLiDataValidator) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 17 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.

the class TestParseUtils method checkProjection.

private void checkProjection(final String input, final DataMap expected) throws Exception {
    MaskTree mask = ArgumentUtils.parseProjectionParameter(input);
    Assert.assertEquals(mask.getDataMap(), expected);
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree)

Example 18 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.

the class TestResourceContext method testResourceContextGetProjectionMask.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "projectionMask")
public void testResourceContextGetProjectionMask(ProtocolVersion version, String stringUri) throws Exception {
    URI uri = URI.create(stringUri);
    Map<String, String> headers = new HashMap<String, String>(1);
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
    ResourceContext context = new ResourceContextImpl(new PathKeysImpl(), new MockRequest(uri, headers), new RequestContext());
    final MaskTree entityMask = context.getProjectionMask();
    final DataMap expectedEntityMaskMap = new DataMap();
    expectedEntityMaskMap.put("locale", 1);
    expectedEntityMaskMap.put("state", 1);
    Assert.assertEquals(entityMask.getDataMap(), expectedEntityMaskMap, "The generated DataMap for the MaskTree should be correct");
    final MaskTree metadataMask = context.getMetadataProjectionMask();
    final DataMap expectedMetadataMaskMap = new DataMap();
    expectedMetadataMaskMap.put("region", 1);
    expectedMetadataMaskMap.put("city", 1);
    Assert.assertEquals(metadataMask.getDataMap(), expectedMetadataMaskMap, "The generated DataMap for the MaskTree should be correct");
    final MaskTree pagingMask = context.getPagingProjectionMask();
    final DataMap expectedPagingMaskMap = new DataMap();
    expectedPagingMaskMap.put("start", 1);
    expectedPagingMaskMap.put("links", 1);
    Assert.assertEquals(pagingMask.getDataMap(), expectedPagingMaskMap, "The generated DataMap for the MaskTree should be correct");
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ByteString(com.linkedin.data.ByteString) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 19 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.

the class TestPatchHelper method testProjectionOnPatch.

@Test(dataProvider = "data")
public void testProjectionOnPatch(String[] patchAndProjection, String expectedResult) throws IOException {
    DataMap patch = dataMapFromString(patchAndProjection[0].replace('\'', '"'));
    DataMap projection = dataMapFromString(patchAndProjection[1].replace('\'', '"'));
    DataMap expected = dataMapFromString(expectedResult.replace('\'', '"'));
    assertEquals(PatchHelper.applyProjection(PatchRequest.<RecordTemplate>createFromPatchDocument(patch), new MaskTree(projection)).getPatchDocument(), expected);
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 20 with MaskTree

use of com.linkedin.data.transform.filter.request.MaskTree in project rest.li by linkedin.

the class URIMaskUtil method decodeMaskUriFormat.

/**
   * Return a MaskTree decoded from the URI-formatted String input.
   *
   * @param toparse StringBuilder containing a URI-formatted String
   *                representation of an encoded MaskTree
   * @return a MaskTree
   * @throws IllegalMaskException if syntax in the input is malformed
   */
public static MaskTree decodeMaskUriFormat(StringBuilder toparse) throws IllegalMaskException {
    ParseState state = ParseState.PARSE_FIELDS;
    DataMap result = new DataMap();
    Deque<DataMap> stack = new ArrayDeque<DataMap>();
    stack.addLast(result);
    while (toparse.length() > 0) {
        switch(state) {
            case TRAVERSE:
                if (toparse.indexOf(",") != 0) {
                    throw new IllegalStateException("Internal Error parsing mask: unexpected parse buffer '" + toparse + "' while traversing");
                }
                toparse.delete(0, 1);
                state = ParseState.PARSE_FIELDS;
                break;
            case DESCEND:
                if (toparse.indexOf(":(") != 0) {
                    throw new IllegalStateException("Internal Error parsing mask: unexpected parse buffer '" + toparse + "' while descending");
                }
                toparse.delete(0, 2);
                state = ParseState.PARSE_FIELDS;
                break;
            case PARSE_FIELDS:
                Integer maskValue = null;
                if (toparse.charAt(0) == '-') {
                    maskValue = MaskOperation.NEGATIVE_MASK_OP.getRepresentation();
                    toparse.delete(0, 1);
                } else {
                    maskValue = MaskOperation.POSITIVE_MASK_OP.getRepresentation();
                }
                int nextToken = -1;
                StringBuilder field = new StringBuilder();
                for (int ii = 0; ii < toparse.length(); ++ii) {
                    char c = toparse.charAt(ii);
                    switch(c) {
                        case ',':
                            state = ParseState.TRAVERSE;
                            nextToken = ii;
                            break;
                        case ':':
                            if (toparse.charAt(ii + 1) != '(') {
                                throw new IllegalMaskException("Malformed mask syntax: expected '(' token");
                            }
                            state = ParseState.DESCEND;
                            nextToken = ii;
                            break;
                        case ')':
                            state = ParseState.ASCEND;
                            nextToken = ii;
                            break;
                        default:
                            field.append(c);
                            break;
                    }
                    if (nextToken != -1) {
                        break;
                    }
                }
                if (toparse.length() != field.length()) {
                    if (nextToken == -1) {
                        throw new IllegalMaskException("Malformed mask syntax: expected closing token");
                    }
                    toparse.delete(0, nextToken);
                } else {
                    toparse.delete(0, toparse.length());
                }
                if (state == ParseState.DESCEND) {
                    if (field.length() == 0) {
                        throw new IllegalMaskException("Malformed mask syntax: empty parent field name");
                    }
                    DataMap subTree = new DataMap();
                    stack.peekLast().put(field.toString().trim(), subTree);
                    stack.addLast(subTree);
                } else if (field.length() != 0) {
                    stack.peekLast().put(field.toString().trim(), maskValue);
                }
                break;
            case ASCEND:
                if (toparse.indexOf(")") != 0) {
                    throw new IllegalStateException("Internal Error parsing mask: unexpected parse buffer '" + toparse + "' while ascending");
                }
                if (stack.isEmpty()) {
                    throw new IllegalMaskException("Malformed mask syntax: unexpected ')' token");
                }
                toparse.delete(0, 1);
                stack.removeLast();
                state = ParseState.PARSE_FIELDS;
                break;
        }
    }
    if (stack.size() != 1) {
        throw new IllegalMaskException("Malformed mask syntax: unmatched nesting");
    }
    result = stack.removeLast();
    return new MaskTree(result);
}
Also used : MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ArrayDeque(java.util.ArrayDeque) DataMap(com.linkedin.data.DataMap)

Aggregations

MaskTree (com.linkedin.data.transform.filter.request.MaskTree)49 Test (org.testng.annotations.Test)40 PathSpec (com.linkedin.data.schema.PathSpec)31 DataMap (com.linkedin.data.DataMap)23 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)9 Foo (com.linkedin.pegasus.generator.examples.Foo)8 HashMap (java.util.HashMap)8 ResourceContext (com.linkedin.restli.server.ResourceContext)7 URI (java.net.URI)7 RequestContext (com.linkedin.r2.message.RequestContext)6 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)6 RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)6 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)5 ByteString (com.linkedin.data.ByteString)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)4 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)4 ProjectionMode (com.linkedin.restli.server.ProjectionMode)4 HashSet (java.util.HashSet)4 TyperefTest (com.linkedin.pegasus.generator.test.TyperefTest)3 UnionTest (com.linkedin.pegasus.generator.test.UnionTest)3