本文整理汇总了Java中io.netty.handler.codec.http.ServerCookieEncoder类的典型用法代码示例。如果您正苦于以下问题:Java ServerCookieEncoder类的具体用法?Java ServerCookieEncoder怎么用?Java ServerCookieEncoder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServerCookieEncoder类属于io.netty.handler.codec.http包,在下文中一共展示了ServerCookieEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toFullHttpResponse
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
/**
* 转换为Netty所用Response
*
* @return FullHttpResponse
*/
public FullHttpResponse toFullHttpResponse() {
final FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(httpVersion, status, content);
// headers
final HttpHeaders httpHeaders = fullHttpResponse.headers().add(headers);
httpHeaders.set(Names.CONTENT_TYPE, contentType + "; charset=" + charset);
httpHeaders.set(Names.CONTENT_ENCODING, charset);
httpHeaders.set(Names.CONTENT_LENGTH, content.readableBytes());
// Cookies
for (Cookie cookie : cookies) {
httpHeaders.add(Names.SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
return fullHttpResponse;
}
示例2: addCookie
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
@Override
public void addCookie(Cookie arg0) {
String cookieString = m_nettyhttpresp.headers().get(HttpHeaders.Names.COOKIE);
Set<io.netty.handler.codec.http.Cookie> cookies;
if (cookieString != null) {
cookies = CookieDecoder.decode(cookieString);
cookies.add(new io.netty.handler.codec.http.DefaultCookie(arg0.getName(), arg0.getValue()));
} else {
cookies = new HashSet<io.netty.handler.codec.http.Cookie>();
cookies.add(new io.netty.handler.codec.http.DefaultCookie(arg0.getName(), arg0.getValue()));
}
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
HttpHeaders.setHeader(m_nettyhttpresp, HttpHeaders.Names.SET_COOKIE, ServerCookieEncoder.encode(cookies));
}
}
示例3: setSigned
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
public void setSigned(String name, String value, long timeout, String path, HttpServerRequest request) {
Cookie cookie = new DefaultCookie(name, value);
cookie.setMaxAge(timeout);
cookie.setSecure("https".equals(Renders.getScheme(request)));
cookie.setHttpOnly(true);
if (path != null && !path.trim().isEmpty()) {
cookie.setPath(path);
}
if (signKey != null) {
try {
signCookie(cookie);
} catch (InvalidKeyException | NoSuchAlgorithmException
| IllegalStateException | UnsupportedEncodingException e) {
log.error(e);
return;
}
request.response().headers().add("Set-Cookie", ServerCookieEncoder.encode(cookie));
}
}
示例4: setCookie
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
void setCookie(HttpRequest request, FullHttpResponse response) {
// Encode the cookie.
String cookieString = request.headers().get(COOKIE);
if (cookieString != null) {
Set<Cookie> cookies = CookieDecoder.decode(cookieString);
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
for (Cookie cookie: cookies) {
if("visit-count".equals(cookie.getName())) {
int count = Integer.parseInt(cookie.getValue()) ;
cookie.setValue(Integer.toString(count + 1));
//System.out.println("Visit: " + count);
}
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
}
} else {
// Browser sent no cookie. Add some.
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("id", UUID.randomUUID().toString()));
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("visit-count", "1"));
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("first-visit-time", new Date().toString()));
}
}
示例5: handleConnect
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
private void handleConnect(ChannelHandlerContext ctx, HttpRequest req) {
boolean isConnected = false;
String sessionId = HttpSessionStore.getClientSessionId(req);
HttpChannelEntity httpChannelEntity = null;
if (!HttpSessionStore.checkJSessionId(sessionId)) {
sessionId = HttpSessionStore.genJSessionId();
httpChannelEntity = new HttpJsonpChannelEntity(sessionId, true);
MemoryMetaPool.registerClienId(sessionId, httpChannelEntity);
} else {
isConnected = true;
httpChannelEntity = (HttpChannelEntity) MemoryMetaPool
.getChannelEntryByClientId(sessionId);
}
Map<String, Object> map = new HashMap<String, Object>(2);
map.put("status", true);
map.put("clientId", sessionId);
String result = gson.toJson(map);
ByteBuf content = ctx.alloc().directBuffer()
.writeBytes(result.getBytes());
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK,
content);
if (isConnected) {
res.headers().add("Set-Cookie",
ServerCookieEncoder.encode("JSESSIONID", sessionId));
}
res.headers().set(CONTENT_TYPE, HEADER_CONTENT_TYPE);
setContentLength(res, content.readableBytes());
ScheduledFuture<?> scheduleTask = ctx.executor().schedule(
new SessionTimeoutTask(sessionId), 60, TimeUnit.SECONDS);
httpChannelEntity.setScheduleTask(scheduleTask);
sendHttpResponse(ctx, req, res);
}
示例6: writeResponse
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的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, "text/plain; charset=UTF-8");
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);
}
// Encode the cookie.
String cookieString = request.headers().get(COOKIE);
if (cookieString != null) {
Set<Cookie> cookies = CookieDecoder.decode(cookieString);
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
for (Cookie cookie: cookies) {
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
}
} else {
// Browser sent no cookie. Add some.
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
}
// Write the response.
ctx.write(response);
return keepAlive;
}
示例7: writeResponse
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的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.decoderResult().isSuccess()? OK : BAD_REQUEST,
Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
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);
}
// Encode the cookie.
String cookieString = request.headers().get(COOKIE);
if (cookieString != null) {
Set<Cookie> cookies = CookieDecoder.decode(cookieString);
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
for (Cookie cookie: cookies) {
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
}
} else {
// Browser sent no cookie. Add some.
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
}
// Write the response.
ctx.write(response);
return keepAlive;
}
示例8: writeResponse
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
// Decide whether to close the connection or not.
boolean keepAlive = 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, "text/plain; charset=UTF-8");
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);
}
// Encode the cookie.
String cookieString = request.headers().get(COOKIE);
if (cookieString != null) {
Set<Cookie> cookies = CookieDecoder.decode(cookieString);
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
for (Cookie cookie: cookies) {
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
}
} else {
// Browser sent no cookie. Add some.
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
}
// Write the response.
ctx.write(response);
return keepAlive;
}
示例9: writeResponse
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
// Decide whether to close the connection or not.
boolean keepAlive = 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, "text/plain; charset=UTF-8");
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);
}
// Encode the cookie.
String cookieString = request.headers().get(COOKIE);
if (cookieString != null) {
Set<Cookie> cookies = CookieDecoder.decode(cookieString);
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
for (Cookie cookie : cookies) {
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
}
} else {
// Browser sent no cookie. Add some.
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
}
// Write the response.
ctx.write(response);
return keepAlive;
}
示例10: writeResponse
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的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, "text/plain; charset=UTF-8");
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);
}
// Encode the cookie.
String cookieString = request.headers().get(COOKIE);
if (cookieString != null) {
Set<Cookie> cookies = CookieDecoder.decode(cookieString);
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
for (Cookie cookie : cookies) {
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
}
} else {
// Browser sent no cookie. Add some.
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
}
// Write the response.
ctx.write(response);
return keepAlive;
}
示例11: set
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
public static void set(String name, String value, long timeout, String path, HttpServerRequest request) {
Cookie cookie = new DefaultCookie(name, value);
cookie.setMaxAge(timeout);
cookie.setSecure("https".equals(Renders.getScheme(request)));
if (path != null && !path.trim().isEmpty()) {
cookie.setPath(path);
}
request.response().headers().add("Set-Cookie", ServerCookieEncoder.encode(cookie));
}
示例12: writeResponse
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
private void writeResponse(Channel channel) {
// Convert the response content to a ChannelBuffer.
ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8);
responseContent.setLength(0);
// Decide whether to close the connection or not.
boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION))
|| request.getProtocolVersion().equals(HttpVersion.HTTP_1_0)
&& !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION));
// Build the response object.
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
if (!close) {
// There's no need to add 'Content-Length' header
// if this is the last response.
response.headers().set(CONTENT_LENGTH, buf.readableBytes());
}
Set<Cookie> cookies;
String value = request.headers().get(COOKIE);
if (value == null) {
cookies = Collections.emptySet();
} else {
cookies = CookieDecoder.decode(value);
}
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
for (Cookie cookie : cookies) {
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
}
// Write the response.
ChannelFuture future = channel.writeAndFlush(response);
// Close the connection after the write operation is done if necessary.
if (close) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
示例13: writeResponse
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的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, "text/plain; charset=UTF-8");
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);
}
// Encode the cookie.
String cookieString = request.headers().get(COOKIE);
if (cookieString != null) {
Set<Cookie> cookies = CookieDecoder.decode(cookieString);
if (!cookies.isEmpty()) {
// Reset the cookies if necessary.
for (Cookie cookie : cookies) {
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
}
}
} else {
// Browser sent no cookie. Add some.
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key1", "value1"));
response.headers().add(SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));
}
// Write the response.
ctx.write(response);
return keepAlive;
}
示例14: encode
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
String encode() {
return ServerCookieEncoder.encode(c);
}
示例15: bake
import io.netty.handler.codec.http.ServerCookieEncoder; //导入依赖的package包/类
public ResponseBuilder bake() {
addCookie(ServerCookieEncoder.encode(cookie));
return ResponseBuilder.this;
}