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

Incorporate latest changes from WebDAV-Push #60

Merged
merged 16 commits into from
Dec 2, 2024
2 changes: 1 addition & 1 deletion src/main/kotlin/at/bitfire/dav4jvm/PropertyRegistry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object PropertyRegistry {
at.bitfire.dav4jvm.property.carddav.MaxResourceSize.Factory,
Owner.Factory,
PushMessage.Factory,
PushSubscribe.Factory,
PushRegister.Factory,
PushTransports.Factory,
QuotaAvailableBytes.Factory,
QuotaUsedBytes.Factory,
Expand Down
40 changes: 40 additions & 0 deletions src/main/kotlin/at/bitfire/dav4jvm/property/push/AuthSecret.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package at.bitfire.dav4jvm.property.push

import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.PropertyFactory
import at.bitfire.dav4jvm.XmlReader
import org.xmlpull.v1.XmlPullParser

/**
* Represents a `{DAV:Push}auth-secret` property.
ArnyminerZ marked this conversation as resolved.
Show resolved Hide resolved
*
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
class AuthSecret private constructor(
val secret: String?
): Property {

companion object {

@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "auth-secret")

}


object Factory: PropertyFactory {

override fun getName() = NAME

override fun create(parser: XmlPullParser): AuthSecret =
AuthSecret(XmlReader(parser).readText())

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package at.bitfire.dav4jvm.property.push

import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.PropertyFactory
import org.xmlpull.v1.XmlPullParser

/**
* Represents a `{DAV:Push}client-public-key` property.
*
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
class ClientPublicKey: PushPublicKey() {
companion object {
@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "client-public-key")
}

ArnyminerZ marked this conversation as resolved.
Show resolved Hide resolved
object Factory : PushPublicKey.Factory<ClientPublicKey>(NAME, ::ClientPublicKey)
}
13 changes: 11 additions & 2 deletions src/main/kotlin/at/bitfire/dav4jvm/property/push/PushMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package at.bitfire.dav4jvm.property.push

import at.bitfire.dav4jvm.PropStat
import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.PropertyFactory
import at.bitfire.dav4jvm.XmlReader
Expand All @@ -17,7 +18,7 @@ import org.xmlpull.v1.XmlPullParser
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
class PushMessage(
val topic: String?
val propStat: PropStat?
): Property {

companion object {
Expand All @@ -32,7 +33,15 @@ class PushMessage(

override fun getName() = NAME

override fun create(parser: XmlPullParser) = PushMessage(XmlReader(parser).readTextProperty(Topic.NAME))
override fun create(parser: XmlPullParser): PushMessage {
var propStat: PropStat? = null

XmlReader(parser).processTag(PropStat.NAME) {
propStat = PropStat.parse(parser)
}

return PushMessage(propStat)
}

}

Expand Down
65 changes: 65 additions & 0 deletions src/main/kotlin/at/bitfire/dav4jvm/property/push/PushPublicKey.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package at.bitfire.dav4jvm.property.push

import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.PropertyFactory
import at.bitfire.dav4jvm.XmlReader
import at.bitfire.dav4jvm.XmlUtils.propertyName
import org.xmlpull.v1.XmlPullParser

/**
* Represents a public key property from Push.
*
* Experimental! See https://github.com/bitfireAT/webdav-push/
*
* @see ClientPublicKey
* @see ServerPublicKey
*/
abstract class PushPublicKey: Property {

companion object {
@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "server-public-key")

val PROP_TYPE = Property.Name(NS_WEBDAV_PUSH, "type")
}

var type: String? = null
var key: String? = null


override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is PushPublicKey) return false

if (type != other.type) return false
if (key != other.key) return false

return true
}

override fun hashCode(): Int {
var result = type?.hashCode() ?: 0
result = 31 * result + (key?.hashCode() ?: 0)
return result
}


abstract class Factory<KeyType: PushPublicKey>(
private val name: Property.Name,
private val constructor: () -> KeyType
): PropertyFactory {

override fun getName() = name

override fun create(parser: XmlPullParser): KeyType {
val publicKey = constructor()

publicKey.type = parser.getAttributeValue(null, "type")
publicKey.key = XmlReader(parser).readText()

return publicKey
}

}
ArnyminerZ marked this conversation as resolved.
Show resolved Hide resolved

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import org.xmlpull.v1.XmlPullParser
import java.time.Instant

/**
* Represents a `{DAV:Push}push-subscribe` property.
* Represents a `{DAV:Push}push-register` property.
*
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
class PushSubscribe: Property {
class PushRegister: Property {

companion object {

@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "push-subscribe")
val NAME = Property.Name(NS_WEBDAV_PUSH, "push-register")

val EXPIRES = Property.Name(NS_WEBDAV_PUSH, "expires")

Expand All @@ -38,25 +38,25 @@ class PushSubscribe: Property {

override fun getName() = NAME

override fun create(parser: XmlPullParser): PushSubscribe {
val subscribe = PushSubscribe()
override fun create(parser: XmlPullParser): PushRegister {
val register = PushRegister()

val depth = parser.depth
var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1)
when (parser.propertyName()) {
EXPIRES ->
subscribe.expires = XmlReader(parser).readText()?.let {
register.expires = XmlReader(parser).readText()?.let {
HttpUtils.parseDate(it)
}
Subscription.NAME ->
subscribe.subscription = Subscription.Factory.create(parser)
register.subscription = Subscription.Factory.create(parser)
}
eventType = parser.next()
}

return subscribe
return register
}

}
Expand Down
40 changes: 40 additions & 0 deletions src/main/kotlin/at/bitfire/dav4jvm/property/push/PushResource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package at.bitfire.dav4jvm.property.push

import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.PropertyFactory
import at.bitfire.dav4jvm.XmlReader
import org.xmlpull.v1.XmlPullParser

/**
* Represents a `{DAV:Push}push-resource` property.
*
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
class PushResource private constructor(
val resource: String?
ArnyminerZ marked this conversation as resolved.
Show resolved Hide resolved
): Property {

companion object {

@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "push-resource")

}


object Factory: PropertyFactory {

override fun getName() = NAME

override fun create(parser: XmlPullParser): PushResource =
PushResource(XmlReader(parser).readText())

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package at.bitfire.dav4jvm.property.push

import at.bitfire.dav4jvm.Property

/**
* Identifies a property as a push transport.
*/
interface PushTransport: Property
26 changes: 12 additions & 14 deletions src/main/kotlin/at/bitfire/dav4jvm/property/push/PushTransports.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,32 @@ import org.xmlpull.v1.XmlPullParser
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
class PushTransports private constructor(
val transports: Set<Property.Name>
val transports: Set<PushTransport>
): Property {

companion object {
@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "push-transports")

val TRANSPORT = Property.Name(NS_WEBDAV_PUSH, "transport")
val WEB_PUSH = Property.Name(NS_WEBDAV_PUSH, "web-push")
val NAME = Property.Name(NS_WEBDAV_PUSH, "transports")
}

fun hasWebPush() = transports.contains(WEB_PUSH)
fun hasWebPush() = transports.find { it is WebPush } != null
ArnyminerZ marked this conversation as resolved.
Show resolved Hide resolved


object Factory: PropertyFactory {

override fun getName() = NAME

override fun create(parser: XmlPullParser): PushTransports {
val transports = mutableListOf<Property.Name>()
XmlReader(parser).processTag(TRANSPORT) {
val depth = parser.depth
var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1)
transports += parser.propertyName()
eventType = parser.next()
val transports = mutableListOf<PushTransport>()
val depth = parser.depth
var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1) {
when (parser.propertyName()) {
WebPush.NAME -> transports += WebPush.Factory.create(parser)
}
}
eventType = parser.next()
}
return PushTransports(transports.toSet())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package at.bitfire.dav4jvm.property.push

import at.bitfire.dav4jvm.Property

/**
* Represents a `{DAV:Push}server-public-key` property.
*
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
class ServerPublicKey: PushPublicKey() {
companion object {
@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "server-public-key")
}

object Factory : PushPublicKey.Factory<ServerPublicKey>(NAME, ::ServerPublicKey)
}
ArnyminerZ marked this conversation as resolved.
Show resolved Hide resolved
44 changes: 44 additions & 0 deletions src/main/kotlin/at/bitfire/dav4jvm/property/push/WebPush.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package at.bitfire.dav4jvm.property.push

import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.PropertyFactory
import org.xmlpull.v1.XmlPullParser

/**
* Represents a `{DAV:Push}web-push` property.
*
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
data class WebPush(
val serverPublicKey: ServerPublicKey?
): PushTransport {

companion object {
@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "web-push")
}


object Factory: PropertyFactory {

override fun getName(): Property.Name = NAME

override fun create(parser: XmlPullParser): WebPush {
var serverPublicKey: ServerPublicKey? = null
val depth = parser.depth
var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.namespace == NS_WEBDAV_PUSH) {
when (parser.name) {
ServerPublicKey.NAME.name
-> serverPublicKey = ServerPublicKey.Factory.create(parser)
}
}
eventType = parser.next()
}
return WebPush(serverPublicKey)
}

}

}
Loading