/**
   * Returns the list of identifiers for the mails in the inbox
   *
   * @return The list of identifiers.
   * @throws ResourceException
   */
  protected List<String> getMailIdentifiers() throws ResourceException {
    final List<String> result = new ArrayList<String>();

    // 1 - Get to mailbox content
    final Request request = new Request(Method.GET, getMailboxUri());
    if (getMailboxChallengeScheme() != null) {
      final ChallengeResponse challengeResponse =
          new ChallengeResponse(
              getMailboxChallengeScheme(), getMailboxLogin(), getMailboxPassword());
      request.setChallengeResponse(challengeResponse);
    }
    final Response response = getContext().getClientDispatcher().handle(request);

    if (!response.getStatus().isSuccess()) {
      throw new ResourceException(response.getStatus(), "Cannot get the mail iddentifiers.");
    }

    // 2 - Parse the list of mails
    if (response.isEntityAvailable()) {
      final DomRepresentation rep = new DomRepresentation(response.getEntity());
      for (final Node node : rep.getNodes("/emails/email/@href")) {
        final String href = node.getNodeValue();
        if (href.startsWith("/")) {
          result.add(href.substring(1));
        } else {
          result.add(href);
        }
      }
    }

    return result;
  }
  /**
   * Tests conditional ranges requests.
   *
   * @throws Exception
   */
  public void testConditionalRanges() throws Exception {
    Client client = new Client(Protocol.HTTP);

    // Test partial Get.
    Request request = new Request(Method.GET, "http://localhost:" + TEST_PORT + "/testGet");
    Response response = client.handle(request);
    Tag entityTag = response.getEntity().getTag();

    request.setRanges(Arrays.asList(new Range(1, Range.SIZE_MAX)));
    request.getConditions().setRangeTag(entityTag);
    response = client.handle(request);
    assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus());
    assertEquals("234567890", response.getEntity().getText());
    assertEquals(10, response.getEntity().getSize());
    assertEquals(9, response.getEntity().getAvailableSize());
    assertEquals(1, response.getEntity().getRange().getIndex());
    assertEquals(9, response.getEntity().getRange().getSize());

    entityTag = new Tag(entityTag.getName() + "-test");
    request.setRanges(Arrays.asList(new Range(1, Range.SIZE_MAX)));
    request.getConditions().setRangeTag(entityTag);
    response = client.handle(request);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("1234567890", response.getEntity().getText());
    client.stop();
  }
  public Response sendRequest(Request request) throws IOException {
    injectCustomSecurityMechanism(request);

    if (log.isLoggable(Level.FINE)) {
      RestClientLogHelper.logHttpRequest(log, Level.FINE, request);
    }

    Client client = initClient();
    Response response = client.handle(request);

    if (log.isLoggable(Level.FINE)) {
      RestClientLogHelper.logHttpResponse(log, Level.FINE, response);
    }

    if (response.getStatus().isSuccess()) {
      return response;
    } else if (response.getStatus().equals(Status.CLIENT_ERROR_UNAUTHORIZED)) {
      // retry request with additional authentication
      Request retryRequest = createChallengeResponse(response);
      return sendRequest(retryRequest);
    }

    throw new RepositoryException(
        "Encountered error while retrieving http response (HttpStatus: "
            + response.getStatus()
            + ", Body: "
            + response.getEntity().getText()
            + ")");
  }
  public void testGetInt() throws IOException {
    Response response = get("int/467");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("467", response.getEntity().getText());

    response = get("int/abc");
    assertTrue(response.getStatus().isError());
  }
 /** Tests status getting/setting. */
 public void testStatus() throws Exception {
   final Request request = getRequest();
   final Response response = getResponse(request);
   response.setStatus(Status.SUCCESS_OK);
   assertEquals(Status.SUCCESS_OK, response.getStatus());
   response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
   assertEquals(Status.CLIENT_ERROR_BAD_REQUEST, response.getStatus());
 }
  public void testGetBigDecimal() throws IOException {
    Response response = get("BigDecimal/413624654744743534745767");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("413624654744743534745767", response.getEntity().getText());

    response = get("BigDecimal/abc");
    assertTrue(response.getStatus().isError());
  }
  public void testDecoded1() throws Exception {
    Response response = get("decoded/x");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("x", response.getEntity().getText());

    response = get("decoded/sjkg");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("sjkg", response.getEntity().getText());
  }
  public void testX2() throws Exception {
    Response response = get("abcdef/1234");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("bcd\n12", response.getEntity().getText());

    response = get("aXYZef/AB34");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("XYZ\nAB", response.getEntity().getText());
  }
  public void testX() throws Exception {
    Response response = get("abc123");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("123", response.getEntity().getText());

    response = get("abcdef");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("def", response.getEntity().getText());
  }
  public void testGetMn() throws IOException {
    Response response = get("mn467");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("467", response.getEntity().getText());

    response = get("mnabc");
    assertTrue(response.getStatus().isError());
    assertEquals(Status.CLIENT_ERROR_NOT_FOUND, response.getStatus());
  }
  public void testGetMediaType() throws IOException {
    Response response = get("MediaType/467");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("467/*", response.getEntity().getText());

    response = get("MediaType/abc");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("abc/*", response.getEntity().getText());
  }
  /**
   * Posts a member to the collection resulting in the creation of a new resource.
   *
   * @param member The member representation to post.
   * @return The reference of the new resource.
   * @throws Exception
   */
  public Reference postMember(Representation member) throws Exception {
    final Request request = new Request(Method.POST, getHref(), member);
    final Response response = getWorkspace().getService().getClientDispatcher().handle(request);

    if (response.getStatus().equals(Status.SUCCESS_CREATED)) {
      return response.getLocationRef();
    }

    throw new Exception(
        "Couldn't post the member representation. Status returned: " + response.getStatus());
  }
  public void testWithDefault() throws Exception {
    Request request = createGetRequest("headerWithDefault");
    Util.getHttpHeaders(request).add(HttpHeaderTestService.TEST_HEADER_NAME, "abc");
    Response response = accessServer(request);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("abc", response.getEntity().getText());

    request = createGetRequest("headerWithDefault");
    response = accessServer(request);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("default", response.getEntity().getText());
  }
  public void testGetInteger() throws IOException {
    Response response = get("Integer/4423467");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("4423467", response.getEntity().getText());

    response = get("Integer/423645365467345743734");
    assertTrue(response.getStatus().isError());
    assertEquals(Status.CLIENT_ERROR_NOT_FOUND, response.getStatus());

    response = get("Integer/abc");
    assertTrue(response.getStatus().isError());
    assertEquals(Status.CLIENT_ERROR_NOT_FOUND, response.getStatus());
  }
  public void testEncodedWithDefault() throws Exception {
    Response response = get("encodedWithDefault;m=1;m=2;x=3");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("[1, 2]", response.getEntity().getText());

    response = get("encodedWithDefault;m=1;i=2;x=3");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("[1]", response.getEntity().getText());

    response = get("encodedWithDefault;a=1;i=2;x=3");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("[default]", response.getEntity().getText());
  }
  @Override
  protected int doHandle(Request request, Response response) {

    super.doHandle(request, response);

    if (response.getStatus().isSuccess() && request.getMethod().equals(Method.GET)) {
      boolean isHtml = false;
      for (Preference<MediaType> mt : request.getClientInfo().getAcceptedMediaTypes()) {
        if (mt.getMetadata().includes(MediaType.APPLICATION_XHTML)
            || mt.getMetadata().includes(MediaType.TEXT_HTML)) {
          isHtml = true;
          break;
        }
      }
      if (isHtml) {
        try {
          response.setEntity(toHtml(request, response));
        } catch (SlipStreamException e) {
          // ok it failed generating html... do we care?
        }
      }
    }

    return CONTINUE;
  }
 public void testCookies() throws IOException {
   final Request request = createGetRequest("cookies/cookieName");
   request.getCookies().add(new Cookie("cookieName", "cookie-value"));
   final Response response = accessServer(request);
   assertEquals(Status.SUCCESS_OK, response.getStatus());
   assertEquals("cookieName=cookie-value", response.getEntity().getText());
 }
예제 #18
0
 @Test
 // TODO check, this seems wrong
 public void returns_bad_request_for_empty_form() {
   resource.init(null, request, response);
   resource.post(new Form());
   assertThat(response.getStatus().getCode(), is(303));
 }
 public void testXmlTransformPost() throws Exception {
   final Response response =
       post("source", new StringRepresentation("abcdefg", MediaType.TEXT_XML));
   sysOutEntityIfError(response);
   assertEquals(Status.SUCCESS_OK, response.getStatus());
   assertEquals("abcdefg", response.getEntity().getText());
 }
  @Test
  @SuppressWarnings("unchecked")
  public void shouldSetAnyOtherExceptionResponse() throws Exception {

    // Given
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    Exception exception = new Exception("MESSAGE");
    Status status = new Status(444, exception);

    given(response.getStatus()).willReturn(status);

    // When
    exceptionFilter.afterHandle(request, response);

    // Then
    ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor =
        ArgumentCaptor.forClass(JacksonRepresentation.class);
    verify(response).setEntity(exceptionResponseCaptor.capture());
    Map<String, String> responseBody =
        (Map<String, String>) exceptionResponseCaptor.getValue().getObject();
    assertThat(responseBody)
        .containsOnly(entry("error", "server_error"), entry("error_description", "MESSAGE"));

    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(response).setStatus(statusCaptor.capture());
    assertThat(statusCaptor.getValue().getCode()).isEqualTo(500);
    assertThat(statusCaptor.getValue().getThrowable()).isEqualTo(exception);
  }
 public void testCharSequenceGet() throws Exception {
   final Response response = get("CharSequence");
   sysOutEntityIfError(response);
   assertEquals(Status.SUCCESS_OK, response.getStatus());
   final Representation entity = response.getEntity();
   assertEquals(ProviderTestService.createCS(), entity.getText());
 }
 public void testXmlTransformGet() throws Exception {
   final Response response = get("source");
   sysOutEntityIfError(response);
   assertEquals(Status.SUCCESS_OK, response.getStatus());
   final String entity = response.getEntity().getText();
   assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><abc/>", entity);
 }
 public void testByteArrayPost() throws Exception {
   final Representation entity =
       new StringRepresentation("big test", MediaType.APPLICATION_OCTET_STREAM);
   final Response response = post("byteArray", entity);
   assertEquals(Status.SUCCESS_OK, response.getStatus());
   assertEquals("big test", response.getEntity().getText());
 }
  /**
   * Tests partial Get requests.
   *
   * @throws IOException
   * @throws NoSuchAlgorithmException
   */
  @Test
  public void testGet() throws IOException, NoSuchAlgorithmException {
    Client client = new Client(Protocol.HTTP);

    // Test partial Get.
    Request request = new Request(Method.PUT, "http://localhost:" + TEST_PORT + "/");
    StringRepresentation rep = new StringRepresentation("0123456789");
    try {
      DigesterRepresentation digester = new DigesterRepresentation(rep);
      // Such representation computes the digest while
      // consuming the wrapped representation.
      digester.exhaust();
      // Set the digest with the computed one
      digester.setDigest(digester.computeDigest());
      request.setEntity(digester);

      Response response = client.handle(request);

      assertEquals(Status.SUCCESS_OK, response.getStatus());
      digester = new DigesterRepresentation(response.getEntity());
      digester.exhaust();
      assertTrue(digester.checkDigest());

      client.stop();
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }
 /** @see ProviderTestService#mMapPost(javax.ws.rs.core.MultivaluedMap) */
 public void testMultivaluedMapPost() throws Exception {
   final Response response = post("MultivaluedMap", createForm().getWebRepresentation());
   assertEquals(Status.SUCCESS_OK, response.getStatus());
   final MediaType respMediaType = response.getEntity().getMediaType();
   assertEqualMediaType(MediaType.TEXT_PLAIN, respMediaType);
   final String respEntity = response.getEntity().getText();
   assertEquals("[(lastname,Merkel), (firstname,Angela)]", respEntity);
 }
  /**
   * Returns the feed representation.
   *
   * @return The feed representation.
   * @throws Exception
   */
  public Feed getFeed() throws Exception {
    final Reference feedRef = getHref();

    if (feedRef.isRelative()) {
      feedRef.setBaseRef(getWorkspace().getService().getReference());
    }

    final Request request = new Request(Method.GET, feedRef.getTargetRef());
    final Response response = getWorkspace().getService().getClientDispatcher().handle(request);

    if (response.getStatus().equals(Status.SUCCESS_OK)) {
      return new Feed(response.getEntity());
    }

    throw new Exception(
        "Couldn't get the feed representation. Status returned: " + response.getStatus());
  }
  public void testWithoutPath() throws Exception {
    Response response = get(";firstname=Angela;lastname=Merkel");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("Angela Merkel", response.getEntity().getText());

    response = get(";lastname=Merkel;firstname=Angela");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("Angela Merkel", response.getEntity().getText());

    response = get(";firstname=Goofy");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("Goofy null", response.getEntity().getText());

    response = get(";lastname=Goofy");
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    assertEquals("null Goofy", response.getEntity().getText());
  }
예제 #28
0
 @Test
 @Ignore
 public void returns_bad_request_for_contact_with_too_short_name() {
   resource.init(null, request, response);
   Form form = new Form();
   form.add("name", "A");
   resource.post(form);
   assertThat(response.getStatus().getCode(), is(400));
 }
  public void testStringGet() throws Exception {
    getAndExpectAlphabet("String");

    final Response response = get("String2");
    sysOutEntityIfError(response);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    final Representation entity = response.getEntity();
    assertEquals(ProviderTestService.STRING2, entity.getText());
  }
 public void testBufferedReaderPost() throws Exception {
   Representation entity =
       new StringRepresentation("big test", MediaType.APPLICATION_OCTET_STREAM);
   final Response response = post("BufferedReader", entity);
   sysOutEntityIfError(response);
   assertEquals(Status.SUCCESS_OK, response.getStatus());
   entity = response.getEntity();
   assertEquals("big test", entity.getText());
 }