Search in sources :

Example 6 with FieldDef

use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method createFieldDef.

/**
   * Helper class to create FieldDefs
   *
   * @param name   the fieldDef name
   * @param type   the fieldDef type
   * @param enclosingClass the class the FieldDef is being created in
   *
   * @return JInvocation of the creation of the FieldDef
   */
private JInvocation createFieldDef(String name, String type, JDefinedClass enclosingClass) {
    final JavaBinding binding = getJavaBindingType(type, enclosingClass);
    final JExpression schema = getCodeModel().ref(DataTemplateUtil.class).staticInvoke("getSchema").arg(binding.schemaClass.dotclass());
    final JInvocation fieldDefInvocation = JExpr._new(getCodeModel().ref(FieldDef.class).narrow(binding.valueClass)).arg(name).arg(binding.valueClass.dotclass()).arg(schema);
    return fieldDefInvocation;
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) JInvocation(com.sun.codemodel.JInvocation) JExpression(com.sun.codemodel.JExpression)

Example 7 with FieldDef

use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.

the class RestLiAnnotationReader method addActionResourceMethod.

/**
   * Add the given action method to the given resource model,  validating the method is a action before adding.
   * @param model provides the model to add the method to.
   * @param method provides the method to add to the model.
   * @throws ResourceConfigException on validation errors.
   */
private static void addActionResourceMethod(final ResourceModel model, final Method method) {
    Action actionAnno = method.getAnnotation(Action.class);
    if (actionAnno == null) {
        return;
    }
    String actionName = actionAnno.name();
    List<Parameter<?>> parameters = getParameters(model, method, ResourceMethod.ACTION);
    Class<?> returnClass = getActionReturnClass(model, method, actionAnno, actionName);
    TyperefDataSchema returnTyperefSchema = getActionTyperefDataSchema(model, actionAnno, actionName);
    validateActionReturnType(model, method, returnClass, returnTyperefSchema);
    if (!Modifier.isPublic(method.getModifiers())) {
        throw new ResourceConfigException(String.format("Resource '%s' contains non-public action method '%s'.", model.getName(), method.getName()));
    }
    RecordDataSchema recordDataSchema = DynamicRecordMetadata.buildSchema(method.getName(), parameters);
    RecordDataSchema actionReturnRecordDataSchema;
    FieldDef<?> returnFieldDef;
    if (returnClass != Void.TYPE) {
        @SuppressWarnings({ "unchecked", "rawtypes" }) FieldDef<?> nonVoidFieldDef = new FieldDef(ActionResponse.VALUE_NAME, returnClass, getDataSchema(returnClass, returnTyperefSchema));
        returnFieldDef = nonVoidFieldDef;
        actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(ActionResponse.class.getName(), Collections.singleton((returnFieldDef)));
    } else {
        returnFieldDef = null;
        actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(ActionResponse.class.getName(), Collections.<FieldDef<?>>emptyList());
    }
    if (model.getResourceLevel() == ResourceLevel.ENTITY && actionAnno.resourceLevel() == ResourceLevel.COLLECTION) {
        throw new ResourceConfigException(String.format("Resource '%s' is a simple resource, it cannot contain actions at resource level \"COLLECTION\".", model.getName()));
    }
    DataMap annotationsMap = ResourceModelAnnotation.getAnnotationsMap(method.getAnnotations());
    addDeprecatedAnnotation(annotationsMap, method);
    model.addResourceMethodDescriptor(ResourceMethodDescriptor.createForAction(method, parameters, actionName, getActionResourceLevel(actionAnno, model), returnFieldDef, actionReturnRecordDataSchema, recordDataSchema, getInterfaceType(method), annotationsMap));
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) Action(com.linkedin.restli.server.annotations.Action) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) DataMap(com.linkedin.data.DataMap)

Example 8 with FieldDef

use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.

the class TestActionResponseBuilder method getMockResourceMethodDescriptor.

@SuppressWarnings("unchecked")
private static ResourceMethodDescriptor getMockResourceMethodDescriptor() {
    ResourceMethodDescriptor mockDescriptor = EasyMock.createMock(ResourceMethodDescriptor.class);
    EasyMock.expect(mockDescriptor.getActionReturnRecordDataSchema()).andReturn(LONG_RETURN.getField().getRecord()).once();
    EasyMock.expect(((FieldDef<Long>) mockDescriptor.getActionReturnFieldDef())).andReturn(LONG_RETURN).once();
    EasyMock.replay(mockDescriptor);
    return mockDescriptor;
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)

Example 9 with FieldDef

use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.

the class TestRestLiResponseHandler method buildRoutingResultAction.

/**
   * Creates a RoutingResult for an Action with the given returnType.
   *
   * @param actionReturnType the return type of the action.
   * @return a RoutingResult
   */
private final RoutingResult buildRoutingResultAction(Class<?> actionReturnType, RestRequest request, Map<String, String> headers) throws NoSuchMethodException, RestLiSyntaxException, URISyntaxException {
    if (actionReturnType == Void.class) {
        actionReturnType = Void.TYPE;
    }
    // actual method passed in is irrelevant, since we are constructing a ResourceMethodDescriptor by hand.
    Method method = ProjectionTestFixture.class.getMethod("batchGet", Set.class);
    ResourceModel model = RestLiTestHelper.buildResourceModel(StatusCollectionResource.class);
    String actionName = "return" + actionReturnType.getSimpleName();
    List<Parameter<?>> parameters = Collections.<Parameter<?>>emptyList();
    RecordDataSchema actionReturnRecordDataSchema;
    FieldDef<?> returnFieldDef;
    if (actionReturnType != Void.TYPE) {
        @SuppressWarnings({ "unchecked", "rawtypes" }) FieldDef<?> nonVoidFieldDef = new FieldDef(ActionResponse.VALUE_NAME, actionReturnType, DataTemplateUtil.getSchema(actionReturnType));
        returnFieldDef = nonVoidFieldDef;
        actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(actionName, Collections.singleton(returnFieldDef));
    } else {
        returnFieldDef = null;
        actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(actionName, Collections.<FieldDef<?>>emptyList());
    }
    ResourceMethodDescriptor methodDescriptor = ResourceMethodDescriptor.createForAction(method, parameters, actionName, ResourceLevel.COLLECTION, returnFieldDef, actionReturnRecordDataSchema, DynamicRecordMetadata.buildSchema(actionName, parameters), InterfaceType.SYNC, new DataMap());
    model.addResourceMethodDescriptor(methodDescriptor);
    ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    RestUtils.validateRequestHeadersAndUpdateResourceContext(headers, resourceContext);
    return new RoutingResult(resourceContext, methodDescriptor);
}
Also used : PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Method(java.lang.reflect.Method) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FieldDef(com.linkedin.data.template.FieldDef) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) Parameter(com.linkedin.restli.internal.server.model.Parameter) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 10 with FieldDef

use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.

the class ActionRequestBuilder method build.

@SuppressWarnings("unchecked")
@Override
public ActionRequest<V> build() {
    if (_name == null) {
        throw new IllegalStateException("name required to build action request");
    }
    RecordDataSchema requestDataSchema;
    RecordDataSchema actionResponseDataSchema;
    FieldDef<V> responseFieldDef;
    if (// old builder code in use
    _resourceSpec.getRequestMetadata(_name) == null) {
        requestDataSchema = DynamicRecordMetadata.buildSchema(_name, _actionParams.keySet());
        Collection<FieldDef<?>> responseFieldDefCollection;
        if (_elementType.getType() == Void.class) {
            responseFieldDef = null;
            responseFieldDefCollection = Collections.emptyList();
        } else {
            responseFieldDef = new FieldDef<V>(ActionResponse.VALUE_NAME, _elementType.getType(), _elementType.getSchema());
            responseFieldDefCollection = Collections.<FieldDef<?>>singleton(responseFieldDef);
        }
        actionResponseDataSchema = DynamicRecordMetadata.buildSchema(_name, responseFieldDefCollection);
    } else {
        requestDataSchema = _resourceSpec.getRequestMetadata(_name).getRecordDataSchema();
        actionResponseDataSchema = _resourceSpec.getActionResponseMetadata(_name).getRecordDataSchema();
        responseFieldDef = (FieldDef<V>) _resourceSpec.getActionResponseMetadata(_name).getFieldDef(ActionResponse.VALUE_NAME);
    }
    @SuppressWarnings("unchecked") ActionResponseDecoder<V> actionResponseDecoder = new ActionResponseDecoder<V>(responseFieldDef, actionResponseDataSchema);
    DynamicRecordTemplate inputParameters = new DynamicRecordTemplate(requestDataSchema, buildReadOnlyActionParameters());
    inputParameters.data().setReadOnly();
    return new ActionRequest<V>(inputParameters, buildReadOnlyHeaders(), buildReadOnlyCookies(), actionResponseDecoder, _resourceSpec, buildReadOnlyQueryParameters(), getQueryParamClasses(), _name, getBaseUriTemplate(), buildReadOnlyPathKeys(), getRequestOptions(), buildReadOnlyId(), _streamingAttachments == null ? null : Collections.unmodifiableList(_streamingAttachments));
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) ActionResponseDecoder(com.linkedin.restli.internal.client.ActionResponseDecoder) DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Aggregations

FieldDef (com.linkedin.data.template.FieldDef)18 HashMap (java.util.HashMap)7 Test (org.testng.annotations.Test)7 ByteString (com.linkedin.data.ByteString)6 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)6 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)6 DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)5 DataMap (com.linkedin.data.DataMap)4 TestRecord (com.linkedin.restli.client.test.TestRecord)4 ResourceSpec (com.linkedin.restli.common.ResourceSpec)4 ResourceSpecImpl (com.linkedin.restli.common.ResourceSpecImpl)4 ArrayList (java.util.ArrayList)4 ActionResponse (com.linkedin.restli.common.ActionResponse)3 CompoundKey (com.linkedin.restli.common.CompoundKey)3 DataSchema (com.linkedin.data.schema.DataSchema)2 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)2 RequestContext (com.linkedin.r2.message.RequestContext)2 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)2 ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)2 Callback (com.linkedin.common.callback.Callback)1