pgyori commented on a change in pull request #4481: URL: https://github.com/apache/nifi/pull/4481#discussion_r484903148
########## File path: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ftp/commands/FtpCommandSTOR.java ########## @@ -0,0 +1,233 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.processors.standard.ftp.commands; + +import org.apache.ftpserver.command.AbstractCommand; +import org.apache.ftpserver.ftplet.DataConnection; +import org.apache.ftpserver.ftplet.DataConnectionFactory; +import org.apache.ftpserver.ftplet.DefaultFtpReply; +import org.apache.ftpserver.ftplet.FtpException; +import org.apache.ftpserver.ftplet.FtpFile; +import org.apache.ftpserver.ftplet.FtpReply; +import org.apache.ftpserver.ftplet.FtpRequest; +import org.apache.ftpserver.impl.FtpIoSession; +import org.apache.ftpserver.impl.FtpServerContext; +import org.apache.ftpserver.impl.IODataConnectionFactory; +import org.apache.ftpserver.impl.LocalizedDataTransferFtpReply; +import org.apache.ftpserver.impl.LocalizedFtpReply; +import org.apache.ftpserver.impl.ServerFtpStatistics; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.flowfile.attributes.CoreAttributes; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.ProcessSessionFactory; +import org.apache.nifi.processors.standard.ListenFTP; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.SocketException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicReference; + +public class FtpCommandSTOR extends AbstractCommand { + + private static final Logger LOG = LoggerFactory.getLogger(FtpCommandSTOR.class); + private final AtomicReference<ProcessSessionFactory> sessionFactory; + private final CountDownLatch sessionFactorySetSignal; + + public FtpCommandSTOR(AtomicReference<ProcessSessionFactory> sessionFactory, CountDownLatch sessionFactorySetSignal) { + this.sessionFactory = sessionFactory; + this.sessionFactorySetSignal = sessionFactorySetSignal; + } + + /** + * Execute command. + */ + public void execute(final FtpIoSession ftpSession, final FtpServerContext context, final FtpRequest request) { + try { + executeCommand(ftpSession, context, request); + } catch (FtpCommandException ftpCommandException) { + if (ftpCommandException.getSubId() == null) { + ftpSession.write(new DefaultFtpReply(ftpCommandException.getFtpReturnCode(), ftpCommandException.getBasicMessage())); + } else { + ftpSession.write(LocalizedDataTransferFtpReply.translate(ftpSession, request, context, + ftpCommandException.getFtpReturnCode(), + ftpCommandException.getSubId(), + ftpCommandException.getBasicMessage(), + ftpCommandException.getFtpFile())); + } + } finally { + ftpSession.resetState(); + ftpSession.getDataConnection().closeDataConnection(); + } + } + + private void executeCommand(FtpIoSession ftpSession, FtpServerContext context, FtpRequest request) + throws FtpCommandException { + + final String fileName = getArgument(request); + + checkDataConnection(ftpSession); + + final FtpFile ftpFile = getFtpFile(ftpSession, fileName); + + checkWritePermission(ftpFile); + + sendReturnCode150(ftpSession, context, request, ftpFile.getAbsolutePath()); + + final DataConnection dataConnection = openDataConnection(ftpSession, ftpFile); + + transferData(dataConnection, ftpSession, context, request, ftpFile); + } + + private String getArgument(final FtpRequest request) throws FtpCommandException { + final String argument = request.getArgument(); + if (argument == null) { + throw new FtpCommandException(FtpReply.REPLY_501_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS, "STOR", null, null); Review comment: This must be the subID because it is passed on to the ftpserver library which uses it as the key to look up the correct reply message which might be subject to translation. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org