Search in sources :

Example 1 with EntityType

use of co.cask.cdap.proto.element.EntityType in project cdap by caskdata.

the class EntityId method fromStringOld.

public static <T extends Id> T fromStringOld(String string, Class<T> oldIdClass) {
    EntityType type = EntityType.valueOfOldIdClass(oldIdClass);
    EntityId id = fromString(string, type.getIdClass());
    return (T) id.toId();
}
Also used : EntityType(co.cask.cdap.proto.element.EntityType)

Example 2 with EntityType

use of co.cask.cdap.proto.element.EntityType in project cdap by caskdata.

the class IdTypeAdapter method deserialize.

@Override
public Id deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject map = json.getAsJsonObject();
    JsonElement elementTypeJson = map.get("elementType");
    if (elementTypeJson == null) {
        throw new JsonParseException("Expected elementType in Id JSON");
    }
    String elementTypeString = elementTypeJson.getAsString();
    EntityType type = EntityType.valueOf(elementTypeString);
    if (type == null) {
        throw new JsonParseException("Invalid elementType: " + elementTypeString);
    }
    return context.deserialize(json, type.getIdClass());
}
Also used : EntityType(co.cask.cdap.proto.element.EntityType) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 3 with EntityType

use of co.cask.cdap.proto.element.EntityType in project cdap by caskdata.

the class EntityId method fromString.

protected static <T extends EntityId> T fromString(String string, @Nullable Class<T> idClass) {
    String[] typeAndId = string.split(IDSTRING_TYPE_SEPARATOR, 2);
    if (typeAndId.length != 2) {
        throw new IllegalArgumentException(String.format("Expected type separator '%s' to be in the ID string: %s", IDSTRING_TYPE_SEPARATOR, string));
    }
    String typeString = typeAndId[0];
    EntityType typeFromString = EntityType.valueOf(typeString.toUpperCase());
    Class<T> idClassFromString = (Class<T>) typeFromString.getIdClass();
    if (idClass != null && !idClassFromString.equals(idClass)) {
        /* idClass can differ from idClassFromString only when typeFromString is EntityType.PROGRAM and idClass is
       * of WorkflowId.class, because when toString method is called for WorkflowId, its superclass ProgramId calls
       * EntityId's toString() method and the string always contains ProgramId as type. */
        if (!idClassFromString.isAssignableFrom(idClass) || !typeFromString.equals(EntityType.PROGRAM)) {
            throw new IllegalArgumentException(String.format("Expected EntityId of class '%s' but got '%s'", idClass.getName(), typeFromString.getIdClass().getName()));
        }
    }
    String idString = typeAndId[1];
    try {
        List<String> idParts = Arrays.asList(IDSTRING_PART_SEPARATOR_PATTERN.split(idString));
        // special case for DatasetId, DatasetModuleId, DatasetTypeId since we allow . in the name
        if (EnumSet.of(EntityType.DATASET, EntityType.DATASET_MODULE, EntityType.DATASET_TYPE).contains(typeFromString)) {
            int namespaceSeparatorPos = idString.indexOf(IDSTRING_PART_SEPARATOR);
            if (namespaceSeparatorPos > 0) {
                idParts = new ArrayList<>();
                idParts.add(idString.substring(0, namespaceSeparatorPos));
                idParts.add(idString.substring(namespaceSeparatorPos + 1));
            }
        }
        return typeFromString.fromIdParts(idParts);
    } catch (IllegalArgumentException e) {
        String message = idClass == null ? String.format("Invalid ID: %s", string) : String.format("Invalid ID for type '%s': %s", idClass.getName(), string);
        throw new IllegalArgumentException(message, e);
    }
}
Also used : EntityType(co.cask.cdap.proto.element.EntityType)

Example 4 with EntityType

use of co.cask.cdap.proto.element.EntityType in project cdap by caskdata.

the class EntityIdTypeAdapter method deserialize.

@Override
public EntityId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject map = json.getAsJsonObject();
    JsonElement entityTypeJson = map.get("entity");
    if (entityTypeJson == null) {
        throw new JsonParseException("Expected entity in EntityId JSON");
    }
    String entityTypeString = entityTypeJson.getAsString();
    EntityType type = EntityType.valueOf(entityTypeString);
    if (type == null) {
        throw new JsonParseException("Invalid entity: " + entityTypeString);
    }
    return context.deserialize(json, type.getIdClass());
}
Also used : EntityType(co.cask.cdap.proto.element.EntityType) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 5 with EntityType

use of co.cask.cdap.proto.element.EntityType in project cdap by caskdata.

the class Authorizable method fromString.

/**
 * Constructs an {@link Authorizable} from the given entityString. The entityString must be a representation of an
 * entity similar to {@link EntityId#toString()} with the exception that the string can contain wildcards (? and *).
 * Note:
 * <ol>
 * <li>
 * The only validation that this class performs on the entityString is that it checks if the string has enough
 * valid parts for the given {@link #entityType}. It does not check if the entity exists or not. This is
 * required to allow pre grants.
 * </li>
 * <li>
 * CDAP Authorization does not support authorization on versions of {@link co.cask.cdap.proto.id.ApplicationId}
 * and {@link co.cask.cdap.proto.id.ArtifactId}. If a version is included while construction an Authorizable
 * through {@link #fromString(String)} an {@link IllegalArgumentException} will be thrown.
 * </li>
 * </ol>
 *
 * @param entityString the {@link EntityId#toString()} of the entity which may or may not contains wildcards(? or *)
 */
public static Authorizable fromString(String entityString) {
    if (entityString == null || entityString.isEmpty()) {
        throw new IllegalArgumentException("Null or empty entity string.");
    }
    String[] typeAndId = entityString.split(EntityId.IDSTRING_TYPE_SEPARATOR, 2);
    if (typeAndId.length != 2) {
        throw new IllegalArgumentException(String.format("Cannot extract the entity type from %s", entityString));
    }
    String typeString = typeAndId[0];
    EntityType type = EntityType.valueOf(typeString.toUpperCase());
    String idString = typeAndId[1];
    List<String> idParts = Collections.emptyList();
    switch(type) {
        case KERBEROSPRINCIPAL:
            // kerberos principal might contain . which is also EntityId.IDSTRING_PART_SEPARATOR_PATTERN so don't split for
            // them and also principal doesn't have other parts so just initialize
            idParts = Collections.singletonList(idString);
            break;
        case DATASET:
        case DATASET_TYPE:
        case DATASET_MODULE:
            int namespaceSeparatorPos = idString.indexOf(EntityId.IDSTRING_PART_SEPARATOR);
            if (namespaceSeparatorPos > 0) {
                idParts = new ArrayList<>();
                idParts.add(idString.substring(0, namespaceSeparatorPos));
                idParts.add(idString.substring(namespaceSeparatorPos + 1));
            }
            break;
        default:
            idParts = Arrays.asList(EntityId.IDSTRING_PART_SEPARATOR_PATTERN.split(idString));
    }
    Map<EntityType, String> entityParts = new LinkedHashMap<>();
    checkParts(type, idParts, idParts.size() - 1, entityParts);
    return new Authorizable(type, entityParts);
}
Also used : EntityType(co.cask.cdap.proto.element.EntityType) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

EntityType (co.cask.cdap.proto.element.EntityType)5 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 JsonParseException (com.google.gson.JsonParseException)2 LinkedHashMap (java.util.LinkedHashMap)1