List of usage examples for com.amazonaws.services.dynamodbv2.document Table getItem
@Override
public Item getItem(String hashKeyName, Object hashKeyValue)
From source file:com.achow101.bittipaddr.server.addressServiceImpl.java
License:Open Source License
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the id String requestURI = request.getRequestURI(); String id = requestURI.substring(requestURI.lastIndexOf("/") + 1); // Setup the aws dynamo db client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); DynamoDB dynamoDB = new DynamoDB(client); // Setup blockcypher API BlockCypherContext blockCypherContext = new BlockCypherContext("v1", "btc", "main", "4d3109a5c07f426da9ccc2943da39244"); // Lookup ID and get current address, increment index String address = ""; Table table = dynamoDB.getTable("Bittipaddrs"); int currAddrInx = 0; try {/*w ww . j a v a 2s. c o m*/ Item item = table.getItem("ID", id); currAddrInx = item.getInt("AddrIndex"); int origIndx = currAddrInx; List<String> addresses = item.getList("Addresses"); if (currAddrInx < addresses.size()) { address = addresses.get(currAddrInx); while (blockCypherContext.getAddressService().getAddress(address).getnTx() > 0 && currAddrInx < addresses.size()) { // Increment index and get next address currAddrInx++; address = addresses.get(currAddrInx); // Wait one third of a second to prevent rate limiting Thread.sleep(334); } } else { address = addresses.get(addresses.size() - 1); } // Update index if DB if it has changed if (currAddrInx != origIndx) { UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("ID", id) .withUpdateExpression("set AddrIndex=:i") .withValueMap(new ValueMap().withNumber(":i", currAddrInx)); table.updateItem(updateItemSpec); } } // Deal with rate limiting from BlockCypher catch (BlockCypherException e) { UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("ID", id) .withUpdateExpression("set AddrIndex=:i") .withValueMap(new ValueMap().withNumber(":i", currAddrInx)); table.updateItem(updateItemSpec); } catch (Exception e) { System.out.println("Error in getting item."); } // Send them a redirect to the bitcoin uri if redirect is set if (request.getParameter("redirect") != null) { response.sendRedirect("bitcoin:" + address); } else { // Set response content type response.setContentType("text/html"); // Actual logic goes here. PrintWriter out = response.getWriter(); out.println("<a href=bitcoin:" + address + ">" + address + "</a>"); } }
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 {/*from w w w .ja v 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.eho.dynamodb.DynamoDBConnection.java
public static Item get_item_by_ID(String id) { DynamoDB dynamoDB = new DynamoDB(dynamoDBClient); Table table = dynamoDB.getTable(PATIENT_TABLE); Item retreived_item = table.getItem(PRIMARY_KEY, id); return retreived_item; }
From source file:com.eho.dynamodb.DynamoDBConnection.java
public static PutItemOutcome upload_resource_old(String resource) throws Exception { String id;/*from w w w. j a va2 s . c om*/ JSONObject json_resource = new JSONObject(resource); //does the resource have a primary key? if (json_resource.has(PRIMARY_KEY))//if it does not have a primary key, create one using uuid id = json_resource.getString(PRIMARY_KEY); else id = UUID.randomUUID().toString(); DynamoDB dynamoDB = new DynamoDB(dynamoDBClient); Table table = dynamoDB.getTable(PATIENT_TABLE); //lets retreive based on the key. if key invalid (not assigned yet) nullis returned. Item retreived_item = table.getItem(PRIMARY_KEY, id); if (retreived_item == null)//if null instantiate it { retreived_item = new Item(); retreived_item.withPrimaryKey(PRIMARY_KEY, id); } Integer new_version = retreived_item.getInt("version") + 1; retreived_item.withInt("version", new_version); Item item_to_upload = Item.fromJSON(retreived_item.toJSONPretty()).withJSON("Document", resource); PutItemSpec putItemSpec = new PutItemSpec().withItem(item_to_upload).withReturnValues(ReturnValue.NONE); return table.putItem(putItemSpec); }
From source file:com.eho.dynamodb.DynamoDBConnection.java
public static String upload_resource(BaseResource resource, String primary_key /* if no primary key in case of post, send null*/ ) throws Exception { String id = add_primary_as_extension(resource, primary_key); String resource_string = DynamoDBConnection.fCtx.newJsonParser().setPrettyPrint(true) .encodeResourceToString(resource); ;//from www .ja v a 2 s . c o m DynamoDB dynamoDB = new DynamoDB(dynamoDBClient); Table table = dynamoDB.getTable(PATIENT_TABLE); //lets retreive based on the key. if key invalid (not assigned yet) nullis returned. Item retreived_item = table.getItem(PRIMARY_KEY, id); if (retreived_item == null)//if null instantiate it { retreived_item = new Item(); retreived_item.withPrimaryKey(PRIMARY_KEY, id); retreived_item.withInt("version", -1); } Integer new_version = retreived_item.getInt("version") + 1; retreived_item.withInt("version", new_version); Item item_to_upload = retreived_item//Item.fromJSON(retreived_item.toJSONPretty()) .withString("text" + new_version.toString(), resource_string) .withMap("json-document", new ObjectMapper().readValue(resource_string, LinkedHashMap.class)); PutItemSpec putItemSpec = new PutItemSpec().withItem(item_to_upload); table.putItem(putItemSpec); return id; }
From source file:com.eho.dynamodb.DynamoDBConnection.java
public static UpdateItemOutcome update_resource(String resource) throws Exception { String id;// www . j a v a2s .co m JSONObject json_resource = new JSONObject(resource); //does the resource have a primary key? if (json_resource.has(PRIMARY_KEY))//if it does not have a primary key, create one using uuid id = json_resource.getString(PRIMARY_KEY); else id = UUID.randomUUID().toString(); DynamoDB dynamoDB = new DynamoDB(dynamoDBClient); Table table = dynamoDB.getTable(PATIENT_TABLE); //lets retreive based on the key. if key invalid (not assigned yet) nullis returned. Item retreived_item = table.getItem(PRIMARY_KEY, id); if (retreived_item == null)//if null instantiate it { retreived_item = new Item(); retreived_item.withPrimaryKey(PRIMARY_KEY, id); } Integer new_version = retreived_item.getInt("version") + 1; retreived_item.withInt("version", new_version); String new_version_str = new_version.toString(); UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(PRIMARY_KEY, id) .withUpdateExpression("SET " + new_version_str + "= :newval") .withValueMap(new ValueMap().withString(":newval", resource)).withReturnValues(ReturnValue.ALL_NEW); return table.updateItem(updateItemSpec); }
From source file:io.ignitr.dispatchr.manager.core.data.ClientRepository.java
License:Apache License
/** * Saves a new client in DynamoDB.// w ww. j a v a 2 s . c om * * @param clientId client identifier * @param ownerName name of the person who owns the client * @param ownerEmail email of the person who owns the client * @return an {@link Observable} that emits the newly saved client */ public Observable<Client> save(String clientId, String ownerName, String ownerEmail) { return Observable.create(subscriber -> { Table clientTable = dynamoDB.getTable(CLIENT_TABLE_NAME); if (clientTable.getItem("clientId", clientId) != null) { throw new ClientAlreadyRegisteredException(clientId); } Client client = new Client(); client.setClientId(clientId); client.setOwnerName(ownerName); client.setOwnerEmail(ownerEmail); clientTable.putItem(new Item().withPrimaryKey("clientId", client.getClientId()) .withString("ownerName", client.getOwnerName()) .withString("ownerEmail", client.getOwnerEmail())); subscriber.onNext(client); subscriber.onCompleted(); }); }
From source file:io.ignitr.dispatchr.manager.core.data.ClientRepository.java
License:Apache License
/** * Finds a single client by id in DynamoDB. * * @param clientId client identifier/*www .j a v a 2s . c om*/ * @return an {@link Observable} that emits the client */ public Observable<Client> findOne(String clientId) { return Observable.create(subscriber -> { Table clientTable = dynamoDB.getTable(CLIENT_TABLE_NAME); Item item = clientTable.getItem("clientId", clientId); if (item == null) { throw new ClientNotFoundException(clientId); } else { subscriber.onNext(convertFromItem(item)); subscriber.onCompleted(); } }); }
From source file:io.ignitr.dispatchr.manager.core.data.ClientRepository.java
License:Apache License
/** * Checks whether or not a client with the supplied id exists in DynamoDB. * * @param clientId client identifier/*from w w w . ja va 2 s .c o m*/ * @return an {@link Observable} that emits <code>true</code> if the delete was successful; otherwise <code>false</code> */ public Observable<Boolean> exists(String clientId) { return Observable.create(subscriber -> { Table clientTable = dynamoDB.getTable(CLIENT_TABLE_NAME); Item item = clientTable.getItem("clientId", clientId); if (item == null) { subscriber.onNext(false); } else { subscriber.onNext(true); } subscriber.onCompleted(); }); }
From source file:io.ignitr.dispatchr.manager.core.data.TopicRepository.java
License:Apache License
/** * Finds the metadata for single topic./*w w w . j a v a2 s. c o m*/ * * @param topicName name of the topic * @return an {@link Observable} of {@link Topic} containing the metadata about the topic */ public Observable<Topic> findOne(String topicName) { return Observable.create(subscriber -> { Table table = dynamoDB.getTable(TOPIC_TABLE_NAME); Item item = table.getItem("name", topicName); if (item == null) { throw new TopicNotFoundException(topicName); } else { Topic metadata = new Topic(); metadata.setName(item.getJSON("name")); metadata.setArn(item.getJSON("arn")); metadata.setRegistered(true); subscriber.onNext(metadata); subscriber.onCompleted(); } }); }