Example usage for com.amazonaws.services.dynamodbv2.document Item getBinary

List of usage examples for com.amazonaws.services.dynamodbv2.document Item getBinary

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document Item getBinary.

Prototype

public byte[] getBinary(String attrName) 

Source Link

Document

Returns the value of the specified attribute in the current item as a byte array; or null if the attribute either doesn't exist or the attribute value is null.

Usage

From source file:tr.com.serkanozal.samba.cache.impl.SambaGlobalCache.java

License:Open Source License

@Override
public <V> V get(String key) {
    V value;/*ww w .  j  av  a2 s  . co  m*/
    Item item = DYNAMO_DB_TABLE.getItem(new GetItemSpec().withPrimaryKey("id", key).withConsistentRead(true));
    if (item == null) {
        value = null;
    } else {
        byte[] data = item.getBinary("data");
        if (data == null) {
            value = null;
        } else {
            value = deserialize(data);
        }
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("Value %s has been retrieved from global cache with key %s", key, value));
    }
    return value;
}

From source file:ws.salient.aws.dynamodb.DynamoDBStore.java

License:Apache License

public Session get(Command command, KnowledgeRepository repository, Properties properties,
        Injector parentInjector, Sessions sessions, QuerySpec sessionQuery) {
    String sessionId = command.getSessionId();
    String accountId = command.getAccountId();

    SecretKeySpec secretKey;/*from   www .ja  v a 2  s. c  o  m*/
    ByteBuffer encryptedKey;
    Session session = new Session(sessionId);
    Page<Item, QueryOutcome> page = dynamodb.getTable("SalientSession").query(sessionQuery).firstPage();
    if (page != null && page.size() > 0) {

        try {
            Item result = page.iterator().next();

            encryptedKey = ByteBuffer.wrap((byte[]) result.getMap("secretKey").get("encrypted"));
            if (encryptedKey != null) {
                DecryptResult decrypt = kms.decrypt(new DecryptRequest()
                        .addEncryptionContextEntry("accountId", accountId)
                        .addEncryptionContextEntry("sessionId", sessionId).withCiphertextBlob(encryptedKey));
                byte[] key = decrypt.getPlaintext().array();
                secretKey = new SecretKeySpec(key, (String) result.getMap("secretKey").get("algorithm"));
            } else {
                secretKey = null;
            }

            result = decrypt(result, secretKey, "properties", "session");

            properties = json.readValue(result.getBinary("properties"), Properties.class);
            String knowledgeBaseId = result.getString("knowledgeBaseId");
            KnowledgeBase knowledgeBase = repository.getKnowledgeBase(knowledgeBaseId);
            String timestamp = result.getString("timestamp");

            session.init(knowledgeBase, properties, parentInjector, Instant.parse(timestamp),
                    result.getBinary("session"), sessions);

            int processCount = session.getProcessCount();

            List<Item> eventItems = new LinkedList();
            ItemCollection<QueryOutcome> query = dynamodb.getTable("SalientSessionEvent")
                    .query(new QuerySpec().withConsistentRead(true).withHashKey("sessionId", sessionId)
                            .withRangeKeyCondition(new RangeKeyCondition("timestamp").gt(timestamp)));
            query.pages().forEach((eventPage) -> {
                eventPage.forEach((eventItem) -> {
                    eventItems.add(eventItem);
                });
            });

            List<Command> commands = new LinkedList();

            eventItems.forEach((eventItem) -> {
                try {
                    eventItem = decrypt(eventItem, secretKey, "command");
                    byte[] value = eventItem.getBinary("command");
                    ObjectInputStream objectIn = new ObjectInputStream(new ByteArrayInputStream(value)) {
                        protected Class<?> resolveClass(ObjectStreamClass desc)
                                throws IOException, ClassNotFoundException {
                            return session.getKnowledgeBase().getContainer().getClassLoader()
                                    .loadClass(desc.getName());
                        }
                    };
                    Command event = (Command) objectIn.readObject();
                    if (event instanceof WorkItem) {
                        session.getWorkItemHandlers().forEach((handler) -> {
                            handler.getCompletedWorkItemIds().add(((WorkItem) event).getWorkItemId());
                        });
                    }
                    commands.add(event);
                } catch (ClassNotFoundException | IOException ex) {
                    throw new RuntimeException(ex);
                }
            });
            commands.forEach((event) -> {
                session.accept(event);
            });
            session.getWorkItemHandlers().forEach((handler) -> {
                handler.getCompletedWorkItemIds().clear();
            });

        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    } else {
        GenerateDataKeyResult dataKey = generateEncryptionKey(accountId, sessionId);
        byte[] key = dataKey.getPlaintext().array();
        secretKey = new SecretKeySpec(key, "AES");
        encryptedKey = dataKey.getCiphertextBlob();
        KnowledgeBase knowledgeBase = repository.getKnowledgeBase(command.getKnowledgeBaseId());
        session.init(knowledgeBase, properties, parentInjector, command.getTimestamp(), sessions);
    }
    session.setEncryptedKey(encryptedKey);
    session.setSecretKey(secretKey);
    return session;
}

From source file:ws.salient.aws.dynamodb.DynamoDBStore.java

License:Apache License

public Item encrypt(Item item, SecretKeySpec key, String... attributes) {
    try {// ww w  .  j a v  a2  s.c om
        if (key != null) {
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] iv = cipher.getIV();
            for (String attribute : attributes) {
                byte[] value = item.getBinary(attribute);
                item.withBinary(attribute, cipher.doFinal(value));
            }
            item.withMap("cipher", new LinkedHashMap());
            item.getMap("cipher").put("transformation", transformation);
            item.getMap("cipher").put("iv", iv);
        }
        return item;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:ws.salient.aws.dynamodb.DynamoDBStore.java

License:Apache License

public Item decrypt(Item item, SecretKeySpec key, String... attributes) {
    try {//w  w  w .  ja  v a  2s . c om
        if (key != null) {
            String transformation = (String) item.getMap("cipher").get("transformation");
            byte[] iv = (byte[]) item.getMap("cipher").get("iv");
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
            for (String attribute : attributes) {
                byte[] value = item.getBinary(attribute);
                item.withBinary(attribute, cipher.doFinal(value));
            }
        }
        return item;

    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException ex) {
        throw new RuntimeException(ex);
    }
}