Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync with up-java for 1.5.6 #23

Merged
merged 9 commits into from
Mar 14, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -25,78 +25,40 @@
package org.eclipse.uprotocol.cloudevent.datamodel

import org.eclipse.uprotocol.v1.UPriority
import java.util.*

/**
* Specifies the properties that can configure the UCloudEvent.
* @param hash an HMAC generated on the data portion of the CloudEvent message using the device key.
* @param hash An HMAC generated on the data portion of the CloudEvent message using the device key.
* @param priority uProtocol Prioritization classifications.
* @param ttl How long this event should live for after it was generated (in milliseconds).
* Events without this attribute (or value is 0) MUST NOT timeout.
* @param token Oauth2 access token to perform the access request defined in the request message.
* @param traceparent An identifier used to correlate observability across related events.
*/
data class UCloudEventAttributes internal constructor(
private val hash: String? = null,
private val priority: UPriority? = null,
private val ttl: Int? = null,
private val token: String? = null,
private val traceparent: String? = null
val hash: String? = null,
val priority: UPriority? = null,
val ttl: Int? = null,
val token: String? = null,
val traceparent: String? = null
) {
private constructor(builder: UCloudEventAttributesBuilder) : this(
builder.hash,
if (builder.hash.isNullOrBlank()) null else builder.hash,
builder.priority,
builder.ttl,
builder.token,
if (builder.token.isNullOrBlank()) null else builder.token,
builder.traceparent
)

/**
* An HMAC generated on the data portion of the CloudEvent message using the device key.
*/
val isEmpty: Boolean
/**
* Indicates that there are no added additional attributes to configure when building a CloudEvent.
* @return Returns true if this attributes container is an empty container and has no valuable information in building a CloudEvent.
*/
get() = hash().isEmpty && priority().isEmpty && ttl().isEmpty && token().isEmpty && traceparent().isEmpty

/**
* An HMAC generated on the data portion of the CloudEvent message using the device key.
* @return Returns an Optional hash attribute.
*/
fun hash(): Optional<String> {
return if (hash.isNullOrBlank()) Optional.empty() else Optional.of(hash)
}

/**
* uProtocol Prioritization classifications.
* @return Returns an Optional priority attribute.
*/
fun priority(): Optional<UPriority> {
return if (priority == null) Optional.empty() else Optional.of(priority)
}

/**
* How long this event should live for after it was generated (in milliseconds).
* @return Returns an Optional time to live attribute.
*/
fun ttl(): Optional<Int> {
return if (ttl == null) Optional.empty() else Optional.of(ttl)
}

/**
* Oauth2 access token to perform the access request defined in the request message.
* @return Returns an Optional OAuth token attribute.
*/
fun token(): Optional<String> {
return if (token.isNullOrBlank()) Optional.empty() else Optional.of(token)
}


/**
* An identifier used to correlate observability across related events.
* @return Returns an Optional traceparent attribute.
*/
fun traceparent(): Optional<String> {
return if (traceparent.isNullOrBlank()) Optional.empty() else Optional.of(traceparent)
}
get() = hash == null && priority == null && ttl == null && token == null && traceparent == null

/**
* Builder for constructing the UCloudEventAttributes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,16 @@ object CloudEventFactory {
.withDataSchema(URI.create(protoPayloadSchema))
*/
.withData(protoPayloadBytes)
attributes.ttl().ifPresent { ttl -> cloudEventBuilder.withExtension("ttl", ttl) }
attributes.priority()
.ifPresent { priority -> cloudEventBuilder.withExtension("priority", priority.toString()) }
attributes.hash().ifPresent { hash -> cloudEventBuilder.withExtension("hash", hash) }
attributes.token().ifPresent { token -> cloudEventBuilder.withExtension("token", token) }
attributes.traceparent().ifPresent { traceparent: String ->
attributes.ttl?.let { ttl -> cloudEventBuilder.withExtension("ttl", ttl) }
attributes.priority?.let { priority ->
cloudEventBuilder.withExtension(
"priority",
UCloudEvent.getCeName(priority.valueDescriptor)
)
}
attributes.hash?.let { hash -> cloudEventBuilder.withExtension("hash", hash) }
attributes.token?.let { token -> cloudEventBuilder.withExtension("token", token) }
attributes.traceparent?.let { traceparent: String ->
cloudEventBuilder.withExtension("traceparent", traceparent)
}

Expand Down
Loading
Loading