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