Skip to content

Commit

Permalink
refactored saving raw messages, documents and decoded messages/events
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Dec 5, 2024
1 parent 1c08f3e commit ccae900
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/main/kotlin/de/gmuth/ipp/client/IppClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import de.gmuth.ipp.core.IppStatus.ClientErrorBadRequest
import de.gmuth.ipp.core.IppStatus.ClientErrorNotFound
import de.gmuth.ipp.core.IppTag.Unsupported
import de.gmuth.ipp.iana.IppRegistrationsSection2
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
Expand All @@ -32,7 +31,9 @@ open class IppClient(val config: IppConfig = IppConfig()) {
protected val logger: Logger = getLogger(javaClass.name)

var responseInterceptor: IppResponseInterceptor? = null
var saveEvents: Boolean = false
var saveMessages: Boolean = false
var saveDocuments: Boolean = false
var saveMessagesDirectory = createTempDirectory().toFile()
var onExceptionSaveMessages: Boolean = false
var throwWhenNotSuccessful: Boolean = true
Expand Down Expand Up @@ -67,7 +68,8 @@ open class IppClient(val config: IppConfig = IppConfig()) {
config.ippVersion,
requestCounter.getAndIncrement(),
config.charset,
naturalLanguage
naturalLanguage,
config.userAgent
)

//------------------------------------
Expand All @@ -76,24 +78,24 @@ open class IppClient(val config: IppConfig = IppConfig()) {

@JvmOverloads
fun exchange(request: IppRequest, ippUri: URI = request.printerOrJobUri) = with(request) {
logger.fine { "Send '$operation' request to $ippUri" }
val httpUri = with(ippUri) {
URI(
scheme.replace("ipp", "http"), userInfo, host,
if (port == -1) 631 else port, path, query, fragment
)
}
httpPost(httpUri, request).also {
logger.finer { "Exchanged request #$requestId @${httpUri.host}: $request => $it" }
log(logger, FINEST, ">>> ") // this=request
it.log(logger, FINEST, "<<< ") // it=response
fun file(extension: String) =
File(saveMessagesDirectory, "%03d-%s.%s".format(requestId, operation, extension))
if (saveMessages) {
saveBytes(file("req"))
saveText(file("req.txt"))
it.saveBytes(file("res"))
it.saveText(file("res.txt"))
IppRequestExchangedEvent(request, it).run {
logger.fine { toString() }
if (saveEvents || saveDocuments || saveMessages)
save(
saveMessagesDirectory,
saveEvent = saveEvents,
saveDocument = saveDocuments,
saveRawMessages = saveMessages
)
}
responseInterceptor?.invoke(request, it)
validateIppResponse(request, it)
Expand All @@ -115,7 +117,7 @@ open class IppClient(val config: IppConfig = IppConfig()) {
}
configure(
chunked = request.hasDocument(),
userAgent = request.httpUserAgent ?: config.userAgent
userAgent = request.httpUserAgent
)
try {
request.write(outputStream)
Expand Down Expand Up @@ -191,6 +193,7 @@ open class IppClient(val config: IppConfig = IppConfig()) {
val upgrade = getHeaderField("Upgrade")
"HTTP status $responseCode, $responseMessage, Try ipps://${request.printerOrJobUri.host}, Upgrade: $upgrade"
}

responseCode != 200 -> "HTTP request failed: $responseCode, $responseMessage"
contentType != null && !contentType.startsWith(APPLICATION_IPP) -> "Invalid Content-Type: $contentType"
exception != null -> exception.message
Expand Down

0 comments on commit ccae900

Please sign in to comment.