Search in sources :

Example 6 with RestLiInternalException

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

the class Jsr330Adapter method getBean.

public <T> T getBean(final Class<T> beanClass) {
    BeanDependencies deps = _fieldDependencyBindings.get(beanClass);
    if (deps == null) {
        throw new RestLiInternalException("Could not find bean of class '" + beanClass.getName() + "'");
    }
    try {
        Constructor<?> constructor = _constructorParameterDependencies.get(beanClass).getConstructor();
        Object[] arguments = _constructorParameterBindings.get(beanClass);
        @SuppressWarnings("unchecked") T bean = (T) constructor.newInstance(arguments);
        for (Entry<Field, Object> fieldDep : deps.iterator()) {
            Field f = fieldDep.getKey();
            f.setAccessible(true);
            f.set(bean, fieldDep.getValue());
        }
        return bean;
    } catch (Throwable t) {
        throw new RestLiInternalException(String.format("Error initializing bean %s", beanClass.getName()), t);
    }
}
Also used : Field(java.lang.reflect.Field) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

Example 7 with RestLiInternalException

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

the class TestInjectResourceFactory method testHappyPath.

@Test
public void testHappyPath() {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(SomeResource1.class, SomeResource2.class, SomeResource5.class);
    // set up mock ApplicationContext
    BeanProvider ctx = createMock(BeanProvider.class);
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep1"))).andReturn(new SomeDependency1()).anyTimes();
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep3"))).andReturn(new SomeDependency1()).anyTimes();
    Map<String, SomeDependency2> map = new HashMap<String, SomeDependency2>();
    map.put("someBeanName", new SomeDependency2());
    EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency2.class))).andReturn(map).anyTimes();
    EasyMock.replay(ctx);
    InjectResourceFactory factory = new InjectResourceFactory(ctx);
    factory.setRootResources(pathRootResourceMap);
    // #1 happy path
    SomeResource1 r1 = factory.create(SomeResource1.class);
    assertNotNull(r1);
    assertNotNull(r1.getDependency1());
    assertNotNull(r1.getDependency2());
    assertNull(r1.getNonInjectedDependency());
    // #2 No deps
    SomeResource2 r2 = factory.create(SomeResource2.class);
    assertNotNull(r2);
    // #3 bean not registered with ResourceFactory
    try {
        factory.create(SomeResource3.class);
        fail("Expected no such bean exception");
    } catch (RestLiInternalException e) {
    // expected
    }
    EasyMock.verify(ctx);
    EasyMock.reset(ctx);
    // #4 derived resource
    SomeResource5 r5 = factory.create(SomeResource5.class);
    assertNotNull(r5);
    assertNotNull(r5.getDependency1());
    assertNotNull(r5.getDerivedDependency1());
    assertNotNull(r5.getDependency2());
    assertNotNull(r5.getDependency3());
    assertNull(r5.getNonInjectedDependency());
}
Also used : HashMap(java.util.HashMap) SomeResource5(com.linkedin.restli.server.resources.fixtures.SomeResource5) SomeResource2(com.linkedin.restli.server.resources.fixtures.SomeResource2) SomeDependency2(com.linkedin.restli.server.resources.fixtures.SomeDependency2) SomeDependency1(com.linkedin.restli.server.resources.fixtures.SomeDependency1) SomeResource1(com.linkedin.restli.server.resources.fixtures.SomeResource1) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Test(org.testng.annotations.Test)

Example 8 with RestLiInternalException

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

the class RestLiAnnotationReader method processResource.

/**
   * Processes an annotated resource class, producing a ResourceModel.
   *
   * @param resourceClass annotated resource class
   * @return {@link ResourceModel} for the provided resource class
   */
public static ResourceModel processResource(final Class<?> resourceClass, ResourceModel parentResourceModel) {
    final ResourceModel model;
    checkAnnotation(resourceClass);
    if ((resourceClass.isAnnotationPresent(RestLiCollection.class) || resourceClass.isAnnotationPresent(RestLiAssociation.class))) {
        // If any of these annotations, a subclass of KeyValueResource is expected
        if (!KeyValueResource.class.isAssignableFrom(resourceClass)) {
            throw new RestLiInternalException("Resource class '" + resourceClass.getName() + "' declares RestLi annotation but does not implement " + KeyValueResource.class.getName() + " interface.");
        }
        @SuppressWarnings("unchecked") Class<? extends KeyValueResource<?, ?>> clazz = (Class<? extends KeyValueResource<?, ?>>) resourceClass;
        model = processCollection(clazz, parentResourceModel);
    } else if (resourceClass.isAnnotationPresent(RestLiActions.class)) {
        model = processActions(resourceClass, parentResourceModel);
    } else if (resourceClass.isAnnotationPresent(RestLiSimpleResource.class)) {
        @SuppressWarnings("unchecked") Class<? extends SingleObjectResource<?>> clazz = (Class<? extends SingleObjectResource<?>>) resourceClass;
        model = processSingleObjectResource(clazz, parentResourceModel);
    } else {
        throw new ResourceConfigException("Class '" + resourceClass.getName() + "' must be annotated with a valid @RestLi... annotation");
    }
    if (parentResourceModel != null) {
        parentResourceModel.addSubResource(model.getName(), model);
    }
    if (!model.isActions()) {
        checkRestLiDataAnnotations(resourceClass, (RecordDataSchema) getDataSchema(model.getValueClass(), null));
    }
    addAlternativeKeys(model, resourceClass);
    DataMap annotationsMap = ResourceModelAnnotation.getAnnotationsMap(resourceClass.getAnnotations());
    addDeprecatedAnnotation(annotationsMap, resourceClass);
    model.setCustomAnnotation(annotationsMap);
    return model;
}
Also used : KeyValueResource(com.linkedin.restli.server.resources.KeyValueResource) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) RestLiActions(com.linkedin.restli.server.annotations.RestLiActions) SingleObjectResource(com.linkedin.restli.server.resources.SingleObjectResource) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) DataMap(com.linkedin.data.DataMap)

Example 9 with RestLiInternalException

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

the class RestLiClasspathScanner method checkForMatchingClass.

public void checkForMatchingClass(final String name) {
    if (name.endsWith(CLASS_SUFFIX)) {
        for (String packagePath : _packagePaths) {
            if (name.contains(packagePath)) {
                int start = name.lastIndexOf(packagePath);
                int end = name.lastIndexOf(CLASS_SUFFIX);
                String clazzPath = name.substring(start, end);
                String clazzName = pathToName(clazzPath);
                try {
                    Class<?> clazz = classForName(clazzName);
                    for (Annotation a : clazz.getAnnotations()) {
                        if (_annotations.contains(a.annotationType())) {
                            _matchedClasses.add(clazz);
                            break;
                        }
                    }
                } catch (ClassNotFoundException e) {
                    throw new RestLiInternalException("Failed to load class while scanning packages", e);
                }
            }
        }
    }
}
Also used : RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) Annotation(java.lang.annotation.Annotation)

Example 10 with RestLiInternalException

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

the class ResourceModelEncoder method buildDataSchemaType.

private static String buildDataSchemaType(final Class<?> type, final DataSchema dataSchema) {
    final DataSchema schemaToEncode;
    if (dataSchema instanceof TyperefDataSchema) {
        return ((TyperefDataSchema) dataSchema).getFullName();
    } else if (dataSchema instanceof PrimitiveDataSchema || dataSchema instanceof NamedDataSchema) {
        return dataSchema.getUnionMemberKey();
    } else if (dataSchema instanceof UnionDataSchema && HasTyperefInfo.class.isAssignableFrom(type)) {
        final TyperefInfo unionRef = DataTemplateUtil.getTyperefInfo(type.asSubclass(DataTemplate.class));
        schemaToEncode = unionRef.getSchema();
    } else {
        schemaToEncode = dataSchema;
    }
    JsonBuilder builder = null;
    try {
        builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
        final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
        encoder.encode(schemaToEncode);
        return builder.result();
    } catch (IOException e) {
        throw new RestLiInternalException("could not encode schema for '" + type.getName() + "'", e);
    } finally {
        if (builder != null) {
            builder.closeQuietly();
        }
    }
}
Also used : UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JsonBuilder(com.linkedin.data.schema.JsonBuilder) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo) DataTemplate(com.linkedin.data.template.DataTemplate) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) IOException(java.io.IOException) TyperefInfo(com.linkedin.data.template.TyperefInfo) HasTyperefInfo(com.linkedin.data.template.HasTyperefInfo)

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