public String getContentType() { return response.getContentType(); }
/** * 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(); }
private String contentType() { String contentType = ahcResponse.getContentType(); if (contentType == null) { // As defined by RFC-2616#7.2.1 contentType = "application/octet-stream"; } return contentType; }
public String getContentType() { return response.getContentType(); }
/** * <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; }
@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()); } }
@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)); }
@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)); }
@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\"}")); }
@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)); }