Search in sources :

Example 16 with RestLiInternalException

use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.

the class TestBatchCreateArgumentBuilder method testFailure.

@Test(dataProvider = "failureData")
public void testFailure(String entity, String expectedExceptionMessage) {
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, entity, 1);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, null, false);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 1, null);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, null, 0);
    RestLiArgumentBuilder argumentBuilder = new BatchCreateArgumentBuilder();
    try {
        argumentBuilder.extractRequestData(routingResult, request);
        fail("Expected RestLiInternalException or ClassCastException");
    } catch (RestLiInternalException e) {
        assertTrue(e.getMessage().contains(expectedExceptionMessage));
    } catch (ClassCastException e) {
        assertTrue(e.getMessage().contains("java.lang.Integer cannot be cast to com.linkedin.data.DataList"));
    }
    verify(request, model, descriptor, routingResult);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Test(org.testng.annotations.Test)

Example 17 with RestLiInternalException

use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.

the class Jsr330Adapter method scanInjectableConstructors.

private void scanInjectableConstructors(Class<?> beanClazz) {
    int annotatedConstructors = 0;
    for (Constructor<?> constructor : beanClazz.getConstructors()) {
        Inject injectAnnotation = constructor.getAnnotation(Inject.class);
        if (injectAnnotation != null) {
            ++annotatedConstructors;
            if (annotatedConstructors > 1) {
                throw new RestLiInternalException("Found multiple constructors annotated with @Inject in " + "class '" + beanClazz.getCanonicalName() + "'.  At most one constructor can be annotated " + "with @Inject.");
            }
            Class<?>[] parameters = constructor.getParameterTypes();
            Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
            List<DependencyDecl> parameterDecls = new ArrayList<DependencyDecl>(parameters.length);
            for (int i = 0; i < parameters.length; ++i) {
                Class<?> parameter = parameters[i];
                AnnotationSet annotations = new AnnotationSet(parameterAnnotations[i]);
                Named namedAnno = annotations.get(Named.class);
                parameterDecls.add(new DependencyDecl(parameter, namedAnno != null ? namedAnno.value() : null));
            }
            constructor.setAccessible(true);
            _constructorParameterDependencies.put(beanClazz, new InjectableConstructor(constructor, parameterDecls));
        }
    }
    if (annotatedConstructors == 0) {
        try {
            Constructor<?> defaultConstructor = beanClazz.getConstructor();
            defaultConstructor.setAccessible(true);
            _constructorParameterDependencies.put(beanClazz, new InjectableConstructor(defaultConstructor, Collections.<DependencyDecl>emptyList()));
        } catch (NoSuchMethodException e) {
            throw new RestLiInternalException(String.format("No injectable constructor defined for class %s.  Classes must define" + " either a default constructor or a constructor annotated " + "with @Inject.", beanClazz.getName()), e);
        }
    }
}
Also used : Inject(javax.inject.Inject) Named(javax.inject.Named) ArrayList(java.util.ArrayList) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

Example 18 with RestLiInternalException

use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.

the class RestLiJSONDocumentationRenderer method renderResource.

@Override
public void renderResource(String resourceName, OutputStream out) {
    final ResourceSchema resourceSchema = _relationships.getResourceSchemaCollection().getResource(resourceName);
    if (resourceSchema == null) {
        throw new RoutingException(String.format("Resource named '%s' does not exist", resourceName), 404);
    }
    final DataMap outputMap = createEmptyOutput();
    try {
        renderResource(resourceSchema, outputMap);
        _codec.writeMap(outputMap, out);
    } catch (IOException e) {
        throw new RestLiInternalException(e);
    }
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap)

Example 19 with RestLiInternalException

use of com.linkedin.restli.internal.server.RestLiInternalException in project rest.li by linkedin.

the class VelocityTemplatingEngine method render.

@Override
public void render(String templateName, Map<String, Object> pageModel, OutputStream out) {
    if (_velocity == null) {
        return;
    }
    final String actualTemplateName = VELOCITY_TEMPLATE_DIR + "/" + templateName;
    final VelocityContext context = new VelocityContext(pageModel);
    final Writer outWriter = new OutputStreamWriter(out);
    try {
        _velocity.mergeTemplate(actualTemplateName, VelocityEngine.ENCODING_DEFAULT, context, outWriter);
    } catch (Exception e) {
        throw new RestLiInternalException(e);
    }
    try {
        outWriter.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) IOException(java.io.IOException)

Aggregations

RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)19 IOException (java.io.IOException)8 DataMap (com.linkedin.data.DataMap)6 NamedDataSchema (com.linkedin.data.schema.NamedDataSchema)4 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)4 RoutingException (com.linkedin.restli.server.RoutingException)4 HashMap (java.util.HashMap)4 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)3 SomeDependency1 (com.linkedin.restli.server.resources.fixtures.SomeDependency1)3 SomeDependency2 (com.linkedin.restli.server.resources.fixtures.SomeDependency2)3 Test (org.testng.annotations.Test)3 JsonBuilder (com.linkedin.data.schema.JsonBuilder)2 PrimitiveDataSchema (com.linkedin.data.schema.PrimitiveDataSchema)2 SchemaToJsonEncoder (com.linkedin.data.schema.SchemaToJsonEncoder)2 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)2 ArrayList (java.util.ArrayList)2 DataSchema (com.linkedin.data.schema.DataSchema)1 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)1 UnionDataSchema (com.linkedin.data.schema.UnionDataSchema)1 SchemaSampleDataGenerator (com.linkedin.data.schema.generator.SchemaSampleDataGenerator)1