Exemple #1
0
  protected void setHeaders(Invocation.Builder invocationBuilder, String token) {
    invocationBuilder.header(Headers.WOR_API_KEY_TOKEN, token);

    String clientId = Utils.getConfigValue(ConfigurationKey.URI_PREFIX);
    if (!StringUtils.isEmpty(clientId)) {
      invocationBuilder.header(Headers.API_KEY_CLIENT_ID, clientId);
    }
  }
 /**
  * Share header settings for invocations.
  *
  * @param webTarget target web
  * @return
  */
 private Response post(WebTarget webTarget) {
   Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
   if (null != authorization) {
     invocationBuilder.header(HEADER_AUTHORIZATION, authorization);
   }
   return invocationBuilder.post(Entity.text(""));
 }
  private <R> void populateHeaders(Invocation.Builder invocation, HttpRequest<R> request) {

    if (!request.hasHeaders()) return;

    for (Map.Entry<String, Object> h : request.getHeaders().entrySet()) {
      invocation.header(h.getKey(), h.getValue());
    }
  }
  @Test
  public void testShouldReturnTwoRegionNamesInEssex() throws OpenStackException {
    // given
    OpenStackRegionImpl openStackRegion = new OpenStackRegionImpl();
    Client client = mock(Client.class);
    openStackRegion.setClient(client);
    SystemPropertiesProvider systemPropertiesProvider = mock(SystemPropertiesProvider.class);
    openStackRegion.setSystemPropertiesProvider(systemPropertiesProvider);
    String token = "123456789";
    String responseError =
        "{\n"
            + "    \"error\": {\n"
            + "        \"message\": \"The action you have requested has not been implemented.\",\n"
            + "        \"code\": 501,\n"
            + "        \"title\": null\n"
            + "    }\n"
            + "}";

    String url = "http://domain.com/v2.0/tokens/" + token + "/endpoints";

    String urlEssex = "http://domain.com/v2.0/tokens";

    WebTarget webResource = mock(WebTarget.class);
    WebTarget webResourceEssex = mock(WebTarget.class);
    Invocation.Builder builder = mock(Invocation.Builder.class);
    Invocation.Builder builderEssex = mock(Invocation.Builder.class);

    Response clientResponse = mock(Response.class);
    Response clientResponseEssex = mock(Response.class);

    // when

    when(systemPropertiesProvider.getProperty(SystemPropertiesProvider.KEYSTONE_URL))
        .thenReturn("http://domain.com/v2.0/");
    when(client.target(url)).thenReturn(webResource);
    when(webResource.request(MediaType.APPLICATION_JSON)).thenReturn(builder);
    when(builder.get()).thenReturn(clientResponse);
    when(clientResponse.getStatus()).thenReturn(501);
    when(clientResponse.readEntity(String.class)).thenReturn(responseError);

    when(client.target(urlEssex)).thenReturn(webResourceEssex);
    when(webResourceEssex.request(MediaType.APPLICATION_JSON)).thenReturn(builderEssex);
    when(builderEssex.accept(MediaType.APPLICATION_JSON)).thenReturn(builderEssex);
    when(builderEssex.header("Content-type", MediaType.APPLICATION_JSON)).thenReturn(builderEssex);
    when(builderEssex.post(Entity.entity(anyString(), MediaType.APPLICATION_JSON)))
        .thenReturn(clientResponseEssex);
    when(clientResponseEssex.getStatus()).thenReturn(200);
    when(clientResponseEssex.readEntity(String.class)).thenReturn(RESPONSE_ESSEX);

    List<String> result = openStackRegion.getRegionNames(token);

    // then
    assertNotNull(result);
    assertEquals(2, result.size());
    assertEquals("RegionOne", result.get(0));
    assertEquals("RegionTwo", result.get(1));
  }
  /**
   * Get a ClientRequest.Builder with common properties already set.
   *
   * @param headers
   * @return
   */
  private Invocation.Builder getCommonRequestBuilder(
      String sessionToken, Invocation.Builder builder, Map<String, Object> headers) {
    if (headers == null) {
      headers = new HashMap<String, Object>();
    }

    headers.put("Authorization", String.format("Bearer %s", sessionToken));

    for (Map.Entry<String, Object> entry : headers.entrySet()) {
      builder.header(entry.getKey(), entry.getValue());
    }

    return builder;
  }
  private <C> C getAt(String uri, String sessionKey, MediaType mediaType, Class<C> c) {
    String thePath = uri;
    String queryParams = "";
    WebTarget resource = resource();
    if (thePath.contains("?")) {
      String[] pathParts = thePath.split("\\?");
      thePath = pathParts[0];
      queryParams = pathParts[1];
    }

    Invocation.Builder requestBuilder =
        applyQueryParameters(queryParams, resource.path(thePath)).request(mediaType);
    if (StringUtil.isDefined(sessionKey)) {
      requestBuilder = requestBuilder.header(HTTP_SESSIONKEY, sessionKey);
    }
    return requestBuilder.get(c);
  }
 @SuppressWarnings("unchecked")
 private <E> E put(final E entity, String atURI, String withSessionKey) {
   String thePath = atURI;
   String queryParams = "";
   WebTarget resource = resource();
   if (thePath.contains("?")) {
     String[] pathParts = thePath.split("\\?");
     thePath = pathParts[0];
     queryParams = pathParts[1];
   }
   Invocation.Builder resourcePutter =
       applyQueryParameters(queryParams, resource.path(thePath))
           .request(MediaType.APPLICATION_JSON);
   if (isDefined(withSessionKey)) {
     resourcePutter = resourcePutter.header(HTTP_SESSIONKEY, withSessionKey);
   }
   Class<E> c = (Class<E>) entity.getClass();
   return resourcePutter.put(Entity.entity(entity, MediaType.APPLICATION_JSON), c);
 }
  private Response getAPIResponse(
      String path,
      String method,
      List<Pair> queryParams,
      Object body,
      byte[] binaryBody,
      Map<String, String> headerParams,
      Map<String, Object> formParams,
      String accept,
      String contentType,
      String[] authNames)
      throws ApiException {

    if (body != null && binaryBody != null) {
      throw new ApiException(500, "either body or binaryBody must be null");
    }

    Client client = getClient();
    Response response = null;

    StringBuilder b = new StringBuilder();
    b.append("?");
    if (queryParams != null) {
      for (Pair queryParam : queryParams) {
        if (!queryParam.getName().isEmpty()) {
          b.append(escapeString(queryParam.getName()));
          b.append("=");
          b.append(escapeString(queryParam.getValue()));
          b.append("&");
        }
      }
    }

    String querystring = b.substring(0, b.length() - 1);

    WebTarget target = client.target(basePath + path + querystring);

    if (debugging) {
      target.register(new LoggingFilter());
    }

    if (authentication != null) {
      authentication.setFilter(target);
    }

    Invocation.Builder builder;
    if (accept != null) {
      builder = target.request(accept);
    } else {
      builder = target.request();
    }

    for (Map.Entry<String, String> entry : headerParams.entrySet()) {
      builder = builder.header(entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, String> entry : defaultHeaderMap.entrySet()) {
      if (!headerParams.containsKey(entry.getKey())) {
        builder = builder.header(entry.getKey(), entry.getValue());
      }
    }

    if ("GET".equals(method)) {
      response = builder.get();
    } else if ("POST".equals(method)) {
      response = builder.post(Entity.entity(body, contentType));
    } else if ("PUT".equals(method)) {
      if (body != null) {
        response = builder.put(Entity.entity(body, contentType));
      } else {
        response = builder.put(Entity.text(""));
      }
    } else if ("DELETE".equals(method)) {
      response = builder.delete();
    } else {
      throw new ApiException(500, "unknown method type " + method);
    }

    return response;
  }