浏览代码

Remove most of apache commons hex decoder usage

Peter Cai 3 年之前
父节点
当前提交
b11c3fc129

+ 3 - 4
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/DisableProfileWorker.java

@@ -7,8 +7,7 @@ import com.truphone.lpad.progress.Progress;
 import com.truphone.lpad.progress.ProgressStep;
 import com.truphone.rsp.dto.asn1.rspdefinitions.DisableProfileResponse;
 import com.truphone.util.LogStub;
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
+import com.truphone.util.TextUtil;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -41,7 +40,7 @@ class DisableProfileWorker {
         progress.stepExecuted(ProgressStep.DISABLE_PROFILE_CONVERTING_RESPONSE, "Converting response");
 
         try {
-            InputStream is = new ByteArrayInputStream(Hex.decodeHex(eResponse.toCharArray()));
+            InputStream is = new ByteArrayInputStream(TextUtil.decodeHex(eResponse));
             DisableProfileResponse disableProfileResponse = new DisableProfileResponse();
 
             disableProfileResponse.decode(is);
@@ -67,7 +66,7 @@ class DisableProfileWorker {
             LOG.log(Level.SEVERE, LogStub.getInstance().getTag() + " - iccid: " + iccid + " profile failed to be disabled");
 
             throw new RuntimeException("Unable to disable profile: " + iccid + ", response: " + eResponse);
-        } catch (DecoderException e) {
+        } catch (NumberFormatException e) {
             LOG.log(Level.SEVERE, LogStub.getInstance().getTag() + " - " + e.getMessage(), e);
             LOG.log(Level.SEVERE, LogStub.getInstance().getTag() + " - iccid: " + iccid + " profile failed to be disabled. Exception in Decoder:" + e.getMessage());
 

+ 3 - 4
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/EnableProfileWorker.java

@@ -7,8 +7,7 @@ import com.truphone.lpad.progress.Progress;
 import com.truphone.lpad.progress.ProgressStep;
 import com.truphone.rsp.dto.asn1.rspdefinitions.EnableProfileResponse;
 import com.truphone.util.LogStub;
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
+import com.truphone.util.TextUtil;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -42,7 +41,7 @@ class EnableProfileWorker {
 
         try {
             EnableProfileResponse enableProfileResponse = new EnableProfileResponse();
-            InputStream is = new ByteArrayInputStream(Hex.decodeHex(eResponse.toCharArray()));
+            InputStream is = new ByteArrayInputStream(TextUtil.decodeHex(eResponse));
 
             enableProfileResponse.decode(is);
 
@@ -72,7 +71,7 @@ class EnableProfileWorker {
             LOG.severe(LogStub.getInstance().getTag() + " - iccid: " + iccid + " profile failed to be enabled. message: " + e.getMessage());
 
             throw new RuntimeException("Unable to enable profile: " + iccid + ", response: " + eResponse);
-        } catch (DecoderException e) {
+        } catch (NumberFormatException e) {
             LOG.log(Level.SEVERE, LogStub.getInstance().getTag() + " - " + e.getMessage(), e);
             LOG.severe(LogStub.getInstance().getTag() + " - iccid: " + iccid + " profile failed to be enabled. Exception in Decoder:" + e.getMessage());
 

+ 0 - 2
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/GetSMDPAddressWorker.java

@@ -16,8 +16,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.logging.Level;
 import java.util.logging.Logger;
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
 
 /**
  *

+ 5 - 5
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/ListProfilesWorker.kt

@@ -18,13 +18,13 @@ import com.truphone.lpa.ApduChannel
 import com.truphone.lpa.LocalProfileInfo
 import com.truphone.rsp.dto.asn1.rspdefinitions.ProfileInfoListResponse
 import com.truphone.util.LogStub
-import org.apache.commons.codec.DecoderException
-import org.apache.commons.codec.binary.Hex
 import com.truphone.lpa.apdu.ApduUtils
+import com.truphone.util.TextUtil
 import com.truphone.util.TextUtil.iccidBigToLittle
 import java.io.ByteArrayInputStream
 import java.io.IOException
 import java.io.InputStream
+import java.lang.NumberFormatException
 import java.lang.RuntimeException
 import java.util.logging.Level
 import java.util.logging.Logger
@@ -56,7 +56,7 @@ internal class ListProfilesWorker(private val apduChannel: ApduChannel) {
                 )
             }
             profileList
-        } catch (e: DecoderException) {
+        } catch (e: NumberFormatException) {
             LOG.log(Level.SEVERE, LogStub.getInstance().tag + " - " + e.message, e)
             LOG.log(
                 Level.SEVERE,
@@ -69,9 +69,9 @@ internal class ListProfilesWorker(private val apduChannel: ApduChannel) {
         }
     }
 
-    @Throws(DecoderException::class, IOException::class)
+    @Throws(NumberFormatException::class, IOException::class)
     private fun decodeProfiles(profilesInfo: String, profiles: ProfileInfoListResponse) {
-        val `is`: InputStream = ByteArrayInputStream(Hex.decodeHex(profilesInfo.toCharArray()))
+        val `is`: InputStream = ByteArrayInputStream(TextUtil.decodeHex(profilesInfo))
         profiles.decode(`is`)
         if (LogStub.getInstance().isDebugEnabled) {
             LogStub.getInstance().logDebug(LOG, "Profile list object: $profiles")

+ 4 - 4
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/SetNicknameWorker.kt

@@ -17,13 +17,13 @@ package com.truphone.lpa.impl
 import com.truphone.lpa.ApduChannel
 import com.truphone.util.LogStub
 import com.truphone.lpa.apdu.ApduUtils
-import org.apache.commons.codec.binary.Hex
 import com.truphone.rsp.dto.asn1.rspdefinitions.SetNicknameResponse
-import org.apache.commons.codec.DecoderException
+import com.truphone.util.TextUtil
 import com.truphone.util.Util
 import java.io.ByteArrayInputStream
 import java.io.IOException
 import java.io.InputStream
+import java.lang.NumberFormatException
 import java.lang.RuntimeException
 import java.util.logging.Level
 import java.util.logging.Logger
@@ -51,7 +51,7 @@ class SetNicknameWorker internal constructor(
         )
         val eResponse = apduChannel.transmitAPDU(apdu)
         return try {
-            val `is`: InputStream = ByteArrayInputStream(Hex.decodeHex(eResponse.toCharArray()))
+            val `is`: InputStream = ByteArrayInputStream(TextUtil.decodeHex(eResponse))
             val response = SetNicknameResponse()
             response.decode(`is`)
             if ("0" == response.setNicknameResult.toString()) {
@@ -75,7 +75,7 @@ class SetNicknameWorker internal constructor(
                 LogStub.getInstance().tag + " - iccid: " + iccid + " profile failed to be renamed"
             )
             throw RuntimeException("Unable to rename profile: $iccid, response: $eResponse")
-        } catch (e: DecoderException) {
+        } catch (e: NumberFormatException) {
             LOG.log(Level.SEVERE, LogStub.getInstance().tag + " - " + e.message, e)
             LOG.log(
                 Level.SEVERE,

+ 0 - 2
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/SmdsRetrieveEventsWorker.java

@@ -16,8 +16,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.logging.Level;
 import java.util.logging.Logger;
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
 
 /**
  *

+ 4 - 5
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/ConnectingPhaseWorker.java

@@ -7,8 +7,7 @@ import com.truphone.lpa.progress.DownloadProgressPhase;
 import com.truphone.lpad.progress.ProgressStep;
 import com.truphone.rsp.dto.asn1.rspdefinitions.EuiccConfiguredAddressesResponse;
 import com.truphone.util.LogStub;
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
+import com.truphone.util.TextUtil;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -59,7 +58,7 @@ public class ConnectingPhaseWorker {
             }
 
             return euiccConfiguredAddress;
-        } catch (DecoderException e) {
+        } catch (NumberFormatException e) {
             LOG.log(Level.SEVERE, "KOL.007" + e.getMessage(), e);
             LOG.severe(LogStub.getInstance().getTag() + " - matchingId: " + matchingId +
                     " Unable to retrieve eUICC configured address. Exception in Decoder:" + e.getMessage());
@@ -74,12 +73,12 @@ public class ConnectingPhaseWorker {
         }
     }
 
-    private EuiccConfiguredAddressesResponse decodeEuiccConfiguredAddressesResponse(String euiCCConfiguredAddressAPDUResponse) throws DecoderException, IOException {
+    private EuiccConfiguredAddressesResponse decodeEuiccConfiguredAddressesResponse(String euiCCConfiguredAddressAPDUResponse) throws NumberFormatException, IOException {
         InputStream is = null;
 
         try {
             EuiccConfiguredAddressesResponse euiccConfiguredAddressesResponse = new EuiccConfiguredAddressesResponse();
-            is = new ByteArrayInputStream(Hex.decodeHex(euiCCConfiguredAddressAPDUResponse.toCharArray()));
+            is = new ByteArrayInputStream(TextUtil.decodeHex(euiCCConfiguredAddressAPDUResponse));
 
             euiccConfiguredAddressesResponse.decode(is);
 

+ 3 - 4
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/GeneratePhaseWorker.java

@@ -8,8 +8,7 @@ import com.truphone.lpa.progress.DownloadProgressPhase;
 import com.truphone.lpad.progress.ProgressStep;
 import com.truphone.rsp.dto.asn1.rspdefinitions.BoundProfilePackage;
 import com.truphone.util.LogStub;
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
+import com.truphone.util.TextUtil;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -100,12 +99,12 @@ public class GeneratePhaseWorker {
             BoundProfilePackage bppObj = new BoundProfilePackage();
 
             try {
-                bppIs = new ByteArrayInputStream(Hex.decodeHex(bpp.toCharArray()));
+                bppIs = new ByteArrayInputStream(TextUtil.decodeHex(bpp));
 
                 bppObj.decode(bppIs);
 
                 LogStub.getInstance().logDebug(LOG, LogStub.getInstance().getTag() + " - BPP Object is: " + bppObj);
-            } catch (DecoderException e) {
+            } catch (NumberFormatException e) {
                 LOG.log(Level.INFO, LogStub.getInstance().getTag() + " - " + e.getMessage(), e);
             } finally {
                 CloseResources.closeResources(bppIs);

+ 3 - 6
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpa/impl/download/InstallationPhaseWorker.java

@@ -9,9 +9,6 @@ import com.truphone.rsp.dto.asn1.rspdefinitions.ProfileInstallationResultData;
 import com.truphone.util.LogStub;
 import com.truphone.util.TextUtil;
 
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
-
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -168,7 +165,7 @@ public class InstallationPhaseWorker {
                 throw new RuntimeException(errorMessage);
             }
 
-        } catch (DecoderException e) {
+        } catch (NumberFormatException e) {
             LOG.log(Level.SEVERE, LogStub.getInstance().getTag() + " - " + e.getMessage(), e);
             LOG.severe(LogStub.getInstance().getTag() + " -  Unable to retrieve Profile Installation Result. Exception in Decoder:" + e.getMessage());
 
@@ -199,7 +196,7 @@ public class InstallationPhaseWorker {
         return errorMessage;
     }
 
-    private ProfileInstallationResult getProfileInstallationResult(String profileInstallationResultRaw) throws DecoderException, IOException {
+    private ProfileInstallationResult getProfileInstallationResult(String profileInstallationResultRaw) throws NumberFormatException, IOException {
         InputStream is = null;
 
         if (LogStub.getInstance().isDebugEnabled()) {
@@ -209,7 +206,7 @@ public class InstallationPhaseWorker {
         try {
             ProfileInstallationResult profileInstallationResult = new ProfileInstallationResult();
 
-            is = new ByteArrayInputStream(Hex.decodeHex(profileInstallationResultRaw.toCharArray()));
+            is = new ByteArrayInputStream(TextUtil.decodeHex(profileInstallationResultRaw));
 
             profileInstallationResult.decode(is, true);
 

+ 2 - 5
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpad/worker/DeleteProfileWorker.java

@@ -9,9 +9,6 @@ import com.truphone.rsp.dto.asn1.rspdefinitions.DeleteProfileResponse;
 import com.truphone.util.LogStub;
 import com.truphone.util.TextUtil;
 
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
-
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -71,7 +68,7 @@ public class DeleteProfileWorker implements LpadWorker<LpadWorkerExchange<Delete
         DeleteProfileResponse deleteProfileResponse = new DeleteProfileResponse();
 
         try {
-            InputStream is = new ByteArrayInputStream(Hex.decodeHex(eResponse.toCharArray()));
+            InputStream is = new ByteArrayInputStream(TextUtil.decodeHex(eResponse));
             deleteProfileResponse.decode(is);
 
             logDebug(" -  Delete response: " + deleteProfileResponse);
@@ -93,7 +90,7 @@ public class DeleteProfileWorker implements LpadWorker<LpadWorkerExchange<Delete
             LOG.severe(LogStub.getInstance().getTag() + " - iccid:" + iccid + " profile failed to be deleted");
 
             throw new RuntimeException("Unable to delete profile: " + iccid + ", response: " + eResponse);
-        } catch (DecoderException e) {
+        } catch (NumberFormatException e) {
             LOG.severe(LogStub.getInstance().getTag() + " - " + e.getMessage());
             LOG.severe(LogStub.getInstance().getTag() + " - iccid: " + iccid + " profile failed to be deleted. Exception in Decoder:" + e.getMessage());
 

+ 2 - 5
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/lpad/worker/GetEidLpadWorker.java

@@ -9,9 +9,6 @@ import com.truphone.rsp.dto.asn1.rspdefinitions.GetEuiccDataResponse;
 import com.truphone.util.LogStub;
 import com.truphone.util.TextUtil;
 
-import org.apache.commons.codec.DecoderException;
-import org.apache.commons.codec.binary.Hex;
-
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -71,7 +68,7 @@ public class GetEidLpadWorker implements LpadWorker<LpadWorkerExchange<String>,
 
             logDebug("Decoding response: " + eidapduResponseStr);
 
-            InputStream is = new ByteArrayInputStream(Hex.decodeHex(eidapduResponseStr.toCharArray()));
+            InputStream is = new ByteArrayInputStream(TextUtil.decodeHex(eidapduResponseStr));
 
             logDebug("Decoding with GetEuiccDataResponse");
 
@@ -82,7 +79,7 @@ public class GetEidLpadWorker implements LpadWorker<LpadWorkerExchange<String>,
             progress.stepExecuted(ProgressStep.GET_EID_CONVERTED, "getEID converted...");
 
             return eidResponse.getEidValue().toString();
-        } catch (DecoderException e) {
+        } catch (NumberFormatException e) {
             LOG.log(Level.SEVERE, LogStub.getInstance().getTag() + " - " + e.getMessage(), e);
             LOG.log(Level.SEVERE, LogStub.getInstance().getTag() + " -  Unable to retrieve EID. Exception in Decoder:" + e.getMessage());
 

+ 16 - 0
libs/lpad-sm-dp-plus-connector/src/main/java/com/truphone/util/TextUtil.kt

@@ -1,6 +1,7 @@
 package com.truphone.util
 
 import java.io.InputStream
+import java.lang.NumberFormatException
 import java.lang.StringBuilder
 
 object TextUtil {
@@ -44,6 +45,21 @@ object TextUtil {
         return String(result)
     }
 
+    /*
+     * Decodes a hex string into a byte array
+     * Adapted from <https://stackoverflow.com/questions/66613717/kotlin-convert-hex-string-to-bytearray>
+     */
+    @JvmStatic
+    fun decodeHex(str: String): ByteArray {
+        if (str.length % 2 == 0) {
+            throw NumberFormatException("Must have an even length")
+        }
+
+        return str.chunked(2)
+            .map { it.toInt(16).toByte() }
+            .toByteArray()
+    }
+
     /**
      * Converts a big-endian representation of ICCID into little-endian
      * Big-endian representation is used internally in communication with the SIM.