Search in sources :

Example 6 with RestMethodSchema

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

the class TestExamplesGenerator method findRestMethod.

private static RestMethodSchema findRestMethod(ResourceSchema resourceSchema, ResourceMethod method) {
    RestMethodSchemaArray methods = null;
    RestMethodSchema methodResult = null;
    final CollectionSchema collectionSchema = resourceSchema.getCollection();
    if (collectionSchema != null) {
        methods = collectionSchema.getMethods();
    }
    final AssociationSchema associationSchema = resourceSchema.getAssociation();
    if (associationSchema != null) {
        methods = associationSchema.getMethods();
    }
    final SimpleSchema simpleSchema = resourceSchema.getSimple();
    if (simpleSchema != null) {
        methods = simpleSchema.getMethods();
    }
    if (methods != null) {
        for (RestMethodSchema restMethodSchema : methods) {
            if (restMethodSchema.getMethod().equalsIgnoreCase(method.name())) {
                methodResult = restMethodSchema;
                break;
            }
        }
    }
    return methodResult;
}
Also used : AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) RestMethodSchemaArray(com.linkedin.restli.restspec.RestMethodSchemaArray) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) SimpleSchema(com.linkedin.restli.restspec.SimpleSchema) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema)

Example 7 with RestMethodSchema

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

the class JavaRequestBuilderGenerator method generateBasicMethods.

@SuppressWarnings("deprecation")
private void generateBasicMethods(JDefinedClass facadeClass, JExpression baseUriExpr, JClass keyClass, JClass valueClass, Set<ResourceMethod> supportedMethods, RestMethodSchemaArray restMethods, JVar resourceSpecField, String resourceName, List<String> pathKeys, Map<String, JClass> pathKeyTypes, Map<String, JClass> assocKeyTypes, Map<String, List<String>> pathToAssocKeys, JExpression requestOptionsExpr, DataMap annotations) throws JClassAlreadyExistsException {
    final Map<ResourceMethod, RestMethodSchema> schemaMap = new HashMap<ResourceMethod, RestMethodSchema>();
    if (restMethods != null) {
        for (RestMethodSchema restMethod : restMethods) {
            schemaMap.put(ResourceMethod.fromString(restMethod.getMethod()), restMethod);
        }
    }
    final Map<ResourceMethod, Class<?>> crudBuilderClasses = new HashMap<ResourceMethod, Class<?>>();
    if (_version == RestliVersion.RESTLI_2_0_0) {
        crudBuilderClasses.put(ResourceMethod.CREATE, CreateIdRequestBuilderBase.class);
    } else {
        // we use fully qualified class name to avoid importing the deprecated class
        // so far in Java there is no way to suppress deprecation warning for import
        crudBuilderClasses.put(ResourceMethod.CREATE, com.linkedin.restli.client.base.CreateRequestBuilderBase.class);
    }
    crudBuilderClasses.put(ResourceMethod.GET, GetRequestBuilderBase.class);
    crudBuilderClasses.put(ResourceMethod.UPDATE, UpdateRequestBuilderBase.class);
    crudBuilderClasses.put(ResourceMethod.PARTIAL_UPDATE, PartialUpdateRequestBuilderBase.class);
    crudBuilderClasses.put(ResourceMethod.DELETE, DeleteRequestBuilderBase.class);
    if (_version == RestliVersion.RESTLI_2_0_0) {
        crudBuilderClasses.put(ResourceMethod.BATCH_CREATE, BatchCreateIdRequestBuilderBase.class);
    } else {
        // we use fully qualified class name to avoid importing the deprecated class
        // so far in Java there is no way to suppress deprecation warning for import
        crudBuilderClasses.put(ResourceMethod.BATCH_CREATE, com.linkedin.restli.client.base.BatchCreateRequestBuilderBase.class);
    }
    crudBuilderClasses.put(ResourceMethod.BATCH_UPDATE, BatchUpdateRequestBuilderBase.class);
    crudBuilderClasses.put(ResourceMethod.BATCH_PARTIAL_UPDATE, BatchPartialUpdateRequestBuilderBase.class);
    crudBuilderClasses.put(ResourceMethod.BATCH_DELETE, BatchDeleteRequestBuilderBase.class);
    crudBuilderClasses.put(ResourceMethod.GET_ALL, GetAllRequestBuilderBase.class);
    if (_version == RestliVersion.RESTLI_2_0_0) {
        crudBuilderClasses.put(ResourceMethod.BATCH_GET, BatchGetEntityRequestBuilderBase.class);
    } else {
        // we use fully qualified class name to avoid importing the deprecated class
        // so far in Java there is no way to suppress deprecation warning for import
        crudBuilderClasses.put(ResourceMethod.BATCH_GET, com.linkedin.restli.client.base.BatchGetRequestBuilderBase.class);
    }
    for (Map.Entry<ResourceMethod, Class<?>> entry : crudBuilderClasses.entrySet()) {
        final ResourceMethod method = entry.getKey();
        final Class<?> refModel = entry.getValue();
        if (supportedMethods.contains(method)) {
            final String methodName = RestLiToolsUtils.normalizeUnderscores(method.toString());
            final RestMethodSchema schema = schemaMap.get(method);
            generateDerivedBuilderAndJavaDoc(facadeClass, baseUriExpr, keyClass, valueClass, resourceSpecField, resourceName, pathKeys, pathKeyTypes, assocKeyTypes, pathToAssocKeys, requestOptionsExpr, annotations, method, refModel, methodName, schema);
            if ((method == ResourceMethod.CREATE || method == ResourceMethod.BATCH_CREATE) && schema != null && schema.getAnnotations() != null && schema.getAnnotations().containsKey("returnEntity")) {
                Class<?> newBuildClass = methodName.equals("create") ? CreateIdEntityRequestBuilderBase.class : BatchCreateIdEntityRequestBuilderBase.class;
                String requestName = methodName.equals("create") ? "createAndGet" : "batchCreateAndGet";
                generateDerivedBuilderAndJavaDoc(facadeClass, baseUriExpr, keyClass, valueClass, resourceSpecField, resourceName, pathKeys, pathKeyTypes, assocKeyTypes, pathToAssocKeys, requestOptionsExpr, annotations, method, newBuildClass, requestName, schema);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) JDefinedClass(com.sun.codemodel.JDefinedClass) JClass(com.sun.codemodel.JClass) Map(java.util.Map) DataMap(com.linkedin.data.DataMap) HashMap(java.util.HashMap) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 8 with RestMethodSchema

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

the class RequestBuilderSpecGenerator method generateBasicMethods.

@SuppressWarnings("deprecation")
private List<RootBuilderMethodSpec> generateBasicMethods(RootBuilderSpec rootBuilderSpec, String keyClass, String valueClass, Set<ResourceMethod> supportedMethods, RestMethodSchemaArray restMethods, String resourceName, List<String> pathKeys, Map<String, String> pathKeyTypes) {
    final Map<ResourceMethod, RestMethodSchema> schemaMap = new HashMap<ResourceMethod, RestMethodSchema>();
    if (restMethods != null) {
        for (RestMethodSchema restMethod : restMethods) {
            schemaMap.put(ResourceMethod.fromString(restMethod.getMethod()), restMethod);
        }
    }
    List<RootBuilderMethodSpec> methodSpecList = new ArrayList<RootBuilderMethodSpec>();
    for (Map.Entry<ResourceMethod, String> entry : _builderBaseMap.entrySet()) {
        ResourceMethod method = entry.getKey();
        if (supportedMethods.contains(method)) {
            String methodName = RestLiToolsUtils.normalizeUnderscores(method.toString());
            final RestMethodSchema schema = schemaMap.get(method);
            RestMethodBuilderSpec requestBuilder = generateRestMethodRequestBuilder(rootBuilderSpec.getResource(), entry.getValue(), keyClass, valueClass, resourceName + RestLiToolsUtils.nameCapsCase(methodName) + getMethodBuilderSuffix(), rootBuilderSpec.getNamespace(), schema);
            generatePathKeyBindingMethods(pathKeys, requestBuilder, pathKeyTypes);
            if (schema != null && schema.hasParameters()) {
                this.generateQueryParamBindingMethods(schema.getParameters(), requestBuilder);
            }
            RootBuilderMethodSpec methodSpec = new RootBuilderMethodSpec(RestLiToolsUtils.nameCamelCase(methodName), schema.getDoc(), requestBuilder, rootBuilderSpec);
            requestBuilder.setRootBuilderMethod(methodSpec);
            methodSpecList.add(methodSpec);
        }
    }
    return methodSpecList;
}
Also used : HashMap(java.util.HashMap) RootBuilderMethodSpec(com.linkedin.restli.tools.clientgen.builderspec.RootBuilderMethodSpec) ArrayList(java.util.ArrayList) RestMethodBuilderSpec(com.linkedin.restli.tools.clientgen.builderspec.RestMethodBuilderSpec) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) Map(java.util.Map) HashMap(java.util.HashMap) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 9 with RestMethodSchema

use of com.linkedin.restli.restspec.RestMethodSchema 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)

Example 10 with RestMethodSchema

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

the class ExampleRequestResponseGenerator method addParams.

private void addParams(RestfulRequestBuilder<?, ?, ?> builder, ResourceMethod method) {
    RestMethodSchema methodSchema = _resourceSchema.getMethod(method.toString().toLowerCase());
    ParameterSchemaArray parameters = methodSchema.getParameters();
    addParams(builder, parameters);
}
Also used : RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) ParameterSchemaArray(com.linkedin.restli.restspec.ParameterSchemaArray)

Aggregations

RestMethodSchema (com.linkedin.restli.restspec.RestMethodSchema)10 ActionSchema (com.linkedin.restli.restspec.ActionSchema)4 DataMap (com.linkedin.data.DataMap)3 ResourceMethod (com.linkedin.restli.common.ResourceMethod)3 EntitySchema (com.linkedin.restli.restspec.EntitySchema)3 FinderSchema (com.linkedin.restli.restspec.FinderSchema)3 AssociationSchema (com.linkedin.restli.restspec.AssociationSchema)2 CollectionSchema (com.linkedin.restli.restspec.CollectionSchema)2 ParameterSchemaArray (com.linkedin.restli.restspec.ParameterSchemaArray)2 RestMethodSchemaArray (com.linkedin.restli.restspec.RestMethodSchemaArray)2 SimpleSchema (com.linkedin.restli.restspec.SimpleSchema)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RecordTemplate (com.linkedin.data.template.RecordTemplate)1 ExampleRequestResponse (com.linkedin.restli.docgen.examplegen.ExampleRequestResponse)1 ExampleRequestResponseGenerator (com.linkedin.restli.docgen.examplegen.ExampleRequestResponseGenerator)1 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)1 AssocKeySchema (com.linkedin.restli.restspec.AssocKeySchema)1 CustomAnnotationContentSchemaMap (com.linkedin.restli.restspec.CustomAnnotationContentSchemaMap)1