List of usage examples for io.netty.buffer Unpooled copiedBuffer
private static ByteBuf copiedBuffer(CharBuffer buffer, Charset charset)
From source file:divconq.http.multipart.InternalAttribute.java
License:Apache License
public void setValue(String value, int rank) { if (value == null) { throw new NullPointerException("value"); }//from w w w . ja v a 2s. c om ByteBuf buf = Unpooled.copiedBuffer(value, charset); ByteBuf old = this.value.set(rank, buf); if (old != null) { size -= old.readableBytes(); } size += buf.readableBytes(); }
From source file:divconq.net.ssl.PemReader.java
License:Apache License
static ByteBuf[] readCertificates(File file) throws CertificateException { String content;//from ww w .j av a2 s . co m try { content = readContent(file); } catch (IOException e) { throw new CertificateException("failed to read a file: " + file, e); } List<ByteBuf> certs = new ArrayList<ByteBuf>(); Matcher m = CERT_PATTERN.matcher(content); int start = 0; for (;;) { if (!m.find(start)) { break; } ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII); ByteBuf der = Base64.decode(base64); base64.release(); certs.add(der); start = m.end(); } if (certs.isEmpty()) { throw new CertificateException("found no certificates: " + file); } return certs.toArray(new ByteBuf[certs.size()]); }
From source file:divconq.net.ssl.PemReader.java
License:Apache License
static ByteBuf readPrivateKey(File file) throws KeyException { String content;//from www . j a va 2 s . c o m try { content = readContent(file); } catch (IOException e) { throw new KeyException("failed to read a file: " + file, e); } Matcher m = KEY_PATTERN.matcher(content); if (!m.find()) { throw new KeyException("found no private key: " + file); } ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII); ByteBuf der = Base64.decode(base64); base64.release(); return der; }
From source file:dpfmanager.shell.modules.server.get.HttpGetHandler.java
License:Open Source License
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:eastwind.webpush.WebPushHandler.java
License:Apache License
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws JsonProcessingException { // Handle a bad request. if (!req.decoderResult().isSuccess()) { sendHttpResponse(ctx, req,/*from w w w .j av a 2 s . co m*/ new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); return; } // Allow only GET methods. if (req.method() != HttpMethod.GET) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN)); return; } String uid = null; String uuid = null; Channel channel = ctx.channel(); int q = req.uri().indexOf("?"); String path = req.uri(); if (q != -1) { path = path.substring(0, q); } List<String> l = Lists.newLinkedList(Splitter.on("/").omitEmptyStrings().trimResults().split(path)); if (l.size() >= 2) { uid = l.get(0); uuid = l.get(1); Session s = sessionManager.get(uid, uuid); if (s == null) { logger.info("expired:{}-{}", uid, uuid); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN)); } if (l.size() == 2) { s.setChannel(channel); s.trySendMessages(); } else { String oper = l.get(2); handleHttpOper(ctx, req, s, oper); } } else { String params = ""; if (q != -1) { String uri = req.uri(); if (q < uri.length() - 1) { params = uri.substring(q + 1); } } try { uid = action.active(channel.remoteAddress(), params); } catch (Throwable th) { logger.warn("active:", th); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.copiedBuffer(th.getClass().getName(), Charset.forName("utf-8")))); return; } if (uid != null) { uuid = sessionManager.create(uid).getUuid(); logger.info("active:{}-{}", uid, uuid); SessionGroup sg = sessionManager.get(uid); timer.newTimeout(new SessionCleaner(sg), lost, TimeUnit.MILLISECONDS); } // websocket if ("Upgrade".equals(req.headers().get(HttpHeaderNames.CONNECTION)) && "websocket".equals(req.headers().get(HttpHeaderNames.UPGRADE))) { // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( getWebSocketLocation(req, ""), null, true); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel); return; } else { handshaker.handshake(channel, req); UserLite.set(channel, new UserLite(uid, uuid)); } } else { String content = String.format("{\"uid\":\"%s\", \"uuid\":\"%s\"}", uid, uuid); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer(content, Charset.forName("utf-8")))); } } }
From source file:eastwind.webpush.WebPushHandler.java
License:Apache License
private void handleHttpOper(ChannelHandlerContext ctx, FullHttpRequest req, Session s, String oper) throws JsonProcessingException { String uid = s.getUid();//from www . j a v a 2s.c om if (oper.equals("registers")) { QueryStringDecoder decoder = new QueryStringDecoder(req.uri()); List<String> types = decoder.parameters().get("type"); s.registerTypes(types); if (types != null) { logger.debug("registers:{}-{}", uid, objectMapper.writeValueAsString(types)); } sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer("{}", Charset.forName("utf-8")))); } else if (oper.equals("register")) { QueryStringDecoder decoder = new QueryStringDecoder(req.uri()); List<String> types = decoder.parameters().get("type"); if (types != null && types.size() > 0) { s.registerType(types.get(0)); logger.debug("register:{}-{}", uid, types.get(0)); } sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer("{}", Charset.forName("utf-8")))); } else if (oper.equals("cancel")) { s.setCanceled(); sessionManager.get(uid).remove(s); logger.debug("cancel:{}-{}", uid, s.getUuid()); } }
From source file:eastwind.webpush.WebPushHandler.java
License:Apache License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);//from ww w . j av a2 s .c o m buf.release(); } // Send the response and close the connection if necessary. res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/json; charset=UTF-8"); res.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*"); HttpUtil.setContentLength(res, res.content().readableBytes()); ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }