Search in sources :

Example 51 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class ResourceModelEncoder method createRestMethods.

private RestMethodSchemaArray createRestMethods(final ResourceModel resourceModel) {
    RestMethodSchemaArray restMethods = new RestMethodSchemaArray();
    ResourceMethod[] crudMethods = { ResourceMethod.CREATE, ResourceMethod.GET, ResourceMethod.UPDATE, ResourceMethod.PARTIAL_UPDATE, ResourceMethod.DELETE, ResourceMethod.BATCH_CREATE, ResourceMethod.BATCH_GET, ResourceMethod.BATCH_UPDATE, ResourceMethod.BATCH_PARTIAL_UPDATE, ResourceMethod.BATCH_DELETE, ResourceMethod.GET_ALL };
    for (ResourceMethod method : crudMethods) {
        ResourceMethodDescriptor descriptor = resourceModel.findMethod(method);
        if (descriptor == null) {
            continue;
        }
        RestMethodSchema restMethod = new RestMethodSchema();
        restMethod.setMethod(method.toString());
        String doc = _docsProvider.getMethodDoc(descriptor.getMethod());
        if (doc != null) {
            restMethod.setDoc(doc);
        }
        ParameterSchemaArray parameters = createParameters(descriptor);
        if (parameters.size() > 0) {
            restMethod.setParameters(parameters);
        }
        final DataMap customAnnotation = descriptor.getCustomAnnotationData();
        String deprecatedDoc = _docsProvider.getMethodDeprecatedTag(descriptor.getMethod());
        if (deprecatedDoc != null) {
            customAnnotation.put(DEPRECATED_ANNOTATION_NAME, deprecateDocToAnnotationMap(deprecatedDoc));
        }
        if (!customAnnotation.isEmpty()) {
            restMethod.setAnnotations(new CustomAnnotationContentSchemaMap(customAnnotation));
        }
        if (method == ResourceMethod.GET_ALL) {
            if (descriptor.getCollectionCustomMetadataType() != null) {
                restMethod.setMetadata(createMetadataSchema(descriptor));
            }
            if (descriptor.isPagingSupported()) {
                restMethod.setPagingSupported(true);
            }
        }
        MaxBatchSizeSchema maxBatchSize = descriptor.getMaxBatchSize();
        if (maxBatchSize != null) {
            restMethod.setMaxBatchSize(maxBatchSize);
        }
        appendServiceErrors(restMethod, descriptor.getServiceErrors());
        appendSuccessStatuses(restMethod, descriptor.getSuccessStatuses());
        restMethods.add(restMethod);
    }
    return restMethods;
}
Also used : RestMethodSchemaArray(com.linkedin.restli.restspec.RestMethodSchemaArray) MaxBatchSizeSchema(com.linkedin.restli.restspec.MaxBatchSizeSchema) RestMethodSchema(com.linkedin.restli.restspec.RestMethodSchema) ParameterSchemaArray(com.linkedin.restli.restspec.ParameterSchemaArray) CustomAnnotationContentSchemaMap(com.linkedin.restli.restspec.CustomAnnotationContentSchemaMap) ResourceMethod(com.linkedin.restli.common.ResourceMethod) DataMap(com.linkedin.data.DataMap)

Example 52 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class ResourceModelEncoder method buildSupportsArray.

private void buildSupportsArray(final ResourceModel resourceModel, final StringArray supportsArray) {
    List<String> supportsStrings = new ArrayList<>();
    for (ResourceMethodDescriptor resourceMethodDescriptor : resourceModel.getResourceMethodDescriptors()) {
        ResourceMethod type = resourceMethodDescriptor.getType();
        if (!type.equals(ResourceMethod.FINDER) && !type.equals(ResourceMethod.BATCH_FINDER) && !type.equals(ResourceMethod.ACTION)) {
            supportsStrings.add(type.toString());
        }
    }
    Collections.sort(supportsStrings);
    supportsArray.addAll(supportsStrings);
}
Also used : ArrayList(java.util.ArrayList) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 53 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class RestLiRouter method setupResourceMethodLookup.

// We use a table match to ensure that we have no subtle ordering dependencies in conditional logic
// 
// Currently only POST requests set RMETHOD header (HEADER_RESTLI_REQUEST_METHOD), however we include
// a table entry for GET methods as well to make sure the routing doesn't fail if the client sets the header
// when it's not necessary, as long as it doesn't conflict with the rest of the parameters.
private static Map<ResourceMethodMatchKey, ResourceMethod> setupResourceMethodLookup() {
    HashMap<ResourceMethodMatchKey, ResourceMethod> result = new HashMap<>();
    // METHOD    RMETHOD                    ACTION   QUERY BATCHFINDER  BATCH   ENTITY
    Object[] config = { new ResourceMethodMatchKey("GET", "", false, false, false, false, true), ResourceMethod.GET, new ResourceMethodMatchKey("GET", "", false, true, false, false, false), ResourceMethod.FINDER, new ResourceMethodMatchKey("PUT", "", false, false, false, false, true), ResourceMethod.UPDATE, new ResourceMethodMatchKey("POST", "", false, false, false, false, true), ResourceMethod.PARTIAL_UPDATE, new ResourceMethodMatchKey("DELETE", "", false, false, false, false, true), ResourceMethod.DELETE, new ResourceMethodMatchKey("POST", "", true, false, false, false, true), ResourceMethod.ACTION, new ResourceMethodMatchKey("POST", "", true, false, false, false, false), ResourceMethod.ACTION, new ResourceMethodMatchKey("POST", "", false, false, false, false, false), ResourceMethod.CREATE, new ResourceMethodMatchKey("GET", "", false, false, false, false, false), ResourceMethod.GET_ALL, new ResourceMethodMatchKey("GET", "GET", false, false, false, false, true), ResourceMethod.GET, new ResourceMethodMatchKey("GET", "FINDER", false, true, false, false, false), ResourceMethod.FINDER, new ResourceMethodMatchKey("PUT", "UPDATE", false, false, false, false, true), ResourceMethod.UPDATE, new ResourceMethodMatchKey("POST", "PARTIAL_UPDATE", false, false, false, false, true), ResourceMethod.PARTIAL_UPDATE, new ResourceMethodMatchKey("DELETE", "DELETE", false, false, false, false, true), ResourceMethod.DELETE, new ResourceMethodMatchKey("POST", "ACTION", true, false, false, false, true), ResourceMethod.ACTION, new ResourceMethodMatchKey("POST", "ACTION", true, false, false, false, false), ResourceMethod.ACTION, new ResourceMethodMatchKey("POST", "CREATE", false, false, false, false, false), ResourceMethod.CREATE, new ResourceMethodMatchKey("GET", "GET_ALL", false, false, false, false, false), ResourceMethod.GET_ALL, new ResourceMethodMatchKey("GET", "", false, false, false, true, false), ResourceMethod.BATCH_GET, new ResourceMethodMatchKey("GET", "", false, false, true, false, false), ResourceMethod.BATCH_FINDER, new ResourceMethodMatchKey("DELETE", "", false, false, false, true, false), ResourceMethod.BATCH_DELETE, new ResourceMethodMatchKey("PUT", "", false, false, false, true, false), ResourceMethod.BATCH_UPDATE, new ResourceMethodMatchKey("POST", "", false, false, false, true, false), ResourceMethod.BATCH_PARTIAL_UPDATE, new ResourceMethodMatchKey("GET", "BATCH_GET", false, false, false, true, false), ResourceMethod.BATCH_GET, new ResourceMethodMatchKey("GET", "BATCH_FINDER", false, false, true, false, false), ResourceMethod.BATCH_FINDER, new ResourceMethodMatchKey("DELETE", "BATCH_DELETE", false, false, false, true, false), ResourceMethod.BATCH_DELETE, new ResourceMethodMatchKey("PUT", "BATCH_UPDATE", false, false, false, true, false), ResourceMethod.BATCH_UPDATE, new ResourceMethodMatchKey("POST", "BATCH_PARTIAL_UPDATE", false, false, false, true, false), ResourceMethod.BATCH_PARTIAL_UPDATE, // batch create signature collides with non-batch create. requires RMETHOD header to distinguish
    new ResourceMethodMatchKey("POST", "BATCH_CREATE", false, false, false, false, false), ResourceMethod.BATCH_CREATE };
    for (int ii = 0; ii < config.length; ii += 2) {
        ResourceMethodMatchKey key = (ResourceMethodMatchKey) config[ii];
        ResourceMethod method = (ResourceMethod) config[ii + 1];
        ResourceMethod prevValue = result.put(key, method);
        if (prevValue != null) {
            throw new RestLiInternalException("Routing Configuration conflict: " + prevValue.toString() + " conflicts with " + method.toString());
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 54 with ResourceMethod

use of com.linkedin.restli.common.ResourceMethod in project rest.li by linkedin.

the class RestLiRouter method findMethodDescriptor.

private ResourceMethodDescriptor findMethodDescriptor(final ResourceModel resource, final ResourceLevel resourceLevel, final ServerResourceContext context) {
    ResourceMethod type = mapResourceMethod(context, resourceLevel);
    String methodName = context.getMethodName(type);
    ResourceMethodDescriptor methodDescriptor = resource.matchMethod(type, methodName, resourceLevel);
    if (methodDescriptor != null) {
        context.getRawRequestContext().putLocalAttr(R2Constants.OPERATION, OperationNameGenerator.generate(methodDescriptor.getMethodType(), methodDescriptor.getMethodName()));
        return methodDescriptor;
    }
    String httpMethod = context.getRequestMethod();
    if (methodName != null) {
        throw new RoutingException(String.format("%s operation " + "named %s " + "not supported on resource '%s' " + "URI: '%s'", httpMethod, methodName, resource.getResourceClass().getName(), context.getRequestURI().toString()), HttpStatus.S_400_BAD_REQUEST.getCode());
    }
    throw new RoutingException(String.format("%s operation not supported " + "for URI: '%s' " + "with " + RestConstants.HEADER_RESTLI_REQUEST_METHOD + ": '%s'", httpMethod, context.getRequestURI().toString(), context.getRestLiRequestMethod()), HttpStatus.S_400_BAD_REQUEST.getCode());
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Aggregations

ResourceMethod (com.linkedin.restli.common.ResourceMethod)54 Test (org.testng.annotations.Test)19 HashMap (java.util.HashMap)18 ResourceSpecImpl (com.linkedin.restli.common.ResourceSpecImpl)13 DataMap (com.linkedin.data.DataMap)12 TestRecord (com.linkedin.restli.client.test.TestRecord)12 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)10 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)6 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)6 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)5 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)4 Foo (com.linkedin.pegasus.generator.examples.Foo)4 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)4 ResourceSchemaArray (com.linkedin.restli.restspec.ResourceSchemaArray)4 JClass (com.sun.codemodel.JClass)4 JDefinedClass (com.sun.codemodel.JDefinedClass)4 ByteString (com.linkedin.data.ByteString)3