Search in sources :

Example 1 with ComplexResourceKey

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

the class RestLiAnnotationReader method processCollection.

@SuppressWarnings("unchecked")
private static ResourceModel processCollection(final Class<? extends KeyValueResource<?, ?>> collectionResourceClass, ResourceModel parentResourceModel) {
    Class<?> keyClass;
    Class<? extends RecordTemplate> keyKeyClass = null;
    Class<? extends RecordTemplate> keyParamsClass = null;
    Class<? extends RecordTemplate> valueClass;
    Class<?> complexKeyResourceBase = null;
    // type V and the resource key type is ComplexResourceKey<K,P>
    if (ComplexKeyResource.class.isAssignableFrom(collectionResourceClass)) {
        complexKeyResourceBase = ComplexKeyResource.class;
    } else if (ComplexKeyResourceAsync.class.isAssignableFrom(collectionResourceClass)) {
        complexKeyResourceBase = ComplexKeyResourceAsync.class;
    } else if (ComplexKeyResourceTask.class.isAssignableFrom(collectionResourceClass)) {
        complexKeyResourceBase = ComplexKeyResourceTask.class;
    } else if (ComplexKeyResourcePromise.class.isAssignableFrom(collectionResourceClass)) {
        complexKeyResourceBase = ComplexKeyResourcePromise.class;
    }
    if (complexKeyResourceBase != null) {
        List<Class<?>> kvParams;
        if (complexKeyResourceBase.equals(ComplexKeyResource.class)) {
            kvParams = ReflectionUtils.getTypeArguments(ComplexKeyResource.class, (Class<? extends ComplexKeyResource<?, ?, ?>>) collectionResourceClass);
        } else if (complexKeyResourceBase.equals(ComplexKeyResourceAsync.class)) {
            kvParams = ReflectionUtils.getTypeArguments(ComplexKeyResourceAsync.class, (Class<? extends ComplexKeyResourceAsync<?, ?, ?>>) collectionResourceClass);
        } else if (complexKeyResourceBase.equals(ComplexKeyResourceTask.class)) {
            kvParams = ReflectionUtils.getTypeArguments(ComplexKeyResourceTask.class, (Class<? extends ComplexKeyResourceTask<?, ?, ?>>) collectionResourceClass);
        } else {
            kvParams = ReflectionUtils.getTypeArguments(ComplexKeyResourcePromise.class, (Class<? extends ComplexKeyResourcePromise<?, ?, ?>>) collectionResourceClass);
        }
        keyClass = ComplexResourceKey.class;
        keyKeyClass = kvParams.get(0).asSubclass(RecordTemplate.class);
        keyParamsClass = kvParams.get(1).asSubclass(RecordTemplate.class);
        valueClass = kvParams.get(2).asSubclass(RecordTemplate.class);
    } else // Otherwise, it's a KeyValueResource, whose parameters are resource key and resource
    // value
    {
        List<Type> actualTypeArguments = ReflectionUtils.getTypeArgumentsParametrized(KeyValueResource.class, collectionResourceClass);
        keyClass = ReflectionUtils.getClass(actualTypeArguments.get(0));
        if (RecordTemplate.class.isAssignableFrom(keyClass)) {
            // ComplexResourceKey
            throw new ResourceConfigException("Class '" + collectionResourceClass.getName() + "' should implement 'ComplexKeyResource' as a complex key '" + keyClass.getName() + "' is being used.");
        } else if (TyperefInfo.class.isAssignableFrom(keyClass)) {
            throw new ResourceConfigException("Typeref '" + keyClass.getName() + "' cannot be key type for class '" + collectionResourceClass.getName() + "'.");
        }
        if (keyClass.equals(ComplexResourceKey.class)) {
            @SuppressWarnings("unchecked") Type[] typeArguments = ((ParameterizedType) actualTypeArguments.get(0)).getActualTypeArguments();
            keyKeyClass = ReflectionUtils.getClass(typeArguments[0]).asSubclass(RecordTemplate.class);
            keyParamsClass = ReflectionUtils.getClass(typeArguments[1]).asSubclass(RecordTemplate.class);
        }
        valueClass = ReflectionUtils.getClass(actualTypeArguments.get(1)).asSubclass(RecordTemplate.class);
    }
    ResourceType resourceType = getResourceType(collectionResourceClass);
    RestLiAnnotationData annotationData;
    if (collectionResourceClass.isAnnotationPresent(RestLiCollection.class)) {
        annotationData = new RestLiAnnotationData(collectionResourceClass.getAnnotation(RestLiCollection.class));
    } else if (collectionResourceClass.isAnnotationPresent(RestLiAssociation.class)) {
        annotationData = new RestLiAnnotationData(collectionResourceClass.getAnnotation(RestLiAssociation.class));
    } else {
        throw new ResourceConfigException("No valid annotation on resource class '" + collectionResourceClass.getName() + "'");
    }
    String name = annotationData.name();
    String namespace = annotationData.namespace();
    String keyName;
    if (annotationData.keyName() == null) {
        keyName = name + "Id";
    } else {
        keyName = annotationData.keyName();
    }
    Key primaryKey = buildKey(name, keyName, keyClass, annotationData.typerefInfoClass());
    Set<Key> keys = new HashSet<Key>();
    if (annotationData.keys() == null) {
        keys.add(primaryKey);
    } else {
        keys.addAll(buildKeys(name, annotationData.keys()));
    }
    Class<?> parentResourceClass = annotationData.parent().equals(RestAnnotations.ROOT.class) ? null : annotationData.parent();
    ResourceModel collectionModel = new ResourceModel(primaryKey, keyKeyClass, keyParamsClass, keys, valueClass, collectionResourceClass, parentResourceClass, name, resourceType, namespace);
    collectionModel.setParentResourceModel(parentResourceModel);
    addResourceMethods(collectionResourceClass, collectionModel);
    log.info("Processed collection resource '" + collectionResourceClass.getName() + "'");
    return collectionModel;
}
Also used : RestLiAssociation(com.linkedin.restli.server.annotations.RestLiAssociation) ComplexKeyResourceTask(com.linkedin.restli.server.resources.ComplexKeyResourceTask) ComplexKeyResource(com.linkedin.restli.server.resources.ComplexKeyResource) ParameterizedType(java.lang.reflect.ParameterizedType) RecordTemplate(com.linkedin.data.template.RecordTemplate) ComplexKeyResourcePromise(com.linkedin.restli.server.resources.ComplexKeyResourcePromise) HashSet(java.util.HashSet) ComplexKeyResourceAsync(com.linkedin.restli.server.resources.ComplexKeyResourceAsync) TyperefInfo(com.linkedin.data.template.TyperefInfo) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) ParameterizedType(java.lang.reflect.ParameterizedType) InterfaceType(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor.InterfaceType) Type(java.lang.reflect.Type) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) AlternativeKey(com.linkedin.restli.server.annotations.AlternativeKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) AssocKey(com.linkedin.restli.server.annotations.AssocKey)

Example 2 with ComplexResourceKey

use of com.linkedin.restli.common.ComplexResourceKey in project incubator-gobblin by apache.

the class ThrottlingClientTest method getPermitAllocation.

private PermitAllocation getPermitAllocation(PermitRequest permitRequest, RestClient restClient, PermitsGetRequestBuilder getBuilder) throws Exception {
    Request<PermitAllocation> request = getBuilder.id(new ComplexResourceKey<>(permitRequest, new EmptyRecord())).build();
    ResponseFuture<PermitAllocation> responseFuture = restClient.sendRequest(request);
    Response<PermitAllocation> response = responseFuture.getResponse();
    return response.getEntity();
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 3 with ComplexResourceKey

use of com.linkedin.restli.common.ComplexResourceKey in project incubator-gobblin by apache.

the class FlowConfigClient method deleteFlowConfig.

/**
 * Delete a flow configuration
 * @param flowId identifier of flow configuration to delete
 * @throws RemoteInvocationException
 */
public void deleteFlowConfig(FlowId flowId) throws RemoteInvocationException {
    LOG.debug("deleteFlowConfig with groupName " + flowId.getFlowGroup() + " flowName " + flowId.getFlowName());
    DeleteRequest<FlowConfig> deleteRequest = _flowconfigsRequestBuilders.delete().id(new ComplexResourceKey<>(flowId, new EmptyRecord())).build();
    ResponseFuture<EmptyRecord> response = _restClient.get().sendRequest(deleteRequest);
    response.getResponse();
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 4 with ComplexResourceKey

use of com.linkedin.restli.common.ComplexResourceKey in project incubator-gobblin by apache.

the class FlowStatusClient method getFlowStatus.

/**
 * Get a flow status
 * @param flowStatusId identifier of flow status to get
 * @return a {@link FlowStatus} with the flow status
 * @throws RemoteInvocationException
 */
public FlowStatus getFlowStatus(FlowStatusId flowStatusId) throws RemoteInvocationException {
    LOG.debug("getFlowConfig with groupName " + flowStatusId.getFlowGroup() + " flowName " + flowStatusId.getFlowName());
    GetRequest<FlowStatus> getRequest = _flowstatusesRequestBuilders.get().id(new ComplexResourceKey<>(flowStatusId, new EmptyRecord())).build();
    Response<FlowStatus> response = _restClient.get().sendRequest(getRequest).getResponse();
    return response.getEntity();
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 5 with ComplexResourceKey

use of com.linkedin.restli.common.ComplexResourceKey in project incubator-gobblin by apache.

the class JobExecutionInfoResource method batchGet.

@Override
public Map<ComplexResourceKey<JobExecutionQuery, EmptyRecord>, JobExecutionQueryResult> batchGet(Set<ComplexResourceKey<JobExecutionQuery, EmptyRecord>> keys) {
    Map<ComplexResourceKey<JobExecutionQuery, EmptyRecord>, JobExecutionQueryResult> results = Maps.newHashMap();
    for (ComplexResourceKey<JobExecutionQuery, EmptyRecord> key : keys) {
        JobExecutionQueryResult result = get(key);
        if (result != null) {
            results.put(key, get(key));
        }
    }
    ResourceContext rc = this.getContext();
    rc.setResponseHeader("Access-Control-Allow-Origin", "*");
    this.setContext(rc);
    return results;
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) ResourceContext(com.linkedin.restli.server.ResourceContext) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Aggregations

ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)89 Test (org.testng.annotations.Test)43 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)19 TestRecord (com.linkedin.restli.client.test.TestRecord)18 CompoundKey (com.linkedin.restli.common.CompoundKey)18 HashMap (java.util.HashMap)17 EmptyRecord (com.linkedin.restli.common.EmptyRecord)16 DataMap (com.linkedin.data.DataMap)15 Message (com.linkedin.restli.examples.greetings.api.Message)14 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)13 Key (com.linkedin.restli.server.Key)13 RecordTemplate (com.linkedin.data.template.RecordTemplate)11 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)11 EntityResponse (com.linkedin.restli.common.EntityResponse)11 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)11 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 DiscoveredItemKey (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKey)11 DiscoveredItemKeyParams (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKeyParams)11 HashSet (java.util.HashSet)11 AfterTest (org.testng.annotations.AfterTest)11