001
014
015 package com.liferay.portal.fabric.netty.fileserver.handlers;
016
017 import com.liferay.portal.fabric.netty.codec.serialization.ObjectDecodeChannelInboundHandler;
018 import com.liferay.portal.fabric.netty.fileserver.FileResponse;
019 import com.liferay.portal.kernel.concurrent.AsyncBroker;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022
023 import io.netty.buffer.ByteBuf;
024 import io.netty.channel.ChannelHandlerContext;
025 import io.netty.channel.ChannelPipeline;
026 import io.netty.util.concurrent.EventExecutorGroup;
027
028 import java.io.IOException;
029
030 import java.nio.file.Path;
031
032
035 public class FileResponseChannelHandler
036 extends ObjectDecodeChannelInboundHandler<FileResponse> {
037
038 public FileResponseChannelHandler(
039 AsyncBroker<Path, FileResponse> asyncBroker,
040 EventExecutorGroup eventExecutorGroup) {
041
042 _asyncBroker = asyncBroker;
043 _eventExecutorGroup = eventExecutorGroup;
044 }
045
046 @Override
047 public FileResponse channelRead0(
048 ChannelHandlerContext channelHandlerContext,
049 FileResponse fileResponse, ByteBuf byteBuf)
050 throws IOException {
051
052 if (fileResponse.isFileNotFound() || fileResponse.isFileNotModified()) {
053 if (!_asyncBroker.takeWithResult(
054 fileResponse.getPath(), fileResponse)) {
055
056 _log.error(
057 "Unable to place result " + fileResponse +
058 " because no future exists with ID " +
059 fileResponse.getPath());
060 }
061
062 return null;
063 }
064
065 ChannelPipeline channelPipeline = channelHandlerContext.pipeline();
066
067 channelPipeline.addFirst(
068 new FileUploadChannelHandler(
069 _asyncBroker, fileResponse, _eventExecutorGroup.next()));
070
071 channelPipeline.fireChannelRead(byteBuf.retain());
072
073 return null;
074 }
075
076 private static final Log _log = LogFactoryUtil.getLog(
077 FileResponseChannelHandler.class);
078
079 private final AsyncBroker<Path, FileResponse> _asyncBroker;
080 private final EventExecutorGroup _eventExecutorGroup;
081
082 }