use of com.linkedin.restli.server.RoutingException in project rest.li by linkedin.
the class RestLiJSONDocumentationRenderer method renderDataModel.
@Override
public void renderDataModel(String dataModelName, OutputStream out) {
final NamedDataSchema schema = _relationships.getDataModels().get(dataModelName);
if (schema == null) {
throw new RoutingException(String.format("Data model named '%s' does not exist", dataModelName), 404);
}
final DataMap outputMap = createEmptyOutput();
try {
renderDataModel(schema, outputMap);
_codec.writeMap(outputMap, out);
} catch (IOException e) {
throw new RestLiInternalException(e);
}
}
use of com.linkedin.restli.server.RoutingException in project rest.li by linkedin.
the class RestLiHTMLDocumentationRenderer method renderDataModel.
@Override
public void renderDataModel(String dataModelName, OutputStream out) {
final NamedDataSchema schema = _relationships.getDataModels().get(dataModelName);
if (schema == null) {
throw new RoutingException(String.format("Data model named '%s' does not exist", dataModelName), 404);
}
final Map<String, Object> pageModel = createPageModel();
pageModel.put("dataModel", schema);
final DataMap example = SchemaSampleDataGenerator.buildRecordData(schema, new SchemaSampleDataGenerator.DataGenerationOptions());
try {
pageModel.put("example", new String(_codec.mapToBytes(example)));
} catch (IOException e) {
throw new RestLiInternalException(e);
}
addRelated(schema, pageModel);
_templatingEngine.render("dataModel.vm", pageModel, out);
}
use of com.linkedin.restli.server.RoutingException in project rest.li by linkedin.
the class TestActionArgumentBuilder method testBuildArgumentsFailure.
@Test
public void testBuildArgumentsFailure() {
String entity = "{\"param2\":5678}";
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, entity, 3);
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, getStringAndIntParams(), null, null);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, null, false);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 2, context, 1);
RestLiArgumentBuilder argumentBuilder = new ActionArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
try {
argumentBuilder.buildArguments(requestData, routingResult);
fail("Expected RoutingException");
} catch (RoutingException e) {
assertEquals(e.getMessage(), "Parameter 'param1' is required");
}
verify(request, descriptor, routingResult);
}
use of com.linkedin.restli.server.RoutingException in project rest.li by linkedin.
the class ActionArgumentBuilder method extractRequestData.
@Override
public RestLiRequestData extractRequestData(RoutingResult routingResult, RestRequest request) {
ResourceMethodDescriptor resourceMethodDescriptor = routingResult.getResourceMethod();
final DataMap data;
if (request.getEntity() == null || request.getEntity().length() == 0) {
data = new DataMap();
} else {
data = DataMapUtils.readMap(request);
}
DynamicRecordTemplate template = new DynamicRecordTemplate(data, resourceMethodDescriptor.getRequestDataSchema());
ValidationResult result = ValidateDataAgainstSchema.validate(data, template.schema(), new ValidationOptions(RequiredMode.IGNORE, CoercionMode.NORMAL));
if (!result.isValid()) {
throw new RoutingException("Parameters of method '" + resourceMethodDescriptor.getActionName() + "' failed validation with error '" + result.getMessages() + "'", HttpStatus.S_400_BAD_REQUEST.getCode());
}
return new RestLiRequestDataImpl.Builder().entity(template).build();
}
use of com.linkedin.restli.server.RoutingException in project rest.li by linkedin.
the class ArgumentBuilder method parseEntityStringKey.
/**
* Parses the provided string key value and returns its corresponding typed key instance. This method should only be
* used to parse keys which appear in the request body.
*
* @param stringKey Key string from the entity body
* @param routingResult {@link RoutingResult} instance for the current request
* @param version {@link ProtocolVersion} instance of the current request
* @return An instance of key's corresponding type
*/
static Object parseEntityStringKey(final String stringKey, final RoutingResult routingResult, final ProtocolVersion version) {
ResourceModel resourceModel = routingResult.getResourceMethod().getResourceModel();
ResourceContext resourceContext = routingResult.getContext();
try {
Key primaryKey = resourceModel.getPrimaryKey();
String altKeyName = resourceContext.getParameter(RestConstants.ALT_KEY_PARAM);
if (altKeyName != null) {
return ArgumentUtils.translateFromAlternativeKey(ArgumentUtils.parseAlternativeKey(stringKey, altKeyName, resourceModel, version), altKeyName, resourceModel);
} else if (ComplexResourceKey.class.equals(primaryKey.getType())) {
return ComplexResourceKey.parseString(stringKey, resourceModel.getKeyKeyClass(), resourceModel.getKeyParamsClass(), version);
} else if (CompoundKey.class.equals(primaryKey.getType())) {
return ArgumentUtils.parseCompoundKey(stringKey, resourceModel.getKeys(), version);
} else {
// The conversion of simple keys doesn't include URL decoding as the current version of Rest.li clients don't
// encode simple keys which appear in the request body for BATCH UPDATE and BATCH PATCH requests.
Key key = resourceModel.getPrimaryKey();
return ArgumentUtils.convertSimpleValue(stringKey, key.getDataSchema(), key.getType());
}
} catch (InvalidAlternativeKeyException | AlternativeKeyCoercerException | PathSegment.PathSegmentSyntaxException | IllegalArgumentException e) {
throw new RoutingException(String.format("Invalid key: '%s'", stringKey), HttpStatus.S_400_BAD_REQUEST.getCode());
}
}
Aggregations