Java Code Examples for com.linecorp.armeria.common.AggregatedHttpRequest#of()

The following examples show how to use com.linecorp.armeria.common.AggregatedHttpRequest#of() . 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: AnnotatedServiceHandlersOrderTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void requestConverterOrder() throws Exception {
    final String body = "{\"foo\":\"bar\"}";
    final AggregatedHttpRequest aReq = AggregatedHttpRequest.of(
            HttpMethod.POST, "/1/requestConverterOrder", MediaType.JSON, body);

    final AggregatedHttpResponse aRes = executeRequest(aReq);

    assertThat(aRes.status()).isEqualTo(HttpStatus.OK);
    // Converted from the default converter which is JacksonRequestConverterFunction.
    assertThat(aRes.contentUtf8()).isEqualTo(body);

    // parameter level(+1) -> method level(+1) -> class level(+1) -> service level(+1) -> server level(+1)
    // -> default
    assertThat(requestCounter.get()).isEqualTo(5);
}
 
Example 2
Source File: AWSSignatureVersion4Test.java    From zipkin-aws with Apache License 2.0 5 votes vote down vote up
@Test public void canonicalString_commasInPath() {
  AggregatedHttpRequest request = AggregatedHttpRequest.of(
      RequestHeaders.builder(HttpMethod.POST,
          "/zipkin-2016-10-05,zipkin-2016-10-06/dependencylink/_search?allow_no_indices=true&expand_wildcards=open&ignore_unavailable=true")
          .set(AWSSignatureVersion4.X_AMZ_DATE, "20161004T132314Z")
          .contentType(MediaType.JSON_UTF_8)
          .build(),
      HttpData.ofUtf8("{\n" + "    \"query\" : {\n" + "      \"match_all\" : { }\n" + "    }")
  );
  ClientRequestContext ctx = ClientRequestContext.builder(request.toHttpRequest())
      .endpoint(Endpoint.of(
          "search-zipkin-2rlyh66ibw43ftlk4342ceeewu.ap-southeast-1.es.amazonaws.com"))
      .build();

  ByteBuf result = Unpooled.buffer();

  writeCanonicalString(ctx, request.headers(), request.content(), result);
  // Ensure that the canonical string encodes commas with %2C
  assertThat(result.toString(UTF_8)).isEqualTo(""
      + "POST\n"
      + "/zipkin-2016-10-05%2Czipkin-2016-10-06/dependencylink/_search\n"
      + "allow_no_indices=true&expand_wildcards=open&ignore_unavailable=true\n"
      + "host:search-zipkin-2rlyh66ibw43ftlk4342ceeewu.ap-southeast-1.es.amazonaws.com\n"
      + "x-amz-date:20161004T132314Z\n"
      + "\n"
      + "host;x-amz-date\n"
      + "2fd35cb36e5de91bbae279313c371fb630a6b3aab1478df378c5e73e667a1747");
}
 
Example 3
Source File: AWSSignatureVersion4Test.java    From zipkin-aws with Apache License 2.0 5 votes vote down vote up
/** Starting with Zipkin 1.31 colons are used to delimit index types in ES */
@Test public void canonicalString_colonsInPath() {
  AggregatedHttpRequest request = AggregatedHttpRequest.of(RequestHeaders.builder(HttpMethod.GET,
      "/_cluster/health/zipkin:span-*")
      .set(AWSSignatureVersion4.X_AMZ_DATE, "20170830T143137Z")
      .build()
  );
  ClientRequestContext ctx = ClientRequestContext.builder(request.toHttpRequest())
      .endpoint(Endpoint.of(
          "search-zipkin53-mhdyquzbwwzwvln6phfzr3mmdi.ap-southeast-1.es.amazonaws.com"))
      .build();

  ByteBuf result = Unpooled.buffer();

  writeCanonicalString(ctx, request.headers(), request.content(), result);

  // Ensure that the canonical string encodes commas with %2C
  assertThat(result.toString(UTF_8)).isEqualTo(""
      + "GET\n"
      + "/_cluster/health/zipkin%3Aspan-%2A\n"
      + "\n"
      + "host:search-zipkin53-mhdyquzbwwzwvln6phfzr3mmdi.ap-southeast-1.es.amazonaws.com\n"
      + "x-amz-date:20170830T143137Z\n"
      + "\n"
      + "host;x-amz-date\n"
      + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
}
 
Example 4
Source File: AWSSignatureVersion4Test.java    From zipkin-aws with Apache License 2.0 5 votes vote down vote up
@Test public void canonicalString_getDomain() {
  String timestamp = "20190730T134617Z";
  String yyyyMMdd = timestamp.substring(0, 8);
  AggregatedHttpRequest request = AggregatedHttpRequest.of(
      RequestHeaders.builder(HttpMethod.GET, "/2015-01-01/es/domain/zipkin")
          .set(AWSSignatureVersion4.X_AMZ_DATE, timestamp)
          .build()
  );
  ClientRequestContext ctx = ClientRequestContext.builder(request.toHttpRequest())
      .endpoint(Endpoint.of("es.ap-southeast-1.amazonaws.com"))
      .build();

  ByteBuf canonicalString = Unpooled.buffer();

  writeCanonicalString(ctx, request.headers(), request.content(), canonicalString);
  assertThat(canonicalString.toString(UTF_8)).isEqualTo(""
      + "GET\n"
      + "/2015-01-01/es/domain/zipkin\n"
      + "\n"
      + "host:es.ap-southeast-1.amazonaws.com\n"
      + "x-amz-date:" + timestamp + "\n"
      + "\n"
      + "host;x-amz-date\n"
      + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");

  ByteBuf toSign = Unpooled.buffer();
  AWSSignatureVersion4.writeToSign(timestamp,
      AWSSignatureVersion4.credentialScope(yyyyMMdd, "ap-southeast-1"), canonicalString, toSign);

  assertThat(toSign.toString(UTF_8)).isEqualTo(""
      + "AWS4-HMAC-SHA256\n"
      + "20190730T134617Z\n"
      + "20190730/ap-southeast-1/es/aws4_request\n"
      + "129dd8ded740553cd28544b4000982b8f88d7199b36a013fa89ee8e56c23f80e");
}
 
Example 5
Source File: WebClientBuilderTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void authorityHeader() {
    final String path = "/echo-path?foo=bar";
    final RequestHeadersBuilder requestHeadersBuilder =
            RequestHeaders.builder()
                          .authority("localhost:" + server.httpPort())
                          .scheme("h2c")
                          .add("param1", "val1")
                          .path(path);

    final AggregatedHttpRequest request = AggregatedHttpRequest.of(
            requestHeadersBuilder.method(HttpMethod.GET).build());
    final HttpResponse response = WebClient.of().execute(request);
    assertThat(response.aggregate().join().contentUtf8()).isEqualTo(path);
}
 
Example 6
Source File: AnnotatedServiceHandlersOrderTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void responseConverterOrder() throws Exception {
    final AggregatedHttpRequest aReq = AggregatedHttpRequest.of(
            HttpMethod.POST, "/1/responseConverterOrder", MediaType.PLAIN_TEXT_UTF_8, "foo");
    final AggregatedHttpResponse aRes = executeRequest(aReq);

    assertThat(aRes.status()).isEqualTo(HttpStatus.OK);
    // Converted from the ServiceLevelResponseConverter.
    assertThat(aRes.contentUtf8()).isEqualTo("hello foo");

    // method level(+1) -> class level(+1) -> service level(+1) -> server level(+1)
    assertThat(responseCounter.get()).isEqualTo(4);
}
 
Example 7
Source File: AnnotatedServiceHandlersOrderTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void exceptionHandlerOrder() throws Exception {
    final AggregatedHttpRequest aReq = AggregatedHttpRequest.of(
            HttpMethod.POST, "/1/exceptionHandlerOrder", MediaType.PLAIN_TEXT_UTF_8, "foo");
    final AggregatedHttpResponse aRes = executeRequest(aReq);

    assertThat(aRes.status()).isEqualTo(HttpStatus.NOT_IMPLEMENTED);
    // Converted from the default Handler which is DefaultExceptionHandler in AnnotatedServices.
    assertThat(aRes.contentUtf8()).isEqualTo("hello foo");

    // method level(+1) -> class level(+1) -> service level(+1) -> server level(+1) -> default
    assertThat(exceptionCounter.get()).isEqualTo(4);
}
 
Example 8
Source File: AWSSignatureVersion4.java    From zipkin-aws with Apache License 2.0 4 votes vote down vote up
AggregatedHttpRequest sign(ClientRequestContext ctx, AggregatedHttpRequest req) {
  AWSCredentials credentials = this.credentials.get();
  if (credentials == null) throw new NullPointerException("credentials == null");

  String timestamp = iso8601.get().format(new Date());
  String yyyyMMdd = timestamp.substring(0, 8);
  String credentialScope = credentialScope(yyyyMMdd, region);

  RequestHeadersBuilder builder = req.headers().toBuilder()
      .set(X_AMZ_DATE, timestamp);

  // TODO: Test this, it requires significant mocking since these ports can't normally be used by
  // a unit test.
  String authority = req.authority();
  int colonIndex = -1;
  if (authority != null) {
    colonIndex = authority.indexOf(':');
  }
  if (colonIndex != -1) {
    if (ctx.sessionProtocol().isTls() && authority.endsWith(":443") ||
        !ctx.sessionProtocol().isTls() && authority.endsWith(":80")) {
      // AWS requests must not include standard ports in host header.
      builder.authority(authority.substring(0, colonIndex));
    }
  }

  if (credentials.sessionToken != null) {
    builder.set(X_AMZ_SECURITY_TOKEN, credentials.sessionToken);
  }

  String signedHeaders = credentials.sessionToken == null ? HOST_DATE : HOST_DATE_TOKEN;
  ByteBuf canonicalString = ctx.alloc().heapBuffer();
  ByteBuf toSign = ctx.alloc().heapBuffer();
  try {
    writeCanonicalString(ctx, builder.build(), req.content(), canonicalString);
    writeToSign(timestamp, credentialScope, canonicalString, toSign);

    // TODO: this key is invalid when the secret key or the date change. both are very infrequent
    byte[] signatureKey = signatureKey(credentials.secretKey, yyyyMMdd);
    String signature = ByteBufUtil.hexDump(hmacSha256(signatureKey, toSign.nioBuffer()));

    String authorization = "AWS4-HMAC-SHA256 Credential="
        + credentials.accessKey
        + '/'
        + credentialScope
        + ", SignedHeaders="
        + signedHeaders
        + ", Signature="
        + signature;

    return AggregatedHttpRequest.of(
        builder.add(HttpHeaderNames.AUTHORIZATION, authorization).build(),
        req.content(),
        req.trailers());
  } finally {
    canonicalString.release();
    toSign.release();
  }
}
 
Example 9
Source File: HttpRequestAggregator.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregatedHttpRequest onSuccess(HttpData content) {
    return AggregatedHttpRequest.of(request.headers(), content, trailers);
}