Exemple #1
0
  protected boolean exists(final Location location, final String uri) {
    try {
      final ClientResponse response = getNexusClient().uri(uri).head();
      if (!ClientResponse.Status.OK.equals(response.getClientResponseStatus())) {
        if (ClientResponse.Status.NOT_FOUND.equals(response.getClientResponseStatus())) {
          return false;
        }

        throw getNexusClient()
            .convert(
                new ContextAwareUniformInterfaceException(response) {
                  @Override
                  public String getMessage(final int status) {
                    if (status == Response.Status.NOT_FOUND.getStatusCode()) {
                      return String.format("Inexistent path: %s", location);
                    }
                    return null;
                  }
                });
      }
      return true;
    } catch (ClientHandlerException e) {
      throw getNexusClient().convert(e);
    }
  }
  /**
   * Deletes the Snapshot with the given id.( It is asynchronous operation )
   *
   * @param snapshotId
   */
  public void deleteSnapshot(String snapshotId) {

    _log.info("CinderApi - start deleteSnapshot");

    String deleteSnapshotUri =
        endPoint.getBaseUri()
            + String.format(
                CinderConstants.URI_DELETE_SNAPSHOT,
                new Object[] {endPoint.getCinderTenantId(), snapshotId});

    ClientResponse deleteResponse = getClient().delete(URI.create(deleteSnapshotUri));

    String s = deleteResponse.getEntity(String.class);
    _log.debug("Got the response {}", s);

    if (deleteResponse.getStatus() == ClientResponse.Status.NOT_FOUND.getStatusCode()) {
      throw CinderException.exceptions.snapshotNotFound(snapshotId);
    }

    if (deleteResponse.getStatus() != ClientResponse.Status.ACCEPTED.getStatusCode()) {
      throw CinderException.exceptions.snapshotDeleteFailed(s);
    }

    _log.info("CinderApi - end deleteSnapshot");
  }
  @Test
  public void testInvalidPutCodeReturns404() 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);
    address.setPutCode(1234567890L);

    ClientResponse response = memberV2ApiClient.updateAddress(user1OrcidId, address, accessToken);
    assertNotNull(response);
    assertEquals(ClientResponse.Status.NOT_FOUND.getStatusCode(), response.getStatus());
  }
  @Test
  public void testInvalidPutCodeReturns404() throws InterruptedException, JSONException {
    String accessToken =
        getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri);
    assertNotNull(accessToken);

    ResearcherUrl rUrlToCreate = new ResearcherUrl();
    rUrlToCreate.setUrl(new Url("http://newurl.com/" + System.currentTimeMillis()));
    rUrlToCreate.setUrlName("url-name-" + System.currentTimeMillis());
    rUrlToCreate.setVisibility(Visibility.PUBLIC);
    rUrlToCreate.setPutCode(1234567890L);

    ClientResponse response =
        memberV2ApiClient.updateResearcherUrls(user1OrcidId, rUrlToCreate, accessToken);
    assertNotNull(response);
    assertEquals(ClientResponse.Status.NOT_FOUND.getStatusCode(), response.getStatus());
  }
  /**
   * Gets the Snapshot information. It is a synchronous operation.
   *
   * @param snapshotId
   * @return
   */
  public SnapshotCreateResponse showSnapshot(String snapshotId) throws Exception {
    _log.info("CinderApi - start showSnapshot");
    String showSnapshotUri =
        endPoint.getBaseUri()
            + String.format(
                CinderConstants.URI_DELETE_SNAPSHOT,
                new Object[] {endPoint.getCinderTenantId(), snapshotId});
    ClientResponse js_response = getClient().get(URI.create(showSnapshotUri));
    String jsonString = js_response.getEntity(String.class);

    if (js_response.getStatus() == ClientResponse.Status.NOT_FOUND.getStatusCode()) {
      throw CinderException.exceptions.snapshotNotFound(snapshotId);
    }

    SnapshotCreateResponse snapshotDetails =
        new Gson().fromJson(jsonString, SnapshotCreateResponse.class);
    _log.info("CinderApi - end showSnapshot");
    return snapshotDetails;
  }
  /**
   * Gets the volume information. Its a synchronous operation.
   *
   * @param volumeId
   * @return
   */
  public VolumeShowResponse showVolume(String volumeId) throws Exception {
    _log.info("CinderApi - start showVolume");

    String showVolumeUri =
        endPoint.getBaseUri()
            + String.format(
                CinderConstants.URI_DELETE_VOLUME,
                new Object[] {endPoint.getCinderTenantId(), volumeId});
    ClientResponse js_response = getClient().get(URI.create(showVolumeUri));

    _log.debug(
        "uri {} : Response status {}", showVolumeUri, String.valueOf(js_response.getStatus()));
    if (js_response.getStatus() == ClientResponse.Status.NOT_FOUND.getStatusCode()) {
      throw CinderException.exceptions.volumeNotFound(volumeId);
    }

    String jsonString = js_response.getEntity(String.class);
    VolumeShowResponse volumeDetails = new Gson().fromJson(jsonString, VolumeShowResponse.class);

    _log.info("CinderApi - end showVolume");
    return volumeDetails;
  }
  /**
   * Detaches the specified volume from the specified initiator. It is an asynchronous operation.
   *
   * @param volumeId the volume id
   * @param initiator the initiator
   * @param host the host
   * @return the volume attachment response
   * @throws Exception
   */
  public void detachVolume(String volumeId, String initiator, String[] wwpns, String host)
      throws Exception {
    _log.info("CinderApi - start detachVolume");
    Gson gson = new Gson();

    VolumeDetachRequest volumeDetach = new VolumeDetachRequest();
    if (initiator != null) {
      volumeDetach.terminateConnection.connector.initiator = initiator;
    } else if (wwpns != null) {
      volumeDetach.terminateConnection.connector.wwpns = Arrays.copyOf(wwpns, wwpns.length);
    }
    volumeDetach.terminateConnection.connector.host = host;

    String volumeDetachmentUri =
        endPoint.getBaseUri()
            + String.format(
                CinderConstants.URI_DETACH_VOLUME,
                new Object[] {endPoint.getCinderTenantId(), volumeId});

    _log.debug("detaching volume from initiator with uri {}", volumeDetachmentUri);
    String json = gson.toJson(volumeDetach);
    _log.info("detaching volume with body {}", json);
    ClientResponse js_response = getClient().postWithHeader(URI.create(volumeDetachmentUri), json);
    String s = js_response.getEntity(String.class);
    _log.debug("Got the response {}", s);
    _log.debug("Response status {}", String.valueOf(js_response.getStatus()));

    if (js_response.getStatus() != ClientResponse.Status.ACCEPTED.getStatusCode()) {
      // if volume is not found on cinder, mark it as passed.
      if (js_response.getStatus() == ClientResponse.Status.NOT_FOUND.getStatusCode()) {
        _log.info(
            "Volume {} is not found on Cinder. It could have been deleted manually.", volumeId);
      } else {
        throw CinderException.exceptions.volumeDetachFailed(s);
      }
    }
    _log.info("CinderApi - end detachVolume");
  }
 private void throwIf404(final ClientResponse response) {
   if (ClientResponse.Status.NOT_FOUND.equals(response.getClientResponseStatus())) {
     throw new NexusClientNotFoundException(
         response.getClientResponseStatus().getReasonPhrase(), getResponseBody(response));
   }
 }