Skip to content

Commit

Permalink
parameter throwIfSupportedAttributesIsNotAvailable
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Sep 9, 2024
1 parent 0b3ae02 commit 4a4fb58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
30 changes: 21 additions & 9 deletions src/main/kotlin/de/gmuth/ipp/client/IppPrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class IppPrinter(

fun identify(actions: List<String>, message: String? = null): IppResponse {
val request = ippRequest(IdentifyPrinter).apply {
checkIfValueIsSupported("identify-actions", actions)
checkIfValueIsSupported("identify-actions", actions, true)
operationGroup.attribute("identify-actions", Keyword, actions)
message?.let { operationGroup.attribute("message", TextWithoutLanguage, it) }
}
Expand Down Expand Up @@ -407,7 +407,7 @@ class IppPrinter(
) = ippRequest(operation).apply {
for (attributeBuilder in attributeBuilders) {
val attribute = attributeBuilder.buildIppAttribute(attributes)
checkIfValueIsSupported(attribute.name, attribute.values)
checkIfValueIsSupported(attribute.name, attribute.values, false)
// put attribute in operation or job group?
val groupTag = IppRegistrationsSection2.selectGroupForAttribute(attribute.name) ?: Job
if (!containsGroup(groupTag)) createAttributesGroup(groupTag)
Expand Down Expand Up @@ -441,7 +441,7 @@ class IppPrinter(
val request = ippRequest(GetJobs, requestedAttributes = requestedAttributes).apply {
operationGroup.run {
whichJobs?.keyword?.let {
checkIfValueIsSupported("which-jobs", it)
checkIfValueIsSupported("which-jobs", it, true)
attribute("which-jobs", Keyword, it)
}
myJobs?.let { attribute("my-jobs", IppTag.Boolean, it) }
Expand Down Expand Up @@ -482,7 +482,11 @@ class IppPrinter(
}

fun checkNotifyEvents(notifyEvents: Collection<String>?) = notifyEvents?.let {
if (it.isNotEmpty() && it.first() != "all") checkIfValueIsSupported("notify-events", it)
if (it.isNotEmpty() && it.first() != "all") {
if (!attributes.containsKey("notify-events-supported"))
throw IppException("Printer does not support request parameter 'notifyEvents'.")
checkIfValueIsSupported("notify-events", it, true)
}
}

//-------------------------------------------------
Expand Down Expand Up @@ -536,9 +540,9 @@ class IppPrinter(
.ippRequest(operation, printerUri, requestedAttributes, userName)

override fun exchange(request: IppRequest) = ippClient.exchange(request.apply {
checkIfValueIsSupported("ipp-versions", version!!)
checkIfValueIsSupported("operations", code!!.toInt())
checkIfValueIsSupported("charset", attributesCharset)
checkIfValueIsSupported("ipp-versions", version!!, true)
checkIfValueIsSupported("operations", code!!.toInt(), true)
checkIfValueIsSupported("charset", attributesCharset, true)
})

private fun exchangeForIppJob(request: IppRequest) =
Expand All @@ -550,8 +554,16 @@ class IppPrinter(
}
}

private fun checkIfValueIsSupported(attributeName: String, value: Any) =
IppValueSupport.checkIfValueIsSupported(attributes, attributeName, value)
private fun checkIfValueIsSupported(
attributeName: String,
value: Any,
throwIfSupportedAttributesIsNotAvailable: Boolean
) = IppValueSupport.checkIfValueIsSupported(
attributes,
attributeName,
value,
throwIfSupportedAttributesIsNotAvailable
)

// -------
// Logging
Expand Down
17 changes: 12 additions & 5 deletions src/main/kotlin/de/gmuth/ipp/client/IppValueSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,32 @@ object IppValueSupport {

private val logger = getLogger(javaClass.name)

fun checkIfValueIsSupported(printerAttributes: IppAttributesGroup, attribute: IppAttribute<*>) {
fun checkIfValueIsSupported(
printerAttributes: IppAttributesGroup, attribute: IppAttribute<*>,
throwIfSupportedAttributesIsNotAvailable: Boolean
) {
val supportedAttribute = printerAttributes["${attribute.name}-supported"]
if (supportedAttribute == null) logger.warning { "${attribute.name}-supported not available in printer attributes" }
else checkIfValueIsSupported(printerAttributes, attribute.name, attribute.value as Any)
else checkIfValueIsSupported(printerAttributes, attribute.name, attribute.value as Any, throwIfSupportedAttributesIsNotAvailable)
}

fun checkIfValueIsSupported(
printerAttributes: IppAttributesGroup,
attributeName: String,
value: Any
value: Any,
throwIfSupportedAttributesIsNotAvailable: Boolean
) {
require(printerAttributes.tag == Printer) { "Printer attributes group expected" }
if (printerAttributes.isEmpty()) return

if (value is Collection<*>) { // instead of providing another signature just check collections iteratively
for (collectionValue in value) {
checkIfValueIsSupported(printerAttributes, attributeName, collectionValue!!)
checkIfValueIsSupported(printerAttributes, attributeName, collectionValue!!, throwIfSupportedAttributesIsNotAvailable)
}
} else {
val supportedAttributeName = "$attributeName-supported"
if(!printerAttributes.containsKey(supportedAttributeName) && throwIfSupportedAttributesIsNotAvailable)
throw IppException("Unable to check value '$value' because printer attribute '$supportedAttributeName' is not available.")
isAttributeValueSupported(printerAttributes, attributeName, value)
}
}
Expand All @@ -54,7 +61,7 @@ object IppValueSupport {
IppTag.Enum, Charset, NaturalLanguage, MimeMediaType, Keyword, Resolution -> when (supportedAttributeName) {
"media-col-supported" -> with(value as IppCollection) {
members
.onEach { checkIfValueIsSupported(printerAttributes, it) }
.onEach { checkIfValueIsSupported(printerAttributes, it, false) }
.filter { !supportedAttribute.values.contains(it.name) }
.forEach { logger.warning { "media-col member unsupported: $it" } }
supportedAttribute.values.containsAll(members.map { it.name })
Expand Down

0 comments on commit 4a4fb58

Please sign in to comment.