diff --git a/core/model/src/main/kotlin/au/com/dius/pact/core/model/OptionalBody.kt b/core/model/src/main/kotlin/au/com/dius/pact/core/model/OptionalBody.kt index cc2be43ba8..31946179da 100644 --- a/core/model/src/main/kotlin/au/com/dius/pact/core/model/OptionalBody.kt +++ b/core/model/src/main/kotlin/au/com/dius/pact/core/model/OptionalBody.kt @@ -9,6 +9,7 @@ import org.apache.commons.codec.binary.Hex import org.apache.tika.config.TikaConfig import org.apache.tika.io.TikaInputStream import org.apache.tika.metadata.Metadata +import java.lang.RuntimeException import java.util.Base64 /** @@ -114,12 +115,16 @@ data class OptionalBody( fun detectContentType(): ContentType? = when { this.isPresent() -> { - val metadata = Metadata() - val mimetype = tika.detector.detect(TikaInputStream.get(value!!), metadata) - if (mimetype.baseType.type == "text") { - detectStandardTextContentType() ?: ContentType(mimetype) + if (tika != null) { + val metadata = Metadata() + val mimetype = tika.detector.detect(TikaInputStream.get(value!!), metadata) + if (mimetype.baseType.type == "text") { + detectStandardTextContentType() ?: ContentType(mimetype) + } else { + ContentType(mimetype) + } } else { - ContentType(mimetype) + detectStandardTextContentType() } } else -> null @@ -185,7 +190,10 @@ data class OptionalBody( } } - private val tika = TikaConfig() + private val tika = try { TikaConfig() } catch (e: RuntimeException) { + logger.warn(e) { "Could not initialise Tika, detecting content types will be disabled" } + null + } } }