public static void main(String[] args) throws Exception {
    ClientResource resource = new ClientResource("http://localhost:8111/");

    // Get a representation
    Representation rep = resource.get();
    System.out.println(resource.getStatus());

    // Get an updated representation, if modified
    resource.getConditions().setModifiedSince(rep.getModificationDate());
    rep = resource.get();
    System.out.println(resource.getStatus());

    // Get an updated representation, if tag changed
    resource.getConditions().setModifiedSince(null);
    resource.getConditions().getNoneMatch().add(new Tag("xyz123"));
    rep = resource.get();
    System.out.println(resource.getStatus());

    // Put a new representation if tag has not changed
    resource.getConditions().getNoneMatch().clear();
    resource.getConditions().getMatch().add(rep.getTag());
    resource.put(rep);
    System.out.println(resource.getStatus());

    // Put a new representation when a different tag
    resource.getConditions().getMatch().clear();
    resource.getConditions().getMatch().add(new Tag("abcd7890"));
    resource.put(rep);
    System.out.println(resource.getStatus());
  }
  /**
   * Assert if the FileOrdered have been copied It gets the list of files from the order, and gets
   * the file described in the list of files
   *
   * @param order the order to assert
   * @param nbFileCopied the expected number of files to be copied
   */
  private void assertFileOrderedExists(Order order, int nbFileCopied) {
    assertNotNull(order);
    assertNotNull(order.getResourceCollection());

    assertEquals(1, order.getResourceCollection().size());
    Representation result = null;
    try {

      String res = order.getResourceCollection().get(0);

      ClientResource cr = new ClientResource(res);

      ChallengeResponse chal =
          new ChallengeResponse(ChallengeScheme.HTTP_BASIC, userLogin, password);

      cr.setChallengeResponse(chal);

      result = cr.get(getMediaTest());

      assertNotNull(result);
      assertTrue(cr.getStatus().isSuccess());

      try {
        // get the list of files
        String text = result.getText();
        String[] contents = text.split("\r\n");
        assertNotNull(contents);
        assertEquals(nbFileCopied, contents.length);
        Reference content = new Reference(contents[0]);
        // asserts
        assertNotNull(content);
        assertNotSame("", content.toString());
        // get the file corresponding to the first url, check that this file
        // exists
        cr = new ClientResource(content);

        chal = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, userLogin, password);

        cr.setChallengeResponse(chal);

        result = cr.get(getMediaTest());
        assertNotNull(result);
        assertTrue(cr.getStatus().isSuccess());

      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } finally {
        RIAPUtils.exhaust(result);
      }

    } finally {
      RIAPUtils.exhaust(result);
    }
  }
  /**
   * Delete the given Order
   *
   * @param order the order to delete
   */
  protected void deleteOrder(Order order) {
    // TODO Auto-generated method stub
    String url = getOrderUrl() + "/" + order.getId();

    ClientResource cr = new ClientResource(url);
    ChallengeResponse chal = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, userLogin, password);

    cr.setChallengeResponse(chal);

    Representation result = cr.delete(getMediaTest());
    assertNotNull(result);
    assertTrue(cr.getStatus().isSuccess());
    Response response = getResponseOrderandUserstorage(getMediaTest(), result, Order.class);
    assertTrue(response.getSuccess());
    result.release();
  }
  /**
   * Get the Order at the given url
   *
   * @param url the url where to find the Order
   * @return the Order found
   */
  protected Order getOrder(String url) {

    ClientResource cr = new ClientResource(url);

    ChallengeResponse chal = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, userLogin, password);

    cr.setChallengeResponse(chal);

    Representation result = cr.get(getMediaTest());
    assertNotNull(result);
    assertTrue(cr.getStatus().isSuccess());
    Response response = getResponseOrderandUserstorage(getMediaTest(), result, Order.class);
    assertTrue(response.getSuccess());
    result.release();
    return (Order) response.getItem();
  }
  /** Assert if there are no Orders */
  protected void assertNoneOrder() {

    ClientResource cr = new ClientResource(getOrderUrl());
    ChallengeResponse chal = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, userLogin, password);

    cr.setChallengeResponse(chal);

    Representation result = cr.get(getMediaTest());

    assertNotNull(result);
    assertTrue(cr.getStatus().isSuccess());

    Response response = getResponseOrderandUserstorage(getMediaTest(), result, Order.class, true);
    assertTrue(response.getSuccess());
    assertEquals(0, response.getTotal().intValue());
  }
  /**
   * Invoke POST
   *
   * @param item DataSet
   */
  public void create(DataSet item) {
    Representation rep =
        GetRepresentationUtils.getRepresentationDataset(item, MediaType.APPLICATION_JSON);
    ClientResource cr = new ClientResource(getBaseUrl());
    Representation result = cr.post(rep, MediaType.APPLICATION_JSON);
    assertTrue(cr.getStatus().isSuccess());
    assertNotNull(result);

    Response response = getResponse(MediaType.APPLICATION_JSON, result, DataSet.class);
    assertTrue(response.getSuccess());
    assertNotNull(response.getItem());

    DataSet rs = (DataSet) response.getItem();
    assertEquals(rs, item);
    RIAPUtils.exhaust(result);
    cr.release();
  }
  /**
   * Invoke an order and return the status returned
   *
   * @param urlAttach2 the url attachment of the resource
   * @return the TaskModel returned representing the status of the Task resource
   */
  protected TaskModel invokeOrder(String urlAttach2, String parameters) {
    String url = getHostUrl() + PROJECT_URL + urlAttach2 + parameters;
    Representation result = null;
    try {
      ClientResource cr = new ClientResource(url);
      ChallengeResponse chal =
          new ChallengeResponse(ChallengeScheme.HTTP_BASIC, userLogin, password);
      cr.setChallengeResponse(chal);
      result = cr.post(null, getMediaTest());
      assertNotNull(result);
      assertTrue(cr.getStatus().isSuccess());
      Response response = getResponse(getMediaTest(), result, TaskModel.class);
      assertTrue(response.getSuccess());

      return (TaskModel) response.getItem();
    } finally {
      RIAPUtils.exhaust(result);
    }
  }
  private String getServicesDescription(String parentApplicationUrl) {
    String url = parentApplicationUrl + "/services";
    System.out.println(url);
    Representation result = null;
    try {
      ClientResource cr = new ClientResource(url);
      result = cr.get(getMediaTest());
      assertNotNull(result);
      assertTrue(cr.getStatus().isSuccess());
      Response response = getResponse(getMediaTest(), result, ResourceModelDTO.class, true);
      assertTrue(response.getSuccess());
      assertNotNull(response.getData());
      assertEquals(1, response.getData().size());

      ResourceModelDTO resDTO = (ResourceModelDTO) response.getData().get(0);
      List<ResourceParameter> parameters = resDTO.getParameters();
      boolean found = false;
      String fileName = null;
      for (ResourceParameter resourceParameter : parameters) {
        if (resourceParameter.getName().equals("fileName")) {
          found = true;
          assertEquals("xs:template", resourceParameter.getValueType());
          String value = resourceParameter.getValue();
          assertFalse(value.contains("${date:"));
          fileName = resourceParameter.getValue();
          break;
        }
      }
      if (!found) {
        fail("Parameter fileName not found");
      }
      return fileName;
    } finally {
      RIAPUtils.exhaust(result);
    }
  }
  /**
   * Invoke an order and assert that the it is successful, that the {@link MediaType} of the result
   * is expectedMediaType and that its name if expectedFileName
   *
   * @param urlAttach2 the url attachment of the resource
   * @param parameters the parameters to add to the request
   * @param expectedMediaType the expected {@link MediaType} in return
   * @param expectedFileName the expected file name in return
   */
  protected void invokeOrderGet(
      String urlAttach2, String parameters, MediaType expectedMediaType, String expectedFileName) {
    String url = getHostUrl() + PROJECT_URL + urlAttach2 + parameters;
    Representation result = null;
    try {
      ClientResource cr = new ClientResource(url);
      ChallengeResponse chal =
          new ChallengeResponse(ChallengeScheme.HTTP_BASIC, userLogin, password);
      cr.setChallengeResponse(chal);
      result = cr.get();
      assertNotNull(result);
      assertTrue(cr.getStatus().isSuccess());

      assertEquals(expectedMediaType, result.getMediaType());

      assertNotNull(result.getDisposition());
      String fileName = result.getDisposition().getFilename();
      assertNotNull(fileName);
      assertEquals(expectedFileName, fileName);

    } finally {
      RIAPUtils.exhaust(result);
    }
  }