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

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

Introduction

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

Prototype

public String getString(String attrName) 

Source Link

Document

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

Usage

From source file:com.achow101.bittipaddr.server.bittipaddrServiceImpl.java

License:Open Source License

public String addAddresses(AddrReq req) {

    // Setup the aws dynamo db client
    AmazonDynamoDBClient client = new AmazonDynamoDBClient();
    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.getTable("Bittipaddrs");

    // Check that the request is for editing an existing one
    if (!req.getId().equals("NEW")) {
        try {/* w ww  . jav a2s  .c o  m*/
            Item item = table.getItem("ID", req.getId());

            // Check the password
            if (getHash(req.getPassword()).equals(item.getString("passhash"))) {
                // If the req has been edited, update DB
                if (req.isEdited()) {
                    // Recalculate addresses if xpub is set
                    if (!req.getXpub().equals("NONE")) {
                        try {
                            // Check Xpub
                            DeterministicKey xpub = DeterministicKey.deserializeB58(req.getXpub(), params);
                            DeterministicKey external = HDKeyDerivation.deriveChildKey(xpub, 0);

                            // Derive 1000 addresses and add to req
                            String[] addrs = new String[1000];
                            for (int i = 0; i < 1000; i++) {
                                addrs[i] = HDKeyDerivation.deriveChildKey(external, i).toAddress(params)
                                        .toBase58();
                            }
                            req.setAddresses(addrs);
                        } catch (Exception e) {
                            return "<p style=\"color:red;\">Invalid xpub" + req.getXpub() + "</p>";
                        }
                    }
                    if (req.getAddresses()[0].isEmpty())
                        return "<p style=\"color:red;\">Must have at least one address</p>";

                    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("ID", req.getId())
                            .withUpdateExpression("set AddrIndex=:i, Addresses=:a, bip32xpub=:x")
                            .withValueMap(new ValueMap().withNumber(":i", 0)
                                    .withList(":a", Arrays.asList(req.getAddresses()))
                                    .withString(":x", req.getXpub()));
                    table.updateItem(updateItemSpec);
                    return req.getHtml();
                }

                String[] addresses = new String[item.getList("Addresses").size()];
                item.getList("Addresses").toArray(addresses);
                req.setAddresses(addresses);
                req.setXpub(item.getString("bip32xpub"));

                if (req.isEditable())
                    return req.getPlain();
                else
                    return req.getHtml();
            } else
                return "<p style=\"color:red;\">Incorrect password</p>";

        } catch (Exception e) {
            return "<p style=\"color:red;\">Could not find unit</p>";
        }

    }
    // Check validity of addresses
    else if (req.getXpub().equals("NONE") && req.getAddresses().length != 0) {
        for (int i = 0; i < req.getAddresses().length; i++) {
            try {
                Address addr = Address.fromBase58(params, req.getAddresses()[i]);
            } catch (AddressFormatException e) {
                return "<p style=\"color:red;\">Invalid address" + req.getAddresses()[i] + "</p>";
            }
        }
    }
    // Check validity of xpub
    else if (!req.getXpub().equals("NONE") && req.getAddresses().length == 0) {
        try {
            // Check Xpub
            DeterministicKey xpub = DeterministicKey.deserializeB58(req.getXpub(), params);
            DeterministicKey external = HDKeyDerivation.deriveChildKey(xpub, 0);

            // Derive 1000 addresses and add to req
            String[] addrs = new String[1000];
            for (int i = 0; i < 1000; i++) {
                addrs[i] = HDKeyDerivation.deriveChildKey(external, i).toAddress(params).toBase58();
            }
            req.setAddresses(addrs);
        } catch (Exception e) {
            return "<p style=\"color:red;\">Invalid xpub" + req.getXpub() + "</p>";
        }
    }

    // Set the request ID and unique password
    req.setId(new BigInteger(40, random).toString(32));
    req.setPassword(new BigInteger(256, random).toString(32));

    // Add request to DynamoDB
    Item item = null;
    try {
        item = new Item().withPrimaryKey("ID", req.getId()).withInt("AddrIndex", 0)
                .withList("Addresses", Arrays.asList(req.getAddresses())).withString("bip32xpub", req.getXpub())
                .withString("passhash", getHash(req.getPassword()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    table.putItem(item);

    return req.getHtml();
}

From source file:com.exorath.service.lastserver.dynamodb.DynamoDBService.java

License:Apache License

@Override
public GetResult getLastServer(UUID playerId) {
    GetItemSpec spec = new GetItemSpec().withPrimaryKey(PRIM_KEY, playerId.toString());
    Item item = table.getItem(spec);
    logger.info("Retrieved the following last server data for player " + playerId + ": " + item);
    if (item == null || !item.hasAttribute(GAMEID_ATTR)) {
        return new GetResult(null, null, null);
    } else {// ww  w.j  a  v  a  2  s.  co  m
        String gameId = null;
        String mapId = null;
        String flavorId = null;
        if (item.hasAttribute(GAMEID_ATTR)) {
            gameId = item.getString(GAMEID_ATTR);
        }
        if (item.hasAttribute(MAPID_ATTR)) {
            mapId = item.getString(MAPID_ATTR);
        }
        if (item.hasAttribute(FLAVORID_ATTR)) {
            flavorId = item.getString(FLAVORID_ATTR);
        }
        return new GetResult(gameId, mapId, flavorId);
    }
}

From source file:com.exorath.service.party.service.DynamoDatabaseProvider.java

License:Apache License

/**
 * Convert an item containing information about a party into a party object
 *
 * @param item The item containing the information
 * @return A party object containing all possible information from the item
 *///from w  w  w . ja v  a  2  s .  co  m
private Party getPartyFromItem(Item item) {
    String owner_uuid = item.hasAttribute(OWNER_UUID) ? item.getString(OWNER_UUID) : null;
    String party_uuid = item.hasAttribute(PARTY_UUID) ? item.getString(PARTY_UUID) : null;
    String serverId = item.hasAttribute(SERVER_ID) ? item.getString(SERVER_ID) : null;
    ArrayList<String> members = item.hasAttribute(MEMBERS) ? new ArrayList<>(item.getStringSet(MEMBERS)) : null;
    Long expiry = item.hasAttribute(EXPIRE) ? item.getLong(EXPIRE) : null;
    return new Party(party_uuid, owner_uuid, serverId, members, expiry);
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java

License:LGPL

@Test
public void test_remove_singlePath() throws Exception {
    // setup//from w  w w.  ja v  a 2  s  . c  o  m
    table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE)
            .put("a", ImmutableMap.of("a", 2, "b", true)).build()));

    // setup
    String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/a\" } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder actual = jsonPatch.get();
    UpdateItemExpressionSpec actualSpec = actual.buildForUpdate();
    table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec);
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertFalse(item.hasAttribute("a"));
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java

License:LGPL

@Test
public void test_remove_nestedPath() throws Exception {
    // setup//  ww w.  j a v  a  2 s.co m
    table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE)
            .put("a", ImmutableMap.of("a", 2, "b", true)).build()));

    // setup
    String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/a/a\" } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder actual = jsonPatch.get();
    UpdateItemExpressionSpec actualSpec = actual.buildForUpdate();
    table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec);
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertTrue(item.getRawMap("a").containsKey("b"));
    Assert.assertEquals(item.getRawMap("a").get("b"), true);
    Assert.assertFalse(item.getRawMap("a").containsKey("a"));
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java

License:LGPL

@Test
public void test_remove_absentPath() throws Exception {
    // setup//  ww w .  ja  v a  2 s. c  o m
    table.putItem(Item.fromMap(
            ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE).put("a", "b").build()));

    // setup
    String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/c\" } ]"; // $.c does not exist in target
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder actual = jsonPatch.get();
    UpdateItemExpressionSpec actualSpec = actual.buildForUpdate();
    table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec);
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertEquals(item.getString("a"), "b");
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java

License:LGPL

@Test
public void testReplaceSinglePathNumberExtant() throws Exception {
    // setup/*from   w  w w.  j a  v  a  2s.c om*/
    table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE)
            .put("a", "peekaboo").build()));
    String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": 1 } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder builder = jsonPatch.get();
    UpdateItemExpressionSpec spec = builder.buildForUpdate();
    UpdateItemOutcome out = table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY_ATTRIBUTE_NAME, VALUE)
            .withExpressionSpec(spec).withReturnValues(ReturnValue.ALL_OLD));

    Item oldItem = Item.fromMap(InternalUtils.toSimpleMapValue(out.getUpdateItemResult().getAttributes()));
    Assert.assertTrue(oldItem.hasAttribute("a"));
    Assert.assertEquals(oldItem.getString("a"), "peekaboo");
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertEquals(item.getNumber("a").longValue(), 1L);
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java

License:LGPL

@Test
public void test_replace_existingNestedPath_string() throws Exception {
    // setup// ww  w .ja  v  a 2s  .  c om
    table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE)
            .put("a", ImmutableMap.of("a", 2L, "b", true)).build()));

    String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a/b\", \"value\": \"bar\" } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder builder = jsonPatch.get();
    UpdateItemExpressionSpec spec = builder.buildForUpdate();
    table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec);
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertTrue(item.getRawMap("a").containsKey("a"));
    Assert.assertEquals(((BigDecimal) item.getRawMap("a").get("a")).longValue(), 2L);
    Assert.assertTrue(item.getRawMap("a").containsKey("b"));
    Assert.assertEquals(item.getRawMap("a").get("b"), "bar");
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java

License:LGPL

@Test
public void test_replace_singlePath_stringSet() throws Exception {
    // setup// w  w  w  .  ja  v  a  2  s.  c om
    table.putItem(Item.fromMap(
            ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE).put("a", 1L).build()));
    String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": [\"foo\",\"bar\"] } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder builder = jsonPatch.get();
    UpdateItemExpressionSpec spec = builder.buildForUpdate();
    table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec);
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertTrue(item.getList("a").contains("foo"));
    Assert.assertTrue(item.getList("a").contains("bar"));
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java

License:LGPL

@Test
public void test_replace_replaceExisting_singlePath_stringSet() throws Exception {
    // setup/* www .  j a  va2 s .  c o m*/
    table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE)
            .put("a", ImmutableSet.of("foo", "bar")).build()));

    String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": [\"baz\",\"qux\"] } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder builder = jsonPatch.get();
    UpdateItemExpressionSpec spec = builder.buildForUpdate();
    table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec);
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertEquals(item.getList("a").size(), 2);
    Assert.assertTrue(item.getList("a").contains("baz"));
    Assert.assertTrue(item.getList("a").contains("qux"));
}