/** * Transforms an event stream message into a {@link SdkHttpFullResponse} so we can reuse our existing generated unmarshallers. * * @param message Message to transform. */ private SdkHttpFullResponse adaptMessageToResponse(Message message, boolean isException) { Map<String, List<String>> headers = message.getHeaders() .entrySet() .stream() .collect(HashMap::new, (m, e) -> m.put(e.getKey(), singletonList(e.getValue().getString())), Map::putAll); if (requestId != null) { headers.put(X_AMZN_REQUEST_ID_HEADER, singletonList(requestId)); } SdkHttpFullResponse.Builder builder = SdkHttpFullResponse.builder() .content(AbortableInputStream.create(new ByteArrayInputStream(message.getPayload()))) .headers(headers); if (!isException) { builder.statusCode(200); } return builder.build(); }
private SdkHttpFullResponse fullResponse(TestItemUnmarshalling item) { AbortableInputStream abortableInputStream = AbortableInputStream.create(new ByteArrayInputStream(item.utf8())); return SdkHttpFullResponse.builder() .statusCode(200) .content(abortableInputStream) .build(); }
/** * Creates and initializes an HttpResponse object suitable to be passed to an HTTP response * handler object. * * @return The new, initialized HttpResponse object ready to be passed to an HTTP response handler object. * @throws IOException If there were any problems getting any response information from the * HttpClient method object. */ private SdkHttpFullResponse createResponse(org.apache.http.HttpResponse apacheHttpResponse, HttpRequestBase apacheRequest) throws IOException { return SdkHttpFullResponse.builder() .statusCode(apacheHttpResponse.getStatusLine().getStatusCode()) .statusText(apacheHttpResponse.getStatusLine().getReasonPhrase()) .content(apacheHttpResponse.getEntity() != null ? toAbortableInputStream(apacheHttpResponse, apacheRequest) : null) .headers(transformHeaders(apacheHttpResponse)) .build(); }
private static SdkHttpFullResponse toFullResponse(SdkHttpResponse response) { SdkHttpFullResponse.Builder builder = SdkHttpFullResponse.builder() .statusCode(response.statusCode()) .headers(response.headers()); response.statusText().ifPresent(builder::statusText); return builder.build(); } }
@Override protected void channelRead0(ChannelHandlerContext channelContext, HttpObject msg) throws Exception { RequestContext requestContext = channelContext.channel().attr(REQUEST_CONTEXT_KEY).get(); if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; SdkHttpResponse sdkResponse = SdkHttpFullResponse.builder() .headers(fromNettyHeaders(response.headers())) .statusCode(response.status().code()) .statusText(response.status().reasonPhrase()) .build(); channelContext.channel().attr(KEEP_ALIVE).set(HttpUtil.isKeepAlive(response)); requestContext.handler().onHeaders(sdkResponse); } CompletableFuture<Void> ef = executeFuture(channelContext); if (msg instanceof StreamedHttpResponse) { requestContext.handler().onStream( new PublisherAdapter((StreamedHttpResponse) msg, channelContext, requestContext, ef)); } else if (msg instanceof FullHttpResponse) { // Be prepared to take care of (ignore) a trailing LastHttpResponse // from the HttpClientCodec if there is one. channelContext.pipeline().replace(HttpStreamsClientHandler.class, channelContext.name() + "-LastHttpContentSwallower", new LastHttpContentSwallower()); ByteBuf fullContent = ((FullHttpResponse) msg).content(); ByteBuffer bb = copyToByteBuffer(fullContent); fullContent.release(); requestContext.handler().onStream(new FullResponseContentPublisher(channelContext, bb, ef)); finalizeResponse(requestContext, channelContext); } }
@Override protected void channelRead0(ChannelHandlerContext channelContext, HttpObject msg) throws Exception { RequestContext requestContext = channelContext.channel().attr(REQUEST_CONTEXT_KEY).get(); if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; SdkHttpResponse sdkResponse = SdkHttpFullResponse.builder() .headers(fromNettyHeaders(response.headers())) .statusCode(response.status().code()) .statusText(response.status().reasonPhrase()) .build(); channelContext.channel().attr(KEEP_ALIVE).set(HttpUtil.isKeepAlive(response)); requestContext.handler().onHeaders(sdkResponse); } CompletableFuture<Void> ef = executeFuture(channelContext); if (msg instanceof StreamedHttpResponse) { requestContext.handler().onStream( new PublisherAdapter((StreamedHttpResponse) msg, channelContext, requestContext, ef)); } else if (msg instanceof FullHttpResponse) { // Be prepared to take care of (ignore) a trailing LastHttpResponse // from the HttpClientCodec if there is one. channelContext.pipeline().replace(HttpStreamsClientHandler.class, channelContext.name() + "-LastHttpContentSwallower", LastHttpContentSwallower.getInstance()); ByteBuf fullContent = ((FullHttpResponse) msg).content(); ByteBuffer bb = copyToByteBuffer(fullContent); fullContent.release(); requestContext.handler().onStream(new FullResponseContentPublisher(channelContext, bb, ef)); finalizeResponse(requestContext, channelContext); } }