protected byte[] toBytes(RestEntity entity) throws RestException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); new JsonRestResponseHandler().serialize(entity, bos); return bos.toByteArray(); }
@Override public <T extends RestEntity> T deserialize(Class<T> dataType, InputStream inputStream) throws RestException { return JsonRestHandlerFactory.getRestResponseHandler(dataType).deserialize(dataType, inputStream); }
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); } }
private QueryResult parseAsEntityCollection(ClientResponse response) throws RemoteException { try { StreamableEntityCollection entityCollection = new JsonRestResponseHandler().deserialize(StreamableEntityCollection.class, new WrappedInputStream(response)); return entityCollection.getResult(); } catch(RestException e) { throw new RemoteException("Unable to parse remote entity data", e); } }
@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()); } }
@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 List<String> getUsers(RemotingContext context) throws RemoteException { ClientResponse clientResponse = doRequest(context, "Users"); try { RestUserList userList = new JsonRestResponseHandler().deserialize(RestUserList.class, clientResponse.getEntityInputStream()); List<String> userNames = new ArrayList<>(); for(RestUser restUser : userList.getUsers()) { userNames.add(restUser.getUsername()); } return userNames; } catch(RestException e) { throw new RemoteException("Unable to retrieve remote user list", e); } }
@Override public <T extends RestEntity> T deserialize(Class<T> dataType, String data) throws RestException { return JsonRestHandlerFactory.getRestResponseHandler(dataType).deserialize(dataType, data); }
@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 Instance getInstance(RemotingContext context, String instanceId) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instanceId).getConnectionString(); try { ClientResponse clientResponse = doRequest(context, connectionString); try { return new JsonRestResponseHandler().deserialize(InstanceRest.class, clientResponse.getEntityInputStream()); } catch(RestException e) { throw new RemoteException("Unable to parse remote instance data", e); } finally { clientResponse.close(); } } catch(ResourceNotFoundException e) { throw new RemoteException("Unable to find instance: " + instanceId); } }
@Override public void serialize(RestEntity data, OutputStream outputStream) throws RestException { JsonRestHandlerFactory.getRestResponseHandler(data.getClass()).serialize(data, outputStream); }
@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); } }
@Override public UserSession loadSession(String userName, String password) throws RemoteException { Map<String, String> parameters = Maps.newHashMap(); parameters.put("client_id", userName); parameters.put("client_secret", password); ClientResponse response = doRequest(new RemotingContext(true), "token", parameters, "", REQUEST_MODE.POST); try { OauthToken token = new JsonRestResponseHandler().deserialize(OauthToken.class, response.getEntityInputStream()); return new UserSessionImpl(token.getSessionId(), token.getOauthToken(), null, null); } catch(RestException e) { throw new RemoteException("Unable to retrieve authentication token", e); } finally { response.close(); } } }
@Override public List<Instance> getInstances(RemotingContext context) throws RemoteException { String connectionString = new RestConnectionBuilder().instance().getConnectionString(); try { ClientResponse clientResponse = doRequest(context, connectionString); try { InstanceCollection instanceCollection = new JsonRestResponseHandler().deserialize(InstanceCollection.class, clientResponse.getEntityInputStream()); return new ArrayList<>(instanceCollection.getInstances()); } catch(RestException e) { throw new RemoteException("Unable to parse remote instance data", e); } finally { clientResponse.close(); } } catch(ResourceNotFoundException e) { throw new RemoteException("Unable to load instances", 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; } }
@Override public List<IndexDefinition> getIndexDefinitions(RemotingContext context, String instance, String bag) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instance).bag(bag).indexes().getConnectionString(); ClientResponse clientResponse = doRequest(context, connectionString); try { IndexCollection indexCollection = new JsonRestResponseHandler().deserialize(IndexCollection.class, clientResponse.getEntityInputStream()); List<IndexDefinition> indexDefinitions = new ArrayList<>(); for(IndexEntry entry : indexCollection.getIndexEntryList()) { indexDefinitions.add(IndexModelMapper.map(entry)); } return indexDefinitions; } catch(RestException e) { throw new RemoteException("Unable to parse remote index definitions", 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 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(); } }
private ClientResponse handleResponse(ClientResponse response) throws RemoteException { if(Range.closedOpen(200, 400).contains(response.getStatus())) { return response; } else if(Range.closedOpen(400, 500).contains(response.getStatus())) { String responseEntity = response.getEntityAsString(); if(LOG.isDebugEnabled()) { LOG.debug("Response: {}", responseEntity); } try { String message = ""; if(responseEntity != null) { ErrorEntity errorEntity = new JsonRestResponseHandler().deserialize(ErrorEntity.class, responseEntity); message = errorEntity.getMessage(); } if(response.getStatus() == HttpStatus.NOT_FOUND.value()) { throw new ResourceNotFoundException("No resource was found, " + message); } else { LOG.error("Raw response: {}", responseEntity); throw new RemoteException("Unable to execute remote operation: " + message + " statuscode: " + response.getStatus()); } } catch(RestException e) { String reason = HttpStatus.valueOf(response.getStatus()).getReasonPhrase(); throw new RemoteException("Unable to execute remote operation: " + response.getStatus() + "(" + reason + ")"); } } else { String responseEntity = response.getEntityAsString(); LOG.error("Remote server response with an error: {}", responseEntity); throw new RemoteException("Unable to execute remote operation: " + response.getStatus()); } }