public <T> T executeCreateObject(final T newObject, final String uri) throws CloudstackRESTException { return executeCreateObject(newObject, uri, new HashMap<String, String>()); }
public <T> T executeRetrieveObject(final Type returnObjectType, final String path) throws CloudstackRESTException { return executeRetrieveObject(returnObjectType, path, new HashMap<String, String>()); }
public <T> void executeUpdateObject(final T newObject, final String path) throws CloudstackRESTException { executeUpdateObject(newObject, path, new HashMap<String, String>()); }
public <T> T executeRetrieveObject(final Type returnObjectType, final String path, final Map<String, String> parameters) throws CloudstackRESTException { s_logger.debug("Executing retrieve object on " + path); final CloseableHttpResponse response = createAndExecuteRequest(HttpMethods.GET, path, parameters, ABSENT); return readResponseBody(response, returnObjectType); }
/** * DELETE Security Profile given a UUID as key * * @param securityProfileUuid * @throws NiciraNvpApiException */ private void deleteWithUri(final String uri) throws NiciraNvpApiException { try { restConnector.executeDeleteObject(uri); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
public <T> void executeUpdateObject(final T newObject, final String path, final Map<String, String> parameters) throws CloudstackRESTException { s_logger.debug("Executing update object on " + path); client.closeResponse(createAndExecuteRequest(HttpMethods.PUT, path, parameters, Optional.fromNullable(gson.toJson(newObject)))); }
private RESTServiceConnector(final Builder builder) { client = builder.client; gson = setGsonDeserializer(builder.classToDeserializerMap); }
private CloseableHttpResponse createAndExecuteRequest(final HttpMethods method, final String path, final Map<String, String> parameters, final Optional<String> jsonPayLoad) throws CloudstackRESTException { final HttpUriRequest httpRequest = HttpUriRequestBuilder.create() .path(path) .parameters(parameters) .jsonPayload(jsonPayLoad) .method(method) .build(); if (jsonPayLoad.isPresent()) { s_logger.debug("Built request '" + httpRequest + "' with payload: " + jsonPayLoad); } return executeRequest(httpRequest); }
public RESTServiceConnector build() { return new RESTServiceConnector(this); } }
@SuppressWarnings("unchecked") public <T> T executeCreateObject(final T newObject, final String path, final Map<String, String> parameters) throws CloudstackRESTException { s_logger.debug("Executing create object on " + path); final CloseableHttpResponse response = createAndExecuteRequest(HttpMethods.POST, path, parameters, Optional.fromNullable(gson.toJson(newObject))); return (T) readResponseBody(response, newObject.getClass()); }
@Test public void testExecuteDeleteObject() throws Exception { final HttpEntity entity = mock(HttpEntity.class); final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getEntity()).thenReturn(entity); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient).build(); connector.executeDeleteObject("/somepath"); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("DELETE"), any(HttpClientContext.class)); verify(response).close(); }
public void executeDeleteObject(final String path) throws CloudstackRESTException { s_logger.debug("Executing delete object on " + path); client.closeResponse(createAndExecuteRequest(HttpMethods.DELETE, path, new HashMap<String, String>(), ABSENT)); }
private RESTServiceConnector(final Builder builder) { client = builder.client; gson = setGsonDeserializer(builder.classToDeserializerMap); }
private CloseableHttpResponse createAndExecuteRequest(final HttpMethods method, final String path, final Map<String, String> parameters, final Optional<String> jsonPayLoad) throws CosmicRESTException { final HttpUriRequest httpRequest = HttpUriRequestBuilder.create() .path(path) .parameters(parameters) .jsonPayload(jsonPayLoad) .method(method) .build(); if (jsonPayLoad.isPresent()) { s_logger.debug("Built request '" + httpRequest + "' with payload: " + jsonPayLoad); } return executeRequest(httpRequest); }
public RESTServiceConnector build() { return new RESTServiceConnector(this); } }
public ControlClusterStatus getControlClusterStatus() throws NiciraNvpApiException { final String uri = NiciraConstants.CONTROL_CLUSTER_STATUS_URL; try { return restConnector.executeRetrieveObject(ControlClusterStatus.class, uri, new HashMap<String, String>()); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
/** * POST * * @param entity * @return * @throws NiciraNvpApiException */ private <T> T createWithUri(final T entity, final String uri) throws NiciraNvpApiException { T createdEntity; try { createdEntity = restConnector.executeCreateObject(entity, uri, Collections.<String, String> emptyMap()); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } return createdEntity; }
/** * PUT item given a UUID as key and an item object with the new data * * @param item * @param uuid * @throws NiciraNvpApiException */ private <T> void updateWithUri(final T item, final String uri) throws NiciraNvpApiException { try { restConnector.executeUpdateObject(item, uri, Collections.<String, String> emptyMap()); } catch (final CloudstackRESTException e) { throw new NiciraNvpApiException(e, e.getErrorCode()); } }
public <T> T executeRetrieveObject(final Type returnObjectType, final String path, final Map<String, String> parameters) throws CosmicRESTException { s_logger.debug("Executing retrieve object on " + path); final CloseableHttpResponse response = createAndExecuteRequest(HttpMethods.GET, path, parameters, ABSENT); return readResponseBody(response, returnObjectType); }
@Test public void testExecuteDeleteObject() throws Exception { final HttpEntity entity = mock(HttpEntity.class); final CloseableHttpResponse response = mock(CloseableHttpResponse.class); when(response.getEntity()).thenReturn(entity); when(response.getStatusLine()).thenReturn(HTTP_200_STATUS_LINE); final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpClientContext.class))).thenReturn(response); final RestClient restClient = new BasicRestClient(httpClient, HttpClientContext.create(), "localhost"); final RESTServiceConnector connector = new RESTServiceConnector.Builder().client(restClient).build(); connector.executeDeleteObject("/somepath"); verify(httpClient).execute(any(HttpHost.class), HttpUriRequestMethodMatcher.aMethod("DELETE"), any(HttpClientContext.class)); verify(response).close(); }