protected byte[] toBytes(RestEntity entity) throws RestException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); new JsonRestResponseHandler().serialize(entity, bos); return bos.toByteArray(); }
private static void sendResponse(final RestEntity entity, int statusCode, HttpServletResponse response) { response.setStatus(statusCode); response.setContentType(restResponseHandler.getMediaType()); try { OutputStream outputStream = response.getOutputStream(); restResponseHandler.serialize(entity, outputStream); } catch (IOException | RestException e) { LOG.debug("Stream error, full stack: {}", e); LOG.info("Could not stream entity: {}", e.getMessage()); } catch(Throwable e) { LOG.error("Unable to stream entity", e); } } }
@Override public Instance addInstance(RemotingContext context, String instanceId) throws RemoteException { String connectionString = new RestConnectionBuilder().instance().getConnectionString(); try { InstanceRest instance = new InstanceRest(null, "OK", null, instanceId); ByteArrayOutputStream bos = new ByteArrayOutputStream(); new JsonRestResponseHandler().serialize(instance, bos); ClientResponse clientResponse = doRequest(context, connectionString, new HashMap<>(), bos.toString(CHARACTER_ENCODING), REQUEST_MODE.POST); try { instance = new JsonRestResponseHandler().deserialize(InstanceRest.class, clientResponse.getEntityInputStream()); } finally { clientResponse.close(); } return instance; } catch(RestException e) { throw new RemoteException("Unable to parse remote bag definition", e); } catch(UnsupportedEncodingException e) { throw new RemoteException("Unable to serialize bag", 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); } }
@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 IndexDefinition createIndex(RemotingContext context, String instance, String bag, IndexDefinition definition, boolean isUnique) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instance).bag(bag).indexes().getConnectionString(); try { IndexEntry entry = IndexModelMapper.map(definition, isUnique); ByteArrayOutputStream bos = new ByteArrayOutputStream(); new JsonRestResponseHandler().serialize(entry, bos); ClientResponse clientResponse = doRequest(context, connectionString, new HashMap<>(), bos.toString(CHARACTER_ENCODING), REQUEST_MODE.POST); try { entry = new JsonRestResponseHandler().deserialize(IndexEntry.class, clientResponse.getEntityInputStream()); } finally { clientResponse.close(); } return IndexModelMapper.map(entry); } catch(RestException e) { throw new RemoteException("Unable to parse remote index definition", e); } catch(UnsupportedEncodingException e) { throw new RemoteException("Unable to serialize entity", e); } }
@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); } }