@Override public List<RemoteBag> getBags(RemotingContext context, String instance) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instance).bags().getConnectionString(); ClientResponse clientResponse = doRequest(context, connectionString); try { BagCollection bagCollection = new JsonRestResponseHandler().deserialize(BagCollection.class, clientResponse.getEntityInputStream()); List<RemoteBag> mappedBags = new ArrayList<>(); for(RestBag bag : bagCollection.getBags()) { mappedBags.add(new RemoteBag(bag.getInstanceId(), bag.getName(), new ArrayList<>(), bag.getSize(), bag.getDiskSize())); } return mappedBags; } catch(RestException e) { throw new RemoteException("Unable to parse remote bag data", e); } finally { clientResponse.close(); } }
@Override public RemoteBag getBag(RemotingContext context, String instance, String bagName) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instance).bag(bagName).getConnectionString(); try { ClientResponse clientResponse = doRequest(context, connectionString); try { String response = clientResponse.getEntityAsString(); log.debug("Response: {}", response); RestBag bag = new JsonRestResponseHandler().deserialize(RestBag.class, response); return new RemoteBag(bag.getInstanceId(), bag.getName(), new ArrayList<>(), bag.getSize(), bag.getDiskSize()); } catch(RestException e) { throw new RemoteException("Unable to parse remote bag data", e); } finally { clientResponse.close(); } } catch(ResourceNotFoundException e) { log.debug("No resource was found for bag: {} on instance: {}", bagName, instance); return null; } }
@Override public RemoteBag createBag(RemotingContext context, String instance, Bag bagMeta) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instance).bags().getConnectionString(); try { RestBag bag = new RestBag(instance, bagMeta.getName(), 0, 0); ByteArrayOutputStream bos = new ByteArrayOutputStream(); new JsonRestResponseHandler().serialize(bag, bos); ClientResponse clientResponse = doRequest(context, connectionString, new HashMap<>(), bos.toString(CHARACTER_ENCODING), REQUEST_MODE.POST); try { bag = new JsonRestResponseHandler().deserialize(RestBag.class, clientResponse.getEntityInputStream()); } finally { clientResponse.close(); } return new RemoteBag(bag.getInstanceId(), bag.getName(), new ArrayList<>(), bag.getSize(), bag.getDiskSize()); } catch(RestException e) { throw new RemoteException("Unable to parse remote bag definition", e); } catch(UnsupportedEncodingException e) { throw new RemoteException("Unable to serialize bag", e); } }