本文整理了Java中org.asynchttpclient.Response.getContentType
方法的一些代码示例,展示了Response.getContentType
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Response.getContentType
方法的具体详情如下:
包路径:org.asynchttpclient.Response
类名称:Response
方法名:getContentType
[英]Return the content-type header value.
[中]返回内容类型标题值。
代码示例来源:origin: AsyncHttpClient/async-http-client
public String getContentType() {
return response.getContentType();
}
代码示例来源:origin: AsyncHttpClient/async-http-client
/**
* Converts async-http-client response to okhttp response.
*
* @param asyncHttpClientResponse async-http-client response
* @return okhttp response.
* @throws NullPointerException in case of null arguments
*/
private Response toOkhttpResponse(org.asynchttpclient.Response asyncHttpClientResponse) {
// status code
val rspBuilder = new Response.Builder()
.request(request())
.protocol(Protocol.HTTP_1_1)
.code(asyncHttpClientResponse.getStatusCode())
.message(asyncHttpClientResponse.getStatusText());
// headers
if (asyncHttpClientResponse.hasResponseHeaders()) {
asyncHttpClientResponse.getHeaders().forEach(e -> rspBuilder.header(e.getKey(), e.getValue()));
}
// body
if (asyncHttpClientResponse.hasResponseBody()) {
val contentType = asyncHttpClientResponse.getContentType() == null
? null : MediaType.parse(asyncHttpClientResponse.getContentType());
val okHttpBody = ResponseBody.create(contentType, asyncHttpClientResponse.getResponseBodyAsBytes());
rspBuilder.body(okHttpBody);
} else {
rspBuilder.body(EMPTY_BODY);
}
return rspBuilder.build();
}
代码示例来源:origin: com.typesafe.play/play-java-ws
private String contentType() {
String contentType = ahcResponse.getContentType();
if (contentType == null) {
// As defined by RFC-2616#7.2.1
contentType = "application/octet-stream";
}
return contentType;
}
代码示例来源:origin: org.asynchttpclient/async-http-client-api
public String getContentType() {
return response.getContentType();
}
代码示例来源:origin: ribasco/async-gamequery-lib
/**
* <p>Function that is responsible for parsing the internal response of the message (e.g. JSON or XML)</p>
*
* @param response
*
* @return Returns instance of {@link AbstractWebApiResponse}
*/
private Res applyContentTypeProcessor(Res response) {
if (response != null && response.getMessage() != null) {
Response msg = response.getMessage();
String body = msg.getResponseBody();
ContentTypeProcessor processor = contentProcessorMap.get(parseContentType(msg.getContentType()));
if (log.isDebugEnabled() && processor == null)
log.debug("No Content-Type processor found for {}. Please register a ContentTypeProcessor using registerContentProcessor()", parseContentType(msg.getContentType()));
response.setProcessedContent((processor != null) ? processor.apply(body) : body);
}
return response;
}
代码示例来源:origin: com.tradeshift.sdk/tradeshift-sdk-core
@Override
public TradeshiftException read(UserContext ctx, Response response) {
final Optional<String> message = parseMsg(response.getContentType(), response.getResponseBodyAsStream());
switch (response.getStatusCode()) {
case HTTP_BAD_REQUEST:
return message.map(m -> new BadRequest(ctx, m)).orElseGet(() -> new BadRequest(ctx));
case HTTP_UNAUTHORIZED:
return message.map(m -> new Unauthorized(ctx, m)).orElseGet(() -> new Unauthorized(ctx));
case HTTP_FORBIDDEN:
return message.map(m -> new Forbidden(ctx, m)).orElseGet(() -> new Forbidden(ctx));
case HTTP_NOT_FOUND:
return message.map(m -> new NotFound(ctx, m)).orElseGet(() -> new NotFound(ctx));
case HTTP_INTERNAL_ERROR:
return message.map(m -> new SystemException(ctx, m))
.orElseGet(() -> new SystemException(ctx, "Internal server error"));
default:
int httpStatus = response.getStatusCode();
if (httpStatus >= HTTP_BAD_REQUEST && httpStatus < HTTP_INTERNAL_ERROR) {
return message.map(m -> new ClientError(ctx, httpStatus, m))
.orElseGet(() -> new ClientError(ctx, httpStatus, null));
} else if (httpStatus >= HTTP_INTERNAL_ERROR) {
return message.map(m -> new ServerError(ctx, m))
.orElseGet(() -> new ServerError(ctx, null));
}
return new SystemException(ctx, "Unexpected status code from server: " + response.getStatusCode());
}
}
代码示例来源:origin: otto-de/edison-microservice
@Test
public void shouldReturn403WhenRequestingWithoutOauthToken() throws Exception {
// Given
// When
final Response response = asyncHttpClient
.prepareGet(baseUrl + "/api/hello")
.addQueryParam("context", "mode")
.addHeader(ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE)
.execute()
.get();
// Then
assertThat(response.getStatusCode(), is(403));
assertThat(response.getContentType(), is(MediaType.APPLICATION_JSON_UTF8_VALUE));
}
代码示例来源:origin: otto-de/edison-microservice
@Test
public void shouldReturn403WhenRequestingWithInvalidOauthToken() throws Exception {
// Given
final String bearerToken = "someInvalidBearerToken";
// When
final Response response = asyncHttpClient
.prepareGet(baseUrl + "/api/hello")
.addQueryParam("context", "mode")
.addHeader(AUTHORIZATION, bearerToken)
.addHeader(ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE)
.execute()
.get();
// Then
assertThat(response.getStatusCode(), is(403));
assertThat(response.getContentType(), is(MediaType.APPLICATION_JSON_UTF8_VALUE));
}
代码示例来源:origin: otto-de/edison-microservice
@Test
public void shouldReturnHelloResponseWithValidOauthToken() throws Exception {
// Given
final String bearerToken = oAuthTestHelper.getBearerToken("hello.read");
// When
final Response response = asyncHttpClient
.prepareGet(baseUrl + "/api/hello")
.addQueryParam("context", "mode")
.addHeader(AUTHORIZATION, bearerToken)
.addHeader(ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE)
.execute()
.get();
// Then
assertThat(response.getStatusCode(), is(200));
assertThat(response.getContentType(), is(MediaType.APPLICATION_JSON_UTF8_VALUE));
assertThat(response.getResponseBody(), is("{\"hello\": \"world\"}"));
}
代码示例来源:origin: otto-de/edison-microservice
@Test
public void shouldReturn403WhenRequestingWithInvalidScopeInOauthToken() throws Exception {
// Given
final String bearerToken = oAuthTestHelper.getBearerToken("hello.write");
// When
final Response response = asyncHttpClient
.prepareGet(baseUrl + "/api/hello")
.addQueryParam("context", "mode")
.addHeader(AUTHORIZATION, bearerToken)
.addHeader(ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE)
.execute()
.get();
// Then
assertThat(response.getStatusCode(), is(403));
assertThat(response.getContentType(), is(MediaType.APPLICATION_JSON_UTF8_VALUE));
}
内容来源于网络,如有侵权,请联系作者删除!