Search in sources :

Example 11 with ActionSchema

use of com.linkedin.restli.restspec.ActionSchema in project rest.li by linkedin.

the class RestLiResourceRelationship method findDataModels.

private void findDataModels() {
    final ResourceSchemaVisitior visitor = new BaseResourceSchemaVisitor() {

        @Override
        public void visitResourceSchema(VisitContext visitContext, ResourceSchema resourceSchema) {
            final String schema = resourceSchema.getSchema();
            // ActionSet resources do not have a schema
            if (schema != null) {
                final NamedDataSchema schemaSchema = extractSchema(schema);
                if (schemaSchema != null) {
                    connectSchemaToResource(visitContext, schemaSchema);
                }
            }
        }

        @Override
        public void visitCollectionResource(VisitContext visitContext, CollectionSchema collectionSchema) {
            final IdentifierSchema id = collectionSchema.getIdentifier();
            final NamedDataSchema typeSchema = extractSchema(id.getType());
            if (typeSchema != null) {
                connectSchemaToResource(visitContext, typeSchema);
            }
            final String params = id.getParams();
            if (params != null) {
                final NamedDataSchema paramsSchema = extractSchema(params);
                if (paramsSchema != null) {
                    connectSchemaToResource(visitContext, paramsSchema);
                }
            }
        }

        @Override
        public void visitAssociationResource(VisitContext visitContext, AssociationSchema associationSchema) {
            for (AssocKeySchema key : associationSchema.getAssocKeys()) {
                final NamedDataSchema keyTypeSchema = extractSchema(key.getType());
                if (keyTypeSchema != null) {
                    connectSchemaToResource(visitContext, keyTypeSchema);
                }
            }
        }

        @Override
        public void visitParameter(VisitContext visitContext, RecordTemplate parentResource, Object parentMethodSchema, ParameterSchema parameterSchema) {
            String parameterTypeString = parameterSchema.getType();
            if (// the parameter type field contains a inline schema, so we traverse into it
            isInlineSchema(parameterTypeString)) {
                visitInlineSchema(visitContext, parameterTypeString);
            } else {
                final NamedDataSchema schema;
                // grab the schema name from it
                if (parameterSchema.hasItems()) {
                    schema = extractSchema(parameterSchema.getItems());
                } else // the only remaining possibility is that the type field contains the name of a data schema
                {
                    schema = extractSchema(parameterTypeString);
                }
                if (schema != null) {
                    connectSchemaToResource(visitContext, schema);
                }
            }
        }

        @Override
        public void visitFinder(VisitContext visitContext, RecordTemplate parentResource, FinderSchema finderSchema) {
            final MetadataSchema metadata = finderSchema.getMetadata();
            if (metadata != null) {
                final NamedDataSchema metadataTypeSchema = extractSchema(metadata.getType());
                if (metadataTypeSchema != null) {
                    connectSchemaToResource(visitContext, metadataTypeSchema);
                }
            }
        }

        @Override
        public void visitAction(VisitContext visitContext, RecordTemplate parentResource, ResourceLevel resourceLevel, ActionSchema actionSchema) {
            final String returns = actionSchema.getReturns();
            if (returns != null) {
                if (// the parameter type field contains a inline schema, so we traverse into it
                isInlineSchema(returns)) {
                    visitInlineSchema(visitContext, returns);
                } else // otherwise the type field contains the name of a data schema
                {
                    final NamedDataSchema returnsSchema = extractSchema(returns);
                    if (returnsSchema != null) {
                        connectSchemaToResource(visitContext, returnsSchema);
                    }
                }
            }
            final StringArray throwsArray = actionSchema.getThrows();
            if (throwsArray != null) {
                for (String errorName : throwsArray) {
                    final NamedDataSchema errorSchema = extractSchema(errorName);
                    if (errorSchema != null) {
                        connectSchemaToResource(visitContext, errorSchema);
                    }
                }
            }
        }

        private boolean isInlineSchema(String schemaString) {
            return schemaString.startsWith("{");
        }

        private void visitInlineSchema(VisitContext visitContext, String schemaString) {
            DataSchema schema = DataTemplateUtil.parseSchema(schemaString, _schemaResolver);
            if (schema instanceof ArrayDataSchema) {
                DataSchema itemSchema = ((ArrayDataSchema) schema).getItems();
                if (itemSchema instanceof NamedDataSchema) {
                    connectSchemaToResource(visitContext, (NamedDataSchema) itemSchema);
                }
            }
            if (schema instanceof MapDataSchema) {
                DataSchema valueSchema = ((MapDataSchema) schema).getValues();
                if (valueSchema instanceof NamedDataSchema) {
                    connectSchemaToResource(visitContext, (NamedDataSchema) valueSchema);
                }
            }
        }

        private void connectSchemaToResource(VisitContext visitContext, final NamedDataSchema schema) {
            final Node<NamedDataSchema> schemaNode = _relationships.get(schema);
            _dataModels.put(schema.getFullName(), schema);
            final DataSchemaTraverse traveler = new DataSchemaTraverse();
            traveler.traverse(schema, new DataSchemaTraverse.Callback() {

                @Override
                public void callback(List<String> path, DataSchema nestedSchema) {
                    if (nestedSchema instanceof RecordDataSchema && nestedSchema != schema) {
                        final RecordDataSchema nestedRecordSchema = (RecordDataSchema) nestedSchema;
                        _dataModels.put(nestedRecordSchema.getFullName(), nestedRecordSchema);
                        final Node<RecordDataSchema> node = _relationships.get(nestedRecordSchema);
                        schemaNode.addAdjacentNode(node);
                    }
                }
            });
            final Node<ResourceSchema> resourceNode = _relationships.get(visitContext.getParentSchema());
            resourceNode.addAdjacentNode(schemaNode);
            schemaNode.addAdjacentNode(resourceNode);
        }
    };
    ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ResourceLevel(com.linkedin.restli.server.ResourceLevel) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) StringArray(com.linkedin.data.template.StringArray) IdentifierSchema(com.linkedin.restli.restspec.IdentifierSchema) RecordTemplate(com.linkedin.data.template.RecordTemplate) AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) MetadataSchema(com.linkedin.restli.restspec.MetadataSchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchemaTraverse(com.linkedin.data.schema.DataSchemaTraverse)

Example 12 with ActionSchema

use of com.linkedin.restli.restspec.ActionSchema in project rest.li by linkedin.

the class SnapshotGenerator method findModelsAssocation.

private void findModelsAssocation(ResourceSchema resourceSchema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
    AssociationSchema association = resourceSchema.getAssociation();
    if (association != null) {
        for (AssocKeySchema assocKeySchema : association.getAssocKeys()) {
            String type = assocKeySchema.getType();
            recordType(type, foundTypes, typeOrder);
        }
        if (association.hasFinders()) {
            for (FinderSchema restMethodSchema : association.getFinders()) {
                findModelsFinder(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasMethods()) {
            for (RestMethodSchema restMethodSchema : association.getMethods()) {
                findModelsMethod(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasActions()) {
            for (ActionSchema actionSchema : association.getActions()) {
                findModelsAction(actionSchema, foundTypes, typeOrder);
            }
        }
        if (association.hasEntity()) {
            EntitySchema entitySchema = association.getEntity();
            findModelsEntity(entitySchema, foundTypes, typeOrder);
        }
    }
}
Also used : AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) EntitySchema(com.linkedin.restli.restspec.EntitySchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema)

Example 13 with ActionSchema

use of com.linkedin.restli.restspec.ActionSchema in project rest.li by linkedin.

the class SnapshotGenerator method findModelsSimple.

private void findModelsSimple(ResourceSchema resourceSchema, Map<String, NamedDataSchema> foundTypes, List<NamedDataSchema> typeOrder) {
    SimpleSchema simple = resourceSchema.getSimple();
    if (simple != null) {
        if (simple.hasMethods()) {
            for (RestMethodSchema restMethodSchema : simple.getMethods()) {
                findModelsMethod(restMethodSchema, foundTypes, typeOrder);
            }
        }
        if (simple.hasActions()) {
            for (ActionSchema actionSchema : simple.getActions()) {
                findModelsAction(actionSchema, foundTypes, typeOrder);
            }
        }
        if (simple.hasEntity()) {
            EntitySchema entity = simple.getEntity();
            findModelsEntity(entity, foundTypes, typeOrder);
        }
    }
}
Also used : SimpleSchema(com.linkedin.restli.restspec.SimpleSchema) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) EntitySchema(com.linkedin.restli.restspec.EntitySchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema)

Example 14 with ActionSchema

use of com.linkedin.restli.restspec.ActionSchema in project rest.li by linkedin.

the class ExampleRequestResponseGenerator method action.

public ExampleRequestResponse action(String name, ResourceLevel resourceLevel) {
    ActionSchema actionSchema;
    switch(resourceLevel) {
        case COLLECTION:
            actionSchema = _resourceSchema.getAction(name);
            break;
        case ENTITY:
            actionSchema = _resourceSchema.getEntityAction(name);
            break;
        default:
            throw new IllegalArgumentException("Unsupported resourceLevel: " + resourceLevel);
    }
    if (actionSchema == null) {
        throw new IllegalArgumentException("No such action for resource: " + name);
    }
    DynamicRecordMetadata returnsMetadata = _resourceSpec.getActionResponseMetadata(name);
    return buildRequestResponse(buildActionRequest(actionSchema, resourceLevel), buildActionResult(actionSchema), buildResourceMethodDescriptorForAction(name, returnsMetadata, resourceLevel));
}
Also used : DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) ActionSchema(com.linkedin.restli.restspec.ActionSchema)

Aggregations

ActionSchema (com.linkedin.restli.restspec.ActionSchema)14 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)5 ActionSchemaArray (com.linkedin.restli.restspec.ActionSchemaArray)5 AssociationSchema (com.linkedin.restli.restspec.AssociationSchema)4 CollectionSchema (com.linkedin.restli.restspec.CollectionSchema)4 EntitySchema (com.linkedin.restli.restspec.EntitySchema)4 FinderSchema (com.linkedin.restli.restspec.FinderSchema)4 RestMethodSchema (com.linkedin.restli.restspec.RestMethodSchema)4 ParameterSchema (com.linkedin.restli.restspec.ParameterSchema)3 ParameterSchemaArray (com.linkedin.restli.restspec.ParameterSchemaArray)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 DataMap (com.linkedin.data.DataMap)2 RecordTemplate (com.linkedin.data.template.RecordTemplate)2 AssocKeySchema (com.linkedin.restli.restspec.AssocKeySchema)2 IdentifierSchema (com.linkedin.restli.restspec.IdentifierSchema)2 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)2 ResourceLevel (com.linkedin.restli.server.ResourceLevel)2 JClass (com.sun.codemodel.JClass)2 JInvocation (com.sun.codemodel.JInvocation)2