From eee7a047fe6b56a99a2e379055b6d650282e40a2 Mon Sep 17 00:00:00 2001 From: Gerhard Muth Date: Sun, 29 Sep 2024 20:39:15 +0200 Subject: [PATCH] fixed sonar issues --- src/main/kotlin/de/gmuth/ipp/client/CupsClient.kt | 10 ++-------- src/main/kotlin/de/gmuth/ipp/client/IppDocument.kt | 2 +- src/main/kotlin/de/gmuth/ipp/client/IppJob.kt | 13 ++++++++++--- src/main/kotlin/de/gmuth/ipp/client/IppPrinter.kt | 3 --- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/de/gmuth/ipp/client/CupsClient.kt b/src/main/kotlin/de/gmuth/ipp/client/CupsClient.kt index 48278516..246492d8 100644 --- a/src/main/kotlin/de/gmuth/ipp/client/CupsClient.kt +++ b/src/main/kotlin/de/gmuth/ipp/client/CupsClient.kt @@ -6,7 +6,6 @@ package de.gmuth.ipp.client import de.gmuth.ipp.client.IppOperationException.ClientErrorNotFoundException import de.gmuth.ipp.client.WhichJobs.All -import de.gmuth.ipp.core.IppException import de.gmuth.ipp.core.IppOperation import de.gmuth.ipp.core.IppOperation.* import de.gmuth.ipp.core.IppRequest @@ -297,17 +296,12 @@ class CupsClient( useJobOwnerAsUserName = true cupsGetDocuments( save = true, - directory = File(cupsDirectory, printerUri.path.substringAfterLast("/")) - .apply { if (!exists()) mkdirs() }, + directory = File(cupsDirectory, printerUri.path.substringAfterLast("/")), optionalCommandToHandleFile = commandToHandleSavedFile ) .apply { numberOfSavedDocuments.addAndGet(size) } } catch (ippExchangeException: IppExchangeException) { - ippExchangeException.run { - logger.info { "Get documents for job #$id failed: $message" } - if (this is HttpPostException && httpStatus == 426) - throw IppException("Server requires TLS encrypted connection") - } + logger.info { "Get documents for job #$id failed: ${ippExchangeException.message}" } } } } diff --git a/src/main/kotlin/de/gmuth/ipp/client/IppDocument.kt b/src/main/kotlin/de/gmuth/ipp/client/IppDocument.kt index cd367e77..b7ec61d6 100644 --- a/src/main/kotlin/de/gmuth/ipp/client/IppDocument.kt +++ b/src/main/kotlin/de/gmuth/ipp/client/IppDocument.kt @@ -14,7 +14,6 @@ import java.io.OutputStream import java.util.logging.Level import java.util.logging.Logger import java.util.logging.Logger.getLogger -import kotlin.io.path.createTempDirectory class IppDocument( val job: IppJob, @@ -74,6 +73,7 @@ class IppDocument( overwrite: Boolean = true ) = File(directory, filename).also { if (it.isFile && !overwrite) throw IOException("File '$it' already exists") + if (!it.exists()) it.mkdirs() copyTo(it.outputStream()) this.file = it logger.info { "Saved $file ${if (attributes.containsKey("document-format")) "($format)" else ""}" } diff --git a/src/main/kotlin/de/gmuth/ipp/client/IppJob.kt b/src/main/kotlin/de/gmuth/ipp/client/IppJob.kt index 3ba8f963..5f45cd27 100644 --- a/src/main/kotlin/de/gmuth/ipp/client/IppJob.kt +++ b/src/main/kotlin/de/gmuth/ipp/client/IppJob.kt @@ -331,9 +331,16 @@ class IppJob( @JvmOverloads fun cupsGetDocument(documentNumber: Int = 1): IppDocument { logger.fine { "CupsGetDocument #$documentNumber for job #$id" } - val response = exchange(ippRequest(CupsGetDocument) - .apply { operationGroup.attribute("document-number", Integer, documentNumber) }) - return IppDocument(this, response.jobGroup, response.documentInputStream!!) + try { + val response = exchange(ippRequest(CupsGetDocument) + .apply { operationGroup.attribute("document-number", Integer, documentNumber) }) + return IppDocument(this, response.jobGroup, response.documentInputStream!!) + } catch (httpPostException: HttpPostException) { + throw if (httpPostException.httpStatus == 426) + IppException("Server requires TLS encrypted connection", httpPostException) + else + httpPostException + } } @JvmOverloads diff --git a/src/main/kotlin/de/gmuth/ipp/client/IppPrinter.kt b/src/main/kotlin/de/gmuth/ipp/client/IppPrinter.kt index 60f9e058..c46ad807 100644 --- a/src/main/kotlin/de/gmuth/ipp/client/IppPrinter.kt +++ b/src/main/kotlin/de/gmuth/ipp/client/IppPrinter.kt @@ -593,9 +593,6 @@ class IppPrinter( .getValues>("printer-icons") .map { it.save() } - //fun printerDirectory(printerName: String = name.text.replace("\\s+".toRegex(), "_")): File = - // File(workDirectory, printerName).createDirectoryIfNotExists() - // -------------------------------------------------- // Internal utilities implemented as Kotlin extension // --------------------------------------------------