private ResponseEntity<RestEntity> handleList(String instanceId) throws RestException { LOG.debug("Retrieving full list of bags on storage instance: {}", instanceId); List<RestBag> bags = new ArrayList<>(); try { DBInstance instance = getInstance(instanceFactory, instanceId); for(Bag bag : instance.getBags()) { StorageService storageService = storageServiceFactory.getStorageService(instance.getInstanceId(), bag.getName()); bags.add(new RestBag(instance.getInstanceId(), bag.getName(), storageService.getSize(), storageService.getDiskSize())); } } catch(JasDBStorageException e) { throw new RestException("Unable to load bags", e); } return ok(new BagCollection(bags)); }
private ResponseEntity<RestEntity> doSearch(String instanceId, String bagName) throws JasDBException { DBInstance instance = getInstance(instanceFactory, instanceId); LOG.debug("Searching for a bag on instance: {} with name: {}", instanceId, bagName); try { StorageService storageService = storageServiceFactory.getStorageService(instance.getInstanceId(), bagName); if(storageService != null) { LOG.debug("Found a bag with name: {}", bagName); return ok(new RestBag(instance.getInstanceId(), bagName, storageService.getSize(), storageService.getDiskSize())); } else { return notFound(new ErrorEntity(404, "No bag was found with name: " + bagName)); } } catch(JasDBStorageException e) { throw new RestException("Unable to load bag metadata", e); } }
@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); } }
private ResponseEntity<RestEntity> createBag(String instanceId, RestBag bag) throws JasDBException { DBInstance instance = getInstance(instanceFactory, instanceId); LOG.debug("Creating on instance: {} bag: {}", instanceId, bag); if(StringUtils.stringNotEmpty(bag.getName())) { StorageService storageService = storageServiceFactory.getOrCreateStorageService(instance.getInstanceId(), bag.getName()); return ok(new RestBag(instance.getInstanceId(), bag.getName(), storageService.getSize(), storageService.getDiskSize())); } else { throw new RestException("Cannot create bag, no name specified"); } }