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);
}
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);
}
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");
}
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);
}
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);
}
Aggregations