Search in sources :

Example 1 with AlternativeKey

use of com.linkedin.restli.server.annotations.AlternativeKey in project rest.li by linkedin.

the class RestLiAnnotationReader method buildAlternativeKey.

/**
   * Create an {@link com.linkedin.restli.server.AlternativeKey} object from an {@link com.linkedin.restli.server.annotations.AlternativeKey} annotation.
   *
   * @param resourceName Name of the resource.
   * @param altKeyAnnotation The {@link com.linkedin.restli.server.annotations.AlternativeKey} annotation.
   * @return {@link com.linkedin.restli.server.AlternativeKey} object.
   */
private static com.linkedin.restli.server.AlternativeKey<?, ?> buildAlternativeKey(String resourceName, AlternativeKey altKeyAnnotation) {
    String keyName = altKeyAnnotation.name();
    Class<?> keyType = altKeyAnnotation.keyType();
    Class<? extends TyperefInfo> altKeyTyperef = altKeyAnnotation.keyTyperefClass();
    KeyCoercer<?, ?> keyCoercer;
    try {
        keyCoercer = altKeyAnnotation.keyCoercer().newInstance();
    } catch (InstantiationException e) {
        throw new ResourceConfigException(String.format("KeyCoercer for alternative key '%s' on resource %s cannot be instantiated, %s", keyName, resourceName, e.getMessage()), e);
    } catch (IllegalAccessException e) {
        throw new ResourceConfigException(String.format("KeyCoercer for alternative key '%s' on resource %s cannot be instantiated, %s", keyName, resourceName, e.getMessage()), e);
    }
    try {
        @SuppressWarnings("unchecked") com.linkedin.restli.server.AlternativeKey<?, ?> altKey = new com.linkedin.restli.server.AlternativeKey(keyCoercer, keyType, getDataSchema(keyType, getSchemaFromTyperefInfo(altKeyTyperef)));
        return altKey;
    } catch (TemplateRuntimeException e) {
        throw new ResourceConfigException(String.format("DataSchema for alternative key '%s' of type %s on resource %s cannot be found; type is invalid or requires typeref.", keyName, keyType, resourceName), e);
    } catch (Exception e) {
        throw new ResourceConfigException(String.format("Typeref for alternative key '%s' on resource %s cannot be instantiated, %s", keyName, resourceName, e.getMessage()), e);
    }
}
Also used : TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) TemplateRuntimeException(com.linkedin.data.template.TemplateRuntimeException) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) AlternativeKey(com.linkedin.restli.server.annotations.AlternativeKey)

Example 2 with AlternativeKey

use of com.linkedin.restli.server.annotations.AlternativeKey in project rest.li by linkedin.

the class RestLiAnnotationReader method addAlternativeKeys.

/**
   * Add alternative keys, if there are any, to the given model.
   *
   * @param model The {@link com.linkedin.restli.internal.server.model.ResourceModel} that we are building.
   * @param resourceClass The resource {@link java.lang.Class}.
   */
private static void addAlternativeKeys(ResourceModel model, Class<?> resourceClass) {
    if (resourceClass.isAnnotationPresent(AlternativeKey.class) || resourceClass.isAnnotationPresent(AlternativeKeys.class)) {
        AlternativeKey[] alternativeKeyAnnotations;
        if (resourceClass.isAnnotationPresent(AlternativeKeys.class)) {
            alternativeKeyAnnotations = resourceClass.getAnnotation(AlternativeKeys.class).alternativeKeys();
        } else //(resourceClass.isAnnotationPresent(AlternativeKey.class) == true)
        {
            alternativeKeyAnnotations = new AlternativeKey[] { resourceClass.getAnnotation(AlternativeKey.class) };
        }
        Map<String, com.linkedin.restli.server.AlternativeKey<?, ?>> alternativeKeyMap = new HashMap<String, com.linkedin.restli.server.AlternativeKey<?, ?>>(alternativeKeyAnnotations.length);
        for (AlternativeKey altKeyAnnotation : alternativeKeyAnnotations) {
            @SuppressWarnings("unchecked") com.linkedin.restli.server.AlternativeKey<?, ?> altKey = buildAlternativeKey(model.getName(), altKeyAnnotation);
            alternativeKeyMap.put(altKeyAnnotation.name(), altKey);
        }
        model.putAlternativeKeys(alternativeKeyMap);
    } else {
        model.putAlternativeKeys(new HashMap<String, com.linkedin.restli.server.AlternativeKey<?, ?>>());
    }
}
Also used : HashMap(java.util.HashMap) AlternativeKeys(com.linkedin.restli.server.annotations.AlternativeKeys) AlternativeKey(com.linkedin.restli.server.annotations.AlternativeKey)

Aggregations

AlternativeKey (com.linkedin.restli.server.annotations.AlternativeKey)2 TemplateRuntimeException (com.linkedin.data.template.TemplateRuntimeException)1 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)1 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)1 AlternativeKeys (com.linkedin.restli.server.annotations.AlternativeKeys)1 HashMap (java.util.HashMap)1