本文整理汇总了Java中io.netty.handler.codec.http.HttpHeaders.setContentLength方法的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaders.setContentLength方法的具体用法?Java HttpHeaders.setContentLength怎么用?Java HttpHeaders.setContentLength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.handler.codec.http.HttpHeaders
的用法示例。
在下文中一共展示了HttpHeaders.setContentLength方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeBack
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
public static int writeBack(Channel channel, boolean isSuccess, String resultStr, boolean isKeepAlive) {
ByteBuf content = Unpooled.copiedBuffer(resultStr, Constants.DEFAULT_CHARSET);
HttpResponseStatus status;
if (isSuccess) {
status = HttpResponseStatus.OK;
} else {
status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
}
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, status, content);
//logger.info("result str:{}", resultStr);
res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
HttpHeaders.setContentLength(res, content.readableBytes());
try {
ChannelFuture f = channel.writeAndFlush(res);
if (isKeepAlive) {
HttpHeaders.setKeepAlive(res, true);
} else {
HttpHeaders.setKeepAlive(res, false);//set keepalive closed
f.addListener(ChannelFutureListener.CLOSE);
}
} catch (Exception e2) {
logger.warn("Failed to send HTTP response to remote, cause by:", e2);
}
return content.readableBytes();
}
示例2: sendHttpResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的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);
}
}
示例3: sendHttpResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的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);
}
}
示例4: handleNonProxyRequest
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
protected HttpResponse handleNonProxyRequest(FullHttpRequest req) {
String uri = req.getUri();
if ("/version".equals(uri)) {
if (HttpMethod.GET.equals(req.getMethod())) {
JsonObject jsonObj = new JsonObject();
jsonObj.addProperty("name", m_appConfig.getAppName());
jsonObj.addProperty("version", m_appConfig.getAppVersion());
byte[] content = jsonObj.toString().getBytes(CharsetUtil.UTF_8);
DefaultFullHttpResponse resp = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
Unpooled.copiedBuffer(content));
HttpHeaders.setKeepAlive(resp, false);
HttpHeaders.setHeader(resp, HttpHeaders.Names.CONTENT_TYPE,
"application/json");
HttpHeaders.setContentLength(resp, content.length);
return resp;
}
}
return RESPONSE_404;
}
示例5: clientToProxyRequest
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的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;
}
示例6: write
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* 写入数据到客户端
*
* @param messageResult
*/
public void write(final MessageResult messageResult) {
String json = messageResult.toJson();
ByteBuf content = Unpooled.copiedBuffer(json, CharsetUtil.UTF_8);
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
HttpHeaders.setContentLength(res, content.readableBytes());
// Send the response
ChannelFuture f = this.channel.writeAndFlush(res);
}
示例7: writeResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private void writeResponse(boolean lastContent) {
HttpResponse response = servletResponse.getNettyResponse();
// TODO implement exceptions required by http://tools.ietf.org/html/rfc2616#section-4.4
if (!HttpHeaders.isContentLengthSet(response)) {
if (lastContent) {
HttpHeaders.setContentLength(response, count);
} else {
HttpHeaders.setTransferEncodingChunked(response);
}
}
ctx.write(response, ctx.voidPromise());
}
示例8: testURIMatchOnly
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Test
public void testURIMatchOnly() throws IOException {
AppConfiguration appConfig = new AppConfiguration(new ConfigLoader(),
"./src/test/resources/testuserconfig.json");
appConfig.init();
RequestKeyGenerator keyGen = new RequestKeyGenerator(appConfig);
ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
buffer.writeBytes("{\"fromDate\":1464251112185,\"toDate\":1464337512185}"
.getBytes());
DefaultFullHttpRequest req1 = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_0, HttpMethod.GET,
"http://test.ebay.com/v1/s1", buffer);
String key1 = keyGen.generateKey(req1);
buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
buffer.writeBytes("{\"fromDate\":1464251113750,\"toDate\":1464337513750}"
.getBytes());
DefaultFullHttpRequest req2 = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_0, HttpMethod.GET,
"http://test.ebay.com/v1/s1", buffer);
String key2 = keyGen.generateKey(req2);
assertEquals(key1, key2);
HttpHeaders.setContentLength(req2, 758);
key2 = keyGen.generateKey(req2);
assertEquals(key1, key2);
appConfig.put("uriMatchOnly", null);
keyGen = new RequestKeyGenerator(appConfig);
key1 = keyGen.generateKey(req1);
key2 = keyGen.generateKey(req2);
assertNotEquals(key1, key2);
}
示例9: writeResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private void writeResponse(boolean lastContent) {
HttpResponse response = servletResponse.getNettyResponse();
// TODO implement exceptions required by container://tools.ietf.org/html/rfc2616#section-4.4
if (!HttpHeaders.isContentLengthSet(response)) {
if (lastContent) {
HttpHeaders.setContentLength(response, count);
} else {
HttpHeaders.setTransferEncodingChunked(response);
}
}
ctx.write(response, ctx.voidPromise());
}
示例10: clientToProxyRequest
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
if (!whitelistEnabled) {
return null;
}
if (httpObject instanceof HttpRequest) {
HttpRequest httpRequest = (HttpRequest) httpObject;
// do not allow HTTP CONNECTs to be short-circuited
if (ProxyUtils.isCONNECT(httpRequest)) {
return null;
}
boolean urlWhitelisted = false;
String url = getFullUrl(httpRequest);
for (Pattern pattern : whitelistUrls) {
if (pattern.matcher(url).matches()) {
urlWhitelisted = true;
break;
}
}
if (!urlWhitelisted) {
HttpResponseStatus status = HttpResponseStatus.valueOf(whitelistResponseCode);
HttpResponse resp = new DefaultFullHttpResponse(httpRequest.getProtocolVersion(), status);
HttpHeaders.setContentLength(resp, 0L);
return resp;
}
}
return null;
}
示例11: clientToProxyRequest
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
if (proxyServer.isStopped()) {
log.warn("Aborting request to {} because proxy is stopped", originalRequest.getUri());
HttpResponse abortedResponse = new DefaultFullHttpResponse(originalRequest.getProtocolVersion(), HttpResponseStatus.SERVICE_UNAVAILABLE);
HttpHeaders.setContentLength(abortedResponse, 0L);
return abortedResponse;
}
for (HttpFilters filter : filters) {
try {
HttpResponse filterResponse = filter.clientToProxyRequest(httpObject);
if (filterResponse != null) {
// if we are short-circuiting the response to an HttpRequest, update ModifiedRequestAwareFilter instances
// with this (possibly) modified HttpRequest before returning the short-circuit response
if (httpObject instanceof HttpRequest) {
updateFiltersWithModifiedResponse((HttpRequest) httpObject);
}
return filterResponse;
}
} catch (RuntimeException e) {
log.warn("Filter in filter chain threw exception. Filter method may have been aborted.", e);
}
}
// if this httpObject is the HTTP request, set the modified request object on all ModifiedRequestAwareFilter
// instances, so they have access to all modifications the request filters made while filtering
if (httpObject instanceof HttpRequest) {
updateFiltersWithModifiedResponse((HttpRequest) httpObject);
}
return null;
}
示例12: createOfflineResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private HttpResponse createOfflineResponse() {
ByteBuf buffer = Unpooled.wrappedBuffer("Offline response".getBytes());
HttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buffer);
HttpHeaders.setContentLength(response, buffer.readableBytes());
HttpHeaders.setHeader(response, HttpHeaders.Names.CONTENT_TYPE,
"text/html");
return response;
}
示例13: sendHttpResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Provide the response as per the requests
* @param ChannelHandlerContext, FullHttpRequest, FullHttpResponse
* @return void
*/
private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) throws Exception {
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());
}
ChannelFuture f = ctx.channel().writeAndFlush(res);
if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
示例14: sendHttpResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private 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);
}
}
开发者ID:spring-cloud,项目名称:spring-cloud-stream-app-starters,代码行数:16,代码来源:WebsocketSinkServerHandler.java
示例15: setContentLength
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public void setContentLength(int len) {
HttpHeaders.setContentLength(response, len);
}