本文整理汇总了Java中io.netty.handler.codec.http.HttpContent.content方法的典型用法代码示例。如果您正苦于以下问题:Java HttpContent.content方法的具体用法?Java HttpContent.content怎么用?Java HttpContent.content使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.handler.codec.http.HttpContent
的用法示例。
在下文中一共展示了HttpContent.content方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: handleChunk
import io.netty.handler.codec.http.HttpContent; //导入方法依赖的package包/类
private void handleChunk(HttpContent chunk,//
final Channel channel,//
final NettyResponseFuture<?> future,//
AsyncHandler<?> handler) throws IOException, Exception {
boolean interrupt = false;
boolean last = chunk instanceof LastHttpContent;
// Netty 4: the last chunk is not empty
if (last) {
LastHttpContent lastChunk = (LastHttpContent) chunk;
HttpHeaders trailingHeaders = lastChunk.trailingHeaders();
if (!trailingHeaders.isEmpty()) {
interrupt = handler.onHeadersReceived(new HttpResponseHeaders(trailingHeaders, true)) != State.CONTINUE;
}
}
ByteBuf buf = chunk.content();
if (!interrupt && !(handler instanceof StreamedAsyncHandler) && (buf.readableBytes() > 0 || last)) {
HttpResponseBodyPart part = config.getResponseBodyPartFactory().newResponseBodyPart(buf, last);
interrupt = updateBodyAndInterrupt(future, handler, part);
}
if (interrupt || last)
finishUpdate(future, channel, !last);
}
示例3: channelRead0
import io.netty.handler.codec.http.HttpContent; //导入方法依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg)
throws Exception {
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
RandomAccessFile output = null;
FileChannel oc = null;
try {
output = new RandomAccessFile(file, "rw");
oc = output.getChannel();
oc.position(oc.size());
ByteBuf buffer = content.content();
for (int i = 0, len = buffer.nioBufferCount(); i < len; i++) {
oc.write(buffer.nioBuffers()[i]);
}
} finally {
IOUtils.closeQuietly(oc);
IOUtils.closeQuietly(output);
}
if (content instanceof LastHttpContent) {
ctx.close();
}
}
}
示例4: receive
import io.netty.handler.codec.http.HttpContent; //导入方法依赖的package包/类
void receive(HttpContent content) {
// Consume the response body.
ByteBuf byteBuf = content.content();
for (int toRead; (toRead = byteBuf.readableBytes()) > 0; ) {
byteBuf.readBytes(buffer, 0, Math.min(buffer.length, toRead));
total += toRead;
}
if (VERBOSE && content instanceof LastHttpContent) {
long finish = System.nanoTime();
System.out.println(String.format("Transferred % 8d bytes in %4d ms",
total, TimeUnit.NANOSECONDS.toMillis(finish - start)));
}
}
示例5: loadPostReqParams
import io.netty.handler.codec.http.HttpContent; //导入方法依赖的package包/类
public static Map<String,Object> loadPostReqParams(HttpContent content){
Map<String,Object> params =null;
try {
Gson gson = new Gson();
Type paraMap = new TypeToken<Map<String, JsonElement>>(){}.getType();
ByteBufInputStream in = new ByteBufInputStream(content.content());
String rawJson = IOUtils.readAll(in);
params = gson.fromJson(rawJson,paraMap);
} catch (IOException e) {
e.printStackTrace();
}
return params;
}
示例6: channelRead
import io.netty.handler.codec.http.HttpContent; //导入方法依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
if (msg instanceof HttpRequest) {
request = (HttpRequest) msg;
String uri = request.getUri();
System.out.println("Uri:" + uri);
}
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
ByteBuf buf = content.content();
System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));
buf.release();
String res = "I am OK";
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
OK, Unpooled.wrappedBuffer(res.getBytes("UTF-8")));
response.headers().set(CONTENT_TYPE, "text/plain");
response.headers().set(CONTENT_LENGTH,
response.content().readableBytes());
if (HttpHeaders.isKeepAlive(request)) {
response.headers().set(CONNECTION, Values.KEEP_ALIVE);
}
ctx.write(response);
ctx.flush();
}
}
示例7: channelRead
import io.netty.handler.codec.http.HttpContent; //导入方法依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpResponse)
{
HttpResponse response = (HttpResponse) msg;
System.out.println("CONTENT_TYPE:" + response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
}
if(msg instanceof HttpContent)
{
HttpContent content = (HttpContent)msg;
ByteBuf buf = content.content();
System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));
buf.release();
}
}
示例8: onContent
import io.netty.handler.codec.http.HttpContent; //导入方法依赖的package包/类
public void onContent(HttpContent httpContent) throws IOException {
ByteBuf content = httpContent.content();
if (content.isReadable()) {
getNettyEntityStream().onContent(content, httpContent instanceof LastHttpContent);
}
}
示例9: captureRequestSize
import io.netty.handler.codec.http.HttpContent; //导入方法依赖的package包/类
/**
* Adds the size of this httpContent to the requestBodySize.
*
* @param httpContent HttpContent to size
*/
protected void captureRequestSize(HttpContent httpContent) {
Log.e("InnerHandle", "captureRequestSize " + harEntry.getId());
ByteBuf bufferedContent = httpContent.content();
int contentSize = bufferedContent.readableBytes();
requestBodySize.addAndGet(contentSize);
}
示例10: captureResponseSize
import io.netty.handler.codec.http.HttpContent; //导入方法依赖的package包/类
/**
* Adds the size of this httpContent to the responseBodySize.
*
* @param httpContent HttpContent to size
*/
protected void captureResponseSize(HttpContent httpContent) {
Log.e("InnerHandle", "captureResponseSize " + harEntry.getId());
ByteBuf bufferedContent = httpContent.content();
int contentSize = bufferedContent.readableBytes();
responseBodySize.addAndGet(contentSize);
proxyManager.dataReceived(contentSize);
}