Пример #1
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();
  }
  /**
   * Tests partial Put requests.
   *
   * @throws Exception
   */
  public void testPut() throws Exception {
    if (!SystemUtils.isWindows()) {
      Request request;
      Response response;

      BioUtils.delete(testDir, true);
      Client client = new Client(new Context(), Protocol.HTTP);
      client.getContext().getParameters().add("tracing", "true");

      // PUT on a file that does not exist
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setEntity(new StringRepresentation("1234567890"));
      request.setRanges(Arrays.asList(new Range(0, 10)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      response = client.handle(new Request(Method.GET, request.getResourceRef()));
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      assertEquals("1234567890", response.getEntity().getText());

      // Partial PUT on a file, the provided representation overflowed the
      // existing file
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setEntity(new StringRepresentation("0000000000"));
      request.setRanges(Arrays.asList(new Range(1, 10)));
      response = client.handle(request);
      System.out.println(response.getStatus() + " / " + response.getStatus().getThrowable());
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      response = client.handle(new Request(Method.GET, request.getResourceRef()));
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      assertEquals("10000000000", response.getEntity().getText());

      // Partial PUT on a file that does not exists, the provided range
      // does not start at the 0 index.
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai2.txt");
      request.setEntity(new StringRepresentation("0000000000"));
      request.setRanges(Arrays.asList(new Range(1, 10)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      request.setMethod(Method.GET);
      response = client.handle(request);
      assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
      assertEquals("0000000000", response.getEntity().getText());

      // Partial PUT on a file, simple range
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setEntity(new StringRepresentation("22"));
      request.setRanges(Arrays.asList(new Range(2, 2)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      response = client.handle(new Request(Method.GET, request.getResourceRef()));
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      assertEquals("10220000000", response.getEntity().getText());

      // Partial PUT on a file, the provided representation will be padded
      // at the very end of the file.
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setEntity(new StringRepresentation("888"));
      request.setRanges(Arrays.asList(new Range(8, Range.SIZE_MAX)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      response = client.handle(new Request(Method.GET, request.getResourceRef()));
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      assertEquals("10220000888", response.getEntity().getText());

      // Partial PUT on a file that does not exist, the range does not
      // specify the range size.
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai3.txt");
      request.setEntity(new StringRepresentation("888"));
      request.setRanges(Arrays.asList(new Range(8, Range.SIZE_MAX)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      request.setMethod(Method.GET);
      response = client.handle(request);
      assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
      assertEquals("888", response.getEntity().getText());

      // Partial PUT on a file, the provided representation will be padded
      // just before the end of the file.
      request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setEntity(new StringRepresentation("99"));
      request.setRanges(Arrays.asList(new Range(8, Range.SIZE_MAX)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      response = client.handle(new Request(Method.GET, request.getResourceRef()));
      assertEquals(Status.SUCCESS_OK, response.getStatus());
      assertEquals("10220000998", response.getEntity().getText());

      request = new Request(Method.GET, "http://localhost:" + TEST_PORT + "/testPut/essai.txt");
      request.setRanges(Arrays.asList(new Range(3, Range.SIZE_MAX)));
      response = client.handle(request);
      assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
      assertEquals("20000998", response.getEntity().getText());

      BioUtils.delete(testDir, true);
      client.stop();
    }
  }