本文整理汇总了Java中io.netty.handler.codec.http.HttpContent类的典型用法代码示例。如果您正苦于以下问题:Java HttpContent类的具体用法?Java HttpContent怎么用?Java HttpContent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpContent类属于io.netty.handler.codec.http包,在下文中一共展示了HttpContent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: channelRead
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//客户端channel已关闭则不转发了
if (!clientChannel.isOpen()) {
ReferenceCountUtil.release(msg);
return;
}
HttpProxyInterceptPipeline interceptPipeline = ((HttpProxyServerHandle) clientChannel.pipeline()
.get("serverHandle")).getInterceptPipeline();
if (msg instanceof HttpResponse) {
interceptPipeline.afterResponse(clientChannel, ctx.channel(), (HttpResponse) msg);
} else if (msg instanceof HttpContent) {
interceptPipeline.afterResponse(clientChannel, ctx.channel(), (HttpContent) msg);
} else {
clientChannel.writeAndFlush(msg);
}
}
示例2: channelRead0
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) {
StringUtils.Pair url = HttpUtils.parseUriIntoUrlBaseAndPath(msg.uri());
HttpRequest request = new HttpRequest();
if (url.left == null) {
String requestScheme = provider.isSsl() ? "https" : "http";
String host = msg.headers().get(HttpUtils.HEADER_HOST);
request.setUrlBase(requestScheme + "://" + host);
} else {
request.setUrlBase(url.left);
}
request.setUri(url.right);
request.setMethod(msg.method().name());
msg.headers().forEach(h -> request.addHeader(h.getKey(), h.getValue()));
QueryStringDecoder decoder = new QueryStringDecoder(url.right);
decoder.parameters().forEach((k, v) -> request.putParam(k, v));
HttpContent httpContent = (HttpContent) msg;
ByteBuf content = httpContent.content();
if (content.isReadable()) {
byte[] bytes = new byte[content.readableBytes()];
content.readBytes(bytes);
request.setBody(bytes);
}
writeResponse(request, ctx);
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
示例3: beforeRequest
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Override
public void beforeRequest(Channel clientChannel, HttpContent httpContent,
HttpProxyInterceptPipeline pipeline) throws Exception {
if (content != null) {
ByteBuf temp = httpContent.content().slice();
content.writeBytes(temp);
if (httpContent instanceof LastHttpContent) {
try {
byte[] contentBts = new byte[content.readableBytes()];
content.readBytes(contentBts);
((HttpRequestInfo) pipeline.getHttpRequest()).setContent(contentBts);
} finally {
ReferenceCountUtil.release(content);
content = null; //状态回归
}
}
}
pipeline.beforeRequest(clientChannel, httpContent);
}
示例4: startDownTask
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
public static void startDownTask(TaskInfo taskInfo, HttpRequest httpRequest,
HttpResponse httpResponse, Channel clientChannel) {
HttpHeaders httpHeaders = httpResponse.headers();
HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest);
HttpDownServer.DOWN_CONTENT.put(taskInfo.getId(), httpDownInfo);
httpHeaders.clear();
httpResponse.setStatus(HttpResponseStatus.OK);
httpHeaders.set(HttpHeaderNames.CONTENT_TYPE, "text/html");
String host = HttpDownServer.isDev() ? "localhost"
: ((InetSocketAddress) clientChannel.localAddress()).getHostString();
String js =
"<script>window.top.location.href='http://" + host + ":" + HttpDownServer.VIEW_SERVER_PORT
+ "/#/tasks/new/" + httpDownInfo
.getTaskInfo().getId()
+ "';</script>";
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(js.getBytes());
httpHeaders.set(HttpHeaderNames.CONTENT_LENGTH, js.getBytes().length);
clientChannel.writeAndFlush(httpResponse);
clientChannel.writeAndFlush(content);
clientChannel.close();
}
示例5: channelRead0
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg)
throws Exception {
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) msg;
LOGGER.info("Response status: " + response.getStatus());
if (response.getStatus().equals(OK)) {
LOGGER.info("Operation is successful");
} else {
LOGGER.error("Operation is failed");
}
}
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
System.out.print(content.content().toString(CharsetUtil.UTF_8));
System.out.flush();
}
}
示例6: logResponseFirstChunk
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
protected void logResponseFirstChunk(HttpResponse response, ChannelHandlerContext ctx) {
if (logger.isDebugEnabled()) {
StringBuilder headers = new StringBuilder();
for (String headerName : response.headers().names()) {
if (headers.length() > 0)
headers.append(", ");
headers.append(headerName).append("=\"")
.append(String.join(",", response.headers().getAll(headerName))).append("\"");
}
StringBuilder sb = new StringBuilder();
sb.append("SENDING RESPONSE:");
sb.append("\n\tHTTP STATUS: ").append(response.getStatus().code());
sb.append("\n\tHEADERS: ").append(headers.toString());
sb.append("\n\tPROTOCOL: ").append(response.getProtocolVersion().text());
if (response instanceof HttpContent) {
HttpContent chunk = (HttpContent) response;
sb.append("\n\tCONTENT CHUNK: ").append(chunk.getClass().getName()).append(", size: ")
.append(chunk.content().readableBytes());
}
runnableWithTracingAndMdc(() -> logger.debug(sb.toString()), ctx).run();
}
}
示例7: write_sets_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_null
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Test
public void write_sets_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_null() throws Exception {
// given
HttpContent msgMock = mock(HttpContent.class);
ByteBuf contentMock = mock(ByteBuf.class);
int contentBytes = (int)(Math.random() * 10000);
doReturn(contentMock).when(msgMock).content();
doReturn(contentBytes).when(contentMock).readableBytes();
assertThat(responseInfo.getFinalContentLength()).isNull();
// when
handler.write(ctxMock, msgMock, promiseMock);
// then
assertThat(responseInfo.getFinalContentLength()).isEqualTo(contentBytes);
}
示例8: write_adds_to_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_not_null
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Test
public void write_adds_to_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_not_null() throws Exception {
// given
HttpContent msgMock = mock(HttpContent.class);
ByteBuf contentMock = mock(ByteBuf.class);
int contentBytes = (int)(Math.random() * 10000);
doReturn(contentMock).when(msgMock).content();
doReturn(contentBytes).when(contentMock).readableBytes();
int initialFinalContentLengthValue = (int)(Math.random() * 10000);
responseInfo.setFinalContentLength((long)initialFinalContentLengthValue);
assertThat(responseInfo.getFinalContentLength()).isEqualTo(initialFinalContentLengthValue);
// when
handler.write(ctxMock, msgMock, promiseMock);
// then
assertThat(responseInfo.getFinalContentLength()).isEqualTo(initialFinalContentLengthValue + contentBytes);
}
示例9: write_does_nothing_to_finalContentLength_if_msg_is_HttpContent_but_state_is_null
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Test
public void write_does_nothing_to_finalContentLength_if_msg_is_HttpContent_but_state_is_null() throws Exception {
// given
HttpContent msgMock = mock(HttpContent.class);
ByteBuf contentMock = mock(ByteBuf.class);
int contentBytes = (int)(Math.random() * 10000);
doReturn(contentMock).when(msgMock).content();
doReturn(contentBytes).when(contentMock).readableBytes();
doReturn(null).when(stateAttrMock).get();
assertThat(responseInfo.getFinalContentLength()).isNull();
// when
handler.write(ctxMock, msgMock, promiseMock);
// then
assertThat(responseInfo.getFinalContentLength()).isNull();
}
示例10: write_does_nothing_to_finalContentLength_if_msg_is_HttpContent_but_responseInfo_is_null
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Test
public void write_does_nothing_to_finalContentLength_if_msg_is_HttpContent_but_responseInfo_is_null() throws Exception {
// given
HttpContent msgMock = mock(HttpContent.class);
ByteBuf contentMock = mock(ByteBuf.class);
int contentBytes = (int)(Math.random() * 10000);
doReturn(contentMock).when(msgMock).content();
doReturn(contentBytes).when(contentMock).readableBytes();
doReturn(null).when(stateMock).getResponseInfo();
assertThat(responseInfo.getFinalContentLength()).isNull();
// when
handler.write(ctxMock, msgMock, promiseMock);
// then
assertThat(responseInfo.getFinalContentLength()).isNull();
}
示例11: beforeMethod
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Before
public void beforeMethod() {
stateMock = mock(HttpProcessingState.class);
ctxMock = mock(ChannelHandlerContext.class);
channelMock = mock(Channel.class);
stateAttrMock = mock(Attribute.class);
endpointMock = mock(Endpoint.class);
maxRequestSizeInBytes = 10;
httpContentMock = mock(HttpContent.class);
byteBufMock = mock(ByteBuf.class);
requestInfo = mock(RequestInfo.class);
handler = new RequestInfoSetterHandler(maxRequestSizeInBytes);
doReturn(channelMock).when(ctxMock).channel();
doReturn(stateAttrMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
doReturn(stateMock).when(stateAttrMock).get();
doReturn(endpointMock).when(stateMock).getEndpointForExecution();
doReturn(byteBufMock).when(httpContentMock).content();
doReturn(null).when(endpointMock).maxRequestSizeInBytesOverride();
doReturn(requestInfo).when(stateMock).getRequestInfo();
}
示例12: contentChunksWillBeReleasedExternally_works_as_expected
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@DataProvider(value = {
"0",
"42"
})
@Test
public void contentChunksWillBeReleasedExternally_works_as_expected(int contentChunkListSize) {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
Assertions.assertThat(requestInfo.contentChunksWillBeReleasedExternally).isFalse();
for (int i = 0; i < contentChunkListSize; i++) {
requestInfo.contentChunks.add(mock(HttpContent.class));
}
// when
requestInfo.contentChunksWillBeReleasedExternally();
// then
Assertions.assertThat(requestInfo.contentChunksWillBeReleasedExternally).isTrue();
Assertions.assertThat(requestInfo.contentChunks).isEmpty();
}
示例13: addContentChunk_adds_chunk_content_length_to_rawContentLengthInBytes
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Test
public void addContentChunk_adds_chunk_content_length_to_rawContentLengthInBytes() throws IOException {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
requestInfo.isCompleteRequestWithAllChunks = false;
String chunk1String = UUID.randomUUID().toString();
String lastChunkString = UUID.randomUUID().toString();
byte[] chunk1Bytes = chunk1String.getBytes();
byte[] lastChunkBytes = lastChunkString.getBytes();
HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer(chunk1Bytes));
HttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastChunkBytes));
// when
requestInfo.addContentChunk(chunk1);
requestInfo.addContentChunk(lastChunk);
// then
assertThat(requestInfo.contentChunks.size(), is(2));
assertThat(requestInfo.isCompleteRequestWithAllChunks(), is(true));
assertThat(requestInfo.getRawContentLengthInBytes(), is(chunk1Bytes.length + lastChunkBytes.length));
}
示例14: releaseContentChunks_calls_release_on_each_chunk_and_calls_clear_on_chunk_list
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Test
public void releaseContentChunks_calls_release_on_each_chunk_and_calls_clear_on_chunk_list() {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
List<HttpContent> contentChunkList = Arrays.asList(mock(HttpContent.class), mock(HttpContent.class));
requestInfo.contentChunks.addAll(contentChunkList);
assertThat(requestInfo.contentChunks.size(), is(contentChunkList.size()));
// when
requestInfo.releaseContentChunks();
// then
for (HttpContent chunkMock : contentChunkList) {
verify(chunkMock).release();
}
assertThat(requestInfo.contentChunks.isEmpty(), is(true));
}
示例15: mock
import io.netty.handler.codec.http.HttpContent; //导入依赖的package包/类
@Test
public void releaseContentChunks_clear_on_chunk_list_but_does_not_release_chunks_if_contentChunksWillBeReleasedExternally_is_true() {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
requestInfo.contentChunksWillBeReleasedExternally();
List<HttpContent> contentChunkList = Arrays.asList(mock(HttpContent.class), mock(HttpContent.class));
requestInfo.contentChunks.addAll(contentChunkList);
assertThat(requestInfo.contentChunks.size(), is(contentChunkList.size()));
// when
requestInfo.releaseContentChunks();
// then
for (HttpContent chunkMock : contentChunkList) {
verify(chunkMock, never()).release();
}
assertThat(requestInfo.contentChunks.isEmpty(), is(true));
}