use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project BRFS by zhangnianli.
the class ResponseSender method sendError.
public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project BRFS by zhangnianli.
the class ResponseSender method sendError.
public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project ambry by linkedin.
the class NettyResponseChannel method write.
@Override
public Future<Long> write(ByteBuffer src, Callback<Long> callback) {
long writeProcessingStartTime = System.currentTimeMillis();
if (!responseMetadataWriteInitiated.get()) {
maybeWriteResponseMetadata(responseMetadata, new ResponseMetadataWriteListener());
}
Chunk chunk = new Chunk(src, callback);
chunksToWrite.add(chunk);
if (HttpUtil.isContentLengthSet(finalResponseMetadata) && totalBytesReceived.get() > HttpUtil.getContentLength(finalResponseMetadata)) {
Exception exception = new IllegalStateException("Size of provided content [" + totalBytesReceived.get() + "] is greater than Content-Length set [" + HttpUtil.getContentLength(finalResponseMetadata) + "]");
if (!writeFuture.isDone()) {
writeFuture.setFailure(exception);
}
writeFuture.addListener(new CleanupCallback(exception));
} else if (!isOpen()) {
// the isOpen() check is not before addition to the queue because chunks need to be acknowledged in the order
// they were received. If we don't add it to the queue and clean up, chunks may be acknowledged out of order.
logger.debug("Scheduling a chunk cleanup on channel {} because response channel is closed", ctx.channel());
writeFuture.addListener(new CleanupCallback(new ClosedChannelException()));
} else if (finalResponseMetadata instanceof FullHttpResponse) {
logger.debug("Scheduling a chunk cleanup on channel {} because Content-Length is 0", ctx.channel());
Exception exception = null;
// this is only allowed to be a 0 sized buffer.
if (src.remaining() > 0) {
exception = new IllegalStateException("Provided non zero size content after setting Content-Length to 0");
if (!writeFuture.isDone()) {
writeFuture.setFailure(exception);
}
}
writeFuture.addListener(new CleanupCallback(exception));
} else {
chunkedWriteHandler.resumeTransfer();
}
long writeProcessingTime = System.currentTimeMillis() - writeProcessingStartTime;
nettyMetrics.writeProcessingTimeInMs.update(writeProcessingTime);
if (request != null) {
request.getMetricsTracker().nioMetricsTracker.addToResponseProcessingTime(writeProcessingTime);
}
return chunk.future;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project ambry by linkedin.
the class ChannelWriteCallback method errorResponseTest.
/**
* Tests that error responses are correctly formed.
*/
@Test
public void errorResponseTest() {
EmbeddedChannel channel = createEmbeddedChannel();
for (RestServiceErrorCode errorCode : RestServiceErrorCode.values()) {
HttpHeaders httpHeaders = new DefaultHttpHeaders();
httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, errorCode);
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.HEAD, TestingUri.OnResponseCompleteWithRestException.toString(), httpHeaders));
HttpResponse response = channel.readOutbound();
HttpResponseStatus expectedStatus = getExpectedHttpResponseStatus(errorCode);
assertEquals("Unexpected response status", expectedStatus, response.status());
boolean containsFailureReasonHeader = response.headers().contains(NettyResponseChannel.FAILURE_REASON_HEADER);
if (expectedStatus == HttpResponseStatus.BAD_REQUEST) {
assertTrue("Could not find failure reason header.", containsFailureReasonHeader);
} else {
assertFalse("Should not have found failure reason header.", containsFailureReasonHeader);
}
if (HttpStatusClass.CLIENT_ERROR.contains(response.status().code())) {
assertEquals("Wrong error code", errorCode, RestServiceErrorCode.valueOf(response.headers().get(NettyResponseChannel.ERROR_CODE_HEADER)));
} else {
assertFalse("Should not have found error code header", response.headers().contains(NettyResponseChannel.ERROR_CODE_HEADER));
}
if (response instanceof FullHttpResponse) {
// assert that there is no content
assertEquals("The response should not contain content", 0, ((FullHttpResponse) response).content().readableBytes());
} else {
HttpContent content = channel.readOutbound();
assertTrue("End marker should be received", content instanceof LastHttpContent);
}
assertNull("There should be no more data in the channel", channel.readOutbound());
boolean shouldBeAlive = !NettyResponseChannel.CLOSE_CONNECTION_ERROR_STATUSES.contains(expectedStatus);
assertEquals("Channel state (open/close) not as expected", shouldBeAlive, channel.isActive());
assertEquals("Connection header should be consistent with channel state", shouldBeAlive, HttpUtil.isKeepAlive(response));
if (!shouldBeAlive) {
channel = createEmbeddedChannel();
}
}
channel.close();
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project dubbo by alibaba.
the class HttpProcessHandler method http_200.
private static final FullHttpResponse http_200(String result) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(result.getBytes()));
HttpHeaders httpHeaders = response.headers();
httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
return response;
}
Aggregations