use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestBatchUpdateArgumentBuilder method testArgumentBuilderSuccess.
@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(ProtocolVersion version, Key primaryKey, Key[] associationKeys, String requestEntity, Object[] keys) throws Exception {
Set<Object> batchKeys = new HashSet<>(Arrays.asList(keys));
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(requestEntity, version);
ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, primaryKey, associationKeys, batchKeys);
@SuppressWarnings("rawtypes") Parameter<BatchUpdateRequest> param = new Parameter<>("", BatchUpdateRequest.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 3, Collections.singletonList(param), CollectionResourceAsyncTemplate.class.getMethod("batchUpdate", BatchUpdateRequest.class, Callback.class));
ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(batchKeys, version, true, false);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, context);
RestLiArgumentBuilder argumentBuilder = new BatchUpdateArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
assertEquals(args.length, 1);
assertTrue(args[0] instanceof BatchUpdateRequest);
Map<?, ?> data = ((BatchUpdateRequest) args[0]).getData();
assertEquals(data.size(), 2);
MyComplexKey entity1 = (MyComplexKey) data.get(keys[0]);
MyComplexKey entity2 = (MyComplexKey) data.get(keys[1]);
assertEquals(entity1.getA(), "abc");
assertEquals((long) entity1.getB(), 123L);
assertEquals(entity2.getA(), "XY");
assertEquals((long) entity2.getB(), 456L);
verify(request, model, descriptor, context, routingResult);
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class RestLiMethodInvoker method getBatchFinderCriteriaNumber.
private int getBatchFinderCriteriaNumber(ResourceMethodDescriptor method, ServerResourceContext resourceContext) {
List<Parameter<?>> parameterList = method.getParameters();
Integer criteriaParamIndex = method.getBatchFinderCriteriaParamIndex();
Parameter<?> criteriaParam = parameterList.get(criteriaParamIndex);
DataList criteriaList = (DataList) resourceContext.getStructuredParameter(criteriaParam.getName());
return criteriaList.size();
}
use of com.linkedin.restli.internal.server.model.Parameter 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 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, Collections.emptySet(), resourceContext);
return new RoutingResult(resourceContext, methodDescriptor);
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestRestLiResourceModels method testSimpleUnstructuredDataResource.
@Test
public void testSimpleUnstructuredDataResource() throws Exception {
ResourceModel resourceModel = buildResourceModel(SingleFeedDownloadResource.class);
assertEquals(resourceModel.getResourceType(), ResourceType.SIMPLE);
assertEquals(resourceModel.getResourceMethodDescriptors().size(), 1);
final ResourceMethodDescriptor getMethod = resourceModel.findMethod(ResourceMethod.GET);
assertNotNull(getMethod);
List<Parameter<?>> parameters = getMethod.getParameters();
Parameter<?> firstParam = parameters.get(0);
assertNotNull(firstParam);
assertEquals(firstParam.getName(), "RestLi Unstructured Data Writer");
assertEquals(firstParam.getType(), UnstructuredDataWriter.class);
assertFalse(firstParam.isOptional());
assertFalse(firstParam.hasDefaultValue());
assertNull(firstParam.getDefaultValue());
assertEquals(resourceModel.getResourceEntityType(), ResourceEntityType.UNSTRUCTURED_DATA);
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class ArgumentBuilder method buildArgs.
/**
* Build arguments for resource method invocation. Combines various types of arguments
* into a single array.
*
* @param positionalArguments pass-through arguments coming from
* {@link RestLiArgumentBuilder}
* @param resourceMethod the resource method
* @param context {@link ResourceContext}
* @param template {@link DynamicRecordTemplate}
* @return array of method argument for method invocation.
*/
@SuppressWarnings("deprecation")
public static Object[] buildArgs(final Object[] positionalArguments, final ResourceMethodDescriptor resourceMethod, final ResourceContext context, final DynamicRecordTemplate template) {
List<Parameter<?>> parameters = resourceMethod.getParameters();
Object[] arguments = Arrays.copyOf(positionalArguments, parameters.size());
fixUpComplexKeySingletonArraysInArguments(arguments);
boolean attachmentsDesired = false;
for (int i = positionalArguments.length; i < parameters.size(); ++i) {
Parameter<?> param = parameters.get(i);
try {
if (param.getParamType() == Parameter.ParamType.KEY || param.getParamType() == Parameter.ParamType.ASSOC_KEY_PARAM) {
Object value = context.getPathKeys().get(param.getName());
if (value != null) {
arguments[i] = value;
continue;
}
} else if (param.getParamType() == Parameter.ParamType.CALLBACK) {
continue;
} else if (param.getParamType() == Parameter.ParamType.PARSEQ_CONTEXT_PARAM || param.getParamType() == Parameter.ParamType.PARSEQ_CONTEXT) {
// don't know what to fill in yet
continue;
} else if (param.getParamType() == Parameter.ParamType.HEADER) {
HeaderParam headerParam = param.getAnnotations().get(HeaderParam.class);
String value = context.getRequestHeaders().get(headerParam.value());
arguments[i] = value;
continue;
} else //we must evaluate based on the param type (annotation used)
if (param.getParamType() == Parameter.ParamType.PROJECTION || param.getParamType() == Parameter.ParamType.PROJECTION_PARAM) {
arguments[i] = context.getProjectionMask();
continue;
} else if (param.getParamType() == Parameter.ParamType.METADATA_PROJECTION_PARAM) {
arguments[i] = context.getMetadataProjectionMask();
continue;
} else if (param.getParamType() == Parameter.ParamType.PAGING_PROJECTION_PARAM) {
arguments[i] = context.getPagingProjectionMask();
continue;
} else if (param.getParamType() == Parameter.ParamType.CONTEXT || param.getParamType() == Parameter.ParamType.PAGING_CONTEXT_PARAM) {
PagingContext ctx = RestUtils.getPagingContext(context, (PagingContext) param.getDefaultValue());
arguments[i] = ctx;
continue;
} else if (param.getParamType() == Parameter.ParamType.PATH_KEYS || param.getParamType() == Parameter.ParamType.PATH_KEYS_PARAM) {
arguments[i] = context.getPathKeys();
continue;
} else if (param.getParamType() == Parameter.ParamType.PATH_KEY_PARAM) {
Object value = context.getPathKeys().get(param.getName());
if (value != null) {
arguments[i] = value;
continue;
}
} else if (param.getParamType() == Parameter.ParamType.RESOURCE_CONTEXT || param.getParamType() == Parameter.ParamType.RESOURCE_CONTEXT_PARAM) {
arguments[i] = context;
continue;
} else if (param.getParamType() == Parameter.ParamType.VALIDATOR_PARAM) {
RestLiDataValidator validator = new RestLiDataValidator(resourceMethod.getResourceModel().getResourceClass().getAnnotations(), resourceMethod.getResourceModel().getValueClass(), resourceMethod.getMethodType());
arguments[i] = validator;
continue;
} else if (param.getParamType() == Parameter.ParamType.RESTLI_ATTACHMENTS_PARAM) {
arguments[i] = ((ServerResourceContext) context).getRequestAttachmentReader();
attachmentsDesired = true;
continue;
} else if (param.getParamType() == Parameter.ParamType.POST) {
// handle action parameters
if (template != null) {
DataMap data = template.data();
if (data.containsKey(param.getName())) {
arguments[i] = template.getValue(param);
continue;
}
}
} else if (param.getParamType() == Parameter.ParamType.QUERY) {
Object value;
if (DataTemplate.class.isAssignableFrom(param.getType())) {
value = buildDataTemplateArgument(context, param);
} else {
value = buildRegularArgument(context, param);
}
if (value != null) {
arguments[i] = value;
continue;
}
} else if (param.getParamType() == Parameter.ParamType.BATCH || param.getParamType() == Parameter.ParamType.RESOURCE_KEY) {
// should not come to this routine since it should be handled by passing in positionalArguments
throw new RoutingException("Parameter '" + param.getName() + "' should be passed in as a positional argument", HttpStatus.S_400_BAD_REQUEST.getCode());
} else {
// unknown param type
throw new RoutingException("Parameter '" + param.getName() + "' has an unknown parameter type '" + param.getParamType().name() + "'", HttpStatus.S_400_BAD_REQUEST.getCode());
}
} catch (TemplateRuntimeException e) {
throw new RoutingException("Parameter '" + param.getName() + "' is invalid", HttpStatus.S_400_BAD_REQUEST.getCode());
}
try {
// check if it is optional parameter
if (param.isOptional() && param.hasDefaultValue()) {
arguments[i] = param.getDefaultValue();
} else if (param.isOptional() && !param.getType().isPrimitive()) {
// optional primitive parameter must have default value or provided
arguments[i] = null;
} else {
throw new RoutingException("Parameter '" + param.getName() + "' is required", HttpStatus.S_400_BAD_REQUEST.getCode());
}
} catch (ResourceConfigException e) {
// Parameter default value format exception should result in server error code 500.
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Parameter '" + param.getName() + "' default value is invalid", e);
}
}
//that were not needed is safe, but not for request attachments.
if (!attachmentsDesired && ((ServerResourceContext) context).getRequestAttachmentReader() != null) {
throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Resource method endpoint invoked does not accept any request attachments.");
}
return arguments;
}
Aggregations