Search in sources :

Example 11 with RestLiInternalException

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

the class ResourceModelAnnotation method getAnnotationData.

private static AnnotationEntry getAnnotationData(Annotation a, boolean isTopLevel) {
    final DataMap data = new DataMap();
    final Class<? extends Annotation> annotationClass = a.annotationType();
    AnnotationTrait trait = _traits.get(annotationClass);
    if (trait == null) {
        trait = getTraits(annotationClass, isTopLevel);
        _traits.put(annotationClass, trait);
    }
    for (Method m : annotationClass.getDeclaredMethods()) {
        final MetaTrait methodTrait = trait.memberTraits.get(m);
        if (methodTrait == null || (isTopLevel && !methodTrait.isRestSpecAnnotated)) {
            continue;
        }
        try {
            final Object memberValue = m.invoke(a);
            if (methodTrait.skipDefault) {
                final Object annotationDefault = m.getDefaultValue();
                if (annotationDefault != null && annotationDefault.equals(memberValue)) {
                    continue;
                }
            }
            final Object valueData = annotationMemberToData(memberValue);
            if (valueData != null) {
                data.put(methodTrait.isRestSpecAnnotated ? methodTrait.name : m.getName(), valueData);
            }
        } catch (IllegalAccessException e) {
            throw new RestLiInternalException(e);
        } catch (IllegalArgumentException e) {
            throw new RestLiInternalException(e);
        } catch (InvocationTargetException e) {
            throw new RestLiInternalException(e);
        }
    }
    if (data.isEmpty()) {
        if (trait.masterTrait.isRestSpecAnnotated) {
            return new AnnotationEntry(trait.masterTrait.name, new DataMap());
        } else {
            return null;
        }
    } else {
        return new AnnotationEntry(trait.masterTrait.name, data);
    }
}
Also used : RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) DataMap(com.linkedin.data.DataMap)

Example 12 with RestLiInternalException

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

the class ResourceModelEncoder method buildDataSchemaType.

/*package*/
static String buildDataSchemaType(DataSchema schema) {
    if (schema instanceof PrimitiveDataSchema || schema instanceof NamedDataSchema) {
        return schema.getUnionMemberKey();
    }
    JsonBuilder builder = null;
    try {
        builder = new JsonBuilder(JsonBuilder.Pretty.SPACES);
        final SchemaToJsonEncoder encoder = new SchemaToJsonEncoder(builder, AbstractSchemaEncoder.TypeReferenceFormat.MINIMIZE);
        encoder.encode(schema);
        return builder.result();
    } catch (IOException e) {
        throw new RestLiInternalException("could not encode schema for '" + schema.toString() + "'", e);
    } finally {
        if (builder != null) {
            builder.closeQuietly();
        }
    }
}
Also used : NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) JsonBuilder(com.linkedin.data.schema.JsonBuilder) PrimitiveDataSchema(com.linkedin.data.schema.PrimitiveDataSchema) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) IOException(java.io.IOException)

Example 13 with RestLiInternalException

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

the class TestInjectResourceFactory method testMissingNamedDependency.

@Test
public void testMissingNamedDependency() {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(SomeResource1.class);
    BeanProvider ctx = EasyMock.createMock(BeanProvider.class);
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep1"))).andReturn(null).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);
    try {
        factory.setRootResources(pathRootResourceMap);
        fail("Expected unresolvable bean exception");
    } catch (RestLiInternalException e) {
        assertTrue(e.getMessage().startsWith("Expected to find"));
    }
    EasyMock.verify(ctx);
}
Also used : SomeDependency2(com.linkedin.restli.server.resources.fixtures.SomeDependency2) SomeDependency1(com.linkedin.restli.server.resources.fixtures.SomeDependency1) HashMap(java.util.HashMap) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Test(org.testng.annotations.Test)

Example 14 with RestLiInternalException

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

the class TestInjectResourceFactory method testAmbiguousBeanResolution.

@Test
public void testAmbiguousBeanResolution() throws Exception {
    Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(SomeResource1.class, SomeResource2.class, SomeResource4.class);
    // set up mock ApplicationContext
    BeanProvider ctx = EasyMock.createMock(BeanProvider.class);
    EasyMock.expect(ctx.getBean(EasyMock.eq("dep1"))).andReturn(new SomeDependency1()).anyTimes();
    Map<String, SomeDependency2> map2 = new HashMap<String, SomeDependency2>();
    map2.put("someBeanName", new SomeDependency2());
    EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency2.class))).andReturn(map2).anyTimes();
    Map<String, SomeDependency1> map1 = new HashMap<String, SomeDependency1>();
    map1.put("someDep1", new SomeDependency1());
    map1.put("anotherDep1", new SomeDependency1());
    EasyMock.expect(ctx.getBeansOfType(EasyMock.eq(SomeDependency1.class))).andReturn(map1).anyTimes();
    EasyMock.replay(ctx);
    InjectResourceFactory factory = new InjectResourceFactory(ctx);
    // #4 ambiguous dep
    try {
        factory.setRootResources(pathRootResourceMap);
        fail("Expected unresolvable bean exception");
    } catch (RestLiInternalException e) {
        assertTrue(e.getMessage().startsWith("Expected to find"));
    }
    EasyMock.verify(ctx);
    EasyMock.reset(ctx);
}
Also used : SomeDependency2(com.linkedin.restli.server.resources.fixtures.SomeDependency2) SomeDependency1(com.linkedin.restli.server.resources.fixtures.SomeDependency1) HashMap(java.util.HashMap) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Test(org.testng.annotations.Test)

Example 15 with RestLiInternalException

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

the class RestLiHTMLDocumentationRenderer method renderResource.

@Override
public void renderResource(String resourceName, OutputStream out) {
    final ResourceSchema resourceSchema = _resourceSchemas.getResource(resourceName);
    final List<ResourceSchema> parentResources = _resourceSchemas.getParentResources(resourceSchema);
    ExampleRequestResponseGenerator generator = new ExampleRequestResponseGenerator(parentResources, resourceSchema, _schemaResolver);
    if (resourceSchema == null) {
        throw new RoutingException(String.format("Resource \"%s\" does not exist", resourceName), HttpStatus.S_404_NOT_FOUND.getCode());
    }
    final Map<String, Object> pageModel = createPageModel();
    pageModel.put("resource", resourceSchema);
    pageModel.put("resourceName", resourceName);
    pageModel.put("resourceFullName", ResourceSchemaUtil.getFullName(resourceSchema));
    pageModel.put("resourceType", getResourceType(resourceSchema));
    pageModel.put("subResources", _resourceSchemas.getSubResources(resourceSchema));
    final List<ResourceMethodDocView> restMethods = new ArrayList<ResourceMethodDocView>();
    final List<ResourceMethodDocView> finders = new ArrayList<ResourceMethodDocView>();
    final List<ResourceMethodDocView> actions = new ArrayList<ResourceMethodDocView>();
    final MethodGatheringResourceSchemaVisitor visitor = new MethodGatheringResourceSchemaVisitor(resourceName);
    ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
    for (RecordTemplate methodSchema : visitor.getAllMethods()) {
        final ExampleRequestResponse capture;
        if (methodSchema instanceof RestMethodSchema) {
            RestMethodSchema restMethodSchema = (RestMethodSchema) methodSchema;
            capture = generator.method(ResourceMethod.valueOf(restMethodSchema.getMethod().toUpperCase()));
        } else if (methodSchema instanceof FinderSchema) {
            FinderSchema finderMethodSchema = (FinderSchema) methodSchema;
            capture = generator.finder(finderMethodSchema.getName());
        } else if (methodSchema instanceof ActionSchema) {
            ActionSchema actionMethodSchema = (ActionSchema) methodSchema;
            final ResourceLevel resourceLevel = (visitor.getCollectionActions().contains(methodSchema) ? ResourceLevel.COLLECTION : ResourceLevel.ENTITY);
            capture = generator.action(actionMethodSchema.getName(), resourceLevel);
        } else {
            capture = null;
        }
        String requestEntity = null;
        String responseEntity = null;
        if (capture != null) {
            try {
                DataMap entityMap;
                if (capture.getRequest().getEntity().length() > 0) {
                    entityMap = DataMapUtils.readMap(capture.getRequest());
                    requestEntity = new String(_codec.mapToBytes(entityMap));
                }
                if (capture.getResponse() != null && capture.getResponse().getEntity() != null && capture.getResponse().getEntity().length() > 0) {
                    entityMap = DataMapUtils.readMap(capture.getResponse());
                    responseEntity = new String(_codec.mapToBytes(entityMap));
                }
            } catch (IOException e) {
                throw new RestLiInternalException(e);
            }
        }
        final ResourceMethodDocView docView = new ResourceMethodDocView(methodSchema, capture, getDoc(methodSchema, resourceSchema.hasSimple()), requestEntity, responseEntity);
        if (methodSchema instanceof RestMethodSchema) {
            restMethods.add(docView);
        } else if (methodSchema instanceof FinderSchema) {
            finders.add(docView);
        } else if (methodSchema instanceof ActionSchema) {
            actions.add(docView);
        }
    }
    pageModel.put("restMethods", restMethods);
    pageModel.put("finders", finders);
    pageModel.put("actions", actions);
    addRelated(resourceSchema, pageModel);
    _templatingEngine.render("resource.vm", pageModel, out);
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ExampleRequestResponse(com.linkedin.restli.docgen.examplegen.ExampleRequestResponse) ResourceLevel(com.linkedin.restli.server.ResourceLevel) ArrayList(java.util.ArrayList) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) IOException(java.io.IOException) ActionSchema(com.linkedin.restli.restspec.ActionSchema) DataMap(com.linkedin.data.DataMap) ExampleRequestResponseGenerator(com.linkedin.restli.docgen.examplegen.ExampleRequestResponseGenerator) RecordTemplate(com.linkedin.data.template.RecordTemplate) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException)

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