Search in sources :

Example 1 with RoutingException

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);
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) RoutingException(com.linkedin.restli.server.RoutingException) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap)

Example 2 with RoutingException

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);
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) RoutingException(com.linkedin.restli.server.RoutingException) SchemaSampleDataGenerator(com.linkedin.data.schema.generator.SchemaSampleDataGenerator) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap)

Example 3 with RoutingException

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);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RoutingException(com.linkedin.restli.server.RoutingException) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceContext(com.linkedin.restli.server.ResourceContext) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 4 with RoutingException

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();
}
Also used : DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) RoutingException(com.linkedin.restli.server.RoutingException) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) DataMap(com.linkedin.data.DataMap)

Example 5 with RoutingException

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());
    }
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) InvalidAlternativeKeyException(com.linkedin.data.template.InvalidAlternativeKeyException) AlternativeKeyCoercerException(com.linkedin.restli.internal.server.util.AlternativeKeyCoercerException) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey)

Aggregations

RoutingException (com.linkedin.restli.server.RoutingException)41 RestRequest (com.linkedin.r2.message.rest.RestRequest)15 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)15 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)15 Test (org.testng.annotations.Test)15 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)12 DataMap (com.linkedin.data.DataMap)7 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)7 TemplateRuntimeException (com.linkedin.data.template.TemplateRuntimeException)6 ResourceContext (com.linkedin.restli.server.ResourceContext)6 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)6 RequestContext (com.linkedin.r2.message.RequestContext)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 CompoundKey (com.linkedin.restli.common.CompoundKey)5 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)5 ByteString (com.linkedin.data.ByteString)4 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)4 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)4 CustomString (com.linkedin.restli.server.custom.types.CustomString)4 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)4