chibenwa commented on a change in pull request #711:
URL: https://github.com/apache/james-project/pull/711#discussion_r737313061



##########
File path: 
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/push_subscription/PushRequest.scala
##########
@@ -0,0 +1,105 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ * http://www.apache.org/licenses/LICENSE-2.0                   *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.push_subscription
+
+import com.google.common.base.CharMatcher
+import eu.timepit.refined
+import eu.timepit.refined.api.{Refined, Validate}
+import org.apache.james.jmap.push_subscription.TTL.PushTTL
+import org.apache.james.jmap.push_subscription.Topic.PushTopic
+
+object PushUrgency {
+  def default: PushUrgency = Normal
+}
+
+sealed trait PushUrgency {
+  def value: String
+}
+
+case object Low extends PushUrgency {
+  val value: String = "LOW"
+}
+
+case object VeryLow extends PushUrgency {
+  val value: String = "VERY_LOW"
+}
+
+case object Normal extends PushUrgency {
+  val value: String = "NORMAL"
+}
+
+case object High extends PushUrgency {
+  val value: String = "HIGH"
+}
+
+object Topic {
+  type PushTopic = String Refined PushTopicConstraint
+  private val charMatcher: CharMatcher = CharMatcher.inRange('a', 'z')
+    .or(CharMatcher.inRange('A', 'Z'))
+    .or(CharMatcher.is('_'))
+    .or(CharMatcher.is('-'))
+    .or(CharMatcher.is('='))
+
+  implicit val validateTopic: Validate.Plain[String, PushTopicConstraint] =
+    Validate.fromPredicate(s => s.nonEmpty && s.length <= 32 && 
charMatcher.matchesAllOf(s),
+      s => s"'$s' contains some invalid characters. Should be [#a-zA-Z] and no 
longer than 32 chars",

Review comment:
       ```suggestion
         s => s"'$s' contains some invalid characters. Should use base64 
alphabet and be no longer than 32 chars",
   ```

##########
File path: 
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/push_subscription/PushRequest.scala
##########
@@ -0,0 +1,105 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ * http://www.apache.org/licenses/LICENSE-2.0                   *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.push_subscription
+
+import com.google.common.base.CharMatcher
+import eu.timepit.refined
+import eu.timepit.refined.api.{Refined, Validate}
+import org.apache.james.jmap.push_subscription.TTL.PushTTL
+import org.apache.james.jmap.push_subscription.Topic.PushTopic
+
+object PushUrgency {
+  def default: PushUrgency = Normal
+}
+
+sealed trait PushUrgency {
+  def value: String
+}
+
+case object Low extends PushUrgency {
+  val value: String = "LOW"
+}
+
+case object VeryLow extends PushUrgency {
+  val value: String = "VERY_LOW"
+}
+
+case object Normal extends PushUrgency {
+  val value: String = "NORMAL"
+}
+
+case object High extends PushUrgency {
+  val value: String = "HIGH"
+}
+
+object Topic {
+  type PushTopic = String Refined PushTopicConstraint
+  private val charMatcher: CharMatcher = CharMatcher.inRange('a', 'z')
+    .or(CharMatcher.inRange('A', 'Z'))
+    .or(CharMatcher.is('_'))
+    .or(CharMatcher.is('-'))
+    .or(CharMatcher.is('='))
+
+  implicit val validateTopic: Validate.Plain[String, PushTopicConstraint] =
+    Validate.fromPredicate(s => s.nonEmpty && s.length <= 32 && 
charMatcher.matchesAllOf(s),
+      s => s"'$s' contains some invalid characters. Should be [#a-zA-Z] and no 
longer than 32 chars",
+      PushTopicConstraint())
+
+  def validate(string: String): Either[IllegalArgumentException, PushTopic] =
+    refined.refineV[PushTopicConstraint](string)
+      .left
+      .map(new IllegalArgumentException(_))
+
+  def from(string: String): Option[Topic] = validate(string)
+    .map(Topic(_)).toOption
+
+}
+
+case class Topic(value: PushTopic)
+
+object TTL {
+  type PushTTL = Long Refined PushTTLConstraint
+
+  implicit val validateTTL: Validate.Plain[Long, PushTTLConstraint] =
+    Validate.fromPredicate(s => s >= 0 && s < 2147483648L,
+      s => s"'$s' invalid. Should be non-negative numeric and no greater than 
2147483648",
+      PushTTLConstraint())
+
+  def validate(value: Long): Either[IllegalArgumentException, PushTTL] =
+    refined.refineV[PushTTLConstraint](value)
+      .left
+      .map(new IllegalArgumentException(_))
+
+  def from(value: Long): Option[TTL] =
+    validate(value)
+      .map(TTL(_))
+      .toOption
+}
+
+case class TTL(value: PushTTL)

Review comment:
       What does the case class adds compared to the refined type? Can't we 
have just the refined type?

##########
File path: 
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/push_subscription/PushRequest.scala
##########
@@ -0,0 +1,105 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ * http://www.apache.org/licenses/LICENSE-2.0                   *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.push_subscription
+
+import com.google.common.base.CharMatcher
+import eu.timepit.refined
+import eu.timepit.refined.api.{Refined, Validate}
+import org.apache.james.jmap.push_subscription.TTL.PushTTL
+import org.apache.james.jmap.push_subscription.Topic.PushTopic
+
+object PushUrgency {
+  def default: PushUrgency = Normal
+}
+
+sealed trait PushUrgency {
+  def value: String
+}
+
+case object Low extends PushUrgency {
+  val value: String = "LOW"
+}
+
+case object VeryLow extends PushUrgency {
+  val value: String = "VERY_LOW"
+}
+
+case object Normal extends PushUrgency {
+  val value: String = "NORMAL"
+}
+
+case object High extends PushUrgency {
+  val value: String = "HIGH"
+}
+
+object Topic {
+  type PushTopic = String Refined PushTopicConstraint
+  private val charMatcher: CharMatcher = CharMatcher.inRange('a', 'z')
+    .or(CharMatcher.inRange('A', 'Z'))
+    .or(CharMatcher.is('_'))
+    .or(CharMatcher.is('-'))
+    .or(CharMatcher.is('='))
+
+  implicit val validateTopic: Validate.Plain[String, PushTopicConstraint] =
+    Validate.fromPredicate(s => s.nonEmpty && s.length <= 32 && 
charMatcher.matchesAllOf(s),
+      s => s"'$s' contains some invalid characters. Should be [#a-zA-Z] and no 
longer than 32 chars",
+      PushTopicConstraint())
+
+  def validate(string: String): Either[IllegalArgumentException, PushTopic] =
+    refined.refineV[PushTopicConstraint](string)
+      .left
+      .map(new IllegalArgumentException(_))
+
+  def from(string: String): Option[Topic] = validate(string)
+    .map(Topic(_)).toOption
+
+}
+
+case class Topic(value: PushTopic)
+
+object TTL {
+  type PushTTL = Long Refined PushTTLConstraint
+
+  implicit val validateTTL: Validate.Plain[Long, PushTTLConstraint] =
+    Validate.fromPredicate(s => s >= 0 && s < 2147483648L,
+      s => s"'$s' invalid. Should be non-negative numeric and no greater than 
2147483648",
+      PushTTLConstraint())
+
+  def validate(value: Long): Either[IllegalArgumentException, PushTTL] =
+    refined.refineV[PushTTLConstraint](value)
+      .left
+      .map(new IllegalArgumentException(_))
+
+  def from(value: Long): Option[TTL] =
+    validate(value)
+      .map(TTL(_))
+      .toOption
+}
+
+case class TTL(value: PushTTL)
+
+case class PushTopicConstraint()
+
+case class PushTTLConstraint()

Review comment:
       Why is this needed? Can we remove it?

##########
File path: 
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/push_subscription/WebPushClient.scala
##########
@@ -0,0 +1,115 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ * http://www.apache.org/licenses/LICENSE-2.0                   *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.push_subscription
+
+import io.netty.buffer.Unpooled
+import io.netty.handler.codec.http.HttpResponseStatus
+import 
org.apache.james.jmap.push_subscription.DefaultWebPushClient.buildHttpClient
+import 
org.apache.james.jmap.push_subscription.WebPushClientHeader.{DEFAULT_TIMEOUT, 
MESSAGE_URGENCY, TIME_TO_LIVE, TOPIC}
+import org.reactivestreams.Publisher
+import reactor.core.scala.publisher.SMono
+import reactor.netty.http.client.{HttpClient, HttpClientResponse}
+import reactor.netty.resources.ConnectionProvider
+import reactor.util.retry.Retry
+
+import java.net.URL
+import java.time.Duration
+import java.time.temporal.ChronoUnit
+import scala.util.Try
+
+trait WebPushClient {
+  def push(pushServerUrl: PushSubscriptionServerURL, request: PushRequest): 
Publisher[Unit]
+}
+
+case class PushClientConfiguration(maxTimeoutSeconds: Option[Int],
+                                   maxConnections: Option[Int],
+                                   maxRetryTimes: Option[Int],
+                                   requestPerSeconds: Option[Int],
+                                   scheduler: reactor.core.scheduler.Scheduler)
+
+object WebPushClientHeader {
+  val TIME_TO_LIVE: String = "TTL"
+  val MESSAGE_URGENCY: String = "Urgency"
+  val TOPIC: String = "Topic"
+  val DEFAULT_TIMEOUT: Duration = Duration.of(30, ChronoUnit.SECONDS)
+}
+
+object PushSubscriptionServerURL {
+  def from(value: String): Try[PushSubscriptionServerURL] = 
Try(PushSubscriptionServerURL(new URL(value)))
+}
+
+case class PushSubscriptionServerURL(value: URL)
+
+sealed abstract class WebPushException(message: String) extends 
RuntimeException(message)
+
+case class WebPushInvalidRequestException() extends WebPushException("Bad 
request when call to Push Server")
+
+case class WebPushTemporarilyUnavailableException() extends 
WebPushException("Error when call to Push Server")

Review comment:
       We need to cary over more details if available...
   
   
https://developers.google.com/web/fundamentals/push-notifications/common-issues-and-reporting-bugs
   
   I think we should try to get the overall JSON payload of the response as 
test and log it.
   
   Bonus points for parsing the JSON and getting the message field <3

##########
File path: 
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/push_subscription/PushRequest.scala
##########
@@ -0,0 +1,105 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ * http://www.apache.org/licenses/LICENSE-2.0                   *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.push_subscription
+
+import com.google.common.base.CharMatcher
+import eu.timepit.refined
+import eu.timepit.refined.api.{Refined, Validate}
+import org.apache.james.jmap.push_subscription.TTL.PushTTL
+import org.apache.james.jmap.push_subscription.Topic.PushTopic
+
+object PushUrgency {
+  def default: PushUrgency = Normal
+}
+
+sealed trait PushUrgency {
+  def value: String
+}
+
+case object Low extends PushUrgency {
+  val value: String = "LOW"
+}
+
+case object VeryLow extends PushUrgency {
+  val value: String = "VERY_LOW"
+}
+
+case object Normal extends PushUrgency {
+  val value: String = "NORMAL"
+}
+
+case object High extends PushUrgency {
+  val value: String = "HIGH"
+}
+
+object Topic {
+  type PushTopic = String Refined PushTopicConstraint
+  private val charMatcher: CharMatcher = CharMatcher.inRange('a', 'z')
+    .or(CharMatcher.inRange('A', 'Z'))
+    .or(CharMatcher.is('_'))
+    .or(CharMatcher.is('-'))
+    .or(CharMatcher.is('='))
+
+  implicit val validateTopic: Validate.Plain[String, PushTopicConstraint] =
+    Validate.fromPredicate(s => s.nonEmpty && s.length <= 32 && 
charMatcher.matchesAllOf(s),
+      s => s"'$s' contains some invalid characters. Should be [#a-zA-Z] and no 
longer than 32 chars",
+      PushTopicConstraint())
+
+  def validate(string: String): Either[IllegalArgumentException, PushTopic] =
+    refined.refineV[PushTopicConstraint](string)
+      .left
+      .map(new IllegalArgumentException(_))
+
+  def from(string: String): Option[Topic] = validate(string)
+    .map(Topic(_)).toOption
+
+}
+
+case class Topic(value: PushTopic)

Review comment:
       What does the case class adds compared to the refined type? Can't we 
have just the refined type?

##########
File path: 
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/push_subscription/PushRequest.scala
##########
@@ -0,0 +1,105 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ * http://www.apache.org/licenses/LICENSE-2.0                   *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.push_subscription
+
+import com.google.common.base.CharMatcher
+import eu.timepit.refined
+import eu.timepit.refined.api.{Refined, Validate}
+import org.apache.james.jmap.push_subscription.TTL.PushTTL
+import org.apache.james.jmap.push_subscription.Topic.PushTopic
+
+object PushUrgency {
+  def default: PushUrgency = Normal
+}
+
+sealed trait PushUrgency {
+  def value: String
+}
+
+case object Low extends PushUrgency {
+  val value: String = "LOW"
+}
+
+case object VeryLow extends PushUrgency {
+  val value: String = "VERY_LOW"
+}
+
+case object Normal extends PushUrgency {
+  val value: String = "NORMAL"
+}
+
+case object High extends PushUrgency {
+  val value: String = "HIGH"
+}
+
+object Topic {
+  type PushTopic = String Refined PushTopicConstraint
+  private val charMatcher: CharMatcher = CharMatcher.inRange('a', 'z')
+    .or(CharMatcher.inRange('A', 'Z'))
+    .or(CharMatcher.is('_'))
+    .or(CharMatcher.is('-'))
+    .or(CharMatcher.is('='))
+
+  implicit val validateTopic: Validate.Plain[String, PushTopicConstraint] =
+    Validate.fromPredicate(s => s.nonEmpty && s.length <= 32 && 
charMatcher.matchesAllOf(s),
+      s => s"'$s' contains some invalid characters. Should be [#a-zA-Z] and no 
longer than 32 chars",
+      PushTopicConstraint())
+
+  def validate(string: String): Either[IllegalArgumentException, PushTopic] =
+    refined.refineV[PushTopicConstraint](string)
+      .left
+      .map(new IllegalArgumentException(_))
+
+  def from(string: String): Option[Topic] = validate(string)
+    .map(Topic(_)).toOption
+
+}
+
+case class Topic(value: PushTopic)
+
+object TTL {
+  type PushTTL = Long Refined PushTTLConstraint
+
+  implicit val validateTTL: Validate.Plain[Long, PushTTLConstraint] =
+    Validate.fromPredicate(s => s >= 0 && s < 2147483648L,
+      s => s"'$s' invalid. Should be non-negative numeric and no greater than 
2147483648",

Review comment:
       ```suggestion
         s => s"'$s' invalid. Should be non-negative numeric and no greater 
than 2^31",
   ```

##########
File path: 
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/push_subscription/PushRequest.scala
##########
@@ -0,0 +1,105 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ * http://www.apache.org/licenses/LICENSE-2.0                   *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.push_subscription
+
+import com.google.common.base.CharMatcher
+import eu.timepit.refined
+import eu.timepit.refined.api.{Refined, Validate}
+import org.apache.james.jmap.push_subscription.TTL.PushTTL
+import org.apache.james.jmap.push_subscription.Topic.PushTopic
+
+object PushUrgency {
+  def default: PushUrgency = Normal
+}
+
+sealed trait PushUrgency {
+  def value: String
+}
+
+case object Low extends PushUrgency {
+  val value: String = "LOW"
+}
+
+case object VeryLow extends PushUrgency {
+  val value: String = "VERY_LOW"
+}
+
+case object Normal extends PushUrgency {
+  val value: String = "NORMAL"
+}
+
+case object High extends PushUrgency {
+  val value: String = "HIGH"
+}

Review comment:
       Urgency test values are lower case: 
https://datatracker.ietf.org/doc/html/rfc8030#section-5.3
   
   ```
      Urgency = urgency-option
      urgency-option = ("very-low" / "low" / "normal" / "high")
   
      In order of increasing urgency:
   
      +----------+-----------------------------+--------------------------+
      | Urgency  | Device State                | Example Application      |
      |          |                             | Scenario                 |
      +----------+-----------------------------+--------------------------+
      | very-low | On power and Wi-Fi          | Advertisements           |
      | low      | On either power or Wi-Fi    | Topic updates            |
      | normal   | On neither power nor Wi-Fi  | Chat or Calendar Message |
      | high     | Low battery                 | Incoming phone call or   |
      |          |                             | time-sensitive alert     |
      +----------+-----------------------------+--------------------------+
   ```
   
   ```suggestion
   case object Low extends PushUrgency {
     val value: String = "low"
   }
   
   case object VeryLow extends PushUrgency {
     val value: String = "very-low"
   }
   
   case object Normal extends PushUrgency {
     val value: String = "normal"
   }
   
   case object High extends PushUrgency {
     val value: String = "high"
   }
   ```

##########
File path: 
server/protocols/jmap-rfc-8621/src/test/java/org/apache/james/jmap/push_subscription/DefaultWebPushClientTest.java
##########
@@ -0,0 +1,49 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ * http://www.apache.org/licenses/LICENSE-2.0                   *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.push_subscription;
+
+import java.net.URL;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+public class DefaultWebPushClientTest implements WebPushClientContract {

Review comment:
       I do not get the benefit of splitting the test class and the contract as 
we will have only one implementation...

##########
File path: 
server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/push_subscription/WebPushClient.scala
##########
@@ -0,0 +1,115 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ * http://www.apache.org/licenses/LICENSE-2.0                   *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.push_subscription
+
+import io.netty.buffer.Unpooled
+import io.netty.handler.codec.http.HttpResponseStatus
+import 
org.apache.james.jmap.push_subscription.DefaultWebPushClient.buildHttpClient
+import 
org.apache.james.jmap.push_subscription.WebPushClientHeader.{DEFAULT_TIMEOUT, 
MESSAGE_URGENCY, TIME_TO_LIVE, TOPIC}
+import org.reactivestreams.Publisher
+import reactor.core.scala.publisher.SMono
+import reactor.netty.http.client.{HttpClient, HttpClientResponse}
+import reactor.netty.resources.ConnectionProvider
+import reactor.util.retry.Retry
+
+import java.net.URL
+import java.time.Duration
+import java.time.temporal.ChronoUnit
+import scala.util.Try
+
+trait WebPushClient {
+  def push(pushServerUrl: PushSubscriptionServerURL, request: PushRequest): 
Publisher[Unit]
+}
+
+case class PushClientConfiguration(maxTimeoutSeconds: Option[Int],
+                                   maxConnections: Option[Int],
+                                   maxRetryTimes: Option[Int],
+                                   requestPerSeconds: Option[Int],
+                                   scheduler: reactor.core.scheduler.Scheduler)
+
+object WebPushClientHeader {
+  val TIME_TO_LIVE: String = "TTL"
+  val MESSAGE_URGENCY: String = "Urgency"
+  val TOPIC: String = "Topic"
+  val DEFAULT_TIMEOUT: Duration = Duration.of(30, ChronoUnit.SECONDS)
+}
+
+object PushSubscriptionServerURL {
+  def from(value: String): Try[PushSubscriptionServerURL] = 
Try(PushSubscriptionServerURL(new URL(value)))
+}
+
+case class PushSubscriptionServerURL(value: URL)
+
+sealed abstract class WebPushException(message: String) extends 
RuntimeException(message)
+
+case class WebPushInvalidRequestException() extends WebPushException("Bad 
request when call to Push Server")
+
+case class WebPushTemporarilyUnavailableException() extends 
WebPushException("Error when call to Push Server")
+
+object DefaultWebPushClient {
+
+  private def buildHttpClient(configuration: PushClientConfiguration): 
HttpClient = {
+    val connectionProviderBuilder: ConnectionProvider.Builder = 
ConnectionProvider.builder(DefaultWebPushClient.getClass.getName)
+    configuration.maxConnections.foreach(configValue => 
connectionProviderBuilder.maxConnections(configValue))
+    configuration.maxTimeoutSeconds.foreach(configValue => 
connectionProviderBuilder.pendingAcquireMaxCount(configValue))
+
+    val responseTimeout: Duration = configuration.maxTimeoutSeconds
+      .map(configValue => Duration.of(configValue, ChronoUnit.SECONDS))
+      .getOrElse(DEFAULT_TIMEOUT)
+
+    HttpClient.create(connectionProviderBuilder.build())
+      .responseTimeout(responseTimeout)
+      .headers(builder => {
+        builder.add("Content-Type", "application/json charset=utf-8")
+      })
+  }
+}
+
+class DefaultWebPushClient(configuration: PushClientConfiguration) extends 
WebPushClient {
+
+  val httpClient: HttpClient = buildHttpClient(configuration)
+
+  override def push(pushServerUrl: PushSubscriptionServerURL, request: 
PushRequest): Publisher[Unit] =
+    httpClient
+      .headers(builder => {
+        builder.add(TIME_TO_LIVE, request.ttl.value.value)
+        builder.add(MESSAGE_URGENCY, 
request.urgency.getOrElse(PushUrgency.default).value)
+        request.topic.foreach(t => builder.add(TOPIC, t.value.value))
+      })
+      .post()
+      .uri(pushServerUrl.value.toString)
+      .send(SMono.just(Unpooled.wrappedBuffer(request.payload)))
+      .response()
+      .doOnNext(httpResponse => afterHTTPResponseHandler(httpResponse))
+//      .retryWhen(retrySpec)

Review comment:
       Let's never retry as JMAP sync is intrinsequely resilient to message 
loss. (remove the comment)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to