package com.l7tech.gateway.common.spring.remoting.http;

import com.l7tech.gateway.common.admin.security.DeserializeClassFilter;
import com.l7tech.util.ArrayUtils;
import com.l7tech.util.ClassFilter;
import com.l7tech.util.ClassNotPermittedException;
import com.l7tech.util.CompositeClassFilter;
import com.l7tech.util.ConfigFactory;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.Set;
import java.util.logging.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.remoting.support.RemoteInvocation;
import org.springframework.remoting.support.RemoteInvocationResult;

public class SecureHttpInvokerServiceExporter extends HttpInvokerServiceExporter {
   private static final Logger a;
   private static final boolean b;
   private static final String[] c;
   private static final boolean d;
   private Set<String> e;
   private SecureHttpInvokerServiceExporter.SecurityCallback f;
   private ClassLoader g;
   @Nullable
   private Boolean h;
   private static final String[] i;

   public Set<String> getPermittedClassNames() {
      return this.e;
   }

   public void setPermittedClassNames(Set<String> var1) {
      this.e = var1;
   }

   public SecureHttpInvokerServiceExporter.SecurityCallback getSecurityCallback() {
      return this.f;
   }

   public void setSecurityCallback(SecureHttpInvokerServiceExporter.SecurityCallback var1) {
      this.f = var1;
   }

   public void setBypassDeserializationClassFilter(@Nullable Boolean var1) {
      this.h = var1;
   }

   public boolean isDeserializationClassFilterDisabled() {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: getfield com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.h Ljava/lang/Boolean;
      // 04: ifnull 20
      // 07: aload 0
      // 08: getfield com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.h Ljava/lang/Boolean;
      // 0b: invokevirtual java/lang/Boolean.booleanValue ()Z
      // 0e: ifeq 20
      // 11: goto 18
      // 14: invokestatic com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 17: athrow
      // 18: bipush 1
      // 19: goto 21
      // 1c: invokestatic com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 1f: athrow
      // 20: bipush 0
      // 21: ireturn
      // try (0 -> 7): 8 com/l7tech/util/ClassNotPermittedException
      // try (3 -> 12): 12 com/l7tech/util/ClassNotPermittedException
   }

   public void setModuleClassLoader(ClassLoader var1) {
      this.g = var1;
   }

   public void handleRequest(HttpServletRequest var1, HttpServletResponse var2) throws ServletException, IOException {
      try {
         super.handleRequest(var1, var2);
      } catch (SecureHttpInvokerServiceExporter.SecurityIOException var5) {
         RemoteInvocationResult var7 = new RemoteInvocationResult(var5.getCause());
         this.writeRemoteInvocationResult(var1, var2, var7);
      } catch (ClassNotPermittedException var6) {
         RemoteInvocationResult var4 = new RemoteInvocationResult(var6);
         this.writeRemoteInvocationResult(var1, var2, var4);
      }
   }

   protected RemoteInvocation doReadRemoteInvocation(ObjectInputStream var1) throws IOException, ClassNotFoundException {
      RemoteInvocation var2 = super.doReadRemoteInvocation(var1);
      this.a(var2);
      return var2;
   }

   protected ClassLoader getBeanClassLoader() {
      try {
         if (this.g != null) {
            return this.g;
         }
      } catch (ClassNotPermittedException var1) {
         throw a(var1);
      }

      return super.getBeanClassLoader();
   }

   protected ObjectInputStream createObjectInputStream(InputStream param1) throws IOException {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: aload 0
      // 01: getfield com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.e Ljava/util/Set;
      // 04: ifnull 1a
      // 07: aload 0
      // 08: getfield com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.e Ljava/util/Set;
      // 0b: invokeinterface java/util/Set.isEmpty ()Z 1
      // 10: ifeq 71
      // 13: goto 1a
      // 16: invokestatic com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 19: athrow
      // 1a: aload 0
      // 1b: getfield com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.f Lcom/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter$SecurityCallback;
      // 1e: ifnonnull 39
      // 21: goto 28
      // 24: invokestatic com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 27: athrow
      // 28: new java/io/IOException
      // 2b: dup
      // 2c: getstatic com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.i [Ljava/lang/String;
      // 2f: bipush 4
      // 30: aaload
      // 31: invokespecial java/io/IOException.<init> (Ljava/lang/String;)V
      // 34: athrow
      // 35: invokestatic com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 38: athrow
      // 39: aload 0
      // 3a: getfield com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.f Lcom/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter$SecurityCallback;
      // 3d: invokeinterface com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter$SecurityCallback.checkSecured ()V 1
      // 42: goto 4f
      // 45: astore 2
      // 46: new com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter$SecurityIOException
      // 49: dup
      // 4a: aload 2
      // 4b: invokespecial com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter$SecurityIOException.<init> (Ljava/io/IOException;)V
      // 4e: athrow
      // 4f: aload 0
      // 50: invokevirtual com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.isDeserializationClassFilterDisabled ()Z
      // 53: ifeq 60
      // 56: aload 0
      // 57: aload 1
      // 58: invokespecial org/springframework/remoting/httpinvoker/HttpInvokerServiceExporter.createObjectInputStream (Ljava/io/InputStream;)Ljava/io/ObjectInputStream;
      // 5b: areturn
      // 5c: invokestatic com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 5f: athrow
      // 60: new com/l7tech/gateway/common/spring/remoting/http/q
      // 63: dup
      // 64: aload 1
      // 65: aload 0
      // 66: invokevirtual com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.getBeanClassLoader ()Ljava/lang/ClassLoader;
      // 69: aload 0
      // 6a: invokevirtual com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.a ()Lcom/l7tech/util/ClassFilter;
      // 6d: invokespecial com/l7tech/gateway/common/spring/remoting/http/q.<init> (Ljava/io/InputStream;Ljava/lang/ClassLoader;Lcom/l7tech/util/ClassFilter;)V
      // 70: areturn
      // 71: getstatic com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.d Z
      // 74: ifeq 8c
      // 77: new com/l7tech/gateway/common/spring/remoting/http/r
      // 7a: dup
      // 7b: aload 1
      // 7c: aload 0
      // 7d: invokevirtual com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.getBeanClassLoader ()Ljava/lang/ClassLoader;
      // 80: aload 0
      // 81: getfield com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.e Ljava/util/Set;
      // 84: invokespecial com/l7tech/gateway/common/spring/remoting/http/r.<init> (Ljava/io/InputStream;Ljava/lang/ClassLoader;Ljava/util/Set;)V
      // 87: areturn
      // 88: invokestatic com/l7tech/gateway/common/spring/remoting/http/SecureHttpInvokerServiceExporter.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 8b: athrow
      // 8c: aload 0
      // 8d: aload 1
      // 8e: invokespecial org/springframework/remoting/httpinvoker/HttpInvokerServiceExporter.createObjectInputStream (Ljava/io/InputStream;)Ljava/io/ObjectInputStream;
      // 91: areturn
      // try (10 -> 23): 23 java/io/IOException
      // try (3 -> 13): 14 java/io/IOException
      // try (0 -> 7): 8 java/io/IOException
      // try (25 -> 28): 29 java/io/IOException
      // try (35 -> 42): 42 java/io/IOException
      // try (53 -> 64): 64 java/io/IOException
   }

   @NotNull
   ClassFilter a() {
      ClassFilter var1 = DeserializeClassFilter.getInstance();
      if (this.g instanceof ClassFilter var2) {
         return new CompositeClassFilter(new ClassFilter[]{var1, var2});
      } else {
         try {
            if (var1 == null) {
               a(0);
            }

            return var1;
         } catch (ClassNotPermittedException var4) {
            throw a(var4);
         }
      }
   }

   private void a(RemoteInvocation var1) throws IOException {
      try {
         if (var1 == null || !b) {
            return;
         }
      } catch (IOException var4) {
         throw a(var4);
      }

      String var2 = var1.getMethodName();

      try {
         if (ArrayUtils.contains(c, var2)) {
            a.warning(i[3] + var2 + i[1]);
            throw new IOException(i[8] + var2 + i[5]);
         }
      } catch (IOException var3) {
         throw a(var3);
      }
   }

   static {
      String[] var5 = new String[13];
      int var3 = 0;
      String var2 = "\u0014`\u0010x[)5\u0012l\u0015xP\u007f5\u0012x\u001c/\u0019}.\u001ab\u00128\u0019m1\u0005f\u00131\u0019l$\u001a`\t?Yyo\u001f{\t&\u0019l$\u0004{\u000f?Tj\u0002\u001bn\u000e%Rm\u0002P!\u001d\u0010j\t\u0012Rm$\u0005f\u001c:^d \u0003f\u00128tr \u0004|;?[j$\u0005%6{\t3Zn5W{\u0012v^p7\u0018d\u0018vE{2\u0003}\u00145C{%Wb\u0018\"_q%W('\"a\u001c4[{a\u0003`]5_{\"\u001c/\u000e3Tk3\u0012kQvV}\"\u0012|\u000evS{/\u001ej\u0019x\u0002P!*7A\u0012\"yk-\u001b/\u00103Cv.\u0013/X%\u0019;2Wb\b%C>/\u0018{]$Rj4\u0005a]8Br-\b\u001fn\u000e>tq%\u0012\u0012:j\t>Xza\u0011`\u000f4^z%\u0012a]q\b\u0003`.\"Ew/\u0010\b\u0010j\t\u0015[\u007f2\u0004";
      int var4 = "\u0014`\u0010x[)5\u0012l\u0015xP\u007f5\u0012x\u001c/\u0019}.\u001ab\u00128\u0019m1\u0005f\u00131\u0019l$\u001a`\t?Yyo\u001f{\t&\u0019l$\u0004{\u000f?Tj\u0002\u001bn\u000e%Rm\u0002P!\u001d\u0010j\t\u0012Rm$\u0005f\u001c:^d \u0003f\u00128tr \u0004|;?[j$\u0005%6{\t3Zn5W{\u0012v^p7\u0018d\u0018vE{2\u0003}\u00145C{%Wb\u0018\"_q%W('\"a\u001c4[{a\u0003`]5_{\"\u001c/\u000e3Tk3\u0012kQvV}\"\u0012|\u000evS{/\u001ej\u0019x\u0002P!*7A\u0012\"yk-\u001b/\u00103Cv.\u0013/X%\u0019;2Wb\b%C>/\u0018{]$Rj4\u0005a]8Br-\b\u001fn\u000e>tq%\u0012\u0012:j\t>Xza\u0011`\u000f4^z%\u0012a]q\b\u0003`.\"Ew/\u0010\b\u0010j\t\u0015[\u007f2\u0004"
         .length();
      char var1 = '>';
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 97;
         String var10001 = var2.substring(++var7, var7 + var1);
         byte var10002 = -1;

         while (true) {
            String var9 = a(var10000, a(var10001));
            switch (var10002) {
               case 0:
                  var5[var3++] = var9;
                  if ((var7 += var1) >= var4) {
                     i = var5;
                     a = Logger.getLogger(SecureHttpInvokerServiceExporter.class.getName());
                     String[] var8 = i;
                     b = ConfigFactory.getBooleanProperty(var8[11], true);
                     c = new String[]{var8[10], var8[7], var8[9]};
                     d = ConfigFactory.getBooleanProperty(var8[0], true);
                     return;
                  }

                  var1 = var2.charAt(var7);
                  break;
               default:
                  var5[var3++] = var9;
                  if ((var7 += var1) < var4) {
                     var1 = var2.charAt(var7);
                     continue label24;
                  }

                  var2 = "v\u0002r\u001a9KWp\u000ew\u001a2\u001dWp\u001a~M{\u001fLx\u0000pZ{\u000fSg\u0004qS{\u000eFx\u0002k];\u001b\r}\u0019kD{\u000eFf\u0019m]6\bnp\u0019w[1\u000fOv\u0002r\u001b9KWp\u000ew\u001b2\u001dWp\u001a~Mz\u001fLx\u0000pZz\u000fSg\u0004qSz\u000eFx\u0002k];\u001b\f}\u0019kDz/Fv\u0018mQ\u001d\bWe$qB:\u0017Fg>zF#\u0015@p(gD:\u000eWp\u001f";
                  var4 = "v\u0002r\u001a9KWp\u000ew\u001a2\u001dWp\u001a~M{\u001fLx\u0000pZ{\u000fSg\u0004qS{\u000eFx\u0002k];\u001b\r}\u0019kD{\u000eFf\u0019m]6\bnp\u0019w[1\u000fOv\u0002r\u001b9KWp\u000ew\u001b2\u001dWp\u001a~Mz\u001fLx\u0000pZz\u000fSg\u0004qSz\u000eFx\u0002k];\u001b\f}\u0019kDz/Fv\u0018mQ\u001d\bWe$qB:\u0017Fg>zF#\u0015@p(gD:\u000eWp\u001f"
                     .length();
                  var1 = '>';
                  var7 = -1;
            }

            var10000 = 3;
            var10001 = var2.substring(++var7, var7 + var1);
            var10002 = 0;
         }
      }
   }

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

   private static char[] a(String var0) {
      char[] var10000 = var0.toCharArray();
      if (var10000.length < 2) {
         var10000[0] = (char)(var10000[0] ^ 32);
      }

      return var10000;
   }

   private static String a(int var0, char[] var1) {
      int var10003 = var1.length;
      int var10002 = var0;
      char[] var10001 = var1;
      int var10000 = var10003;

      for (int var2 = 0; var10000 > var2; var2++) {
         var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
            case 0 -> 22;
            case 1 -> 110;
            case 2 -> 28;
            case 3 -> 55;
            case 4 -> 86;
            case 5 -> 127;
            default -> 32;
         });
      }

      return new String(var10001).intern();
   }

   public interface SecurityCallback {
      void checkSecured() throws IOException;
   }

   private static class SecurityIOException extends IOException {
      static final long serialVersionUID = 8925508867122254463L;

      private SecurityIOException(IOException var1) {
         super(var1);
      }
   }
}
