You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2021/04/01 14:44:24 UTC

[GitHub] [james-project] tungtv202 commented on a change in pull request #351: JAMES-3520 Implement JMAP MDN draft - Parser

tungtv202 commented on a change in pull request #351:
URL: https://github.com/apache/james-project/pull/351#discussion_r605715059



##########
File path: server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/MDNParse.scala
##########
@@ -0,0 +1,166 @@
+/****************************************************************
+ * 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.mail
+
+import eu.timepit.refined.api.Refined
+import eu.timepit.refined.collection.NonEmpty
+import org.apache.james.jmap.core.AccountId
+import org.apache.james.jmap.mail.MDNParse._
+import org.apache.james.jmap.method.WithAccountId
+import org.apache.james.mdn.MDN
+import org.apache.james.mdn.fields.{Disposition => JavaDisposition}
+import org.apache.james.mime4j.dom.Message
+
+import scala.jdk.CollectionConverters._
+import scala.jdk.OptionConverters._
+
+object MDNParse {
+  type UnparsedBlobIdConstraint = NonEmpty
+  type UnparsedBlobId = String Refined UnparsedBlobIdConstraint
+}
+
+case class BlobIds(value: Seq[UnparsedBlobId])
+
+case class RequestTooLargeException(description: String) extends Exception
+
+case object MDNParseRequest {
+  val MAXIMUM_NUMBER_OF_BLOB_IDS: 16 = 16
+}
+
+case class MDNParseRequest(accountId: AccountId,
+                           blobIds: BlobIds) extends WithAccountId {
+
+  import MDNParseRequest._
+
+  def validate: Either[RequestTooLargeException, MDNParseRequest] = {
+    if (blobIds.value.length > MAXIMUM_NUMBER_OF_BLOB_IDS) {
+      Left(RequestTooLargeException("The number of ids requested by the client exceeds the maximum number the server is willing to process in a single method call"))
+    } else {
+      scala.Right(this)
+    }
+  }
+}
+
+case class MDNNotFound(value: Set[UnparsedBlobId]) {
+  def merge(other: MDNNotFound): MDNNotFound = MDNNotFound(this.value ++ other.value)
+}
+
+object MDNNotParsable {
+  def merge(notParsable1: MDNNotParsable, notParsable2: MDNNotParsable): MDNNotParsable = MDNNotParsable(notParsable1.value ++ notParsable2.value)
+}
+
+case class MDNNotParsable(value: Set[UnparsedBlobId]) {
+  def merge(other: MDNNotParsable): MDNNotParsable = MDNNotParsable(this.value ++ other.value)
+}
+
+object MDNDisposition {
+  def fromJava(javaDisposition: JavaDisposition): MDNDisposition =
+    MDNDisposition(actionMode = javaDisposition.getActionMode.getValue,
+      sendingMode = javaDisposition.getSendingMode.getValue,
+      `type` = javaDisposition.getType.getValue)
+}
+
+case class MDNDisposition(actionMode: String,
+                          sendingMode: String,
+                          `type`: String)
+
+case class ForEmailIdField(value: String) extends AnyVal
+
+case class SubjectField(value: String) extends AnyVal
+
+case class TextBodyField(value: String) extends AnyVal
+
+case class ReportUAField(value: String) extends AnyVal
+
+case class FinalRecipientField(value: String) extends AnyVal
+
+case class OriginalMessageIdField(value: String) extends AnyVal
+
+case class ErrorField(value: String) extends AnyVal
+
+object IncludeOriginalMessageField {
+  def default: IncludeOriginalMessageField = IncludeOriginalMessageField(false)
+}
+
+case class IncludeOriginalMessageField(value: Boolean) extends AnyVal
+
+object MDNParsed {
+  def fromMDN(mdn: MDN, message: Message): MDNParsed = {
+    val report = mdn.getReport
+    MDNParsed(forEmailId = None,
+      subject = Option(message.getSubject).map(SubjectField),
+      textBody = Some(TextBodyField(mdn.getHumanReadableText)),
+      reportingUA = report.getReportingUserAgentField
+        .map(userAgent => ReportUAField(userAgent.fieldValue()))
+        .toScala,
+      finalRecipient = FinalRecipientField(report.getFinalRecipientField.fieldValue()),
+      originalMessageId = report.getOriginalMessageIdField
+        .map(originalMessageId => OriginalMessageIdField(originalMessageId.getOriginalMessageId))
+        .toScala,
+      includeOriginalMessage = IncludeOriginalMessageField(mdn.isIncludeOriginalMessage),
+      disposition = MDNDisposition.fromJava(report.getDispositionField),
+      error = Option(report.getErrorFields.asScala
+        .map(error => ErrorField(error.getText.formatted()))
+        .toSeq)
+        .filter(error => error.nonEmpty),
+      extensionFields = Option(report.getExtensionFields.asScala
+        .map(extension => (extension.getFieldName, extension.getRawValue))
+        .toMap).filter(_.nonEmpty))

Review comment:
       Yes, It has been formated with `Ctrl + L` 




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org