@Override public void serialize(RestEntity entity, OutputStream outputStream) throws RestException { if(entity instanceof StreamedEntity) { try { StreamedEntity streamedEntity = (StreamedEntity) entity; JsonGenerator generator = factory.createGenerator(outputStream); generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); generator.configure(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM, false); try { JsonEntitySerializer entitySerializer = new JsonEntitySerializer(); entitySerializer.serializeEntity(streamedEntity.getEntity(), generator); } finally { generator.close(); } } catch(IOException | MetadataParseException e) { throw new RestException("Unable to serialize entity", e); } } else { throw new RestException("Unable to serialize entity not of type Entity"); } }
@Override public Entity insertEntity(RemotingContext context, String instance, String bag, Entity entity) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instance).bag(bag).entities().getConnectionString(); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); new JsonRestResponseHandler().serialize(new StreamedEntity(entity), bos); ClientResponse clientResponse = doInternalRequest(context, connectionString, new HashMap<>(), bos.toByteArray(), REQUEST_MODE.POST); try { StreamedEntity returnedEntity = new JsonRestResponseHandler().deserialize(StreamedEntity.class, clientResponse.getEntityInputStream()); entity.setInternalId(returnedEntity.getEntity().getInternalId()); return returnedEntity.getEntity(); } finally { clientResponse.close(); } } catch(RestException e) { throw new RemoteException("Unable to insert entity on remote destination", e); } }
private RestEntity doModificationOperation(String instanceId, String bagName, String rawData, RequestContext context, OPERATION_TYPE type) throws RestException { if(stringNotEmpty(instanceId) && stringNotEmpty(bagName)) { LOG.debug("Raw entity data received: {}", rawData); StreamedEntity streamedEntity = ENTITY_HANDLER.deserialize(StreamedEntity.class, rawData); Entity storeEntity = streamedEntity.getEntity(); try { StorageService storageService = storageServiceFactory.getOrCreateStorageService(instanceId, bagName); if(type == OPERATION_TYPE.UPDATE) { LOG.debug("Updating entity with id: {}", storeEntity.getInternalId()); storageService.persistEntity(context, storeEntity); } else if(type == OPERATION_TYPE.INSERT) { LOG.debug("Inserting new entity into bag: {}", bagName); storageService.insertEntity(context, storeEntity); } return new StreamedEntity(storeEntity); } catch(JasDBStorageException e) { throw new RestException("Unable to store entity: " + e.getMessage()); } } else { throw new SyntaxException("Cannot store entity, no Bag specified"); } }
@Override public Entity updateEntity(RemotingContext context, String instance, String bag, Entity entity) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instance).bag(bag).entities().getConnectionString(); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); new JsonRestResponseHandler().serialize(new StreamedEntity(entity), bos); ClientResponse clientResponse = doRequest(context, connectionString, new HashMap<>(), bos.toString(CHARACTER_ENCODING), REQUEST_MODE.PUT); try { StreamedEntity returnedEntity = new JsonRestResponseHandler().deserialize(StreamedEntity.class, clientResponse.getEntityInputStream()); return returnedEntity.getEntity(); } finally { clientResponse.close(); } } catch(RestException e) { throw new RemoteException("Unable to update entity on remote destination", e); } catch(UnsupportedEncodingException e) { throw new RemoteException("Unable to serialize entity", e); } }
@Override public Entity findById(RemotingContext context, String instance, String bag, String id) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instance).bag(bag).entityById(id).getConnectionString(); try { ClientResponse response = doRequest(context, connectionString); try { StreamedEntity entity = new JsonRestResponseHandler().deserialize(StreamedEntity.class, response.getEntityInputStream()); return entity.getEntity(); } catch (RestException e) { throw new RemoteException("Unable to parse remote entity data", e); } finally { response.close(); } } catch(ResourceNotFoundException e) { LOG.debug("Entity: {} not found in bag: {} on instance: {}", id, bag, instance); return null; } }