001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
033     * @author Shuyang Zhou
034     */
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    }