Skip to content

Commit

Permalink
ISSUE-1240 Use JMAPExtensionConfiguration to handle calendar reply la…
Browse files Browse the repository at this point in the history
…nguages
  • Loading branch information
quantranhong1999 authored and vttranlina committed Oct 29, 2024
1 parent 9d1579d commit 97ab5a8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.linagora.tmail.james.jmap

import com.linagora.tmail.james.jmap.JMAPExtensionConfiguration.{PUBLIC_ASSET_TOTAL_SIZE_LIMIT_DEFAULT, TICKET_IP_VALIDATION_ENABLED}
import java.util.Locale

import com.linagora.tmail.james.jmap.JMAPExtensionConfiguration.{CALENDAR_EVENT_REPLY_SUPPORTED_LANGUAGES_DEFAULT, PUBLIC_ASSET_TOTAL_SIZE_LIMIT_DEFAULT, TICKET_IP_VALIDATION_ENABLED}
import com.linagora.tmail.james.jmap.method.CalendarEventReplySupportedLanguage.LANGUAGE_DEFAULT
import com.linagora.tmail.james.jmap.model.LanguageLocation
import eu.timepit.refined
import org.apache.commons.configuration2.Configuration
import org.apache.james.core.MailAddress
import org.apache.james.jmap.core.UnsignedInt.{UnsignedInt, UnsignedIntConstraint}
import org.apache.james.server.core.MissingArgumentException
import org.apache.james.util.Size

import scala.util.{Failure, Success, Try}
Expand All @@ -14,6 +19,8 @@ object JMAPExtensionConfiguration {
val PUBLIC_ASSET_TOTAL_SIZE_LIMIT_DEFAULT: PublicAssetTotalSizeLimit = PublicAssetTotalSizeLimit.of(Size.of(20L, Size.Unit.M)).get
val TICKET_IP_VALIDATION_PROPERTY: String = "authentication.strategy.rfc8621.tickets.ip.validation.enabled"
val TICKET_IP_VALIDATION_ENABLED: TicketIpValidationEnable = TicketIpValidationEnable(true)
val CALENDAR_EVENT_REPLY_SUPPORTED_LANGUAGES_PROPERTY: String = "calendarEvent.reply.supportedLanguages"
val CALENDAR_EVENT_REPLY_SUPPORTED_LANGUAGES_DEFAULT: CalendarEventReplySupportedLanguagesConfig = CalendarEventReplySupportedLanguagesConfig(Set(LANGUAGE_DEFAULT))

val SUPPORT_MAIL_ADDRESS_PROPERTY: String = "support.mail.address"

Expand All @@ -31,7 +38,16 @@ object JMAPExtensionConfiguration {
.map(TicketIpValidationEnable(_))
.getOrElse(TICKET_IP_VALIDATION_ENABLED)

JMAPExtensionConfiguration(publicAssetTotalSizeLimit, supportMailAddressOpt, ticketIpValidationEnable)
val calendarEventReplySupportedLanguagesConfig: CalendarEventReplySupportedLanguagesConfig = CalendarEventReplySupportedLanguagesConfig(
Try(configuration.getStringArray(CALENDAR_EVENT_REPLY_SUPPORTED_LANGUAGES_PROPERTY).toSet)
.map(_.map(lgTag => LanguageLocation.detectLocale(lgTag) match {
case Success(value) => value
case Failure(error) => throw new MissingArgumentException("Invalid language tag in the configuration file." + error.getMessage)
}))
.fold(_ => Set.empty, identity))

JMAPExtensionConfiguration(publicAssetTotalSizeLimit, supportMailAddressOpt, ticketIpValidationEnable,
calendarEventReplySupportedLanguagesConfig)
}
}

Expand All @@ -44,7 +60,8 @@ object PublicAssetTotalSizeLimit {

case class JMAPExtensionConfiguration(publicAssetTotalSizeLimit: PublicAssetTotalSizeLimit = PUBLIC_ASSET_TOTAL_SIZE_LIMIT_DEFAULT,
supportMailAddress: Option[MailAddress] = Option.empty,
ticketIpValidationEnable: TicketIpValidationEnable = TICKET_IP_VALIDATION_ENABLED) {
ticketIpValidationEnable: TicketIpValidationEnable = TICKET_IP_VALIDATION_ENABLED,
calendarEventReplySupportedLanguagesConfig: CalendarEventReplySupportedLanguagesConfig = CALENDAR_EVENT_REPLY_SUPPORTED_LANGUAGES_DEFAULT) {
def this(publicAssetTotalSizeLimit: PublicAssetTotalSizeLimit) = {
this(publicAssetTotalSizeLimit, Option.empty)
}
Expand All @@ -63,3 +80,5 @@ case class PublicAssetTotalSizeLimit(value: UnsignedInt) {
}

case class TicketIpValidationEnable(value: Boolean)

case class CalendarEventReplySupportedLanguagesConfig(supportedLanguages: Set[Locale])
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import java.util.{Locale, UUID, Map => JavaMap}
import com.github.mustachejava.{DefaultMustacheFactory, MustacheFactory}
import com.google.common.base.Preconditions
import com.google.common.collect.ImmutableMap
import com.linagora.tmail.james.jmap.JMAPExtensionConfiguration
import com.linagora.tmail.james.jmap.method.CalendarEventReplyMustacheFactory.MUSTACHE_FACTORY
import com.linagora.tmail.james.jmap.method.CalendarEventReplyPerformer.{I18N_MAIL_TEMPLATE_LOCATION_DEFAULT, I18N_MAIL_TEMPLATE_LOCATION_PROPERTY, LOGGER}
import com.linagora.tmail.james.jmap.model.{AttendeeReply, CalendarAttendeeField, CalendarEndField, CalendarEventNotParsable, CalendarEventParsed, CalendarEventReplyGenerator, CalendarEventReplyRequest, CalendarEventReplyResults, CalendarLocationField, CalendarOrganizerField, CalendarParticipantsField, CalendarStartField, CalendarTitleField, InvalidCalendarFileException, LanguageLocation}
import com.linagora.tmail.james.jmap.model.{AttendeeReply, CalendarAttendeeField, CalendarEndField, CalendarEventNotParsable, CalendarEventParsed, CalendarEventReplyGenerator, CalendarEventReplyRequest, CalendarEventReplyResults, CalendarLocationField, CalendarOrganizerField, CalendarParticipantsField, CalendarStartField, CalendarTitleField, InvalidCalendarFileException}
import eu.timepit.refined.auto._
import jakarta.annotation.PreDestroy
import jakarta.inject.{Inject, Named}
Expand All @@ -32,9 +33,8 @@ import org.apache.james.lifecycle.api.{LifecycleUtil, Startable}
import org.apache.james.mailbox.MailboxSession
import org.apache.james.queue.api.MailQueueFactory.SPOOL
import org.apache.james.queue.api.{MailQueue, MailQueueFactory}
import org.apache.james.server.core.{MailImpl, MimeMessageInputStreamSource, MimeMessageWrapper, MissingArgumentException}
import org.apache.james.server.core.{MailImpl, MimeMessageInputStreamSource, MimeMessageWrapper}
import org.apache.james.user.api.UsersRepository
import org.apache.james.utils.PropertiesProvider
import org.apache.mailet.{Attribute, AttributeValue, Mail}
import org.slf4j.{Logger, LoggerFactory}
import reactor.core.scala.publisher.{SFlux, SMono}
Expand All @@ -46,7 +46,6 @@ import scala.util.{Failure, Success, Try, Using}
object CalendarEventReplyPerformer {
val I18N_MAIL_TEMPLATE_LOCATION_PROPERTY: String = "calendarEvent.reply.mailTemplateLocation"
val I18N_MAIL_TEMPLATE_LOCATION_DEFAULT: String = "file://eml-template/"
val SUPPORTED_LANGUAGES_PROPERTY: String = "calendarEvent.reply.supportedLanguages"
val LOGGER: Logger = LoggerFactory.getLogger(classOf[CalendarEventReplyPerformer])
}

Expand Down Expand Up @@ -129,25 +128,17 @@ class BlobCalendarResolver @Inject()(blobResolvers: BlobResolvers) {
}
}

private object CalendarEventReplySupportedLanguage {
object CalendarEventReplySupportedLanguage {
val LANGUAGE_DEFAULT: Locale = Locale.ENGLISH
}

class CalendarEventReplySupportedLanguage @Inject()(propertiesProvider: PropertiesProvider) {
class CalendarEventReplySupportedLanguage @Inject()(jmapExtensionConfiguration: JMAPExtensionConfiguration) {

private val supportedLanguages: Set[Locale] = getSupportedLanguagesConfiguration match {
private val supportedLanguages: Set[Locale] = jmapExtensionConfiguration.calendarEventReplySupportedLanguagesConfig.supportedLanguages match {
case supportedLanguages if supportedLanguages.isEmpty => Set(CalendarEventReplySupportedLanguage.LANGUAGE_DEFAULT)
case supportedLanguages => supportedLanguages
}

private def getSupportedLanguagesConfiguration: Set[Locale] =
Try(propertiesProvider.getConfiguration("jmap"))
.map(configuration => configuration.getStringArray(CalendarEventReplyPerformer.SUPPORTED_LANGUAGES_PROPERTY).toSet)
.map(_.map(lgTag => LanguageLocation.detectLocale(lgTag) match {
case Success(value) => value
case Failure(error) => throw new MissingArgumentException("Invalid language tag in the configuration file." + error.getMessage)
})).fold(_ => Set.empty, identity)

def value: Set[Locale] = supportedLanguages

def valueAsStringSet: Set[String] = supportedLanguages.map(_.getLanguage)
Expand Down

0 comments on commit 97ab5a8

Please sign in to comment.