List of usage examples for io.netty.buffer ByteBuf release
boolean release();
From source file:faststore.net.netty.common.faststoreprotocol.netty.FastStoreNettyDecoder.java
License:Apache License
@Override public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = null; try {//from w w w.j ava 2s . c om frame = (ByteBuf) super.decode(ctx, in); if (null == frame) { return null; } ByteBuffer byteBuffer = frame.nioBuffer(); return RequestDecoder.decode(byteBuffer); } catch (Exception e) { // log.error("decode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); // RemotingUtil.closeChannel(ctx.channel()); } finally { if (null != frame) { frame.release(); } } return null; }
From source file:firebats.http.server.exts.form.Form.java
License:Apache License
private static Form decodeWithContent(Context context, ByteBuf content) { //new DefaultHttpDataFactory(/*useDisk*/true)decoder? //Mix?16K???//from w w w .j a va 2s . c o m // final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(/*useDisk*/true),toNettyHttpRequest(context.request)); HttpServerRequest<ByteBuf> rxRequest = context.getRequest(); HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, rxRequest.getHttpMethod(), rxRequest.getUri()); for (Map.Entry<String, String> header : rxRequest.getHeaders().entries()) { nettyRequest.headers().add(header.getKey(), header.getValue()); } final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(nettyRequest); HttpContent httpContent = new DefaultHttpContent(content); decoder.offer(httpContent); decoder.offer(LastHttpContent.EMPTY_LAST_CONTENT); Map<String, String> formParams = new LinkedHashMap<>(); Map<String, UploadedFile> files = new LinkedHashMap<>(); try { while (decoder.hasNext()) { InterfaceHttpData data = decoder.next(); if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) { try { Attribute attr = (Attribute) data; if (!formParams.containsKey(data.getName())) { formParams.put(attr.getName(), attr.getValue()); } } catch (IOException e) { Throwables.propagate(e); } finally { //? data.release(); } } else if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.FileUpload)) { try { if (!files.containsKey(data.getName())) { final FileUpload nettyFileUpload = (FileUpload) data; final ByteBuf byteBuf = nettyFileUpload.content(); byteBuf.retain(); context.onComplete(new Action0() { @Override public void call() { if (log.isDebugEnabled()) { log.debug("form upload file release[" + data.getName() + ":" + nettyFileUpload.getFilename() + "]"); } byteBuf.release(); } }); UploadedFile fileUpload = new UploadedFile(nettyFileUpload.getFilename(), nettyFileUpload.getContentType(), byteBuf); files.put(data.getName(), fileUpload); } } finally { data.release(); } } } } catch (HttpPostRequestDecoder.EndOfDataDecoderException ignore) { // ignore } finally { decoder.destroy(); } Map<String, String> query = Form.toFlatQueryParams(context.getRequest().getQueryParameters()); return fromAll(query, formParams, files); }
From source file:fr.meuret.web.HttpStaticFileServerHandler.java
License:Apache License
private static void sendListing(ChannelHandlerContext ctx, Path root, Path absolutePath) { logger.debug("Send listing for absolutePath={}", absolutePath); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); final StringBuilder buf = new StringBuilder(); buf.append("<!DOCTYPE html>\r\n"); buf.append("<html><head><title>"); buf.append("Listing of: "); //Root filename (in case the root path corresponds to the root of the filesystem (i.e c:\\) String filename = "/"; Path relativePath = root.relativize(absolutePath); if (!relativePath.getFileName().toString().isEmpty()) filename = absolutePath.getFileName().toString(); buf.append(filename);/* ww w. j ava 2 s.c o m*/ buf.append("</title></head><body>\r\n"); buf.append("<h3>Listing of: "); buf.append(filename); buf.append("</h3>\r\n"); buf.append("<ul>"); buf.append("<li><a href=\"../\">..</a></li>\r\n"); logger.debug("Browsing the path : {}", absolutePath); try (DirectoryStream<Path> stream = Files.newDirectoryStream(absolutePath, new DirectoryStream.Filter<Path>() { @Override public boolean accept(Path entry) throws IOException { return !Files.isHidden(entry) && Files.isReadable(entry) && ALLOWED_FILE_NAME.matcher(entry.getFileName().toString()).matches(); } })) { for (Path entry : stream) { logger.debug("Appending the path entry {} to the listing.", entry); buf.append("<li><a href=\""); buf.append(entry.getFileName().toString()); buf.append("\">"); buf.append(entry.getFileName()); buf.append("</a></li>\r\n"); } } catch (IOException | DirectoryIteratorException e) { logger.error("Error occuring when browsing " + absolutePath, e); } buf.append("</ul></body></html>\r\n"); ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8); response.content().writeBytes(buffer); buffer.release(); // Close the connection as soon as the error message is sent. logger.debug("Writing the listing to the channel"); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:gribbit.server.siteresources.CacheExtension.java
License:Open Source License
/** * Create a hash URI (which allows the browser to cache this resource indefinitely) if the last modified * timestamp has increased, or if there is no hash URI yet for this resource. For a new hash URI to be created, * the passed object is scheduled to be hashed by a background thread. * * This method can be called by any route handler that stores or returns database objects. It should be called * both when storing objects and when returning them, since the hash URI cache is held in RAM and is empty when * the server starts, so it needs to be built as requests start to come in. IMPORTANT NOTE: If the database can * be written to by other database clients, then this method must also be called when those changes are * detected, otherwise web clients connected to this web server will continue to serve old linked resources. * /*from www .j a v a2 s .c o m*/ * This method should only be used when the total keyspace of URIs that map to database objects easily fits in * RAM, and when the objects that need to be hashed are not large (i.e. tens of MB is OK, hundreds of MB is * probably not, since there are several background worker threads and they all can be hashing objects in * parallel). */ public static void updateHashURI(String origURI, ByteBuf content, long lastModifiedEpochSeconds) { // Can only hash absolute but local (i.e. domain-less) URIs that have not already been hashed if (origURI.startsWith("/") && !origURI.startsWith("//") && !origURI.startsWith("/_/")) { // Check to see if there is already a mapping to hash URI for this original URI HashInfo hashInfo = origURIToHashInfo.get(origURI); if (hashInfo == null || hashInfo.lastModifiedEpochSeconds < lastModifiedEpochSeconds) { // There is no hash URI yet for origURI, or there is already a hash URI corresponding to origURI, // but the modification time has increased since the cached version, so need to re-hash. // Check if another thread has already enqueued the URI for hashing. Object alreadyInQueue = scheduledURIsToHash.put(origURI, new Object()); if (alreadyInQueue == null) { content.retain(); // This URI is not currently queued for hashing by background workers, add it to the queue scheduleHasher(origURI, lastModifiedEpochSeconds, new Hasher() { @Override public String computeHashKey() { // Compute MD5 hash of the ByteBuf contents, then base64-encode the results try { String hash = Base64Safe .base64Encode(DigestUtils.md5(new ByteBufInputStream(content))); content.release(); // TODO: does ByteBufInputStream call release()? return hash; } catch (IOException e) { return null; } } }); } } } }
From source file:http.HttpFileServerHandler.java
License:Apache License
private static void sendListing(ChannelHandlerContext ctx, File dir) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); StringBuilder buf = new StringBuilder(); String dirPath = dir.getPath(); buf.append("<!DOCTYPE html>\r\n"); buf.append("<html><head><title>"); buf.append(dirPath);/*from w w w.j av a 2 s .c om*/ buf.append(" "); buf.append("</title></head><body>\r\n"); buf.append("<h3>"); buf.append(dirPath).append(" "); buf.append("</h3>\r\n"); buf.append("<ul>"); buf.append("<li><a href=\"../\">..</a></li>\r\n"); for (File f : dir.listFiles()) { if (f.isHidden() || !f.canRead()) { continue; } String name = f.getName(); if (!ALLOWED_FILE_NAME.matcher(name).matches()) { continue; } buf.append("<li><a href=\""); buf.append(name); buf.append("\">"); buf.append(name); buf.append("</a></li>\r\n"); } buf.append("</ul></body></html>\r\n"); // ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8); response.content().writeBytes(buffer); buffer.release(); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:io.advantageous.conekt.net.impl.ConektHandler.java
License:Open Source License
protected static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) { if (buf == Unpooled.EMPTY_BUFFER) { return buf; }/*from w ww . j a va2 s .c o m*/ if (buf.isDirect() || buf instanceof CompositeByteBuf) { try { if (buf.isReadable()) { ByteBuf buffer = allocator.heapBuffer(buf.readableBytes()); buffer.writeBytes(buf); return buffer; } else { return Unpooled.EMPTY_BUFFER; } } finally { buf.release(); } } return buf; }
From source file:io.advantageous.conekt.test.core.FileSystemTest.java
License:Open Source License
@Test public void testWriteStreamWithCompositeBuffer() throws Exception { String fileName = "some-file.dat"; int chunkSize = 1000; int chunks = 10; byte[] content1 = TestUtils.randomByteArray(chunkSize * (chunks / 2)); byte[] content2 = TestUtils.randomByteArray(chunkSize * (chunks / 2)); ByteBuf byteBuf = Unpooled.wrappedBuffer(content1, content2); Buffer buff = Buffer.buffer(byteBuf); conekt.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), ar -> { if (ar.succeeded()) { WriteStream<Buffer> ws = ar.result(); ws.exceptionHandler(t -> fail(t.getMessage())); ws.write(buff);/*from w w w . java 2 s . co m*/ ar.result().close(ar2 -> { if (ar2.failed()) { fail(ar2.cause().getMessage()); } else { assertTrue(fileExists(fileName)); byte[] readBytes; try { readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName)); } catch (IOException e) { fail(e.getMessage()); return; } assertEquals(buff, Buffer.buffer(readBytes)); byteBuf.release(); testComplete(); } }); } else { fail(ar.cause().getMessage()); } }); await(); }
From source file:io.airlift.drift.transport.netty.client.ThriftClientHandler.java
License:Apache License
private void sendMessage(ChannelHandlerContext context, ThriftRequest thriftRequest, ChannelPromise promise) throws Exception { // todo ONEWAY_SEQUENCE_ID is a header protocol thing... make sure this works with framed and unframed int sequenceId = thriftRequest.isOneway() ? ONEWAY_SEQUENCE_ID : this.sequenceId.incrementAndGet(); RequestHandler requestHandler = new RequestHandler(thriftRequest, sequenceId); // register timeout requestHandler.registerRequestTimeout(context.executor()); // write request ByteBuf requestBuffer = requestHandler.encodeRequest(context.alloc()); // register request if we are expecting a response if (!thriftRequest.isOneway()) { if (pendingRequests.putIfAbsent(sequenceId, requestHandler) != null) { requestHandler.onChannelError( new TTransportException("Another request with the same sequenceId is already in progress")); requestBuffer.release(); return; }//from w ww . j av a2s . co m } // if this connection is failed, immediately fail the request TException channelError = this.channelError.get(); if (channelError != null) { thriftRequest.failed(channelError); requestBuffer.release(); return; } try { ThriftFrame thriftFrame = new ThriftFrame(sequenceId, requestBuffer, thriftRequest.getHeaders(), transport, protocol, true); ChannelFuture sendFuture = context.write(thriftFrame, promise); sendFuture.addListener(future -> messageSent(context, sendFuture, requestHandler)); } catch (Throwable t) { onError(context, t, Optional.of(requestHandler)); requestBuffer.release(); } }
From source file:io.airlift.drift.transport.netty.codec.HeaderTransport.java
License:Apache License
/** * Decodes the ByteBuf into a HeaderFrame transferring the reference ownership. * @param buffer buffer to be decoded; reference count ownership is transferred to this method * @return the decoded frame; caller is responsible for releasing this object *///w ww .java 2 s . c o m public static ThriftFrame decodeFrame(ByteBuf buffer) { ByteBuf messageHeader = null; try { // frame header short magic = buffer.readShort(); verify(magic == HEADER_MAGIC, "Invalid header magic"); short flags = buffer.readShort(); boolean outOfOrderResponse; switch (flags) { case FLAGS_NONE: outOfOrderResponse = false; break; case FLAG_SUPPORT_OUT_OF_ORDER: outOfOrderResponse = true; break; default: throw new IllegalArgumentException("Unsupported header flags: " + flags); } int frameSequenceId = buffer.readInt(); int headerSize = buffer.readShort() << 2; messageHeader = buffer.readBytes(headerSize); // encoding info byte protocolId = messageHeader.readByte(); Protocol protocol = Protocol.getProtocolByHeaderTransportId(protocolId); byte numberOfTransforms = messageHeader.readByte(); if (numberOfTransforms > 0) { // currently there are only two transforms, a cryptographic extension which is deprecated, and gzip which is too expensive throw new IllegalArgumentException("Unsupported transform"); } // headers // todo what about duplicate headers? ImmutableMap.Builder<String, String> allHeaders = ImmutableMap.builder(); allHeaders.putAll(decodeHeaders(NORMAL_HEADERS, messageHeader)); allHeaders.putAll(decodeHeaders(PERSISTENT_HEADERS, messageHeader)); // message ByteBuf message = buffer.readBytes(buffer.readableBytes()); // header frame wraps message byte buffer, so message should not be release yet return new ThriftFrame(frameSequenceId, message, allHeaders.build(), HEADER, protocol, outOfOrderResponse); } finally { // message header in an independent buffer and must be released if (messageHeader != null) { messageHeader.release(); } // input buffer has been consumed and transformed into a HeaderFrame, so release it buffer.release(); } }
From source file:io.airlift.drift.transport.netty.codec.HeaderTransport.java
License:Apache License
public static Optional<FrameInfo> tryDecodeFrameInfo(ByteBuf input) { ByteBuf buffer = input.retainedDuplicate(); try {/*ww w . j ava 2 s . c om*/ if (buffer.readableBytes() < FRAME_HEADER_SIZE) { return Optional.empty(); } // skip magic buffer.readShort(); short flags = buffer.readShort(); boolean outOfOrderResponse = (flags & FLAG_SUPPORT_OUT_OF_ORDER_MASK) == 1; int headerSequenceId = buffer.readInt(); int headerSize = buffer.readShort() << 2; if (buffer.readableBytes() < headerSize) { return Optional.empty(); } byte protocolId = buffer.getByte(buffer.readerIndex()); Protocol protocol = Protocol.getProtocolByHeaderTransportId(protocolId); buffer.skipBytes(headerSize); SimpleFrameInfoDecoder simpleFrameInfoDecoder = new SimpleFrameInfoDecoder(HEADER, protocol, outOfOrderResponse); Optional<FrameInfo> frameInfo = simpleFrameInfoDecoder.tryDecodeFrameInfo(buffer); if (frameInfo.isPresent()) { int messageSequenceId = frameInfo.get().getSequenceId(); checkArgument(headerSequenceId == messageSequenceId, "Sequence ids don't match. headerSequenceId: %s. messageSequenceId: %s", headerSequenceId, messageSequenceId); } return frameInfo; } finally { buffer.release(); } }