本文整理汇总了Java中io.netty.handler.codec.http.HttpHeaders.set方法的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaders.set方法的具体用法?Java HttpHeaders.set怎么用?Java HttpHeaders.set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.handler.codec.http.HttpHeaders
的用法示例。
在下文中一共展示了HttpHeaders.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNettyResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Get a Netty {@link HttpResponse}, committing the {@link HttpServletResponse}.
*/
public HttpResponse getNettyResponse() {
if (committed) {
return response;
}
committed = true;
HttpHeaders headers = response.headers();
if (null != contentType) {
String value = null == characterEncoding ? contentType : contentType + "; charset=" + characterEncoding;
headers.set(HttpHeaders.Names.CONTENT_TYPE, value);
}
CharSequence date = getFormattedDate();
headers.set(HttpHeaders.Names.DATE, date);
headers.set(HttpHeaders.Names.SERVER, servletContext.getServerInfoAscii());
// TODO cookies
return response;
}
示例2: createErrorMessage
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Create new HTTP carbon message.
*
* @param payload
* @param statusCode
* @return
*/
private static HTTPCarbonMessage createErrorMessage(String payload, int statusCode) {
HTTPCarbonMessage response = createHttpCarbonMessage(false);
StringDataSource stringDataSource = new StringDataSource(payload
, new HttpMessageDataStreamer(response).getOutputStream());
response.setMessageDataSource(stringDataSource);
byte[] errorMessageBytes = payload.getBytes(Charset.defaultCharset());
HttpHeaders httpHeaders = response.getHeaders();
httpHeaders.set(org.wso2.transport.http.netty.common.Constants.HTTP_CONNECTION,
org.wso2.transport.http.netty.common.Constants.CONNECTION_KEEP_ALIVE);
httpHeaders.set(org.wso2.transport.http.netty.common.Constants.HTTP_CONTENT_TYPE,
org.wso2.transport.http.netty.common.Constants.TEXT_PLAIN);
httpHeaders.set(org.wso2.transport.http.netty.common.Constants.HTTP_CONTENT_LENGTH,
(String.valueOf(errorMessageBytes.length)));
response.setProperty(org.wso2.transport.http.netty.common.Constants.HTTP_STATUS_CODE, statusCode);
response.setProperty(org.wso2.carbon.messaging.Constants.DIRECTION,
org.wso2.carbon.messaging.Constants.DIRECTION_RESPONSE);
return response;
}
示例3: startDownTask
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
public static void startDownTask(TaskInfo taskInfo, HttpRequest httpRequest,
HttpResponse httpResponse, Channel clientChannel) {
HttpHeaders httpHeaders = httpResponse.headers();
HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest);
HttpDownServer.DOWN_CONTENT.put(taskInfo.getId(), httpDownInfo);
httpHeaders.clear();
httpResponse.setStatus(HttpResponseStatus.OK);
httpHeaders.set(HttpHeaderNames.CONTENT_TYPE, "text/html");
String host = HttpDownServer.isDev() ? "localhost"
: ((InetSocketAddress) clientChannel.localAddress()).getHostString();
String js =
"<script>window.top.location.href='http://" + host + ":" + HttpDownServer.VIEW_SERVER_PORT
+ "/#/tasks/new/" + httpDownInfo
.getTaskInfo().getId()
+ "';</script>";
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(js.getBytes());
httpHeaders.set(HttpHeaderNames.CONTENT_LENGTH, js.getBytes().length);
clientChannel.writeAndFlush(httpResponse);
clientChannel.writeAndFlush(content);
clientChannel.close();
}
示例4: writeResponseHead
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public void writeResponseHead(Response restletResponse) throws IOException {
setNettyResponse(new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(getStatusCode(), getReasonPhrase())));
HttpHeaders headers = getNettyResponse().headers();
// this.response.clear();
for (Header header : getResponseHeaders()) {
headers.add(header.getName(), header.getValue());
}
// Decide whether to close the connection or not.
if (isKeepAlive()) {
headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
getNettyChannel().write(getNettyResponse());
} else {
getNettyChannel().writeAndFlush(getNettyResponse()).addListener(ChannelFutureListener.CLOSE);
}
}
示例5: getNettyResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Get a Netty {@link io.netty.handler.codec.http.HttpResponse}, committing the {@link javax.servlet.http.HttpServletResponse}.
*/
public HttpResponse getNettyResponse() {
if (committed) {
return response;
}
committed = true;
HttpHeaders headers = response.headers();
if (null != contentType) {
String value = null == characterEncoding ? contentType : contentType + "; charset=" + characterEncoding;
headers.set(HttpHeaders.Names.CONTENT_TYPE, value);
}else {
headers.set(HttpHeaders.Names.CONTENT_TYPE,
DEFAULT_CONTENT_TYPE);
}
headers.set(HttpHeaders.Names.DATE, new Date());
headers.set(HttpHeaders.Names.SERVER, servletContext.getServerInfoAscii());
for(Cookie ck : cookies) {
io.netty.handler.codec.http.cookie.Cookie nettyCookie = new DefaultCookie(ck.getName(),ck.getValue());
nettyCookie.setDomain(ck.getDomain());
nettyCookie.setPath(ck.getPath());
if( ck.getMaxAge() > 0) {
nettyCookie.setMaxAge(ck.getMaxAge());
}
// response.headers().add("Set-Cookie", nettyCookie);
response.headers().add("Set-Cookie", ServerCookieEncoder.STRICT.encode(nettyCookie));
}
return response;
}
示例6: directHandleMethod
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private boolean directHandleMethod(ChannelHandlerContext ctx, FullHttpRequest request, HttpMethod method) {
if (method.equals(HttpMethod.GET) || method.equals(HttpMethod.POST)) {
return false;
}
// 处理 OPTIONS 请求
HttpResponseStatus status = HttpResponseStatus.OK;
boolean invalid = false;
if (!method.equals(HttpMethod.OPTIONS)) {
invalid = true;
status = HttpResponseStatus.METHOD_NOT_ALLOWED;
}
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.EMPTY_BUFFER);
HttpHeaders headers = response.headers();
// headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS,
// "X-Requested-With, accept, origin, content-type");
headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "X-Requested-With, content-type");
headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "GET,POST,OPTIONS");
headers.set(HttpHeaderNames.SERVER, "Netty5");
if (invalid) {
headers.set("Client-Warning", "Invalid Method");
}
boolean keepAlive = HttpHeaderUtil.isKeepAlive(request);
if (keepAlive) {
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
ctx.write(response);
ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
if (!keepAlive) {
future.addListener(ChannelFutureListener.CLOSE);
}
return true;
}
示例7: wrapMessage
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public Object wrapMessage(ChannelHandlerContext ctx, ThriftMessage msg) {
HttpResponseStatus status = HttpResponseStatus.OK;
DefaultFullHttpResponse httpResp = new DefaultFullHttpResponse(HTTP_1_1, status, msg.getContent());
HttpHeaders headers = httpResp.headers();
headers.set(HttpHeaderNames.CONTENT_TYPE, "application/json");
headers.set(HttpHeaderNames.SERVER, "Netty5");
filterResponse(httpResp, msg);
return httpResp;
}
示例8: kerberosProxyChallenge
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private void kerberosProxyChallenge(Channel channel,//
List<String> proxyAuth,//
Request request,//
ProxyServer proxyServer,//
Realm proxyRealm,//
HttpHeaders headers,//
NettyResponseFuture<?> future) throws SpnegoEngineException {
String challengeHeader = SpnegoEngine.instance().generateToken(proxyServer.getHost());
headers.set(HttpHeaders.Names.PROXY_AUTHORIZATION, NEGOTIATE + " " + challengeHeader);
}
示例9: kerberosChallenge
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private void kerberosChallenge(Channel channel,//
List<String> authHeaders,//
Request request,//
HttpHeaders headers,//
Realm realm,//
NettyResponseFuture<?> future) throws SpnegoEngineException {
Uri uri = request.getUri();
String host = withDefault(request.getVirtualHost(), uri.getHost());
String challengeHeader = SpnegoEngine.instance().generateToken(host);
headers.set(HttpHeaders.Names.AUTHORIZATION, NEGOTIATE + " " + challengeHeader);
}
示例10: buildStartMessage
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public HttpRequest buildStartMessage(TransportRequest request, AttributeMap channelAttrs) {
DefaultHttpRequest httpRequest =
new DefaultHttpRequest(HttpTransport.HTTP_VERSION, HttpMethod.POST, url.getPath());
HttpHeaders httpHeaders = httpRequest.headers();
setCommonHeaders(httpHeaders, request, channelAttrs);
httpHeaders.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
return httpRequest;
}
示例11: setCommonHeaders
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private void setCommonHeaders(
HttpHeaders httpHeaders, TransportRequest request, AttributeMap channelAttrs) {
HeaderMapper.toHttpHeaders(request.getHeaders(), httpHeaders);
httpHeaders.set(HeaderMapper.SERVICE, request.getService());
httpHeaders.set(HeaderMapper.PROCEDURE, request.getProcedure());
httpHeaders.set(HeaderMapper.CALLER, request.getCaller());
httpHeaders.set(HeaderMapper.ENCODING, request.getEncoding());
// Required headers for HTTP
httpHeaders.set(HttpHeaderNames.HOST, hostString);
if (request.getDeadline() != null) {
Instant now = Instant.now();
long timeRemaining = ChronoUnit.MILLIS.between(now, request.getDeadline());
if (timeRemaining <= 0) {
throw new DeadlineExceededException();
}
httpHeaders.set(HeaderMapper.TIMEOUT, String.valueOf(timeRemaining));
}
if (request.getShardKey() != null) {
httpHeaders.set(HeaderMapper.SHARD_KEY, request.getShardKey());
}
if (request.getRoutingKey() != null) {
httpHeaders.set(HeaderMapper.ROUTING_KEY, request.getRoutingKey());
}
if (request.getRoutingDelegate() != null) {
httpHeaders.set(HeaderMapper.ROUTING_DELEGATE, request.getRoutingDelegate());
}
if (request.getSpan() != null) {
channelAttrs.attr(ChannelAttributes.SPAN).set(request.getSpan());
tracer.inject(
request.getSpan().context(), Builtin.HTTP_HEADERS, new TextMapInjectAdapter(httpHeaders));
}
}
示例12: removeSDCHEncoding
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Remove sdch from encodings we accept since we can't decode it.
*
* @param headers
* The headers to modify
*/
private void removeSDCHEncoding(HttpHeaders headers) {
String ae = headers.get(HttpHeaders.Names.ACCEPT_ENCODING);
if (StringUtils.isNotBlank(ae)) {
//
String noSdch = ae.replace(",sdch", "").replace("sdch", "");
headers.set(HttpHeaders.Names.ACCEPT_ENCODING, noSdch);
LOG.debug("Removed sdch and inserted: {}", noSdch);
}
}
示例13: switchProxyConnectionHeader
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Switch the de-facto standard "Proxy-Connection" header to "Connection"
* when we pass it along to the remote host. This is largely undocumented
* but seems to be what most browsers and servers expect.
*
* @param headers
* The headers to modify
*/
private void switchProxyConnectionHeader(HttpHeaders headers) {
String proxyConnectionKey = "Proxy-Connection";
if (headers.contains(proxyConnectionKey)) {
String header = headers.get(proxyConnectionKey);
headers.remove(proxyConnectionKey);
headers.set(HttpHeaders.Names.CONNECTION, header);
}
}
示例14: generateCarbonMessage
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* The method is responsible of generating carbon message to send.
*
* @param headers the headers set.
* @param contentType the content type. Value is if user has to given it as a header or if not it is map type.
* @param httpMethod http method type.
* @param cMessage carbon message to be send to the endpoint.
* @return generated carbon message.
*/
private HTTPCarbonMessage generateCarbonMessage(List<Header> headers, String contentType,
String httpMethod, HTTPCarbonMessage cMessage) {
/*
* set carbon message properties which is to be used in carbon transport.
*/
// Set protocol type http or https
cMessage.setProperty(Constants.PROTOCOL, httpURLProperties.get(HttpConstants.SCHEME));
// Set uri
cMessage.setProperty(Constants.TO, httpURLProperties.get(HttpConstants.TO));
// set Host
cMessage.setProperty(Constants.HOST, httpURLProperties.get(HttpConstants.HOST));
//set port
cMessage.setProperty(Constants.PORT, Integer.valueOf(httpURLProperties.get(HttpConstants.PORT)));
// Set method
cMessage.setProperty(HttpConstants.HTTP_METHOD, httpMethod);
HttpHeaders httpHeaders = cMessage.getHeaders();
//if Authentication enabled
if (!(userName.equals(HttpConstants.EMPTY_STRING)) && !(userPassword.equals
(HttpConstants.EMPTY_STRING))) {
httpHeaders.set(HttpConstants.AUTHORIZATION_HEADER, authorizationHeader);
} else if (!(userName.equals(HttpConstants.EMPTY_STRING)) || !(userPassword.equals
(HttpConstants.EMPTY_STRING))) {
log.error("One of the basic authentication username or password missing. Hence basic authentication not " +
"supported.");
}
/*
*set request headers.
*/
// Set user given Headers
if (headers != null) {
for (Header header : headers) {
httpHeaders.set(header.getName(), header.getValue());
}
}
// Set content type if content type s not included in headers
if (contentType.contains(mapType)) {
httpHeaders.set(HttpConstants.HTTP_CONTENT_TYPE, contentType);
}
//set method-type header
httpHeaders.set(HttpConstants.HTTP_METHOD, httpMethod);
return cMessage;
}
示例15: getHttpRequest
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
protected HttpRequest getHttpRequest(final O ioTask, final String nodeAddr)
throws URISyntaxException {
final I item = ioTask.getItem();
final IoType ioType = ioTask.getIoType();
final String srcPath = ioTask.getSrcPath();
final HttpMethod httpMethod;
final String uriPath;
if(item instanceof DataItem) {
httpMethod = getDataHttpMethod(ioType);
uriPath = getDataUriPath(item, srcPath, ioTask.getDstPath(), ioType);
} else if(item instanceof TokenItem) {
httpMethod = getTokenHttpMethod(ioType);
uriPath = getTokenUriPath(item, srcPath, ioTask.getDstPath(), ioType);
} else if(item instanceof PathItem) {
httpMethod = getPathHttpMethod(ioType);
uriPath = getPathUriPath(item, srcPath, ioTask.getDstPath(), ioType);
} else {
throw new AssertionError("Unsupported item class: " + item.getClass().getName());
}
final HttpHeaders httpHeaders = new DefaultHttpHeaders();
if(nodeAddr != null) {
httpHeaders.set(HttpHeaderNames.HOST, nodeAddr);
}
httpHeaders.set(HttpHeaderNames.DATE, DATE_SUPPLIER.get());
final HttpRequest httpRequest = new DefaultHttpRequest(
HTTP_1_1, httpMethod, uriPath, httpHeaders
);
switch(ioType) {
case CREATE:
if(srcPath == null || srcPath.isEmpty()) {
if(item instanceof DataItem) {
try {
httpHeaders.set(
HttpHeaderNames.CONTENT_LENGTH, ((DataItem) item).size()
);
} catch(final IOException ignored) {
}
} else {
httpHeaders.set(HttpHeaderNames.CONTENT_LENGTH, 0);
}
} else {
applyCopyHeaders(httpHeaders, getDataUriPath(item, srcPath, null, ioType));
httpHeaders.set(HttpHeaderNames.CONTENT_LENGTH, 0);
}
break;
case READ:
httpHeaders.set(HttpHeaderNames.CONTENT_LENGTH, 0);
if(ioTask instanceof DataIoTask) {
applyRangesHeaders(httpHeaders, (DataIoTask) ioTask);
}
break;
case UPDATE:
final DataIoTask dataIoTask = (DataIoTask) ioTask;
httpHeaders.set(HttpHeaderNames.CONTENT_LENGTH, dataIoTask.getMarkedRangesSize());
applyRangesHeaders(httpHeaders, dataIoTask);
break;
case DELETE:
httpHeaders.set(HttpHeaderNames.CONTENT_LENGTH, 0);
break;
}
applyMetaDataHeaders(httpHeaders);
applyDynamicHeaders(httpHeaders);
applySharedHeaders(httpHeaders);
applyAuthHeaders(httpHeaders, httpMethod, uriPath, ioTask.getCredential());
return httpRequest;
}