Java Code Examples for com.linecorp.armeria.common.AggregatedHttpResponse#contentUtf8()

The following examples show how to use com.linecorp.armeria.common.AggregatedHttpResponse#contentUtf8() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArmeriaCentralDogma.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
private static <T> T handleErrorResponse(AggregatedHttpResponse res) {
    final HttpStatus status = res.status();
    if (status.codeClass() != HttpStatusClass.SUCCESS) {
        final JsonNode node = toJson(res, JsonNodeType.OBJECT);
        final JsonNode exceptionNode = node.get("exception");
        final JsonNode messageNode = node.get("message");

        if (exceptionNode != null) {
            final String typeName = exceptionNode.textValue();
            if (typeName != null) {
                final Function<String, CentralDogmaException> exceptionFactory =
                        EXCEPTION_FACTORIES.get(typeName);
                if (exceptionFactory != null) {
                    throw exceptionFactory.apply(messageNode.textValue());
                }
            }
        }
    }

    throw new CentralDogmaException("unexpected response: " + res.headers() + ", " + res.contentUtf8());
}
 
Example 2
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Test
void watchRepository() {
    final WebClient client = dogma.httpClient();
    addFooJson(client);
    final RequestHeaders headers = RequestHeaders.of(HttpMethod.GET, CONTENTS_PREFIX,
                                                     HttpHeaderNames.IF_NONE_MATCH, "-1");
    final CompletableFuture<AggregatedHttpResponse> future = client.execute(headers).aggregate();

    assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS))
            .isExactlyInstanceOf(TimeoutException.class);

    editFooJson(client);

    await().atMost(3, TimeUnit.SECONDS).untilAsserted(future::isDone);
    final AggregatedHttpResponse res = future.join();

    final String expectedJson =
            '{' +
            "   \"revision\" : 3" +
            '}';
    final String actualJson = res.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);
}
 
Example 3
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Test
void editFileWithJsonPatch() throws IOException {
    final WebClient client = dogma.httpClient();
    addFooJson(client);
    final AggregatedHttpResponse res1 = editFooJson(client);
    final String expectedJson =
            '{' +
            "   \"revision\": 3," +
            "   \"pushedAt\": \"${json-unit.ignore}\"" +
            '}';
    final String actualJson = res1.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);

    final AggregatedHttpResponse res2 = client.get(CONTENTS_PREFIX + "/foo.json").aggregate().join();
    assertThat(Jackson.readTree(res2.contentUtf8()).get("content").get("a").textValue())
            .isEqualToIgnoringCase("baz");
}
 
Example 4
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Test
void getFileWithJsonPath() {
    final WebClient client = dogma.httpClient();
    addFooJson(client);
    final AggregatedHttpResponse aRes = client
            .get(CONTENTS_PREFIX + "/foo.json?jsonpath=$[?(@.a == \"bar\")]&jsonpath=$[0].a")
            .aggregate().join();

    final String expectedJson =
            '{' +
            "   \"revision\": 2," +
            "   \"path\": \"/foo.json\"," +
            "   \"type\": \"JSON\"," +
            "   \"content\" : \"bar\"," +
            "   \"url\": \"/api/v1/projects/myPro/repos/myRepo/contents/foo.json\"" +
            '}';
    final String actualJson = aRes.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);
}
 
Example 5
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Test
void getFile() {
    final WebClient client = dogma.httpClient();
    addFooJson(client);
    final AggregatedHttpResponse aRes = client.get(CONTENTS_PREFIX + "/foo.json").aggregate().join();

    final String expectedJson =
            '{' +
            "   \"revision\": 2," +
            "   \"path\": \"/foo.json\"," +
            "   \"type\": \"JSON\"," +
            "   \"content\" : {\"a\":\"bar\"}," +
            "   \"url\": \"/api/v1/projects/myPro/repos/myRepo/contents/foo.json\"" +
            '}';
    final String actualJson = aRes.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);
}
 
Example 6
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Test
void renameDirectory() {
    final WebClient client = dogma.httpClient();
    addBarTxt(client);
    final String body =
            '{' +
            "   \"path\" : \"/a\"," +
            "   \"type\" : \"RENAME\"," +
            "   \"content\" : \"/b\"," +
            "   \"commitMessage\" : {" +
            "       \"summary\" : \"Rename /a\"," +
            "       \"detail\": \"Rename to /b\"," +
            "       \"markup\": \"PLAINTEXT\"" +
            "   }" +
            '}';
    final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST, CONTENTS_PREFIX,
                                                     HttpHeaderNames.CONTENT_TYPE, MediaType.JSON);
    final AggregatedHttpResponse aRes = client.execute(headers, body).aggregate().join();
    final String expectedJson =
            '{' +
            "   \"revision\": 3," +
            "   \"pushedAt\": \"${json-unit.ignore}\"" +
            '}';
    final String actualJson = aRes.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);
}
 
Example 7
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Test
void renameFile() {
    final WebClient client = dogma.httpClient();
    addFooJson(client);
    final String body =
            '{' +
            "   \"path\" : \"/foo.json\"," +
            "   \"type\" : \"RENAME\"," +
            "   \"content\" : \"/bar.json\"," +
            "   \"commitMessage\" : {" +
            "       \"summary\" : \"Rename foo.json\"," +
            "       \"detail\": \"Rename to bar.json\"," +
            "       \"markup\": \"PLAINTEXT\"" +
            "   }" +
            '}';
    final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST, CONTENTS_PREFIX,
                                                     HttpHeaderNames.CONTENT_TYPE, MediaType.JSON);
    final AggregatedHttpResponse aRes = client.execute(headers, body).aggregate().join();
    final String expectedJson =
            '{' +
            "   \"revision\": 3," +
            "   \"pushedAt\": \"${json-unit.ignore}\"" +
            '}';
    final String actualJson = aRes.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);
}
 
Example 8
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Test
void addFiles() {
    final WebClient client = dogma.httpClient();
    final String body =
            '{' +
            "   \"commitMessage\" : {" +
            "       \"summary\" : \"Add foo.json\"," +
            "       \"detail\": \"Add because we need it.\"," +
            "       \"markup\": \"PLAINTEXT\"" +
            "   }," +
            "   \"changes\" : [" +
            "       {" +
            "           \"path\" : \"/foo0.json\"," +
            "           \"type\" : \"UPSERT_JSON\"," +
            "           \"content\" : {\"a\": \"bar\"}" +
            "       }," +
            "       {" +
            "           \"path\" : \"/foo1.json\"," +
            "           \"type\" : \"UPSERT_JSON\"," +
            "           \"content\" : {\"b\": \"bar\"}" +
            "       }" +
            "   ]" +
            '}';
    final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST, CONTENTS_PREFIX,
                                                     HttpHeaderNames.CONTENT_TYPE, MediaType.JSON);
    final AggregatedHttpResponse aRes = client.execute(headers, body).aggregate().join();
    final String expectedJson =
            '{' +
            "   \"revision\": 2," +
            "   \"pushedAt\": \"${json-unit.ignore}\"" +
            '}';
    final String actualJson = aRes.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);
}
 
Example 9
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Test
void addFile() {
    final WebClient client = dogma.httpClient();
    final AggregatedHttpResponse aRes = addFooJson(client);
    final String expectedJson =
            '{' +
            "   \"revision\": 2," +
            "   \"pushedAt\": \"${json-unit.ignore}\"" +
            '}';
    final String actualJson = aRes.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);
}
 
Example 10
Source File: ProjectServiceV1Test.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Test
void listRemovedProjects() throws IOException {
    final WebClient client = dogma.httpClient();
    createProject(client, "trustin");
    createProject(client, "hyangtack");
    createProject(client, "minwoox");
    client.delete(PROJECTS_PREFIX + "/hyangtack").aggregate().join();
    client.delete(PROJECTS_PREFIX + "/minwoox").aggregate().join();

    final AggregatedHttpResponse removedRes = client.get(PROJECTS_PREFIX + "?status=removed")
                                                    .aggregate().join();
    assertThat(ResponseHeaders.of(removedRes.headers()).status()).isEqualTo(HttpStatus.OK);
    final String expectedJson =
            '[' +
            "   {" +
            "       \"name\": \"hyangtack\"" +
            "   }," +
            "   {" +
            "       \"name\": \"minwoox\"" +
            "   }" +
            ']';
    assertThatJson(removedRes.contentUtf8()).isEqualTo(expectedJson);

    final AggregatedHttpResponse remainedRes = client.get(PROJECTS_PREFIX).aggregate().join();
    final String remains = remainedRes.contentUtf8();
    final JsonNode jsonNode = Jackson.readTree(remains);

    // Only trustin project is left
    assertThat(jsonNode.size()).isOne();
}
 
Example 11
Source File: RepositoryServiceV1Test.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Test
void listRemovedRepositories() throws IOException {
    final WebClient client = dogma.httpClient();
    createRepository(client, "trustin");
    createRepository(client, "hyangtack");
    createRepository(client, "minwoox");
    client.delete(REPOS_PREFIX + "/hyangtack").aggregate().join();
    client.delete(REPOS_PREFIX + "/minwoox").aggregate().join();

    final AggregatedHttpResponse removedRes = client.get(REPOS_PREFIX + "?status=removed")
                                                    .aggregate().join();
    assertThat(ResponseHeaders.of(removedRes.headers()).status()).isEqualTo(HttpStatus.OK);
    final String expectedJson =
            '[' +
            "   {" +
            "       \"name\": \"hyangtack\"" +
            "   }," +
            "   {" +
            "       \"name\": \"minwoox\"" +
            "   }" +
            ']';
    assertThatJson(removedRes.contentUtf8()).isEqualTo(expectedJson);

    final AggregatedHttpResponse remainedRes = client.get(REPOS_PREFIX).aggregate().join();
    final String remains = remainedRes.contentUtf8();
    final JsonNode jsonNode = Jackson.readTree(remains);

    // dogma, meta and trustin repositories are left
    assertThat(jsonNode).hasSize(3);
}
 
Example 12
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Test
void editFileWithTextPatch() throws IOException {
    final WebClient client = dogma.httpClient();
    addBarTxt(client);
    final String patch =
            '{' +
            "   \"path\": \"/a/bar.txt\"," +
            "   \"type\": \"APPLY_TEXT_PATCH\"," +
            "   \"content\" : \"--- /a/bar.txt\\n" +
            "+++ /a/bar.txt\\n" +
            "@@ -1,1 +1,1 @@\\n" +
            "-text in the file.\\n" +
            "+text in some file.\\n\"," +
            "   \"commitMessage\" : {" +
            "       \"summary\" : \"Edit bar.txt\"," +
            "       \"detail\": \"Edit because we need it.\"," +
            "       \"markup\": \"PLAINTEXT\"" +
            "   }" +
            '}';

    final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST, CONTENTS_PREFIX,
                                                     HttpHeaderNames.CONTENT_TYPE, MediaType.JSON);
    final AggregatedHttpResponse res1 = client.execute(headers, patch).aggregate().join();
    final String expectedJson =
            '{' +
            "   \"revision\": 3," +
            "   \"pushedAt\": \"${json-unit.ignore}\"" +
            '}';
    final String actualJson = res1.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);

    final AggregatedHttpResponse res2 = client.get(CONTENTS_PREFIX + "/a/bar.txt").aggregate().join();
    assertThat(Jackson.readTree(res2.contentUtf8()).get("content").textValue())
            .isEqualTo("text in some file.\n");
}
 
Example 13
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Test
void watchFileWithIdentityQuery() {
    final WebClient client = dogma.httpClient();
    addFooJson(client);
    final RequestHeaders headers = RequestHeaders.of(HttpMethod.GET, CONTENTS_PREFIX + "/foo.json",
                                                     HttpHeaderNames.IF_NONE_MATCH, "-1");
    final CompletableFuture<AggregatedHttpResponse> future = client.execute(headers).aggregate();

    assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS))
            .isExactlyInstanceOf(TimeoutException.class);

    // An irrelevant change should not trigger a notification.
    addBarTxt(client);
    assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS))
            .isExactlyInstanceOf(TimeoutException.class);

    // Make a relevant change now.
    editFooJson(client);
    final String expectedJson =
            '{' +
            "   \"revision\" : 4," +
            "   \"entry\": {" +
            "       \"revision\" : 4," +
            "       \"path\": \"/foo.json\"," +
            "       \"type\": \"JSON\"," +
            "       \"content\": {\"a\":\"baz\"}," +
            "       \"url\": \"/api/v1/projects/myPro/repos/myRepo/contents/foo.json\"" +
            "   }" +
            '}';
    final AggregatedHttpResponse res = future.join();
    final String actualJson = res.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);
}
 
Example 14
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Test
void watchFileWithJsonPathQuery() {
    final WebClient client = dogma.httpClient();
    addFooJson(client);
    final RequestHeaders headers = RequestHeaders.of(HttpMethod.GET,
                                                     CONTENTS_PREFIX + "/foo.json?jsonpath=%24.a",
                                                     HttpHeaderNames.IF_NONE_MATCH, "-1");
    final CompletableFuture<AggregatedHttpResponse> future = client.execute(headers).aggregate();

    assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS))
            .isExactlyInstanceOf(TimeoutException.class);

    // An irrelevant change should not trigger a notification.
    addBarTxt(client);
    assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS))
            .isExactlyInstanceOf(TimeoutException.class);

    // Make a relevant change now.
    editFooJson(client);
    final String expectedJson =
            '{' +
            "   \"revision\" : 4," +
            "   \"entry\": {" +
            "       \"revision\" : 4," +
            "       \"path\": \"/foo.json\"," +
            "       \"type\": \"JSON\"," +
            "       \"content\": \"baz\"," +
            "       \"url\": \"/api/v1/projects/myPro/repos/myRepo/contents/foo.json\"" +
            "   }" +
            '}';
    final AggregatedHttpResponse res = future.join();
    final String actualJson = res.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);
}
 
Example 15
Source File: AnnotatedServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void testMessageConverterService() {
    AggregatedHttpResponse res;
    String body;

    // JSON
    for (final String path : Arrays.asList("/messageConverter/node/node",
                                           "/messageConverter/node/obj",
                                           "/messageConverter/obj/obj",
                                           "/messageConverter/obj/future")) {
        res = client.execute(RequestHeaders.of(HttpMethod.POST, path,
                                               HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8),
                             "{\"name\":\"armeria\"}").aggregate().join();

        assertThat(res.status()).isEqualTo(HttpStatus.OK);
        assertThat(res.contentType().is(MediaType.JSON_UTF_8)).isTrue();

        body = res.contentUtf8();
        assertThatJson(body).node("result").isStringEqualTo("success");
        assertThatJson(body).node("from").isStringEqualTo("armeria");
    }

    // custom(text protocol)
    res = client.execute(RequestHeaders.of(HttpMethod.POST, "/messageConverter/custom",
                                           HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8),
                         "armeria").aggregate().join();
    assertThat(res.status()).isEqualTo(HttpStatus.OK);
    assertThat(res.contentType().is(MediaType.PLAIN_TEXT_UTF_8)).isTrue();
    assertThat(res.contentUtf8()).isEqualTo("success:armeria");
}
 
Example 16
Source File: HttpClientSniTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static String get(String fqdn) throws Exception {
    final WebClient client = WebClient.builder("https://" + fqdn + ':' + httpsPort)
                                      .factory(clientFactory)
                                      .build();

    final AggregatedHttpResponse response = client.get("/").aggregate().get();

    assertThat(response.status()).isEqualTo(HttpStatus.OK);
    return response.contentUtf8();
}
 
Example 17
Source File: ContentServiceV1Test.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
@Test
void editFiles() {
    final WebClient client = dogma.httpClient();
    addFooJson(client);
    addBarTxt(client);
    final String body =
            '{' +
            "   \"commitMessage\" : {" +
            "       \"summary\" : \"Edit files\"," +
            "       \"detail\": \"Edit because we need it.\"," +
            "       \"markup\": \"PLAINTEXT\"" +
            "   }," +
            "   \"changes\" : [" +
            "       {" +
            "           \"path\" : \"/foo.json\"," +
            "           \"type\" : \"UPSERT_JSON\"," +
            "           \"content\" : {\"b\": \"bar\"}" +
            "       }," +
            "       {" +
            "           \"path\" : \"/a/bar.txt\"," +
            "           \"type\" : \"UPSERT_TEXT\"," +
            "           \"content\" : \"text in a file.\\n\"" +
            "       }" +
            "   ]" +
            '}';
    final RequestHeaders headers = RequestHeaders.of(HttpMethod.POST, CONTENTS_PREFIX,
                                                     HttpHeaderNames.CONTENT_TYPE, MediaType.JSON);
    client.execute(headers, body).aggregate().join();
    final AggregatedHttpResponse aRes = client
            .get("/api/v1/projects/myPro/repos/myRepo/compare?from=3&to=4").aggregate().join();
    final String expectedJson =
            '[' +
            "   {" +
            "       \"path\": \"/a/bar.txt\"," +
            "       \"type\": \"APPLY_TEXT_PATCH\"," +
            "       \"content\": \"--- /a/bar.txt\\n" +
            "+++ /a/bar.txt\\n" +
            "@@ -1,1 +1,1 @@\\n" +
            "-text in the file.\\n" +
            "+text in a file.\"" +
            "   }," +
            "   {" +
            "       \"path\": \"/foo.json\"," +
            "       \"type\": \"APPLY_JSON_PATCH\"," +
            "       \"content\": [{" +
            "           \"op\": \"remove\"," +
            "           \"path\": \"/a\"" +
            "       },{" +
            "           \"op\": \"add\"," +
            "           \"path\": \"/b\"," +
            "           \"value\": \"bar\"" +
            "       }]" +
            "   }" +
            ']';
    final String actualJson = aRes.contentUtf8();
    assertThatJson(actualJson).isEqualTo(expectedJson);
}
 
Example 18
Source File: KafkaStoreListCall.java    From zipkin-storage-kafka with Apache License 2.0 4 votes vote down vote up
protected String content(AggregatedHttpResponse response) {
  if (!response.status().equals(HttpStatus.OK)) return null;
  return response.contentUtf8();
}