当前位置: 首页>>代码示例>>Java>>正文


Java HttpHeaders.setContentLength方法代码示例

本文整理汇总了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();
}
 
开发者ID:tiglabs,项目名称:jsf-sdk,代码行数:27,代码来源:HttpJsonHandler.java

示例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);
    }
}
 
开发者ID:ninelook,项目名称:wecard-server,代码行数:23,代码来源:NettyServerHandler.java

示例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);
   }
}
 
开发者ID:SpreadServe,项目名称:TFWebSock,代码行数:17,代码来源:NettyHttpFileHandler.java

示例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;
}
 
开发者ID:eBay,项目名称:ServiceCOLDCache,代码行数:22,代码来源:NettyRequestProxyFilter.java

示例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;
}
 
开发者ID:misakuo,项目名称:Dream-Catcher,代码行数:26,代码来源:BlacklistFilter.java

示例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);
}
 
开发者ID:ninelook,项目名称:wecard-server,代码行数:16,代码来源:NHttpMessage.java

示例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());
}
 
开发者ID:geeker-lait,项目名称:tasfe-framework,代码行数:13,代码来源:HttpResponseOutputStream.java

示例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);
}
 
开发者ID:eBay,项目名称:ServiceCOLDCache,代码行数:38,代码来源:RequestKeyGeneratorTest.java

示例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());
}
 
开发者ID:paullyphang,项目名称:nebo,代码行数:13,代码来源:HttpResponseOutputStream.java

示例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;
}
 
开发者ID:misakuo,项目名称:Dream-Catcher,代码行数:37,代码来源:WhitelistFilter.java

示例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;
}
 
开发者ID:misakuo,项目名称:Dream-Catcher,代码行数:35,代码来源:BrowserMobHttpFilterChain.java

示例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;
}
 
开发者ID:wxyzZ,项目名称:little_mitm,代码行数:10,代码来源:LittleProxyMitmProxy.java

示例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);
	}
}
 
开发者ID:iotracks,项目名称:iofabric,代码行数:19,代码来源:LocalApiServerHandler.java

示例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);
}
 
开发者ID:geeker-lait,项目名称:tasfe-framework,代码行数:5,代码来源:NettyHttpServletResponse.java


注:本文中的io.netty.handler.codec.http.HttpHeaders.setContentLength方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。