protected T get(URI uri) {
   ClientResponse response =
       ClientUtil.readEntity(
           uri, getHttpClient(), getResourceRepresentationType(), ClientResponse.class);
   if (logger.isDebugEnabled()) {
     logger.debug("Request Accept header: " + getResourceRepresentationType());
     logger.debug("Response header: " + response.getType());
   }
   final int status = response.getStatus();
   if (followRedirectionEnabled
       && status == ClientResponse.Status.MOVED_PERMANENTLY.getStatusCode()) {
     final URI location = response.getLocation();
     if (location != null) {
       this.thisResourceUri = location;
       this.absoluteThisResourceUri = generateAbsoluteUri();
       return get();
     }
   }
   if (followRedirectionEnabled
       && (status == ClientResponse.Status.FOUND.getStatusCode()
           || status == ClientResponse.Status.SEE_OTHER.getStatusCode())) {
     final URI location = response.getLocation();
     if (location != null) {
       URI absolutionLocation = getHttpClient().getAbsoluteUri(location, getReferrerUri());
       return get(absolutionLocation);
     }
   }
   if (status < 300 || (status == ClientResponse.Status.NOT_MODIFIED.getStatusCode())) {
     if (response.hasEntity()
         && response.getStatus() != ClientResponse.Status.NO_CONTENT.getStatusCode()) {
       lastReadStateOfEntity = response.getEntity(getEntityClass());
       if (getClientUtil() != null) {
         try {
           getClientUtil().parseLinks(lastReadStateOfEntity, getRelatedResourceUris());
         } catch (Exception ex) {
           logger.warn(ex.getMessage(), ex);
         }
       }
       if (invokeGet) {
         invokeGETOnNestedResources();
       }
     } else {
       lastReadStateOfEntity = null;
     }
     getInvocationCount++;
     Map<String, Object> headers = new HashMap<String, Object>();
     EntityTag tag = response.getEntityTag();
     if (tag != null) {
       headers.put(HttpHeaders.ETAG, tag);
     }
     Date date = response.getLastModified();
     if (date != null) {
       headers.put(HttpHeaders.LAST_MODIFIED, date);
     }
     cachedHeaders.put(uri.toString(), headers);
     return lastReadStateOfEntity;
   }
   throw new UniformInterfaceException(response);
 }
示例#2
0
  @SuppressWarnings({"deprecation", "rawtypes"})
  @Test
  public void testCreateGetUpdateAndDeleteKeyword() throws InterruptedException, JSONException {
    String accessToken =
        getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri);
    assertNotNull(accessToken);

    Address address = new Address();
    address.setCountry(new Country(Iso3166Country.CR));
    address.setVisibility(Visibility.PUBLIC);
    // Create
    ClientResponse response = memberV2ApiClient.createAddress(user1OrcidId, address, accessToken);
    assertNotNull(response);
    assertEquals(ClientResponse.Status.CREATED.getStatusCode(), response.getStatus());
    Map map = response.getMetadata();
    assertNotNull(map);
    assertTrue(map.containsKey("Location"));
    List resultWithPutCode = (List) map.get("Location");
    String location = resultWithPutCode.get(0).toString();
    Long putCode = Long.valueOf(location.substring(location.lastIndexOf('/') + 1));

    // Get all and verify
    response = memberV2ApiClient.viewAddresses(user1OrcidId, accessToken);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    Addresses addresses = response.getEntity(Addresses.class);
    assertNotNull(addresses);
    assertNotNull(addresses.getAddress());
    assertEquals(2, addresses.getAddress().size());

    boolean foundUS = false;
    boolean foundCR = false;

    for (Address add : addresses.getAddress()) {
      assertEquals(Visibility.PUBLIC, add.getVisibility());
      assertNotNull(add.getCountry());
      assertNotNull(add.getCountry().getValue());
      if (add.getCountry().getValue().equals(Iso3166Country.US)) {
        foundUS = true;
      } else if (add.getCountry().getValue().equals(Iso3166Country.CR)) {
        foundCR = true;
      }
    }

    assertTrue(foundUS);
    assertTrue(foundCR);

    // Get it
    response = memberV2ApiClient.viewAddress(user1OrcidId, putCode, accessToken);
    assertNotNull(response);
    address = response.getEntity(Address.class);
    assertNotNull(address);
    assertNotNull(address.getSource());
    assertEquals(client1ClientId, address.getSource().retrieveSourcePath());
    assertNotNull(address.getCountry());
    assertNotNull(address.getCountry().getValue());
    assertEquals(Iso3166Country.CR, address.getCountry().getValue());
    assertEquals(Visibility.PUBLIC, address.getVisibility());

    // Update
    address.getCountry().setValue(Iso3166Country.PA);
    response = memberV2ApiClient.updateAddress(user1OrcidId, address, accessToken);
    assertNotNull(response);
    assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus());
    response = memberV2ApiClient.viewAddress(user1OrcidId, putCode, accessToken);
    assertNotNull(response);
    Address updatedAddress = response.getEntity(Address.class);
    assertNotNull(updatedAddress);
    assertNotNull(updatedAddress.getCountry());
    assertEquals(Iso3166Country.PA, updatedAddress.getCountry().getValue());
    assertEquals(address.getPutCode(), updatedAddress.getPutCode());

    // Delete
    response = memberV2ApiClient.deleteAddress(user1OrcidId, putCode, accessToken);
    assertNotNull(response);
    assertEquals(ClientResponse.Status.NO_CONTENT.getStatusCode(), response.getStatus());

    // Check it was deleted
    testGetAddressWithMembersAPI();
  }