本文整理匯總了Java中io.netty.handler.codec.http.FullHttpRequest類的典型用法代碼示例。如果您正苦於以下問題:Java FullHttpRequest類的具體用法?Java FullHttpRequest怎麽用?Java FullHttpRequest使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
FullHttpRequest類屬於io.netty.handler.codec.http包,在下文中一共展示了FullHttpRequest類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getHttpRequest
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
public FullHttpRequest getHttpRequest() {
if (h1Request != null) {
return h1Request;
}
if (h2Headers != null) {
try {
// Fake out a full HTTP request.
FullHttpRequest synthesizedRequest =
HttpConversionUtil.toFullHttpRequest(0, h2Headers, alloc, true);
if (data != null) {
synthesizedRequest.replace(data);
}
return synthesizedRequest;
} catch (Http2Exception e) {
// TODO(JR): Do something more meaningful with this exception
e.printStackTrace();
}
}
throw new IllegalStateException("Cannot get the http request for an empty XrpcRequest");
}
示例2: handleHttpRequest
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
/**
* 接受http信息
* @param ctx
* @param req
*/
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
// Handle a bad request.
if (!req.getDecoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
return;
}
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
getWebSocketLocation(req), null, true);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
}
}
示例3: sendHttpResponse
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
/**
* 返回http信息
* @param ctx
* @param req
* @param res
*/
private static void sendHttpResponse(
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200).
if (res.getStatus().code() != 200) {
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
HttpHeaders.setContentLength(res, res.content().readableBytes());
}
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.channel().writeAndFlush(res);
if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
示例4: sendHttpResponse
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
public void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
if (res.getStatus().code() != 200) {
ByteBuf f = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().clear();
res.content().writeBytes(f);
f.release();
}
HttpHeaders.setContentLength(res, res.content().readableBytes());
ChannelFuture f1;
f1 = ctx.channel().writeAndFlush(res);
if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
f1.addListener(ChannelFutureListener.CLOSE);
}
}
示例5: handleHttpRequest
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
/**
* HTTP握手反饋
*/
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest request){
//判斷是否是WebSocket協議
if (!request.decoderResult().isSuccess() || !"websocket".equals(request.headers().get("Upgrade"))) {
logger.warn("protobuf don't support WebSocket");
ctx.channel().close();
return;
}
WebSocketServerHandshakerFactory handshakerFactory = new WebSocketServerHandshakerFactory(
WEBSOCKET_URL, null, true);
handshaker = handshakerFactory.newHandshaker(request);
if (handshaker == null){
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
}else {
// 動態加入websocket的編解碼處理
handshaker.handshake(ctx.channel(), request);
// 存儲已經連接的Channel
manager.addChannel(ctx.channel());
}
}
示例6: channelRead
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
LOGGER.info("[Client ({})] => [Server ({})] : {}",
connectionInfo.getClientAddr(), connectionInfo.getServerAddr(),
msg);
if (msg instanceof FullHttpRequest) {
String streamId = ((HttpRequest) msg).headers().get(
HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
if (streamId == null) {
throw new IllegalStateException("No streamId");
}
streams.offer(streamId);
} else if (msg instanceof HttpObject) {
throw new IllegalStateException("Cannot handle message: " + msg.getClass());
}
outboundChannel.writeAndFlush(msg);
}
示例7: testReleaseOnSendToClosedChannel
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
public void testReleaseOnSendToClosedChannel() {
final Settings settings = Settings.builder().build();
final NamedXContentRegistry registry = xContentRegistry();
try (Netty4HttpServerTransport httpServerTransport =
new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, registry, new NullDispatcher())) {
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
final EmbeddedChannel embeddedChannel = new EmbeddedChannel();
final Netty4HttpRequest request = new Netty4HttpRequest(registry, httpRequest, embeddedChannel);
final HttpPipelinedRequest pipelinedRequest = randomBoolean() ? new HttpPipelinedRequest(request.request(), 1) : null;
final Netty4HttpChannel channel =
new Netty4HttpChannel(httpServerTransport, request, pipelinedRequest, randomBoolean(), threadPool.getThreadContext());
final TestResponse response = new TestResponse(bigArrays);
assertThat(response.content(), instanceOf(Releasable.class));
embeddedChannel.close();
channel.sendResponse(response);
// ESTestCase#after will invoke ensureAllArraysAreReleased which will fail if the response content was not released
}
}
示例8: executeRequest
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
private FullHttpResponse executeRequest(final Settings settings, final String originValue, final String host) {
// construct request and send it over the transport layer
try (Netty4HttpServerTransport httpServerTransport =
new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(),
new NullDispatcher())) {
httpServerTransport.start();
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
if (originValue != null) {
httpRequest.headers().add(HttpHeaderNames.ORIGIN, originValue);
}
httpRequest.headers().add(HttpHeaderNames.HOST, host);
final WriteCapturingChannel writeCapturingChannel = new WriteCapturingChannel();
final Netty4HttpRequest request = new Netty4HttpRequest(xContentRegistry(), httpRequest, writeCapturingChannel);
Netty4HttpChannel channel =
new Netty4HttpChannel(httpServerTransport, request, null, randomBoolean(), threadPool.getThreadContext());
channel.sendResponse(new TestResponse());
// get the response
List<Object> writtenObjects = writeCapturingChannel.getWrittenObjects();
assertThat(writtenObjects.size(), is(1));
return (FullHttpResponse) writtenObjects.get(0);
}
}
示例9: channelRead
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;
Object response = null;
if (staticFilePattern.matcher(request.uri()).find()) {
super.channelRead(ctx, msg);
return;
}
try {
response = dispatcher.doDispatcher(request);
} catch (Exception ex) {
// TODO: 異常處理
ex.printStackTrace();
}
ObjectMapper om = new ObjectMapper();
String jsonStr = om.writer().writeValueAsString(response);
sendResponse(ctx, request, jsonStr);
}
}
示例10: doDispatcher
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
/**
* 請求分發與處理
*
* @param request http協議請求
* @return 處理結果
* @throws InvocationTargetException 調用異常
* @throws IllegalAccessException 參數異常
*/
public Object doDispatcher(FullHttpRequest request) throws InvocationTargetException, IllegalAccessException {
Object[] args;
String uri = request.uri();
if (uri.endsWith("favicon.ico")) {
return "";
}
AceServiceBean aceServiceBean = Context.getAceServiceBean(uri);
AceHttpMethod aceHttpMethod = AceHttpMethod.getAceHttpMethod(request.method().toString());
ByteBuf content = request.content();
//如果要多次解析,請用 request.content().copy()
QueryStringDecoder decoder = new QueryStringDecoder(uri);
Map<String, List<String>> requestMap = decoder.parameters();
Object result = aceServiceBean.exec(uri, aceHttpMethod, requestMap, content == null ? null : content.toString(CharsetUtil.UTF_8));
String contentType = request.headers().get("Content-Type");
if (result == null) {
ApplicationInfo mock = new ApplicationInfo();
mock.setName("ace");
mock.setVersion("1.0");
mock.setDesc(" mock !!! ");
result = mock;
}
return result;
}
示例11: channelRead0
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req)
throws Exception {
if (!req.getDecoderResult().isSuccess()) {
logger.debug("invalid http request");
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1,
BAD_REQUEST));
return;
}
if (req.getUri().equalsIgnoreCase(this.websocketUri)) {
logger.debug("it is websocket request");
ctx.fireChannelRead(req.retain());
return;
}
HttpTransport transport = getTransport(req);
if (transport == null) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1,
BAD_REQUEST));
} else {
transport.handleRequest(ctx, req);
}
}
示例12: handleRequest
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
@Override
public void handleRequest(ChannelHandlerContext ctx, FullHttpRequest req)
throws Exception {
if (req.getUri().contains("/jsonp/connect")) {
handleConnect(ctx, req);
} else if (req.getUri().contains("/jsonp/subscribe")) {
handleSubscrible(ctx, req);
} else if (req.getUri().contains("/jsonp/waiting")) {
handleWaitingMsg(ctx, req);
} else if (req.getUri().contains("/jsonp/unsubscrible")) {
handleUnsubscrible(ctx, req);
} else if (req.getUri().contains("/jsonp/publish")) {
handlePublish(ctx, req);
} else if (req.getUri().contains("/jsonp/disconnect")) {
handleDisconnect(ctx, req);
} else { // invalid request
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1,
BAD_REQUEST));
}
}
示例13: channelRead0
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的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);
}
示例14: handleHttpRequest
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest request) {
if (!request.decoderResult().isSuccess() || !"websocket".equals(request.headers().get("Upgrade"))) {
logger.warn("protobuf don't support websocket");
ctx.channel().close();
return;
}
WebSocketServerHandshakerFactory handshakerFactory = new WebSocketServerHandshakerFactory(
Constants.WEBSOCKET_URL, null, true);
handshaker = handshakerFactory.newHandshaker(request);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
// 動態加入websocket的編解碼處理
handshaker.handshake(ctx.channel(), request);
UserInfo userInfo = new UserInfo();
userInfo.setAddr(NettyUtil.parseChannelRemoteAddr(ctx.channel()));
// 存儲已經連接的Channel
UserInfoManager.addChannel(ctx.channel());
}
}
示例15: getSessionId
import io.netty.handler.codec.http.FullHttpRequest; //導入依賴的package包/類
public static String getSessionId(FullHttpRequest msg, boolean anonymousAccessAllowed) {
final StringBuilder buf = new StringBuilder();
msg.headers().getAll(Names.COOKIE).forEach(h -> {
ServerCookieDecoder.STRICT.decode(h).forEach(c -> {
if (c.name().equals(Constants.COOKIE_NAME)) {
if (buf.length() == 0) {
buf.append(c.value());
}
}
});
});
String sessionId = buf.toString();
if (sessionId.length() == 0 && anonymousAccessAllowed) {
sessionId = NO_AUTHORIZATIONS;
} else if (sessionId.length() == 0) {
sessionId = null;
}
return sessionId;
}