Search in sources :

Example 81 with CompoundKey

use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.

the class RestLiRouter method processResourceTree.

private RoutingResult processResourceTree(final ResourceModel resource, final ServerResourceContext context, final Queue<String> remainingPath) {
    ResourceModel currentResource = resource;
    // iterate through all path segments, simultaneously descending the resource hierarchy
    // and parsing path keys where applicable;
    // the goal of this loop is to locate the leaf resource, which will be set in
    // currentResource, and to parse the necessary information into the context
    ResourceLevel currentLevel = currentResource.getResourceLevel();
    while (remainingPath.peek() != null) {
        String currentPathSegment = remainingPath.poll();
        if (currentLevel.equals(ResourceLevel.ENTITY)) {
            currentResource = currentResource.getSubResource(parseSubresourceName(currentPathSegment));
            currentLevel = currentResource == null ? ResourceLevel.ANY : currentResource.getResourceLevel();
        } else {
            ResourceModel currentCollectionResource = currentResource;
            if (currentResource.getKeys().isEmpty()) {
                throw new RoutingException(String.format("Path key not supported on resource '%s' for URI '%s'", currentResource.getName(), context.getRequestURI()), HttpStatus.S_400_BAD_REQUEST.getCode());
            } else if (context.getParameters().containsKey(RestConstants.ALT_KEY_PARAM)) {
                parseAlternativeKey(currentResource, context, currentPathSegment);
                currentLevel = ResourceLevel.ENTITY;
            } else if (currentResource.getKeyClass() == ComplexResourceKey.class) {
                parseComplexKey(currentResource, context, currentPathSegment);
                currentLevel = ResourceLevel.ENTITY;
            } else if (currentResource.getKeyClass() == CompoundKey.class) {
                CompoundKey compoundKey;
                try {
                    compoundKey = parseCompoundKey(currentCollectionResource, context, currentPathSegment);
                } catch (IllegalArgumentException e) {
                    throw new RoutingException(String.format("Malformed Compound Key: '%s'", currentPathSegment), HttpStatus.S_400_BAD_REQUEST.getCode(), e);
                }
                if (compoundKey != null && compoundKey.getPartKeys().containsAll(currentResource.getKeyNames())) {
                    // full match on key parts means that we are targeting a unique entity
                    currentLevel = ResourceLevel.ENTITY;
                }
            } else // Must be a simple key then
            {
                parseSimpleKey(currentResource, context, currentPathSegment);
                currentLevel = ResourceLevel.ENTITY;
            }
        }
        if (currentResource == null) {
            throw new RoutingException(HttpStatus.S_404_NOT_FOUND.getCode());
        }
    }
    //now we know the key type, look for batch parameter
    parseBatchKeysParameter(currentResource, context);
    return findMethodDescriptor(currentResource, currentLevel, context);
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) CompoundKey(com.linkedin.restli.common.CompoundKey) ResourceLevel(com.linkedin.restli.server.ResourceLevel) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel)

Example 82 with CompoundKey

use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.

the class ChainedTyperefResource method batchUpdate.

@Override
public BatchUpdateResult<CompoundKey, Greeting> batchUpdate(BatchUpdateRequest<CompoundKey, Greeting> entities) {
    Set<CompoundKey> keys = entities.getData().keySet();
    Map<CompoundKey, UpdateResponse> responseMap = new HashMap<CompoundKey, UpdateResponse>();
    Map<CompoundKey, RestLiServiceException> errorMap = new HashMap<CompoundKey, RestLiServiceException>();
    for (CompoundKey key : keys) {
        responseMap.put(key, new UpdateResponse(HttpStatus.S_201_CREATED));
    }
    return new BatchUpdateResult<CompoundKey, Greeting>(responseMap);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap)

Example 83 with CompoundKey

use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.

the class TestGroupsRequestBuilders method requestMembershipsBuilderDataProviderEntityFinderByGroupWithOptional.

@DataProvider(name = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestMembershipsBuilderDataProviderEntityFinderByGroupWithOptional")
private static Object[][] requestMembershipsBuilderDataProviderEntityFinderByGroupWithOptional() {
    //Sample URIs:
    //"groupMemberships/groupID=1?firstName=Bruce&q=group"
    //"groupMemberships/(groupID:1)?firstName=Bruce&q=group"
    final Map<String, String> queryParamsMap = new HashMap<String, String>();
    queryParamsMap.put("firstName", "Bruce");
    queryParamsMap.put("q", "group");
    final URIDetails uriDetails1 = new URIDetails(AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), "groupMemberships/groupID=1", null, queryParamsMap, null);
    final URIDetails uriDetails2 = new URIDetails(AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), "groupMemberships/(groupID:1)", null, queryParamsMap, null);
    return new Object[][] { { new RootBuilderWrapper<CompoundKey, GroupMembership>(new GroupMembershipsBuilders()), uriDetails1 }, { new RootBuilderWrapper<CompoundKey, GroupMembership>(new GroupMembershipsBuilders()), uriDetails2 }, { new RootBuilderWrapper<CompoundKey, GroupMembership>(new GroupMembershipsRequestBuilders()), uriDetails1 }, { new RootBuilderWrapper<CompoundKey, GroupMembership>(new GroupMembershipsRequestBuilders()), uriDetails2 } };
}
Also used : URIDetails(com.linkedin.restli.internal.testutils.URIDetails) HashMap(java.util.HashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) GroupMembershipsRequestBuilders(com.linkedin.restli.examples.groups.client.GroupMembershipsRequestBuilders) GroupMembership(com.linkedin.restli.examples.groups.api.GroupMembership) GroupMembershipsBuilders(com.linkedin.restli.examples.groups.client.GroupMembershipsBuilders) DataProvider(org.testng.annotations.DataProvider)

Example 84 with CompoundKey

use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.

the class ExampleRequestResponseGenerator method generateKey.

private Object generateKey(ResourceSpec resourceSpec, ResourceSchema resourceSchema, Integer batchIdx) {
    if (resourceSpec.getKeyType() == null) {
        throw new IllegalArgumentException("Cannot generate key for keyless resource.");
    }
    switch(toResourceKeyType(resourceSpec.getKeyType().getType())) {
        case COMPLEX:
            RecordDataSchema keySchema = (RecordDataSchema) resourceSpec.getComplexKeyType().getKeyType().getSchema();
            DataMap keyData = (DataMap) _dataGenerator.buildData(postfixBatchIdx(keySchema.getName() + "Key", batchIdx), keySchema);
            RecordDataSchema paramsSchema = (RecordDataSchema) resourceSpec.getComplexKeyType().getParamsType().getSchema();
            DataMap paramsData = (DataMap) _dataGenerator.buildData(postfixBatchIdx(keySchema.getName() + "Params", batchIdx), paramsSchema);
            return new ComplexResourceKey<RecordTemplatePlaceholder, RecordTemplatePlaceholder>(new RecordTemplatePlaceholder(keyData, keySchema), new RecordTemplatePlaceholder(paramsData, paramsSchema));
        case COMPOUND:
            CompoundKey compoundKey = new CompoundKey();
            for (Map.Entry<String, CompoundKey.TypeInfo> keyPart : resourceSpec.getKeyParts().entrySet()) {
                String key = keyPart.getKey();
                CompoundKey.TypeInfo typeInfo = keyPart.getValue();
                compoundKey.append(key, _dataGenerator.buildData(postfixBatchIdx(key, batchIdx), typeInfo.getBinding().getSchema()));
            }
            return compoundKey;
        case PRIMITIVE:
            String keyName = resourceSchema.getCollection().getIdentifier().getName();
            return _dataGenerator.buildData(postfixBatchIdx(keyName, batchIdx), resourceSpec.getKeyType().getSchema());
        case NONE:
            return null;
        default:
            throw new IllegalStateException("Unknown enum value: " + resourceSpec.getKeyType().getType());
    }
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Map(java.util.Map) DataMap(com.linkedin.data.DataMap) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap)

Example 85 with CompoundKey

use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.

the class TestGroupsRequestBuilders method requestMembershipsBuilderDataProviderEntityFinderByMember.

@DataProvider(name = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestMembershipsBuilderDataProviderEntityFinderByMember")
private static Object[][] requestMembershipsBuilderDataProviderEntityFinderByMember() {
    //Sample URIs:
    //"groupMemberships/memberID=1?q=member"
    //"groupMemberships/(memberID:1)?q=member"
    final Map<String, String> queryParamsMap = new HashMap<String, String>();
    queryParamsMap.put("q", "member");
    final URIDetails uriDetails1 = new URIDetails(AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), "groupMemberships/memberID=1", null, queryParamsMap, null);
    final URIDetails uriDetails2 = new URIDetails(AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), "groupMemberships/(memberID:1)", null, queryParamsMap, null);
    return new Object[][] { { new RootBuilderWrapper<CompoundKey, GroupMembership>(new GroupMembershipsBuilders()), uriDetails1 }, { new RootBuilderWrapper<CompoundKey, GroupMembership>(new GroupMembershipsBuilders()), uriDetails2 }, { new RootBuilderWrapper<CompoundKey, GroupMembership>(new GroupMembershipsRequestBuilders()), uriDetails1 }, { new RootBuilderWrapper<CompoundKey, GroupMembership>(new GroupMembershipsRequestBuilders()), uriDetails2 } };
}
Also used : URIDetails(com.linkedin.restli.internal.testutils.URIDetails) HashMap(java.util.HashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) GroupMembershipsRequestBuilders(com.linkedin.restli.examples.groups.client.GroupMembershipsRequestBuilders) GroupMembership(com.linkedin.restli.examples.groups.api.GroupMembership) GroupMembershipsBuilders(com.linkedin.restli.examples.groups.client.GroupMembershipsBuilders) DataProvider(org.testng.annotations.DataProvider)

Aggregations

CompoundKey (com.linkedin.restli.common.CompoundKey)94 Test (org.testng.annotations.Test)48 HashMap (java.util.HashMap)26 DataProvider (org.testng.annotations.DataProvider)20 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)17 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)16 GroupMembership (com.linkedin.restli.examples.groups.api.GroupMembership)15 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)14 AfterTest (org.testng.annotations.AfterTest)13 BeforeTest (org.testng.annotations.BeforeTest)13 DataMap (com.linkedin.data.DataMap)12 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)12 Key (com.linkedin.restli.server.Key)11 Followed (com.linkedin.restli.server.twitter.TwitterTestDataModels.Followed)10 HashSet (java.util.HashSet)10 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)9 TestRecord (com.linkedin.restli.client.test.TestRecord)9 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)9 AsyncFollowsAssociativeResource (com.linkedin.restli.server.twitter.AsyncFollowsAssociativeResource)9 EmptyRecord (com.linkedin.restli.common.EmptyRecord)8