/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ package org.opensearch.commons.notifications.action import org.opensearch.commons.notifications.NotificationConstants.CONFIG_ID_TAG import org.opensearch.commons.utils.logger 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 /** * Action Response for creating new configuration. */ class CreateNotificationConfigResponse : BaseResponse { val configId: String companion object { private val log by logger(CreateNotificationConfigResponse::class.java) /** * reader to create instance of class from writable. */ val reader = Writeable.Reader { CreateNotificationConfigResponse(it) } /** * Creator used in REST communication. * @param parser XContentParser to deserialize data from. */ @JvmStatic @Throws(IOException::class) fun parse(parser: XContentParser): CreateNotificationConfigResponse { var configId: String? = 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) { CONFIG_ID_TAG -> configId = parser.text() else -> { parser.skipChildren() log.info("Unexpected field: $fieldName, while parsing CreateNotificationConfigResponse") } } } configId ?: throw IllegalArgumentException("$CONFIG_ID_TAG field absent") return CreateNotificationConfigResponse(configId) } } /** * constructor for creating the class * @param configId the id of the created notification configuration */ constructor(configId: String) { this.configId = configId } /** * {@inheritDoc} */ @Throws(IOException::class) constructor(input: StreamInput) : super(input) { configId = input.readString() } /** * {@inheritDoc} */ @Throws(IOException::class) override fun writeTo(output: StreamOutput) { output.writeString(configId) } /** * {@inheritDoc} */ override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder { builder!! return builder.startObject() .field(CONFIG_ID_TAG, configId) .endObject() } }