本文整理了Java中io.sphere.sdk.http.HttpRequest.getHeaders()
方法的一些代码示例,展示了HttpRequest.getHeaders()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.getHeaders()
方法的具体详情如下:
包路径:io.sphere.sdk.http.HttpRequest
类名称:HttpRequest
方法名:getHeaders
暂无
代码示例来源:origin: commercetools/commercetools-jvm-sdk
@Override
public CompletionStage<HttpResponse> execute(final HttpRequest httpRequest) {
lastUserAgent = httpRequest.getHeaders().getHeader(HttpHeaders.USER_AGENT).stream().findFirst().orElse(null);
return delegate.execute(httpRequest);
}
代码示例来源: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-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
@Test
public void shouldSendCorrelationId() throws Exception {
final CompletableFuture<HttpResponse> successful = CompletableFutureUtils
.successful(HttpResponse.of(200, "{\"access_token\": \"access_token\"}"));
when(httpClient.execute(requestCaptor.capture())).thenReturn(successful);
tokensSupplier.get().toCompletableFuture().get();
final HttpRequest httpRequest = requestCaptor.getValue();
final Optional<String> correlationIdHeader = httpRequest.getHeaders().findFlatHeader(HttpHeaders.X_CORRELATION_ID);
assertThat(correlationIdHeader).isPresent();
final String correlationId = correlationIdHeader.get();
final String[] correlationIdParts = correlationId.split("/");
assertThat(correlationIdParts).hasSize(2);
assertThat(correlationIdParts[0]).isEqualTo(authConfig.getProjectKey());
}
}
代码示例来源: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
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: 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: 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: 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: 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: commercetools/commercetools-jvm-sdk
assertThat(response).isEqualTo(DummySphereRequest.DEFAULT_RESPONSE_OBJECT);
final HttpHeaders headers = httpRequestArgumentCaptor.getValue().getHeaders();
final Optional<String> correlationId = headers.findFlatHeader(HttpHeaders.X_CORRELATION_ID);
代码示例来源: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: commercetools/commercetools-jvm-sdk
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: commercetools/commercetools-jvm-sdk
@Test
public void httpRequestsContainUserAgents() throws Exception {
final SphereAuthConfig authConfig = SphereAuthConfig.of("a", "b", "c");
final RecordHttpClient httpClient = new RecordHttpClient();
SphereAccessTokenSupplier.ofOneTimeFetchingToken(authConfig, httpClient, true).get();
final String expectedValue = BuildInfo.userAgent();
final String header = httpClient.request.getHeaders().findFlatHeader(HttpHeaders.USER_AGENT).orElse("");
assertThat(header).matches("^(?<sdkOrClient>[^\\s\\/]+)(\\/(?<sdkOrClientVersion>\\S+))?(\\s+\\((?<sdkOrClientInfo>[^(]*)\\))?\\s+(?<environment>[^\\s\\/]+)(\\/(?<environmentVersion>\\S+))?(\\s+\\((?<environmentInfo>[^(]*)\\))?\\s+(?<solution>[^\\s\\/]+)(\\/(?<solutionVersion>\\S+))?(\\s+\\((?<solutionInfo>[^(]*)\\))?$").contains(expectedValue);
}
代码示例来源:origin: commercetools/commercetools-jvm-sdk
private HttpResponse executeSync(final HttpRequest httpRequest) {
if (httpRequest.getUrl().contains("oauth")) {
if (tokenValid && !tokenNew) {
return HttpResponse.of(200, String.format("{\"access_token\":\"first-token\",\"token_type\":\"Bearer\",\"expires_in\":172800,\"scope\":\"manage_project:%s\"}", config.getProjectKey()));
} else {
tokenValid = true;
tokenNew = true;
return HttpResponse.of(200, String.format("{\"access_token\":\"second-token\",\"token_type\":\"Bearer\",\"expires_in\":172800,\"scope\":\"manage_project:%s\"}", config.getProjectKey()));
}
}
if (httpRequest.getUrl().contains("cat-id")) {
tokenValid = false;//after that, the token expires
return HttpResponse.of(404);
}
if (httpRequest.getUrl().contains("channel-id")) {
if (tokenValid && httpRequest.getHeaders().getHeader(HttpHeaders.AUTHORIZATION).get(0).equals("Bearer second-token")) {
return HttpResponse.of(404);
} else {
return HttpResponse.of(401, "{\"statusCode\":401,\"message\":\"invalid_token\",\"errors\":[{\"code\":\"invalid_token\",\"message\":\"invalid_token\"}],\"error\":\"invalid_token\"}");
}
}
return HttpResponse.of(500);
}
代码示例来源: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: 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();
}
内容来源于网络,如有侵权,请联系作者删除!