migrate our custom SetNicknameWorker to Kotlin

This commit is contained in:
Peter Cai 2022-05-03 18:42:41 -04:00
parent 033a8c7e36
commit eb862301cc
2 changed files with 87 additions and 67 deletions

View File

@ -1,67 +0,0 @@
package com.truphone.lpa.impl;
import com.truphone.lpa.ApduChannel;
import com.truphone.lpa.apdu.ApduUtils;
import com.truphone.rsp.dto.asn1.rspdefinitions.SetNicknameResponse;
import com.truphone.util.LogStub;
import com.truphone.util.Util;
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;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SetNicknameWorker {
private static final Logger LOG = Logger.getLogger(ListProfilesWorker.class.getName());
private final String iccid;
private final String nickname;
private final ApduChannel apduChannel;
SetNicknameWorker(String iccid, String nickname, ApduChannel apduChannel) {
this.apduChannel = apduChannel;
this.iccid = iccid;
this.nickname = nickname;
}
boolean run() {
if (LogStub.getInstance().isDebugEnabled()) {
LogStub.getInstance().logDebug(LOG, LogStub.getInstance().getTag() + " - Renaming profile: " + iccid);
}
String apdu = ApduUtils.setNicknameApdu(iccid, Util.byteArrayToHexString(nickname.getBytes(), ""));
String eResponse = apduChannel.transmitAPDU(apdu);
try {
InputStream is = new ByteArrayInputStream(Hex.decodeHex(eResponse.toCharArray()));
SetNicknameResponse response = new SetNicknameResponse();
response.decode(is);
if ("0".equals(response.getSetNicknameResult().toString())) {
if (LogStub.getInstance().isDebugEnabled()) {
LogStub.getInstance().logDebug(LOG, LogStub.getInstance().getTag() + " - Profile renamed: " + iccid);
}
return true;
} else {
if (LogStub.getInstance().isDebugEnabled()) {
LogStub.getInstance().logDebug(LOG, LogStub.getInstance().getTag() + " - Profile not renamed: " + iccid);
}
return false;
}
} catch (IOException ioe) {
LOG.log(Level.SEVERE, LogStub.getInstance().getTag() + " - iccid: " + iccid + " profile failed to be renamed");
throw new RuntimeException("Unable to rename profile: " + iccid + ", response: " + eResponse);
} catch (DecoderException e) {
LOG.log(Level.SEVERE, LogStub.getInstance().getTag() + " - " + e.getMessage(), e);
LOG.log(Level.SEVERE, LogStub.getInstance().getTag() + " - iccid: " + iccid + " profile failed to be renamed. Exception in Decoder:" + e.getMessage());
throw new RuntimeException("Unable to rename profile: " + iccid + ", response: " + eResponse);
}
}
}

View File

@ -0,0 +1,87 @@
/*
* Copyright 2022 Peter Cai & Pierre-Hugues Husson
*
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
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.Util
import java.io.ByteArrayInputStream
import java.io.IOException
import java.io.InputStream
import java.lang.RuntimeException
import java.util.logging.Level
import java.util.logging.Logger
class SetNicknameWorker internal constructor(
private val iccid: String,
private val nickname: String,
private val apduChannel: ApduChannel
) {
companion object {
private val LOG = Logger.getLogger(
ListProfilesWorker::class.java.name
)
}
fun run(): Boolean {
if (LogStub.getInstance().isDebugEnabled) {
LogStub.getInstance()
.logDebug(LOG, LogStub.getInstance().tag + " - Renaming profile: " + iccid)
}
val apdu = ApduUtils.setNicknameApdu(
iccid, Util.byteArrayToHexString(
nickname.toByteArray(), ""
)
)
val eResponse = apduChannel.transmitAPDU(apdu)
return try {
val `is`: InputStream = ByteArrayInputStream(Hex.decodeHex(eResponse.toCharArray()))
val response = SetNicknameResponse()
response.decode(`is`)
if ("0" == response.setNicknameResult.toString()) {
if (LogStub.getInstance().isDebugEnabled) {
LogStub.getInstance()
.logDebug(LOG, LogStub.getInstance().tag + " - Profile renamed: " + iccid)
}
true
} else {
if (LogStub.getInstance().isDebugEnabled) {
LogStub.getInstance().logDebug(
LOG,
LogStub.getInstance().tag + " - Profile not renamed: " + iccid
)
}
false
}
} catch (ioe: IOException) {
LOG.log(
Level.SEVERE,
LogStub.getInstance().tag + " - iccid: " + iccid + " profile failed to be renamed"
)
throw RuntimeException("Unable to rename profile: $iccid, response: $eResponse")
} catch (e: DecoderException) {
LOG.log(Level.SEVERE, LogStub.getInstance().tag + " - " + e.message, e)
LOG.log(
Level.SEVERE,
LogStub.getInstance().tag + " - iccid: " + iccid + " profile failed to be renamed. Exception in Decoder:" + e.message
)
throw RuntimeException("Unable to rename profile: $iccid, response: $eResponse")
}
}
}