/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ package org.opensearch.commons.notifications.model import org.opensearch.commons.notifications.NotificationConstants.DELIVERY_STATUS_TAG import org.opensearch.commons.notifications.NotificationConstants.RECIPIENT_TAG import org.opensearch.commons.utils.logger import org.opensearch.commons.utils.validateEmail import org.opensearch.core.common.io.stream.StreamInput import org.opensearch.core.common.io.stream.StreamOutput import org.opensearch.core.common.io.stream.Writeable import org.opensearch.core.xcontent.ToXContent import org.opensearch.core.xcontent.XContentBuilder import org.opensearch.core.xcontent.XContentParser import org.opensearch.core.xcontent.XContentParserUtils import java.io.IOException /** * Data class representing Email Recipient Status. */ data class EmailRecipientStatus( val recipient: String, val deliveryStatus: DeliveryStatus ) : BaseModel { init { validateEmail(recipient) } companion object { private val log by logger(EmailRecipientStatus::class.java) /** * reader to create instance of class from writable. */ val reader = Writeable.Reader { EmailRecipientStatus(it) } /** * Creator used in REST communication. * @param parser XContentParser to deserialize data from. */ @JvmStatic @Throws(IOException::class) fun parse(parser: XContentParser): EmailRecipientStatus { var recipient: String? = null var deliveryStatus: DeliveryStatus? = null XContentParserUtils.ensureExpectedToken( XContentParser.Token.START_OBJECT, parser.currentToken(), parser ) while (parser.nextToken() != XContentParser.Token.END_OBJECT) { val fieldName = parser.currentName() parser.nextToken() when (fieldName) { RECIPIENT_TAG -> recipient = parser.text() DELIVERY_STATUS_TAG -> deliveryStatus = DeliveryStatus.parse(parser) else -> { parser.skipChildren() log.info("Unexpected field: $fieldName, while parsing Email Recipient Status") } } } recipient ?: throw IllegalArgumentException("$RECIPIENT_TAG field absent") deliveryStatus ?: throw IllegalArgumentException("$DELIVERY_STATUS_TAG field absent") return EmailRecipientStatus(recipient, deliveryStatus) } } /** * Constructor used in transport action communication. * @param input StreamInput stream to deserialize data from. */ constructor(input: StreamInput) : this( recipient = input.readString(), deliveryStatus = DeliveryStatus.reader.read(input) ) /** * {@inheritDoc} */ override fun writeTo(output: StreamOutput) { output.writeString(recipient) deliveryStatus.writeTo(output) } /** * {@inheritDoc} */ override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder { builder!! return builder.startObject() .field(RECIPIENT_TAG, recipient) .field(DELIVERY_STATUS_TAG, deliveryStatus) .endObject() } }