@Override public <T extends RestEntity> T deserialize(Class<T> dataType, String data) throws RestException { throw new RestException("Streaming of raw string data not supported"); }
@Override public void serialize(RestEntity entity, OutputStream outputStream) throws RestException { try { mapper.writeValue(outputStream, entity); } catch (IOException e) { throw new RestException("Unable to serialize entity", e); } }
@Override public <T extends RestEntity> T deserialize(Class<T> dataType, InputStream inputStream) throws RestException { try { return mapper.readValue(inputStream, dataType); } catch (IOException e) { throw new RestException("Unable to parse input document: " + e.getMessage()); } }
@RequestMapping(value = "/Instances({instanceId})", method = DELETE, produces = "application/json") public RestEntity removeEntry(@PathVariable String instanceId) throws RestException { try { LOG.debug("Receiving a instance delete operation for instance: {}", instanceId); instanceFactory.deleteInstance(instanceId); return null; } catch(JasDBStorageException e) { throw new RestException("Unable to remove instance: " + instanceId); } } }
@Override public <T extends RestEntity> T deserialize(Class<T> dataType, String data) throws RestException { try { return deserialize(dataType, new ByteArrayInputStream(data.getBytes(ENCODING))); } catch (UnsupportedEncodingException e) { throw new RestException("Unable to parse input document: " + e.getMessage()); } }
@RequestMapping(value = "/Instances({instanceId})/Bags({bagName})/Indexes({indexName})", method = DELETE, consumes = "application/json", produces = "application/json") public ResponseEntity<?> removeEntry(@PathVariable String instanceId, @PathVariable String bagName, @PathVariable String indexName) throws RestException { if(StringUtils.hasText(instanceId) && StringUtils.hasText(bagName)) { LOG.debug("Removing index from instance: {} bag: {} with name: {}", instanceId, bagName, indexName); try { StorageService storageService = storageServiceFactory.getStorageService(instanceId, bagName); storageService.removeIndex(indexName); return response(null, HttpStatus.NO_CONTENT); } catch(JasDBStorageException e) { throw new RestException("Unable to remove index data: " + e.getMessage()); } } else { throw new RestException("Cannot remove index without a specified bag or InstanceId"); } }
@RequestMapping(value = "/Users({userId})", produces = "application/json", consumes = "application/json", method = DELETE) public RestEntity removeEntry(@PathVariable String userId, HttpServletRequest request) throws RestException { if (StringUtils.stringNotEmpty(userId)) { try { RequestContext requestContext = ControllerUtil.getRequestContext(request); userManager.deleteUser(requestContext.getUserSession(), userId); return null; } catch (JasDBStorageException e) { throw new RestException("Unable to remove user", e); } } else { throw new RestException("Unable to delete user, no id specified"); } } }
@Override public <T extends RestEntity> T deserialize(Class<T> dataType, String data) throws RestException { JsonEntityDeserializer entityDeserializer = new JsonEntityDeserializer(); try { return dataType.cast(new StreamedEntity(entityDeserializer.deserializeEntity(data))); } catch(MetadataParseException e) { throw new RestException("Unable to parse entity", e); } }
@Override public <T extends RestEntity> T deserialize(Class<T> dataType, InputStream inputStream) throws RestException { try { try (JsonParser parser = factory.createParser(inputStream)) { return dataType.cast(new StreamedEntity(new JsonEntityDeserializer().deserializeEntity(parser))); } } catch(IOException | MetadataParseException e) { throw new RestException("Unable to parse entity", e); } }
private RestEntity loadAllGrantObjects(RequestContext context) throws RestException { try { List<GrantObject> grantObjects = userManager.getGrantObjects(context.getUserSession()); List<RestGrantObject> restGrantObjects = new ArrayList<>(); for(GrantObject grantObject : grantObjects) { restGrantObjects.add(GrantModelMapper.map(grantObject)); } return new RestGrantObjectCollection(restGrantObjects); } catch(JasDBStorageException e) { throw new RestException("Unable to load grant objects", e); } }
private RestEntity loadSpecificGrantObject(RequestContext context, String object) throws RestException { try { GrantObject grantObject = userManager.getGrantObject(context.getUserSession(), object); return GrantModelMapper.map(grantObject); } catch(JasDBStorageException e) { throw new RestException("Unable to load grant objects", e); } }
@Override public void serialize(RestEntity entity, OutputStream outputStream) throws RestException { if(entity instanceof StreamableEntityCollection) { StreamableEntityCollection entityCollection = (StreamableEntityCollection) entity; generateEntityOutput(entityCollection.getSize(), entityCollection.getTimeMilliseconds(), entityCollection.getResult(), outputStream); } else { throw new RestException("Unable to serialize the entity, not of type: " + StreamableEntityCollection.class.getName()); } }
@RequestMapping(value = "/Instances", method = POST, consumes = "application/json", produces = "application/json") public RestEntity writeEntry(@RequestBody InstanceRest dbInstance) throws RestException { try { instanceFactory.addInstance(dbInstance.getInstanceId()); return getInstance(dbInstance.getInstanceId()); } catch(JasDBStorageException e) { throw new RestException("Unable to create new instance: " + e.getMessage()); } }
private RestEntity loadUserList(RequestContext context) throws RestException { try { List<String> userList = userManager.getUsers(context.getUserSession()); return new RestUserList(userList); } catch(JasDBStorageException e) { throw new RestException("Unable to load user list", e); } }
@RequestMapping(value = "/Instances({instanceId})/Bags({bagName})/Entities({entityId})", produces = "application/json", method = DELETE) public RestEntity removeEntry(@PathVariable String instanceId, @PathVariable String bagName, @PathVariable String entityId, HttpServletRequest request) throws RestException { if(stringNotEmpty(instanceId) && stringNotEmpty(bagName) && stringNotEmpty(entityId)) { try { LOG.debug("Doing remove of entity with id: {}", entityId); StorageService storageService = storageServiceFactory.getOrCreateStorageService(instanceId, bagName); storageService.removeEntity(getRequestContext(request), entityId); return null; } catch(JasDBStorageException e) { throw new RestException("Unable to remove entity: " + e.getMessage()); } } else { throw new SyntaxException("Cannot remove entity, invalid parameters for delete operation"); } }
@RequestMapping(value = "/Grants({grantId})", produces = "application/json", method = DELETE) public RestEntity removeEntry(@RequestBody RestGrant grant, HttpServletRequest request) throws RestException { if(StringUtils.stringNotEmpty(grant.getObjectName()) && StringUtils.stringNotEmpty(grant.getUsername())) { try { RequestContext requestContext = getRequestContext(request); userManager.revoke(requestContext.getUserSession(), grant.getObjectName(), grant.getUsername()); return null; } catch(JasDBStorageException e) { throw new RestException("Unable to revoke grant", e); } } else { throw new RestException("Cannot remove without user and object specified"); } } }
private InstanceRest loadInstance(String instanceId) throws RestException { try { DBInstance dbInstance = instanceFactory.getInstance(instanceId); return new InstanceRest(dbInstance.getPath(), "OK", engineManager.getEngineVersion(), dbInstance.getInstanceId()); } catch(ConfigurationException e) { throw new RestException("Unable to retrieve the instance", e); } }
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); } }
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"); } }