本文整理汇总了Java中io.netty.handler.codec.http.HttpObject类的典型用法代码示例。如果您正苦于以下问题:Java HttpObject类的具体用法?Java HttpObject怎么用?Java HttpObject使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpObject类属于io.netty.handler.codec.http包,在下文中一共展示了HttpObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeResponse
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
// Decide whether to close the connection or not.
boolean keepAlive = HttpHeaders.isKeepAlive(request);
// Build the response object.
FullHttpResponse response = new DefaultFullHttpResponse(
HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, "application/json");
if (keepAlive) {
// Add 'Content-Length' header only for a keep-alive connection.
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
// Add keep alive header as per:
// - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
// Write the response.
ctx.write(response);
return keepAlive;
}
示例2: channelRead
import io.netty.handler.codec.http.HttpObject; //导入依赖的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);
}
示例3: debugRequestInfo
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
private void debugRequestInfo(HttpObject httpObject, String key) {
if (m_debugInfo && httpObject instanceof HttpRequest) {
if (key != null) {
LOGGER.debug("Cache Key: " + key);
}
if (httpObject instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) httpObject;
HttpHeaders headers = req.headers();
LOGGER.debug("Headers:");
for (Iterator<Entry<String, String>> it = headers.iterator(); it
.hasNext();) {
Entry<String, String> entry = it.next();
LOGGER.debug("\t" + entry.getKey() + ":\t"
+ entry.getValue());
}
ByteBuf content = req.content();
int length = content.readableBytes();
LOGGER.debug("Content Length: " + length);
if (length != 0) {
LOGGER.debug("Content: "
+ content.toString(Charset.forName("UTF-8")));
}
}
}
}
示例4: channelRead0
import io.netty.handler.codec.http.HttpObject; //导入依赖的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();
}
}
示例5: mock
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
@Test
public void doChannelRead_checks_for_fully_send_responses_but_does_nothing_else_if_msg_is_not_HttpRequest_or_HttpContent() {
// given
HttpObject msgMock = mock(HttpObject.class);
// when
PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, msgMock);
// then
verify(ctxMock).channel();
verifyNoMoreInteractions(ctxMock);
verify(stateMock).isResponseSendingLastChunkSent();
verifyNoMoreInteractions(stateMock);
verifyNoMoreInteractions(msgMock);
assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
示例6: doChannelRead_does_nothing_if_msg_is_not_HttpRequest
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
@Test
public void doChannelRead_does_nothing_if_msg_is_not_HttpRequest() {
// given
String pathTemplate = "/some/path/with/{id}";
Collection<String> pathTemplates = new ArrayList<String>() {{ add(pathTemplate); }};
doReturn(pathTemplates).when(matcherMock).matchingPathTemplates();
HttpObject msg = mock(HttpObject.class);
// when
PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, msg);
// then
verify(handlerSpy).doChannelRead(ctxMock, msg);
verifyNoMoreInteractions(handlerSpy);
verifyNoMoreInteractions(requestInfoMock);
verifyNoMoreInteractions(stateMock);
assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
示例7: createNettyHttpClientBootstrap
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
public static Bootstrap createNettyHttpClientBootstrap() {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpClientCodec());
p.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
p.addLast("clientResponseHandler", new SimpleChannelInboundHandler<HttpObject>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
throw new RuntimeException("Client response handler was not setup before the call");
}
});
}
});
return bootstrap;
}
示例8: setupNettyHttpClientResponseHandler
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
public static CompletableFuture<NettyHttpClientResponse> setupNettyHttpClientResponseHandler(
Channel ch, Consumer<ChannelPipeline> pipelineAdjuster
) {
CompletableFuture<NettyHttpClientResponse> responseFromServerFuture = new CompletableFuture<>();
ch.pipeline().replace("clientResponseHandler", "clientResponseHandler", new SimpleChannelInboundHandler<HttpObject>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg)
throws Exception {
if (msg instanceof FullHttpResponse) {
// Store the proxyServer response for asserting on later.
responseFromServerFuture.complete(new NettyHttpClientResponse((FullHttpResponse) msg));
} else {
// Should never happen.
throw new RuntimeException("Received unexpected message type: " + msg.getClass());
}
}
});
if (pipelineAdjuster != null)
pipelineAdjuster.accept(ch.pipeline());
return responseFromServerFuture;
}
示例9: clientToProxyRequest
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
if (httpObject instanceof HttpRequest) {
this.httpRequest = (HttpRequest) httpObject;
}
if (httpObject instanceof HttpContent) {
HttpContent httpContent = (HttpContent) httpObject;
storeRequestContent(httpContent);
if (httpContent instanceof LastHttpContent) {
LastHttpContent lastHttpContent = (LastHttpContent) httpContent;
trailingHeaders = lastHttpContent .trailingHeaders();
}
}
return null;
}
示例10: clientToProxyRequest
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
if (httpObject instanceof HttpRequest) {
HttpRequest httpRequest = (HttpRequest) httpObject;
if (ProxyUtils.isCONNECT(httpRequest)) {
Attribute<String> hostname = ctx.attr(AttributeKey.<String>valueOf(HttpsAwareFiltersAdapter.HOST_ATTRIBUTE_NAME));
String hostAndPort = httpRequest.getUri();
// CONNECT requests contain the port, even when using the default port. a sensible default is to remove the
// default port, since in most cases it is not explicitly specified and its presence (in a HAR file, for example)
// would be unexpected.
String hostNoDefaultPort = BrowserMobHttpUtil.removeMatchingPort(hostAndPort, 443);
hostname.set(hostNoDefaultPort);
}
}
return null;
}
示例11: serverToProxyResponse
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
HttpObject processedHttpObject = httpObject;
for (HttpFilters filter : filters) {
try {
processedHttpObject = filter.serverToProxyResponse(processedHttpObject);
if (processedHttpObject == null) {
return null;
}
} catch (RuntimeException e) {
log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e);
}
}
return processedHttpObject;
}
示例12: proxyToClientResponse
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
@Override
public HttpObject proxyToClientResponse(HttpObject httpObject) {
HttpObject processedHttpObject = httpObject;
for (HttpFilters filter : filters) {
try {
processedHttpObject = filter.proxyToClientResponse(processedHttpObject);
if (processedHttpObject == null) {
return null;
}
} catch (RuntimeException e) {
log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e);
}
}
return processedHttpObject;
}
示例13: clientToProxyRequest
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
if (httpObject instanceof HttpRequest) {
HttpRequest httpRequest = (HttpRequest) httpObject;
String url = getFullUrl(httpRequest);
for (BlacklistEntry entry : blacklistedUrls) {
if (HttpMethod.CONNECT.equals(httpRequest.getMethod()) && entry.getHttpMethodPattern() == null) {
// do not allow CONNECTs to be blacklisted unless a method pattern is explicitly specified
continue;
}
if (entry.matches(url, httpRequest.getMethod().name())) {
HttpResponseStatus status = HttpResponseStatus.valueOf(entry.getStatusCode());
HttpResponse resp = new DefaultFullHttpResponse(httpRequest.getProtocolVersion(), status);
HttpHeaders.setContentLength(resp, 0L);
return resp;
}
}
}
return null;
}
示例14: clientToProxyRequest
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
if (credentialsByHostname.isEmpty()) {
return null;
}
if (httpObject instanceof HttpRequest) {
HttpRequest httpRequest = (HttpRequest) httpObject;
// providing authorization during a CONNECT is generally not useful
if (ProxyUtils.isCONNECT(httpRequest)) {
return null;
}
String hostname = getHost(httpRequest);
// if there is an entry in the credentials map matching this hostname, add the credentials to the request
String base64CredentialsForHostname = credentialsByHostname.get(hostname);
if (base64CredentialsForHostname != null) {
httpRequest.headers().add(HttpHeaders.Names.AUTHORIZATION, "Basic " + base64CredentialsForHostname);
}
}
return null;
}
示例15: channelRead0
import io.netty.handler.codec.http.HttpObject; //导入依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject)
throws Exception {
// initial request
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("%s: Reading from client %s", System.currentTimeMillis(), httpObject));
}
if (httpObject instanceof HttpRequest) {
HttpRequest initialRequest = (HttpRequest) httpObject;
if (_channelHandlerDelegate == null) {
_channelHandlerDelegate =
HandlerDelegateFactory.create(initialRequest, _channelMediator, _connectionFlowRegistry);
_channelHandlerDelegate.onCreate();
}
}
_channelHandlerDelegate.onRead(httpObject);
}