io.sphere.sdk.http.HttpRequest.getBody()方法的使用及代码示例

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

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

HttpRequest.getBody介绍

暂无

代码示例

代码示例来源:origin: commercetools/commercetools-jvm-sdk

private Optional<String> stringBodyOfHttpRequest() {
  return Optional.ofNullable(httpRequest)
      .map(r -> r.getBody())
      .filter(x -> x instanceof StringHttpRequestBody)
      .map(b -> ((StringHttpRequestBody) b).getSecuredBody());
}

代码示例来源:origin: io.sphere.jvmsdk/common

public void setUnderlyingHttpRequest(final HttpRequest httpRequest) {
  final String body = httpRequest.getBody().map(s -> JsonUtils.prettyPrintJsonStringSecureWithFallback(s)).orElse("<no body>");
  final String requestAsString = new StringBuilder(httpRequest.getHttpMethod().toString()).append(" ").append(httpRequest.getPath()).append("\n").append(body).toString();
  setUnderlyingHttpRequest(requestAsString);
}

代码示例来源:origin: io.sphere.sdk.jvm/java-client-core

private <T> CompletableFuture<T> execute(final SphereRequest<T> sphereRequest, final String token) {
  final HttpRequest httpRequest = createHttpRequest(sphereRequest, token);
  final SphereInternalLogger logger = getLogger(httpRequest);
  logger.debug(() -> sphereRequest);
  logger.trace(() -> {
    final String output;
    if (httpRequest.getBody().isPresent() && httpRequest.getBody().get() instanceof StringHttpRequestBody) {
      final StringHttpRequestBody body = (StringHttpRequestBody) httpRequest.getBody().get();
      final String unformattedJson = body.getString();
      output = "send: " + unformattedJson + "\nformatted: " + JsonUtils.prettyPrintJsonStringSecure(unformattedJson);
    } else {
      output = "no request body present";
    }
    return output;
  });
  return httpClient.
      execute(httpRequest).
      thenApply(preProcess(sphereRequest, objectMapper, config));
}

代码示例来源:origin: io.sphere.jvmsdk/java-client

@Override
public <T> CompletableFuture<T> execute(final ClientRequest<T> clientRequest) {
  final SphereInternalLogger logger = getLogger(clientRequest);
  logger.debug(() -> clientRequest);
  logger.trace(() -> {
    final Optional<String> requestBody = clientRequest.httpRequest().getBody();
    return requestBody.map(body -> JsonUtils.prettyPrintJsonStringSecure(body)).orElse("no request body present");
  });
  return requestExecutor.
      execute(clientRequest).
      thenApply(preProcess(clientRequest, clientRequest.resultMapper()));
}

代码示例来源:origin: com.commercetools.sdk.jvm.core/commercetools-java-client-core

private <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest, final String token, final int ttl) {
  final HttpRequest httpRequest = createHttpRequest(sphereRequest, token);
  final SphereInternalLogger logger = getLogger(httpRequest);
  logger.debug(() -> sphereRequest);
  logger.trace(() -> {
    final String output;
    final String httpMethodAndUrl = httpRequest.getHttpMethod() + " " + httpRequest.getUrl();
    if (httpRequest.getBody() != null && httpRequest.getBody() instanceof StringHttpRequestBody) {
      final StringHttpRequestBody body = (StringHttpRequestBody) httpRequest.getBody();
      final String unformattedBody = body.getSecuredBody();
      final boolean isJsonRequest = httpRequest.getHeaders().findFlatHeader(HttpHeaders.CONTENT_TYPE).map(ct -> ct.toLowerCase().contains("json")).orElse(true);
      if (isJsonRequest) {
        String prettyPrint;
        try {
          prettyPrint = SphereJsonUtils.prettyPrint(unformattedBody);
        } catch (final JsonException e) {
          classLogger.warn("pretty print failed", e);
          prettyPrint = unformattedBody;
        }
        output = "send: " + httpMethodAndUrl + "\nformatted: " + prettyPrint;
      } else {
        output = "send: " + httpRequest.getHttpMethod() + " " + httpRequest.getUrl() + " " + unformattedBody;
      }
    } else {
      output = httpMethodAndUrl + " <no body>";
    }
    return output;
  });
  return executeWithRecover(sphereRequest, httpRequest, ttl);
}

代码示例来源:origin: io.sphere.sdk.jvm/sphere-java-client-core

private <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest, final String token) {
  final HttpRequest httpRequest = createHttpRequest(sphereRequest, token);
  final SphereInternalLogger logger = getLogger(httpRequest);
  logger.debug(() -> sphereRequest);
  logger.trace(() -> {
    final String output;
    if (httpRequest.getBody() != null && httpRequest.getBody() instanceof StringHttpRequestBody) {
      final StringHttpRequestBody body = (StringHttpRequestBody) httpRequest.getBody();
      final String unformattedJson = body.getSecuredBody();
      output = "send: " + unformattedJson + "\nformatted: " + SphereJsonUtils.prettyPrint(unformattedJson);
    } else {
      output = "no request body present";
    }
    return output;
  });
  return httpClient.execute(httpRequest).thenApply(httpResponse -> {
    try {
      return processHttpResponse(sphereRequest, objectMapper, config, httpResponse);
    } catch (final SphereException e) {
      fillExceptionWithData(sphereRequest, httpResponse, e, config);
      throw e;
    }
  });
}

代码示例来源:origin: io.sphere.sdk.jvm/sdk-http-ahc-1_8

<T> Request asAhcRequest(final HttpRequest request) {
  final RequestBuilder builder = new RequestBuilder()
      .setUrl(request.getUrl())
      .setMethod(request.getHttpMethod().toString());
  request.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  Optional.ofNullable(request.getBody()).ifPresent(body -> {
    if (body instanceof StringHttpRequestBody) {
      final String bodyAsString = ((StringHttpRequestBody) body).getString();
      builder.setBodyEncoding(StandardCharsets.UTF_8.name()).setBody(bodyAsString);
    } else if (body instanceof FileHttpRequestBody) {
      final File file = ((FileHttpRequestBody) body).getFile();
      builder.setBody(out -> FileUtils.copyFile(file, out));
      final long length = file.length();
      builder.addHeader(HttpHeaders.CONTENT_LENGTH, "" + length);
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      final FormUrlEncodedHttpRequestBody formUrlEncodedHttpRequestBody = (FormUrlEncodedHttpRequestBody) body;
      formUrlEncodedHttpRequestBody.getParameters().forEach(pair -> builder.addParameter(pair.getName(), pair.getValue()));
    }
  });
  final Request ahcRequest = builder.build();
  return ahcRequest;
}

代码示例来源:origin: com.commercetools.sdk.jvm.core/sdk-http-ahc-1_8

Request asAhcRequest(final HttpRequest request) {
  final RequestBuilder builder = new RequestBuilder()
      .setUrl(request.getUrl())
      .setMethod(request.getHttpMethod().toString());
  request.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  Optional.ofNullable(request.getBody()).ifPresent(body -> {
    if (body instanceof StringHttpRequestBody) {
      final String bodyAsString = ((StringHttpRequestBody) body).getString();
      builder.setBodyEncoding(StandardCharsets.UTF_8.name()).setBody(bodyAsString);
    } else if (body instanceof FileHttpRequestBody) {
      final File file = ((FileHttpRequestBody) body).getFile();
      builder.setBody(out -> FileUtils.copyFile(file, out));
      final long length = file.length();
      builder.addHeader(HttpHeaders.CONTENT_LENGTH, "" + length);
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      final FormUrlEncodedHttpRequestBody formUrlEncodedHttpRequestBody = (FormUrlEncodedHttpRequestBody) body;
      formUrlEncodedHttpRequestBody.getParameters().forEach(pair -> builder.addParameter(pair.getName(), pair.getValue()));
    }
  });
  final Request ahcRequest = builder.build();
  return ahcRequest;
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

Request asAhcRequest(final HttpRequest request) {
  final RequestBuilder builder = new RequestBuilder()
      .setUrl(request.getUrl())
      .setMethod(request.getHttpMethod().toString());
  request.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  Optional.ofNullable(request.getBody()).ifPresent(body -> {
    if (body instanceof StringHttpRequestBody) {
      final String bodyAsString = ((StringHttpRequestBody) body).getString();
      builder.setBodyEncoding(StandardCharsets.UTF_8.name()).setBody(bodyAsString);
    } else if (body instanceof FileHttpRequestBody) {
      final File file = ((FileHttpRequestBody) body).getFile();
      builder.setBody(out -> FileUtils.copyFile(file, out));
      final long length = file.length();
      builder.addHeader(HttpHeaders.CONTENT_LENGTH, "" + length);
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      final FormUrlEncodedHttpRequestBody formUrlEncodedHttpRequestBody = (FormUrlEncodedHttpRequestBody) body;
      formUrlEncodedHttpRequestBody.getParameters().forEach(pair -> builder.addParameter(pair.getName(), pair.getValue()));
    }
  });
  final Request ahcRequest = builder.build();
  return ahcRequest;
}

代码示例来源:origin: com.commercetools.sdk.jvm.core/sdk-http-ahc-1_9

<T> Request asAhcRequest(final HttpRequest request) {
  final RequestBuilder builder = new RequestBuilder()
      .setUrl(request.getUrl())
      .setMethod(request.getHttpMethod().toString());
  request.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  Optional.ofNullable(request.getBody()).ifPresent(body -> {
    if (body instanceof StringHttpRequestBody) {
      final String bodyAsString = ((StringHttpRequestBody) body).getString();
      builder.setBodyEncoding(StandardCharsets.UTF_8.name()).setBody(bodyAsString);
    } else if (body instanceof FileHttpRequestBody) {
      builder.setBody(((FileHttpRequestBody) body).getFile());
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      final FormUrlEncodedHttpRequestBody formUrlEncodedHttpRequestBody = (FormUrlEncodedHttpRequestBody) body;
      formUrlEncodedHttpRequestBody.getParameters().forEach(pair -> builder.addFormParam(pair.getName(), pair.getValue()));
    }
  });
  final Request build = builder.build();
  return build;
}

代码示例来源:origin: io.sphere.sdk.jvm/java-client

<T> Request asNingRequest(final HttpRequest request) {
  final RequestBuilder builder = new RequestBuilder()
      .setUrl(request.getUrl())
      .setMethod(request.getHttpMethod().toString());
  request.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach( value -> builder.addHeader(name, value)));
  request.getBody().ifPresent(body -> {
    if (body instanceof StringHttpRequestBody) {
      final String bodyAsString = ((StringHttpRequestBody) body).getString();
      builder.setBodyEncoding(StandardCharsets.UTF_8.name()).setBody(bodyAsString);
    } else if (body instanceof FileHttpRequestBody) {
      builder.setBody(((FileHttpRequestBody) body).getFile());
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      ((FormUrlEncodedHttpRequestBody) body).getData().forEach((name, value) -> builder.addQueryParameter(name, value));
    }
  });
  final Request build = builder.build();
  return build;
}

代码示例来源:origin: io.sphere.sdk.jvm/sdk-http-ning-1_8

<T> Request asNingRequest(final HttpRequest request) {
  final RequestBuilder builder = new RequestBuilder()
      .setUrl(request.getUrl())
      .setMethod(request.getHttpMethod().toString());
  request.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach( value -> builder.addHeader(name, value)));
  request.getBody().ifPresent(body -> {
    if (body instanceof StringHttpRequestBody) {
      final String bodyAsString = ((StringHttpRequestBody) body).getString();
      builder.setBodyEncoding(StandardCharsets.UTF_8.name()).setBody(bodyAsString);
    } else if (body instanceof FileHttpRequestBody) {
      builder.setBody(((FileHttpRequestBody) body).getFile());
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      ((FormUrlEncodedHttpRequestBody) body).getData().forEach((name, value) -> builder.addQueryParameter(name, value));
    }
  });
  final Request build = builder.build();
  return build;
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

<T> Request asAhcRequest(final HttpRequest request) {
  final RequestBuilder builder = new RequestBuilder()
      .setUrl(request.getUrl())
      .setMethod(request.getHttpMethod().toString());
  request.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  Optional.ofNullable(request.getBody()).ifPresent(body -> {
    if (body instanceof StringHttpRequestBody) {
      final String bodyAsString = ((StringHttpRequestBody) body).getString();
      builder.setBodyEncoding(StandardCharsets.UTF_8.name()).setBody(bodyAsString);
    } else if (body instanceof FileHttpRequestBody) {
      builder.setBody(((FileHttpRequestBody) body).getFile());
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      final FormUrlEncodedHttpRequestBody formUrlEncodedHttpRequestBody = (FormUrlEncodedHttpRequestBody) body;
      formUrlEncodedHttpRequestBody.getParameters().forEach(pair -> builder.addFormParam(pair.getName(), pair.getValue()));
    }
  });
  final Request build = builder.build();
  return build;
}

代码示例来源:origin: com.commercetools.sdk.jvm.core/sdk-http-apache-async

private HttpUriRequest toApacheRequest(final HttpRequest httpRequest) throws UnsupportedEncodingException {
  final String method = httpRequest.getHttpMethod().toString();
  final String uri = httpRequest.getUrl();
  final RequestBuilder builder = RequestBuilder
      .create(method)
      .setUri(uri);
  httpRequest.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  if (httpRequest.getBody() != null) {
    final HttpRequestBody body = httpRequest.getBody();
    final HttpEntity httpEntity;
    if (body instanceof StringHttpRequestBody) {
      final StringEntity stringEntity = new StringEntity(((StringHttpRequestBody) body).getString(), StandardCharsets.UTF_8);
      stringEntity.setContentType(ContentType.APPLICATION_JSON.toString());
      httpEntity = stringEntity;
    } else if (body instanceof FileHttpRequestBody) {
      httpEntity = new FileEntity(((FileHttpRequestBody)body).getFile());
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      httpEntity = urlEncodedOf((FormUrlEncodedHttpRequestBody) body);
    } else {
      throw new HttpException("Cannot interpret request " + httpRequest);
    }
    builder.setEntity(httpEntity);
  }
  return builder.build();
}

代码示例来源:origin: io.sphere.jvmsdk/java-client

<T> Request asNingRequest(final Requestable requestable) {
  final HttpRequest request = requestable.httpRequest();
  final RequestBuilder builder = new RequestBuilder().
      setUrl(CharMatcher.is('/').trimTrailingFrom(coreUrl) + "/" + projectKey + request.getPath()).
      setMethod(request.getHttpMethod().toString()).
      setHeader("User-Agent", "SPHERE.IO JVM SDK version " + BuildInfo.version()).
      setHeader("Authorization", "Bearer " + clientCredentials.getAccessToken());
  return request.getBody().map(builder::setBody).orElse(builder).build();
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

Request asAhcRequest(final HttpRequest request) {
  final RequestBuilder builder = new RequestBuilder()
      .setUrl(request.getUrl())
      .setMethod(request.getHttpMethod().toString());
  request.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  Optional.ofNullable(request.getBody()).ifPresent(body -> {
    if (body instanceof StringHttpRequestBody) {
      final String bodyAsString = ((StringHttpRequestBody) body).getString();
      builder.setBody(bodyAsString);
      if (!request.getHeaders().findFlatHeader(HttpHeaders.CONTENT_TYPE).isPresent()) {
        builder.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
      }
    } else if (body instanceof FileHttpRequestBody) {
      builder.setBody(((FileHttpRequestBody) body).getFile());
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      final FormUrlEncodedHttpRequestBody formUrlEncodedHttpRequestBody = (FormUrlEncodedHttpRequestBody) body;
      formUrlEncodedHttpRequestBody.getParameters().forEach(pair -> builder.addFormParam(pair.getName(), pair.getValue()));
    }
  });
  return builder.build();
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

private HttpUriRequest toApacheRequest(final HttpRequest httpRequest) throws UnsupportedEncodingException {
  final String method = httpRequest.getHttpMethod().toString();
  final String uri = httpRequest.getUrl();
  final RequestBuilder builder = RequestBuilder
      .create(method)
      .setUri(uri);
  httpRequest.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  if (httpRequest.getBody() != null) {
    final HttpRequestBody body = httpRequest.getBody();
    final HttpEntity httpEntity;
    if (body instanceof StringHttpRequestBody) {
      final StringEntity stringEntity = new StringEntity(((StringHttpRequestBody) body).getString(), StandardCharsets.UTF_8);
      stringEntity.setContentType(ContentType.APPLICATION_JSON.toString());
      httpEntity = stringEntity;
    } else if (body instanceof FileHttpRequestBody) {
      httpEntity = new FileEntity(((FileHttpRequestBody)body).getFile());
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      httpEntity = urlEncodedOf((FormUrlEncodedHttpRequestBody) body);
    } else {
      throw new HttpException("Cannot interpret request " + httpRequest);
    }
    builder.setEntity(httpEntity);
  }
  return builder.build();
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

private HttpUriRequest toApacheRequest(final HttpRequest httpRequest) throws UnsupportedEncodingException {
  final String method = httpRequest.getHttpMethod().toString();
  final String uri = httpRequest.getUrl();
  final RequestBuilder builder = RequestBuilder
      .create(method)
      .setUri(uri);
  httpRequest.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  if (httpRequest.getBody() != null) {
    final HttpRequestBody body = httpRequest.getBody();
    final HttpEntity httpEntity;
    if (body instanceof StringHttpRequestBody) {
      final StringEntity stringEntity = new StringEntity(((StringHttpRequestBody) body).getString(), StandardCharsets.UTF_8);
      stringEntity.setContentType(ContentType.APPLICATION_JSON.toString());
      httpEntity = stringEntity;
    } else if (body instanceof FileHttpRequestBody) {
      httpEntity = new FileEntity(((FileHttpRequestBody)body).getFile());
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      httpEntity = urlEncodedOf((FormUrlEncodedHttpRequestBody) body);
    } else {
      throw new HttpException("Cannot interpret request " + httpRequest);
    }
    builder.setEntity(httpEntity);
  }
  return builder.build();
}

代码示例来源:origin: com.commercetools.sdk.jvm.core/sdk-http-ahc-2_0

Request asAhcRequest(final HttpRequest request) {
  final RequestBuilder builder = new RequestBuilder()
      .setUrl(request.getUrl())
      .setMethod(request.getHttpMethod().toString());
  request.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  Optional.ofNullable(request.getBody()).ifPresent(body -> {
    if (body instanceof StringHttpRequestBody) {
      final String bodyAsString = ((StringHttpRequestBody) body).getString();
      builder.setBody(bodyAsString);
      if (!request.getHeaders().findFlatHeader(HttpHeaders.CONTENT_TYPE).isPresent()) {
        builder.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
      }
    } else if (body instanceof FileHttpRequestBody) {
      builder.setBody(((FileHttpRequestBody) body).getFile());
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      final FormUrlEncodedHttpRequestBody formUrlEncodedHttpRequestBody = (FormUrlEncodedHttpRequestBody) body;
      formUrlEncodedHttpRequestBody.getParameters().forEach(pair -> builder.addFormParam(pair.getName(), pair.getValue()));
    }
  });
  return builder.build();
}

代码示例来源:origin: commercetools/commercetools-jvm-sdk

Request asAhcRequest(final HttpRequest request) {
  final RequestBuilder builder = new RequestBuilder()
      .setUrl(request.getUrl())
      .setMethod(request.getHttpMethod().toString());
  request.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value)));
  Optional.ofNullable(request.getBody()).ifPresent(body -> {
    if (body instanceof StringHttpRequestBody) {
      final String bodyAsString = ((StringHttpRequestBody) body).getString();
      builder.setBody(bodyAsString);
      if (!request.getHeaders().findFlatHeader(HttpHeaders.CONTENT_TYPE).isPresent()) {
        builder.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
      }
    } else if (body instanceof FileHttpRequestBody) {
      builder.setBody(((FileHttpRequestBody) body).getFile());
    } else if (body instanceof FormUrlEncodedHttpRequestBody) {
      final FormUrlEncodedHttpRequestBody formUrlEncodedHttpRequestBody = (FormUrlEncodedHttpRequestBody) body;
      formUrlEncodedHttpRequestBody.getParameters().forEach(pair -> builder.addFormParam(pair.getName(), pair.getValue()));
    }
  });
  return builder.build();
}

相关文章