com.hotels.styx.api.HttpResponse.newBuilder()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(158)

本文整理了Java中com.hotels.styx.api.HttpResponse.newBuilder()方法的一些代码示例,展示了HttpResponse.newBuilder()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpResponse.newBuilder()方法的具体详情如下:
包路径:com.hotels.styx.api.HttpResponse
类名称:HttpResponse
方法名:newBuilder

HttpResponse.newBuilder介绍

[英]Return a new HttpResponse.Builder that will inherit properties from this response.

This allows a new response to be made that is identical to this one except for the properties overridden by the builder methods.
[中]返回一个新的HttpResponse。将从此响应继承属性的生成器。
这允许做出与此响应相同的新响应,但生成器方法覆盖的属性除外。

代码示例

代码示例来源:origin: com.hotels.styx/styx-api

private DecodedResponse(HttpResponse response, T content) {
  this.responseBuilder = response.newBuilder().body(NO_BODY);
  this.content = content;
}

代码示例来源:origin: com.hotels.styx/styx-client

private HttpResponse addOriginId(Id originId, HttpResponse response) {
    return response.newBuilder()
        .header(originIdHeaderName, originId)
        .build();
  }
}

代码示例来源:origin: com.hotels.styx/styx-client

private HttpResponse removeRedundantContentLengthHeader(HttpResponse response) {
  if (contentValidation && response.contentLength().isPresent() && response.chunked()) {
    return response.newBuilder()
        .removeHeader(CONTENT_LENGTH)
        .build();
  }
  return response;
}

代码示例来源:origin: com.hotels.styx/styx-client

private static HttpResponse responseWithoutBody(HttpResponse response) {
  return response.newBuilder()
      .header(CONTENT_LENGTH, 0)
      .removeHeader(TRANSFER_ENCODING)
      .removeBody()
      .build();
}

代码示例来源:origin: HotelsDotCom/styx

@Override
public Eventual<LiveHttpResponse> handle(LiveHttpRequest request, HttpInterceptor.Context context) {
  HttpResponse.Builder responseBuilder = standardResponse.newBuilder()
      .headers(request.headers())
      .header(STUB_ORIGIN_INFO, origin.applicationInfo());
  return Eventual.of(Optional.ofNullable(responseBuilder)
      .map(it -> request.queryParam("status")
          .map(status -> it.status(httpResponseStatus(status))
              .body("Returning requested status (" + status + ")", UTF_8))
          .orElse(it))
      .map(it -> request.queryParam("length")
          .map(length -> it.body(generateContent(parseInt(length)), UTF_8))
          .orElse(it))
      .orElse(responseBuilder)
      .build()
      .stream());
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void shouldNotFailToRemoveNonExistentContentLength() {
  HttpResponse response = HttpResponse.response().build();
  HttpResponse chunkedResponse = response.newBuilder().setChunked().build();
  assertThat(chunkedResponse.chunked(), is(true));
  assertThat(chunkedResponse.header(CONTENT_LENGTH).isPresent(), is(false));
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void shouldRemoveContentLengthFromChunkedMessages() {
  HttpResponse response = HttpResponse.response().header(CONTENT_LENGTH, 5).build();
  HttpResponse chunkedResponse = response.newBuilder().setChunked().build();
  assertThat(chunkedResponse.chunked(), is(true));
  assertThat(chunkedResponse.header(CONTENT_LENGTH).isPresent(), is(false));
}

代码示例来源:origin: com.hotels.styx/styx-client

private HttpResponse addStickySessionIdentifier(HttpResponse httpResponse, Origin origin) {
  if (this.loadBalancer instanceof StickySessionLoadBalancingStrategy) {
    int maxAge = backendService.stickySessionConfig().stickySessionTimeoutSeconds();
    return httpResponse.newBuilder()
        .addCookie(newStickySessionCookie(id, origin.id(), maxAge))
        .build();
  } else {
    return httpResponse;
  }
}

代码示例来源:origin: com.hotels.styx/styx-client

}).map(response -> response.newBuilder().request(request).build());

代码示例来源:origin: HotelsDotCom/styx

@Test
public void canRemoveResponseBody() {
  HttpResponse response = response(NO_CONTENT)
      .body("shouldn't be here", UTF_8)
      .build();
  HttpResponse shouldClearBody = response.newBuilder()
      .body("", UTF_8)
      .build();
  assertThat(shouldClearBody.bodyAs(UTF_8), is(""));
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void allowsModificationOfBodyBasedOnExistingBody() {
  HttpResponse response = HttpResponse.response()
      .body("foobar", UTF_8)
      .build();
  HttpResponse newResponse = response.newBuilder()
      .body(response.bodyAs(UTF_8) + "x", UTF_8)
      .build();
  assertThat(newResponse.bodyAs(UTF_8), is("foobarx"));
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void canRemoveAHeader() {
  Object headerValue = "b";
  HttpResponse response = HttpResponse.response()
      .header("a", headerValue)
      .addHeader("c", headerValue)
      .build();
  HttpResponse shouldRemoveHeader = response.newBuilder()
      .removeHeader("c")
      .build();
  assertThat(shouldRemoveHeader.headers(), contains(header("a", "b")));
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void allowsModificationOfHeadersBasedOnBody() {
  HttpResponse response = HttpResponse.response()
      .body("foobar", UTF_8)
      .build();
  assertThat(response.header("some-header"), isAbsent());
  HttpResponse newResponse = response.newBuilder()
      .header("some-header", response.body().length)
      .build();
  assertThat(newResponse.header("some-header"), isValue("6"));
  assertThat(newResponse.bodyAs(UTF_8), is("foobar"));
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void newCookiesWithDuplicateNamesOverridePreviousOnes() {
  HttpResponse r1 = response()
      .cookies(responseCookie("y", "y1").build())
      .build();
  HttpResponse r2 = r1.newBuilder().addCookies(
      responseCookie("y", "y2").build())
      .build();
  assertThat(r2.cookies(), containsInAnyOrder(responseCookie("y", "y2").build()));
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void removesCookies() {
  HttpResponse r1 = response()
      .addCookies(responseCookie("x", "x1").build(), responseCookie("y", "y1").build())
      .build();
  HttpResponse r2 = r1.newBuilder()
      .removeCookies("x")
      .removeCookies("foo") // ensure that trying to remove a non-existent cookie does not cause Exception
      .build();
  assertThat(r2.cookies(), contains(responseCookie("y", "y1").build()));
}

代码示例来源:origin: HotelsDotCom/styx

@Override
public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) {
  String header = xHcomPluginsHeader(request);
  final String configPath = environment.pluginConfig(String.class);
  String pluginsList = environment.configuration().get("plugins.active").get();
  LiveHttpRequest newRequest = request.newBuilder()
      .header(X_HCOM_PLUGINS_HEADER, header)
      .header(X_HCOM_PLUGINS_CONFIGURATION_PATH, configPath)
      .header(X_HCOM_PLUGINS_LIST, pluginsList)
      .header("X-Hcom-Styx-Started", styxStarted)
      .header("X-Hcom-Styx-Stopped", styxStopped)
      .build();
  Function<ByteBuf, String> byteBufStringFunction = byteBuf -> byteBuf.toString(Charsets.UTF_8);
  return chain.proceed(newRequest)
      .flatMap(response -> response.aggregate(1 * 1024 * 1024))
      .map(response ->
          response.newBuilder()
              .header(X_HCOM_PLUGINS_HEADER, header)
              .header(X_HCOM_PLUGINS_CONFIGURATION_PATH, configPath)
              .header(X_HCOM_PLUGINS_LIST, pluginsList)
              .header("X-Hcom-Styx-Started", styxStarted)
              .header("X-Hcom-Styx-Stopped", styxStopped)
              .build())
      .map(HttpResponse::stream);
}

相关文章