public RESTServiceConnector build() { return new RESTServiceConnector(this); } }
private RESTServiceConnector(final Builder builder) { client = builder.client; gson = setGsonDeserializer(builder.classToDeserializerMap); }
@SuppressWarnings("rawtypes") public static Builder create() { return new Builder(); }
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); }
@BeforeClass public static void setupClass() throws Exception { request = HttpUriRequestBuilder.create() .method(HttpMethods.GET) .path("/path") .build(); httpClient = spy(HttpClientHelper.createHttpClient(2)); }
@Test(expected = CloudstackRESTException.class) public void testExecuteRequestWhenClientThrowsIOException() throws Exception { final BasicRestClient restClient = BasicRestClient.create() .host(LOCALHOST) .client(HttpClientHelper.createHttpClient(5)) .build(); final CloseableHttpResponse response = restClient.execute(request); fail("A CloudstackRESTException should have been thrown by now."+ "\nWe got " + response.getStatusLine() + "\nMake sure you cannot reach '" + request.getURI() + "' by method " + request.getMethod()); }
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()); } }
/** * 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()); } }
@Override public boolean matches(final Object actual) { if (actual instanceof HttpUriRequest) { final HttpUriRequest converted = (HttpUriRequest) actual; return checkMethod(converted) && checkUri(converted) && checkPayload(converted); } else { return wanted == actual; } }
protected NiciraNvpApi createNiciraNvpApi(final String host, final String username, final String password) throws CloudstackRESTException { try { return NiciraNvpApi.create().host(host).username(username).password(password).httpClient(HttpClientHelper.createHttpClient(MAX_REDIRECTS)).build(); } catch (final KeyManagementException e) { throw new CloudstackRESTException("Could not create HTTP client", e); } catch (final NoSuchAlgorithmException e) { throw new CloudstackRESTException("Could not create HTTP client", e); } catch (final KeyStoreException e) { throw new CloudstackRESTException("Could not create HTTP client", e); } }
public BasicRestClient build() { return new BasicRestClient(this); } }
public static Builder create() { return new Builder(); }
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); }
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)); }
/** * 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()); } }
@Override public CloseableHttpResponse execute(final HttpUriRequest request) throws CloudstackRESTException { logRequestExecution(request); try { return client.execute(clientContext.getTargetHost(), request, clientContext); } catch (final IOException e) { throw new CloudstackRESTException("Could not execute request " + request, e); } }
private boolean checkPayload(final HttpUriRequest actual) { final String wantedPayload = getPayload(wanted); final String actualPayload = getPayload(actual); return equalsString(wantedPayload, actualPayload); }
public static HttpUriRequestBuilder create() { return new HttpUriRequestBuilder(); }
private boolean checkMethod(final HttpUriRequest actual) { if (wanted instanceof HttpUriRequest) { final String wantedMethod = ((HttpUriRequest) wanted).getMethod(); final String actualMethod = actual.getMethod(); return equalsString(wantedMethod, actualMethod); } else { return wanted == actual; } }