/**
   * 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);
    }
  }
Пример #2
0
  public static void main(String[] args) throws Exception {
    // Create and configure HTTPS client
    Client client = new Client(new Context(), Protocol.HTTPS);
    Series<Parameter> parameters = client.getContext().getParameters();
    parameters.add("truststorePath", "certs/client-truststore.jks");
    parameters.add("truststorePassword", "password");
    parameters.add("truststoreType", "JKS");

    // Create and configure client resource
    ClientResource clientResource =
        new ClientResource("https://localhost:8183/accounts/chunkylover53/mails/123");
    clientResource.setNext(client);

    // Preemptively configure the authentication credentials
    ChallengeResponse authentication =
        new ChallengeResponse(ChallengeScheme.HTTP_BASIC, "chunkylover53", "pwd");
    clientResource.setChallengeResponse(authentication);

    // Communicate with remote resource
    MailResource mailClient = clientResource.wrap(MailResource.class);
    Mail m = mailClient.retrieve();
    System.out.println("Subject: " + m.getSubject());
    System.out.println("Content: " + m.getContent());

    // Store HTTPS client
    client.stop();
  }
  /**
   * Invoke GET
   *
   * @param uri String
   * @param params String
   * @param parameters Map<String, String>
   * @param uriTemplate String
   */
  public void retrieveDocAPI(
      String uri,
      String params,
      Map<String, String> parameters,
      String uriTemplate,
      ChallengeResponse challengeResponse) {
    ClientResource cr = new ClientResource(uri);
    cr.setChallengeResponse(challengeResponse);

    System.out.println("URI: " + uriTemplate);
    Representation result = cr.get(docAPI.getMediaTest());
    docAPI.appendSection("Format");
    // url type
    // request
    ClientResource crLocal = new ClientResource(uri);
    docAPI.appendRequest(Method.GET, crLocal);
    // parameters
    docAPI.appendParameters(parameters);
    docAPI.appendSection("Example");
    docAPI.appendRequest(Method.GET, cr);
    // response
    docAPI.appendResponse(result);

    RIAPUtils.exhaust(result);
  }
  private void resetGame() {
    final Reference reference = new Reference("http://localhost/clue/game");
    reference.setHostPort(port);

    final ClientResource resource = new ClientResource(reference);
    resource.setProtocol(Protocol.HTTP);
    resource.setChallengeResponse(getChallengeResponse());
    resource.delete();
    resource.release();

    assertEquals(324, getRemainingCount());
  }
  private int getRemainingCount() {
    final Reference reference = new Reference("http://localhost/clue/game.json");
    reference.setHostPort(port);

    final ClientResource resource = new ClientResource(reference);
    resource.setProtocol(Protocol.HTTP);
    resource.setChallengeResponse(getChallengeResponse());

    final ClueServerStatus response = resource.get(ClueServerStatus.class);
    resource.release();
    return response.getRemainingTriples().size();
  }
  @Test
  public void testGetProbabilities() throws ResourceException, IOException {
    final Reference reference = new Reference("http://localhost/clue/probability.json");
    reference.setHostPort(port);

    final ClientResource resource = new ClientResource(reference);
    resource.setProtocol(Protocol.HTTP);
    resource.setChallengeResponse(getChallengeResponse());

    System.out.println(resource.get().getText());
    final ProbabilityReport report = resource.get(ProbabilityReport.class);
    System.out.println(report.getMostLikelyRoom().getCardProbability());
    resource.release();
  }
  @Test
  public void testServerGetStatus() throws Exception {
    final Reference reference = new Reference("http://localhost/clue/game.xml");
    reference.setHostPort(port);

    final ClientResource resource = new ClientResource(reference);
    resource.setProtocol(Protocol.HTTP);
    resource.setChallengeResponse(getChallengeResponse());

    final ClueServerStatus response = resource.get(ClueServerStatus.class);
    resource.release();

    assertNotNull(response);
  }
  @Test
  public void testPutCard() throws Exception {
    final Reference reference = new Reference("http://localhost/clue/cards");
    reference.setHostPort(port);

    final ClientResource client = new ClientResource(reference);
    client.setChallengeResponse(getChallengeResponse());

    final Card card = WeaponEnum.LEADPIPE.getWeapon();
    client.put(new JacksonRepresentation<Card>(card));
    client.release();

    assertEquals(270, getRemainingCount());
  }
  @Test
  public void testRemainingTriples() {
    final Reference reference = new Reference("http://localhost/clue/triples/remaining.json");
    reference.setHostPort(port);

    final ClientResource resource = new ClientResource(reference);
    resource.setProtocol(Protocol.HTTP);
    resource.setChallengeResponse(getChallengeResponse());

    final TripleList response = resource.get(TripleList.class);
    resource.release();

    assertNotNull(response);
    assertEquals(324, response.size());
  }
  /**
   * 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 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);
    }
  }
  /** Post some JSON to the user userstorage */
  private void postJSON() {
    // http://localhost:8182/sitools/userstorage/admin?filepath=%2Ftmp&filename=SvaRecordDefinitionFile.json
    StringRepresentation repr =
        new StringRepresentation(urlFileContent, MediaType.APPLICATION_JSON);

    Reference reference =
        new Reference(
            getBaseUrl()
                + settings
                    .getString(Consts.APP_USERSTORAGE_USER_URL)
                    .replace("{identifier}", userLogin)
                + "/files");
    reference.addQueryParameter("filename", cartFileName);
    reference.addQueryParameter("filepath", cartFilePath);

    ClientResource cr = new ClientResource(reference);
    ChallengeResponse challenge =
        new ChallengeResponse(ChallengeScheme.HTTP_BASIC, userLogin, password);
    cr.setChallengeResponse(challenge);
    Representation result = cr.post(repr, MediaType.APPLICATION_JSON);
    assertNotNull(result);
    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);
    }
  }
Пример #16
0
 /**
  * Sets the authentication response sent by a client to an origin server given a scheme,
  * identifier and secret.
  *
  * @param scheme The challenge scheme.
  * @param identifier The user identifier, such as a login name or an access key.
  * @param secret The user secret, such as a password or a secret key.
  */
 public void setChallengeResponse(ChallengeScheme scheme, final String identifier, String secret) {
   setChallengeResponse(new ChallengeResponse(scheme, identifier, secret));
 }