/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.ftp.FtpCommand
 *  com.l7tech.common.log.HybridDiagnosticContext
 *  com.l7tech.common.mime.ContentTypeHeader
 *  com.l7tech.gateway.common.transport.SsgConnector
 *  com.l7tech.message.FtpRequestKnob
 *  com.l7tech.message.HasServiceId
 *  com.l7tech.message.HasServiceIdImpl
 *  com.l7tech.message.Message
 *  com.l7tech.message.MessageKnob
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.util.CausedIOException
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.ResourceUtils
 *  org.apache.ftpserver.ftplet.DataConnection
 *  org.apache.ftpserver.ftplet.DefaultFtpReply
 *  org.apache.ftpserver.ftplet.FtpException
 *  org.apache.ftpserver.ftplet.FtpRequest
 *  org.apache.ftpserver.ftplet.FtpSession
 *  org.apache.ftpserver.ftplet.User
 *  org.apache.ftpserver.impl.FtpIoSession
 *  org.apache.ftpserver.impl.FtpServerContext
 *  org.apache.ftpserver.impl.IODataConnectionFactory
 *  org.apache.ftpserver.impl.LocalizedFtpReply
 *  org.apache.ftpserver.impl.ServerDataConnectionFactory
 */
package com.l7tech.server.transport.ftp;

import com.l7tech.common.ftp.FtpCommand;
import com.l7tech.common.log.HybridDiagnosticContext;
import com.l7tech.common.mime.ContentTypeHeader;
import com.l7tech.gateway.common.transport.SsgConnector;
import com.l7tech.message.FtpRequestKnob;
import com.l7tech.message.HasServiceId;
import com.l7tech.message.HasServiceIdImpl;
import com.l7tech.message.Message;
import com.l7tech.message.MessageKnob;
import com.l7tech.objectmodel.Goid;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.server.MessageProcessor;
import com.l7tech.server.StashManagerFactory;
import com.l7tech.server.event.FaultProcessed;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.message.PolicyEnforcementContextFactory;
import com.l7tech.server.policy.PolicyVersionException;
import com.l7tech.server.transport.ftp.FtpRequestProcessor;
import com.l7tech.server.transport.ftp.a;
import com.l7tech.server.transport.ftp.b;
import com.l7tech.server.util.EventChannel;
import com.l7tech.server.util.SoapFaultManager;
import com.l7tech.util.CausedIOException;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.ResourceUtils;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.ftpserver.ftplet.DataConnection;
import org.apache.ftpserver.ftplet.DefaultFtpReply;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.User;
import org.apache.ftpserver.impl.FtpIoSession;
import org.apache.ftpserver.impl.FtpServerContext;
import org.apache.ftpserver.impl.IODataConnectionFactory;
import org.apache.ftpserver.impl.LocalizedFtpReply;
import org.apache.ftpserver.impl.ServerDataConnectionFactory;

public abstract class AbstractFtpRequestProcessor
implements FtpRequestProcessor {
    private final Logger a;
    private final MessageProcessor b;
    private final SoapFaultManager c;
    private final StashManagerFactory d;
    private final EventChannel e;
    private final ExecutorService f;
    private final ContentTypeHeader g;
    private final Goid h;
    private final SsgConnector i;
    private final long j;
    private static final String[] l;

    public AbstractFtpRequestProcessor(MessageProcessor messageProcessor, SoapFaultManager soapFaultManager, StashManagerFactory stashManagerFactory, EventChannel eventChannel, ExecutorService executorService, ContentTypeHeader contentTypeHeader, Goid goid, SsgConnector ssgConnector, long l2, Logger logger) {
        this.b = messageProcessor;
        this.c = soapFaultManager;
        this.d = stashManagerFactory;
        this.e = eventChannel;
        this.f = executorService;
        this.g = contentTypeHeader;
        this.h = goid;
        this.i = ssgConnector;
        this.j = l2;
        this.a = logger;
    }

    protected void addDiagnosticContextInfo(FtpIoSession ftpIoSession) {
        String string = ((InetSocketAddress)ftpIoSession.getRemoteAddress()).getAddress().getHostAddress();
        String[] stringArray = l;
        HybridDiagnosticContext.put((String)stringArray[7], (String)this.i.getGoid().toString());
        HybridDiagnosticContext.put((String)stringArray[19], (String)this.i.getName());
        HybridDiagnosticContext.put((String)stringArray[1], (String)string);
    }

    protected void removeDiagnosticContextInfo() {
        String[] stringArray = l;
        HybridDiagnosticContext.remove((String)stringArray[6]);
        HybridDiagnosticContext.remove((String)stringArray[2]);
        HybridDiagnosticContext.remove((String)stringArray[16]);
    }

    protected boolean validateConnectionFactoryState(FtpIoSession ftpIoSession) {
        InetAddress inetAddress;
        boolean bl2 = true;
        ServerDataConnectionFactory serverDataConnectionFactory = ftpIoSession.getDataConnection();
        if (serverDataConnectionFactory instanceof IODataConnectionFactory && (inetAddress = ((IODataConnectionFactory)serverDataConnectionFactory).getInetAddress()) == null) {
            bl2 = false;
        }
        return bl2;
    }

    protected void resetStateAndCloseConnection(FtpIoSession ftpIoSession) {
        ftpIoSession.resetState();
        ftpIoSession.getDataConnection().closeDataConnection();
    }

    protected void handleCreateRequestMessageFailure(FtpIoSession ftpIoSession, FtpCommand ftpCommand, IOException iOException) {
        this.a.log(Level.WARNING, l[8] + ExceptionUtils.getMessage((Throwable)iOException), ExceptionUtils.getDebugException((Throwable)iOException));
        this.reply550ProcessingError(ftpIoSession, ftpCommand);
    }

    protected void handleMessageProcessingFailure(FtpIoSession ftpIoSession, FtpCommand ftpCommand, AssertionStatus assertionStatus) {
        String[] stringArray = l;
        this.a.log(Level.WARNING, stringArray[10] + String.valueOf(ftpCommand) + stringArray[5] + assertionStatus.getMessage());
        this.reply550ProcessingError(ftpIoSession, ftpCommand);
    }

    protected void handleOpenConnectionFailure(FtpIoSession ftpIoSession, FtpServerContext ftpServerContext, FtpRequest ftpRequest, Exception exception) {
        this.a.log(Level.WARNING, l[18] + ExceptionUtils.getMessage((Throwable)exception), ExceptionUtils.getDebugException((Throwable)exception));
        ftpIoSession.write((Object)LocalizedFtpReply.translate((FtpIoSession)ftpIoSession, (FtpRequest)ftpRequest, (FtpServerContext)ftpServerContext, (int)425, (String)ftpRequest.getCommand(), (String)ftpRequest.getArgument()));
    }

    protected void handleDataTransferFailure(FtpIoSession ftpIoSession, FtpServerContext ftpServerContext, FtpRequest ftpRequest, Exception exception) {
        this.a.log(Level.WARNING, l[11] + exception.getMessage(), ExceptionUtils.getDebugException((Throwable)exception));
        ftpIoSession.write((Object)LocalizedFtpReply.translate((FtpIoSession)ftpIoSession, (FtpRequest)ftpRequest, (FtpServerContext)ftpServerContext, (int)426, (String)ftpRequest.getCommand(), (String)ftpRequest.getArgument()));
    }

    protected void reply150FileStatusOk(FtpIoSession ftpIoSession, FtpServerContext ftpServerContext, FtpRequest ftpRequest) {
        ftpIoSession.write((Object)LocalizedFtpReply.translate((FtpIoSession)ftpIoSession, (FtpRequest)ftpRequest, (FtpServerContext)ftpServerContext, (int)150, (String)ftpRequest.getCommand(), (String)ftpRequest.getArgument()));
    }

    protected void reply226ClosingConnection(FtpIoSession ftpIoSession, FtpServerContext ftpServerContext, FtpRequest ftpRequest) {
        ftpIoSession.write((Object)LocalizedFtpReply.translate((FtpIoSession)ftpIoSession, (FtpRequest)ftpRequest, (FtpServerContext)ftpServerContext, (int)226, (String)ftpRequest.getCommand(), (String)ftpRequest.getArgument()));
    }

    protected void reply501SyntaxError(FtpIoSession ftpIoSession, FtpServerContext ftpServerContext, FtpRequest ftpRequest) {
        ftpIoSession.write((Object)LocalizedFtpReply.translate((FtpIoSession)ftpIoSession, (FtpRequest)ftpRequest, (FtpServerContext)ftpServerContext, (int)501, (String)ftpRequest.getCommand(), null));
    }

    protected void reply503BadSequence(FtpIoSession ftpIoSession) {
        ftpIoSession.write((Object)new DefaultFtpReply(503, l[0]));
    }

    protected void reply550ProcessingError(FtpIoSession ftpIoSession, FtpCommand ftpCommand) {
        String[] stringArray = l;
        ftpIoSession.write((Object)new DefaultFtpReply(550, stringArray[15] + String.valueOf(ftpCommand) + stringArray[17]));
    }

    protected AssertionStatus submitRequestMessage(PolicyEnforcementContext policyEnforcementContext) {
        String string = null;
        AssertionStatus assertionStatus = AssertionStatus.UNDEFINED;
        try {
            assertionStatus = this.b.processMessage(policyEnforcementContext);
            try {
                if (this.a.isLoggable(Level.FINER)) {
                    this.a.log(Level.FINER, l[3], assertionStatus);
                }
            }
            catch (PolicyVersionException policyVersionException) {
                throw AbstractFtpRequestProcessor.a(policyVersionException);
            }
            if (assertionStatus != AssertionStatus.NONE) {
                string = this.c.constructReturningFault(policyEnforcementContext.getFaultlevel(), policyEnforcementContext).getContent();
            }
        }
        catch (PolicyVersionException policyVersionException) {
            String[] stringArray = l;
            this.a.log(Level.INFO, stringArray[9]);
            string = this.c.constructExceptionFault(policyVersionException, policyEnforcementContext.getFaultlevel(), policyEnforcementContext).getContent();
        }
        catch (Throwable throwable) {
            this.a.log(Level.WARNING, l[4] + ExceptionUtils.getMessage((Throwable)throwable), ExceptionUtils.getDebugException((Throwable)throwable));
            string = this.c.constructExceptionFault(throwable, policyEnforcementContext.getFaultlevel(), policyEnforcementContext).getContent();
        }
        try {
            if (string != null) {
                this.e.publishEvent(new FaultProcessed(policyEnforcementContext, string, this.b));
            }
        }
        catch (PolicyVersionException policyVersionException) {
            throw AbstractFtpRequestProcessor.a(policyVersionException);
        }
        return assertionStatus;
    }

    protected Message createRequestMessage(FtpIoSession ftpIoSession, FtpRequest ftpRequest, FtpCommand ftpCommand, InputStream inputStream) throws FtpException, IOException {
        boolean bl2;
        FtpCommand ftpCommand2;
        FtpRequest ftpRequest2;
        FtpIoSession ftpIoSession2;
        AbstractFtpRequestProcessor abstractFtpRequestProcessor;
        try {
            abstractFtpRequestProcessor = this;
            ftpIoSession2 = ftpIoSession;
            ftpRequest2 = ftpRequest;
            ftpCommand2 = ftpCommand;
            bl2 = FtpCommand.STOU == ftpCommand;
        }
        catch (FtpException ftpException) {
            throw AbstractFtpRequestProcessor.a((Exception)((Object)ftpException));
        }
        return abstractFtpRequestProcessor.createRequestMessage(ftpIoSession2, ftpRequest2, ftpCommand2, bl2, inputStream);
    }

    protected Message createRequestMessage(FtpIoSession ftpIoSession, FtpRequest ftpRequest, FtpCommand ftpCommand, boolean bl2, InputStream inputStream) throws FtpException, IOException {
        ContentTypeHeader contentTypeHeader;
        long l2;
        User user = ftpIoSession.getUser();
        String string = ftpRequest.getArgument();
        String string2 = ftpIoSession.getFileSystemView().getWorkingDirectory().getAbsolutePath();
        boolean bl3 = this.a(ftpIoSession);
        try {
            l2 = this.j == -1L ? Message.getMaxBytes() : this.j;
        }
        catch (FtpException ftpException) {
            throw AbstractFtpRequestProcessor.a((Exception)((Object)ftpException));
        }
        long l3 = l2;
        try {
            contentTypeHeader = this.g != null ? this.g : this.getDefaultMessageContentType();
        }
        catch (FtpException ftpException) {
            throw AbstractFtpRequestProcessor.a((Exception)((Object)ftpException));
        }
        ContentTypeHeader contentTypeHeader2 = contentTypeHeader;
        Message message = new Message();
        message.initialize(this.d.createStashManager(), contentTypeHeader2, inputStream, l3);
        InetSocketAddress inetSocketAddress = (InetSocketAddress)ftpIoSession.getLocalAddress();
        try {
            message.attachFtpRequestKnob(this.buildFtpKnob(inetSocketAddress.getAddress(), inetSocketAddress.getPort(), ((InetSocketAddress)ftpIoSession.getRemoteAddress()).getAddress(), ftpCommand, string, string2, bl3, bl2, user));
            if (null != this.h) {
                message.attachKnob(HasServiceId.class, (MessageKnob)new HasServiceIdImpl(this.h));
            }
        }
        catch (FtpException ftpException) {
            throw AbstractFtpRequestProcessor.a((Exception)((Object)ftpException));
        }
        return message;
    }

    protected abstract ContentTypeHeader getDefaultMessageContentType();

    protected PolicyEnforcementContext createPolicyEnforcementContext(Message message) {
        return PolicyEnforcementContextFactory.createPolicyEnforcementContext(message, new Message(), true);
    }

    private boolean a(FtpIoSession ftpIoSession) {
        boolean bl2 = ftpIoSession.getDataConnection().isSecure();
        boolean bl3 = ftpIoSession.getListener().isImplicitSsl();
        if (this.a.isLoggable(Level.FINE)) {
            this.a.log(Level.FINE, l[13], new Object[]{bl3, bl2});
        }
        return bl2 && bl3;
    }

    protected FtpRequestKnob buildFtpKnob(InetAddress inetAddress, int n2, InetAddress inetAddress2, FtpCommand ftpCommand, String string, String string2, boolean bl2, boolean bl3, User user) {
        return new a(this, n2, inetAddress2, inetAddress, ftpCommand, string, string2, bl2, bl3, user);
    }

    protected InputStream transferDataFromClient(FtpSession ftpSession, DataConnection dataConnection, String string) throws IOException {
        PipedInputStream pipedInputStream = new PipedInputStream();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        this.f.execute(new b(this, string, pipedInputStream, countDownLatch, dataConnection, ftpSession));
        try {
            countDownLatch.await();
        }
        catch (InterruptedException interruptedException) {
            Thread.currentThread().interrupt();
            throw new CausedIOException(l[14], (Throwable)interruptedException);
        }
        return pipedInputStream;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void transferDataToClient(FtpSession ftpSession, DataConnection dataConnection, InputStream inputStream) throws IOException {
        try {
            dataConnection.transferToClient(ftpSession, inputStream);
        }
        finally {
            ResourceUtils.closeQuietly((Closeable)inputStream);
        }
    }

    @Override
    public void dispose() {
        if (this.f != null) {
            this.f.shutdown();
            try {
                this.f.awaitTermination(5000L, TimeUnit.MILLISECONDS);
            }
            catch (InterruptedException interruptedException) {
                this.a.log(Level.FINE, l[12] + interruptedException.getMessage());
            }
        }
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[20];
                var3_1 = 0;
                var2_2 = "!XM\u000e}4\bQG^\t\u000b{\u0017\u0004dkz?>Z\u0018dl/8?Z\u0017~m))\t\u0012{v?3/W\u0018g\u000b\u001d~l.85W\u0001xm.\"!xs3>\"Z\u0003rl/1/\u001f\u00157v4}(\u000e\u0010cj)}|]\n'b}zu(4o|?-/\u0013\u001ey?-52\u0016\u00147o(28\u001f\u0002dv4:{<%G?78(\t\u0010pz`}\u001cQzz).:\u001d\u00147o(28\u001f\u0002dv4:{\u001c\u0010~s?9aZ\u000e\u001d~l.85W\u0001xm.p2\u001e\u000e\u001d~l.85W\u0001xm.p2\u001e 4em5/{\u0019\u0003r~.45\u001dQez+(>\t\u00057r?.(\u001b\u0016r%z2#rn/8(\u000eQez<8)\b\u0014s?.2{\u001b\u001f7p/)?\u001b\u0005r{z+>\b\u0002~p4}4\u001cQgp648\u0003_\u00047COz\u001c4em5/{\u001e\u0004ev4:{\u001e\u0010c~z))\u001b\u001fdy?/aZ\"%\u007fm?<?Z\u0001xp6}(\u0012\u0004c{5*5Z\u0018yk?/)\u000f\u0001cz>g{=\"r|//2\u000e\b7s?+>\u0016\u0002;?925\u000e\u0003xsz.>\u0019\u0004ezzz|\u0001Aj8}q{\u001e\u0010c~z.>\u0019\u0004ezzz|\u0001@j8}s\u001d8yk?/)\u000f\u0001cz>},\u001b\u0018cv4:{\u001c\u001ee?></\u001b_\u00184em5/{\n\u0003x|?.(\u0013\u001fp?\u001c\t\u000bR\">?\t\u0012{v?3/W\u0018g\tQez+(>\t\u00059";
                var4_3 = "!XM\u000e}4\bQG^\t\u000b{\u0017\u0004dkz?>Z\u0018dl/8?Z\u0017~m))\t\u0012{v?3/W\u0018g\u000b\u001d~l.85W\u0001xm.\"!xs3>\"Z\u0003rl/1/\u001f\u00157v4}(\u000e\u0010cj)}|]\n'b}zu(4o|?-/\u0013\u001ey?-52\u0016\u00147o(28\u001f\u0002dv4:{<%G?78(\t\u0010pz`}\u001cQzz).:\u001d\u00147o(28\u001f\u0002dv4:{\u001c\u0010~s?9aZ\u000e\u001d~l.85W\u0001xm.p2\u001e\u000e\u001d~l.85W\u0001xm.p2\u001e 4em5/{\u0019\u0003r~.45\u001dQez+(>\t\u00057r?.(\u001b\u0016r%z2#rn/8(\u000eQez<8)\b\u0014s?.2{\u001b\u001f7p/)?\u001b\u0005r{z+>\b\u0002~p4}4\u001cQgp648\u0003_\u00047COz\u001c4em5/{\u001e\u0004ev4:{\u001e\u0010c~z))\u001b\u001fdy?/aZ\"%\u007fm?<?Z\u0001xp6}(\u0012\u0004c{5*5Z\u0018yk?/)\u000f\u0001cz>g{=\"r|//2\u000e\b7s?+>\u0016\u0002;?925\u000e\u0003xsz.>\u0019\u0004ezzz|\u0001Aj8}q{\u001e\u0010c~z.>\u0019\u0004ezzz|\u0001@j8}s\u001d8yk?/)\u000f\u0001cz>},\u001b\u0018cv4:{\u001c\u001ee?></\u001b_\u00184em5/{\n\u0003x|?.(\u0013\u001fp?\u001c\t\u000bR\">?\t\u0012{v?3/W\u0018g\tQez+(>\t\u00059".length();
                var1_4 = 33;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 54;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = AbstractFtpRequestProcessor.a(v0, AbstractFtpRequestProcessor.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = ")hh(&!D\u001bf!+3 \nOh! \"1\u0005Ojn*- \u0007\u001b`n*ye\u000b\u0003`r0&+I\u001ffs0";
                    var4_3 = ")hh(&!D\u001bf!+3 \nOh! \"1\u0005Ojn*- \u0007\u001b`n*ye\u000b\u0003`r0&+I\u001ffs0".length();
                    var1_4 = 34;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 40;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = AbstractFtpRequestProcessor.a(v0, AbstractFtpRequestProcessor.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        AbstractFtpRequestProcessor.l = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x4C);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 71;
                    case 1 -> 33;
                    case 2 -> 41;
                    case 3 -> 108;
                    case 4 -> 107;
                    case 5 -> 109;
                    default -> 76;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

