本文整理汇总了Java中org.jboss.netty.handler.codec.http.HttpHeaders类的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaders类的具体用法?Java HttpHeaders怎么用?Java HttpHeaders使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpHeaders类属于org.jboss.netty.handler.codec.http包,在下文中一共展示了HttpHeaders类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
@Override
public void handle(Channel channel, Token<DelegationTokenIdentifier> token,
String serviceUrl) throws IOException {
Assert.assertEquals(testToken, token);
Credentials creds = new Credentials();
creds.addToken(new Text(serviceUrl), token);
DataOutputBuffer out = new DataOutputBuffer();
creds.write(out);
int fileLength = out.getData().length;
ChannelBuffer cbuffer = ChannelBuffers.buffer(fileLength);
cbuffer.writeBytes(out.getData());
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH,
String.valueOf(fileLength));
response.setContent(cbuffer);
channel.write(response).addListener(ChannelFutureListener.CLOSE);
}
示例2: executeString
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
private Promise<Response> executeString(String method, String body) {
FluentCaseInsensitiveStringsMap headers = new FluentCaseInsensitiveStringsMap(this.headers);
// Detect and maybe add charset
String contentType = headers.getFirstValue(HttpHeaders.Names.CONTENT_TYPE);
if (contentType == null) {
contentType = "text/plain";
}
String charset = AsyncHttpProviderUtils.parseCharset(contentType);
if (charset == null) {
charset = "utf-8";
headers.replace(HttpHeaders.Names.CONTENT_TYPE, contentType + "; charset=utf-8");
}
WSRequest req = new WSRequest(method).setBody(body)
.setUrl(url)
.setHeaders(headers)
.setQueryParameters(new FluentStringsMap(queryParameters))
.setBodyEncoding(charset);
return execute(req);
}
示例3: needCache
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
private boolean needCache(Request request, StaticAction staticAction) {
String ifModifiedSince = request.header(HttpHeaders.Names.IF_MODIFIED_SINCE);
if (!needCache || StringUtils.isBlank(ifModifiedSince)) {
return false;
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
try {
Date ifModifiedSinceDate = simpleDateFormat.parse(ifModifiedSince);
return ifModifiedSinceDate.getTime() / 1000 == staticAction.timestamp() / 1000;
} catch (ParseException ignored) {
}
return false;
}
示例4: getRequestBody
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
@Override
protected Object getRequestBody(Exchange exchange) throws Exception {
// creating the url to use takes 2-steps
String uri = NettyHttpHelper.createURL(exchange, getEndpoint());
URI u = NettyHttpHelper.createURI(exchange, uri, getEndpoint());
HttpRequest request = getEndpoint().getNettyHttpBinding().toNettyRequest(exchange.getIn(), u.toString(), getConfiguration());
String actualUri = request.getUri();
exchange.getIn().setHeader(Exchange.HTTP_URL, actualUri);
// Need to check if we need to close the connection or not
if (!HttpHeaders.isKeepAlive(request)) {
// just want to make sure we close the channel if the keepAlive is not true
exchange.setProperty(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, true);
}
if (getConfiguration().isBridgeEndpoint()) {
// Need to remove the Host key as it should be not used when bridging/proxying
exchange.getIn().removeHeader("host");
}
return request;
}
示例5: createRouteBuilder
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("netty-http:http://0.0.0.0:{{port}}/foo")
.to("mock:input")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.setContent(ChannelBuffers.copiedBuffer("Bye World".getBytes()));
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 9);
exchange.getOut().setBody(response);
}
});
}
};
}
示例6: removeTransferEncodingChunked
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
static void removeTransferEncodingChunked(HttpMessage m) {
List<String> values = m.headers().getAll(HttpHeaders.Names.TRANSFER_ENCODING);
if (values.isEmpty()) {
return;
}
Iterator<String> valuesIt = values.iterator();
while (valuesIt.hasNext()) {
String value = valuesIt.next();
if (value.equalsIgnoreCase(HttpHeaders.Values.CHUNKED)) {
valuesIt.remove();
}
}
if (values.isEmpty()) {
m.headers().remove(HttpHeaders.Names.TRANSFER_ENCODING);
} else {
m.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, values);
}
}
示例7: removeTransferEncodingChunked
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
public static void removeTransferEncodingChunked(HttpMessage worksReponse) {
List<String> values = worksReponse.headers().getAll(HttpHeaders.Names.TRANSFER_ENCODING);
if (values.isEmpty()) {
return;
}
Iterator<String> valuesIt = values.iterator();
while (valuesIt.hasNext()) {
String value = valuesIt.next();
if (value.equalsIgnoreCase(HttpHeaders.Values.CHUNKED)) {
valuesIt.remove();
}
}
if (values.isEmpty()) {
worksReponse.headers().remove(HttpHeaders.Names.TRANSFER_ENCODING);
} else {
worksReponse.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, values);
}
}
示例8: encode
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg)
throws Exception
{
RestResponse response = (RestResponse) msg;
HttpResponse nettyResponse =
new DefaultHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.valueOf(response.getStatus()));
for (Map.Entry<String, String> e : response.getHeaders().entrySet())
{
nettyResponse.setHeader(e.getKey(), e.getValue());
}
final ByteString entity = response.getEntity();
ChannelBuffer buf = ChannelBuffers.wrappedBuffer(entity.asByteBuffer());
nettyResponse.setContent(buf);
nettyResponse.setHeader(HttpHeaders.Names.CONTENT_LENGTH, entity.length());
return nettyResponse;
}
示例9: messageReceived
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
if (event.getMessage() instanceof HttpRequest) {
try {
HttpServletRequest httpServletRequest = new NettyHttpServletRequestAdaptor((HttpRequest) event.getMessage(), ctx.getChannel());
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.setContent(new DynamicChannelBuffer(200));
HttpServletResponse httpServletResponse = new NettyHttpServletResponseAdaptor(response, ctx.getChannel());
dispatcher.dispatch(httpServletRequest,httpServletResponse);
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH,response.getContent().writerIndex());
ChannelFuture future = ctx.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例10: writeResponse
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
private void writeResponse(MessageEvent e) {
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/json; charset=UTF-8");
ChannelBuffer content = ChannelBuffers.dynamicBuffer();
Writer writer = new OutputStreamWriter(new ChannelBufferOutputStream(content), CharsetUtil.UTF_8);
reportAdapter.toJson(report.get(), writer);
try {
writer.close();
} catch (IOException e1) {
LOG.error("error writing resource report", e1);
}
response.setContent(content);
ChannelFuture future = e.getChannel().write(response);
future.addListener(ChannelFutureListener.CLOSE);
}
示例11: executeString
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
private CompletableFuture<RestResponse> executeString(String body) {
FluentCaseInsensitiveStringsMap headers = new FluentCaseInsensitiveStringsMap(this.headers);
// Detect and maybe add charset
String contentType = headers.getFirstValue(HttpHeaders.Names.CONTENT_TYPE);
if (contentType == null) {
contentType = "text/plain";
}
String charset = AsyncHttpProviderUtils.parseCharset(contentType);
if (charset == null) {
charset = "utf-8";
headers.replace(HttpHeaders.Names.CONTENT_TYPE, contentType + "; charset=utf-8");
}
byte[] bodyBytes;
try {
bodyBytes = body.getBytes(charset);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
NingRestRequest req = new NingRestRequest(client, method, url, queryParameters, headers, bodyBytes)
.setBody(body)
.setBodyEncoding(charset);
return execute(req);
}
示例12: executeJson
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
private CompletableFuture<RestResponse> executeJson(JsonNode body) {
FluentCaseInsensitiveStringsMap headers = new FluentCaseInsensitiveStringsMap(this.headers);
headers.replace(HttpHeaders.Names.CONTENT_TYPE, "application/json; charset=utf-8");
String bodyStr = Json.stringify(body);
byte[] bodyBytes;
try {
bodyBytes = bodyStr.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
NingRestRequest req = new NingRestRequest(client, method, url, queryParameters, headers, bodyBytes)
.setBody(bodyStr)
.setBodyEncoding("utf-8");
return execute(req);
}
示例13: toHttpResponse
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
private HttpResponse toHttpResponse(HttpResponseStatus status, String content, Object jsonObject) {
// headers
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put(HttpHeaders.Names.CONTENT_TYPE, Arrays.asList("application/json"));
// response
byte[] response = content.getBytes(Charset.forName("UTF-8"));
if (jsonObject != null) {
try {
response = objectMapper.writeValueAsBytes(jsonObject);
} catch (Exception e) {
log.warn(e.getMessage(), e);
}
}
return new HttpResponse(status.getCode(), headers, response);
}
示例14: invalidRequestSent
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
public void invalidRequestSent(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
Channel channel = ctx.getChannel();
if (channel == null || !channel.isOpen()) {
log.debug("Not writing any response, channel is already closed.", e.getCause());
return;
}
final DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.BAD_REQUEST);
response.headers().add(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
response.headers().add(HttpHeaders.Names.CONNECTION, "close");
final ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
new ChannelBufferOutputStream(buffer).writeBytes("Your client has sent a malformed or illegal request.\n");
response.setContent(buffer);
final ChannelFuture channelFuture = channel.write(response);
channelFuture.addListener(ChannelFutureListener.CLOSE);
}
示例15: writeRequested
import org.jboss.netty.handler.codec.http.HttpHeaders; //导入依赖的package包/类
@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
final HttpRequest request = (HttpRequest) e.getMessage();
// If we have a proxy auth header, add it into the request
final ProxyAuthHandler handler = proxyAuthHandler.get();
final PasswordAuthentication auth = proxyAuthCredentials.get();
if (handler != null && auth != null) {
final String proxyAuthHeader = handler.authenticate(request, auth.getUserName(), new String(auth.getPassword()));
request.setHeader(HttpHeaders.Names.PROXY_AUTHORIZATION, proxyAuthHeader);
}
// request the connection be kept open for pipeling
request.setHeader(HttpHeaders.Names.CONNECTION, "Keep-Alive");
// request any proxy doesn't try give us a cached response
request.setHeader(HttpHeaders.Names.PRAGMA, "No-Cache");
lastRequest.set(request);
ctx.sendDownstream(e);
}