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

JAMES-3534 Cassandra implementation for CustomIdentityDAO #747

Merged
merged 3 commits into from Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -40,6 +40,8 @@
import org.apache.james.jmap.cassandra.change.CassandraEmailChangeModule;
import org.apache.james.jmap.cassandra.change.CassandraMailboxChangeModule;
import org.apache.james.jmap.cassandra.filtering.FilteringRuleSetDefineDTOModules;
import org.apache.james.jmap.cassandra.identity.CassandraCustomIdentityDAO;
import org.apache.james.jmap.cassandra.identity.CassandraCustomIdentityModule;
import org.apache.james.jmap.cassandra.projections.CassandraEmailQueryView;
import org.apache.james.jmap.cassandra.projections.CassandraEmailQueryViewModule;
import org.apache.james.jmap.cassandra.projections.CassandraMessageFastViewProjection;
Expand All @@ -54,7 +56,6 @@
import org.apache.james.jmap.cassandra.vacation.CassandraNotificationRegistryModule;
import org.apache.james.jmap.cassandra.vacation.CassandraVacationModule;
import org.apache.james.jmap.cassandra.vacation.CassandraVacationRepository;
import org.apache.james.jmap.memory.identity.MemoryCustomIdentityDAO;

import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
Expand All @@ -76,8 +77,8 @@ protected void configure() {
bind(CassandraVacationRepository.class).in(Scopes.SINGLETON);
bind(VacationRepository.class).to(CassandraVacationRepository.class);

bind(MemoryCustomIdentityDAO.class).in(Scopes.SINGLETON);
bind(CustomIdentityDAO.class).to(MemoryCustomIdentityDAO.class);
bind(CassandraCustomIdentityDAO.class).in(Scopes.SINGLETON);
bind(CustomIdentityDAO.class).to(CassandraCustomIdentityDAO.class);

bind(CassandraNotificationRegistry.class).in(Scopes.SINGLETON);
bind(NotificationRegistry.class).to(CassandraNotificationRegistry.class);
Expand Down Expand Up @@ -108,6 +109,7 @@ protected void configure() {
cassandraDataDefinitions.addBinding().toInstance(CassandraEmailChangeModule.MODULE);
cassandraDataDefinitions.addBinding().toInstance(UploadModule.MODULE);
cassandraDataDefinitions.addBinding().toInstance(CassandraPushSubscriptionModule.MODULE);
cassandraDataDefinitions.addBinding().toInstance(CassandraCustomIdentityModule.MODULE());

Multibinder<EventDTOModule<? extends Event, ? extends EventDTO>> eventDTOModuleBinder = Multibinder.newSetBinder(binder(), new TypeLiteral<EventDTOModule<? extends Event, ? extends EventDTO>>() {});
eventDTOModuleBinder.addBinding().toInstance(FilteringRuleSetDefineDTOModules.FILTERING_RULE_SET_DEFINED);
Expand Down
4 changes: 4 additions & 0 deletions server/data/data-jmap-cassandra/pom.xml
Expand Up @@ -141,6 +141,10 @@

<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
@@ -0,0 +1,142 @@
/** **************************************************************
* 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.cassandra.identity

import com.datastax.driver.core.querybuilder.QueryBuilder
import com.datastax.driver.core.querybuilder.QueryBuilder.{bindMarker, insertInto, select}
import com.datastax.driver.core.{BoundStatement, PreparedStatement, Row, Session, UDTValue}
import javax.inject.Inject
import org.apache.james.backends.cassandra.init.CassandraTypesProvider
import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor
import org.apache.james.core.{MailAddress, Username}
import org.apache.james.jmap.api.identity.{CustomIdentityDAO, IdentityCreationRequest, IdentityNotFoundException, IdentityUpdate}
import org.apache.james.jmap.api.model.{EmailAddress, EmailerName, HtmlSignature, Identity, IdentityId, IdentityName, MayDeleteIdentity, TextSignature}
import org.apache.james.jmap.cassandra.identity.tables.CassandraCustomIdentityTable
import org.apache.james.jmap.cassandra.identity.tables.CassandraCustomIdentityTable.{BCC, EMAIL, HTML_SIGNATURE, ID, MAY_DELETE, NAME, REPLY_TO, TABLE_NAME, TEXT_SIGNATURE, USER}
import org.apache.james.jmap.cassandra.utils.EmailAddressTupleUtil
import reactor.core.publisher.Mono
import reactor.core.scala.publisher.{SFlux, SMono}

import scala.jdk.javaapi.CollectionConverters

case class CassandraCustomIdentityDAO @Inject()(session: Session,
typesProvider: CassandraTypesProvider) extends CustomIdentityDAO {
val executor: CassandraAsyncExecutor = new CassandraAsyncExecutor(session)
val emailAddressTupleUtil: EmailAddressTupleUtil = EmailAddressTupleUtil(typesProvider)

val insertStatement: PreparedStatement = session.prepare(insertInto(TABLE_NAME)
.value(USER, bindMarker(USER))
.value(ID, bindMarker(ID))
.value(NAME, bindMarker(NAME))
.value(EMAIL, bindMarker(EMAIL))
.value(REPLY_TO, bindMarker(REPLY_TO))
.value(BCC, bindMarker(BCC))
.value(TEXT_SIGNATURE, bindMarker(TEXT_SIGNATURE))
.value(HTML_SIGNATURE, bindMarker(HTML_SIGNATURE))
.value(MAY_DELETE, bindMarker(MAY_DELETE)))

val selectAllStatement: PreparedStatement = session.prepare(select()
.from(TABLE_NAME)
.where(QueryBuilder.eq(USER, bindMarker(USER))))

val selectOneStatement: PreparedStatement = session.prepare(select()
.from(TABLE_NAME)
.where(QueryBuilder.eq(USER, bindMarker(USER)))
.and(QueryBuilder.eq(ID, bindMarker(ID))))

val deleteOneStatement: PreparedStatement = session.prepare(QueryBuilder.delete()
.from(TABLE_NAME)
.where(QueryBuilder.eq(USER, bindMarker(USER)))
.and(QueryBuilder.eq(ID, bindMarker(ID))))

override def save(user: Username, creationRequest: IdentityCreationRequest): SMono[Identity] = {
val id = IdentityId.generate
SMono.just(id)
.map(creationRequest.asIdentity)
.flatMap(identity => insert(user, identity))
}

override def list(user: Username): SFlux[Identity] =
SFlux.fromPublisher(executor.executeRows(selectAllStatement.bind().setString(USER, user.asString()))
.map(toIdentity(_)))

override def update(user: Username, identityId: IdentityId, identityUpdate: IdentityUpdate): SMono[Unit] =
SMono.fromPublisher(executor.executeSingleRow(selectOneStatement.bind().setString(USER, user.asString())
.setUUID(ID, identityId.id))
.switchIfEmpty(Mono.error(() => IdentityNotFoundException(identityId)))
.map(toIdentity)
.map(identityUpdate.update)
.flatMap(patch => insert(user, patch).`then`().asJava()))

override def delete(username: Username, ids: Seq[IdentityId]): SMono[Unit] =
SFlux.fromIterable(ids)
.flatMap(id => executor.executeVoid(deleteOneStatement.bind()
.setString(USER, username.asString())
.setUUID(ID, id.id)))
.`then`()

private def insert(username: Username, identity: Identity): SMono[Identity] = {
val insertIdentity: BoundStatement = insertStatement.bind()
.setString(USER, username.asString())
.setUUID(ID, identity.id.id)
.setString(NAME, identity.name.name)
.setString(EMAIL, identity.email.asString())
.setString(TEXT_SIGNATURE, identity.textSignature.name)
.setString(HTML_SIGNATURE, identity.htmlSignature.name)
.setBool(MAY_DELETE, identity.mayDelete.value)

identity.replyTo
.map(listEmailAddress => insertIdentity.setSet(REPLY_TO, toJavaSet(listEmailAddress)))
identity.bcc
.map(listEmailAddress => insertIdentity.setSet(BCC, toJavaSet(listEmailAddress)))

SMono.fromPublisher(executor.executeVoid(insertIdentity)
.thenReturn(identity))
}

private def toIdentity(row: Row): Identity =
Identity(IdentityId(row.getUUID(ID)),
IdentityName(row.getString(NAME)),
new MailAddress(row.getString(EMAIL)),
toReplyTo(row),
toBcc(row),
TextSignature(row.getString(TEXT_SIGNATURE)),
HtmlSignature(row.getString(HTML_SIGNATURE)),
MayDeleteIdentity(row.getBool(MAY_DELETE)))

private def toReplyTo(row: Row): Option[List[EmailAddress]] =
Option(CollectionConverters.asScala(row.getSet(REPLY_TO, classOf[UDTValue]))
.toList
.map(toEmailAddress))

private def toBcc(row: Row): Option[List[EmailAddress]] =
Option(CollectionConverters.asScala(row.getSet(BCC, classOf[UDTValue]))
.toList
.map(toEmailAddress))

private def toJavaSet(listEmailAddress: List[EmailAddress]): java.util.Set[UDTValue] =
CollectionConverters.asJava(listEmailAddress.map(emailAddress =>
emailAddressTupleUtil.createEmailAddressUDT(emailAddress.name.map(name => name.value), emailAddress.email.asString()))
.toSet)

private def toEmailAddress(udtValue: UDTValue): EmailAddress =
EmailAddress(name = Option(udtValue.getString(CassandraCustomIdentityTable.EmailAddress.NAME)).map(string => EmailerName(string)),
email = new MailAddress(udtValue.getString(CassandraCustomIdentityTable.EmailAddress.EMAIL)))
}
@@ -0,0 +1,48 @@
/** **************************************************************
* 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.cassandra.identity

import com.datastax.driver.core.DataType.{cboolean, text, uuid}
import com.datastax.driver.core.schemabuilder.SchemaBuilder
import org.apache.james.backends.cassandra.components.CassandraModule
import org.apache.james.jmap.cassandra.identity.tables.CassandraCustomIdentityTable
import org.apache.james.jmap.cassandra.identity.tables.CassandraCustomIdentityTable.{BCC, EMAIL, EMAIL_ADDRESS, EmailAddress, HTML_SIGNATURE, ID, MAY_DELETE, NAME, REPLY_TO, TEXT_SIGNATURE, USER}

object CassandraCustomIdentityModule {
val MODULE: CassandraModule = CassandraModule.builder()
.`type`(EMAIL_ADDRESS)
.statement(statement => statement
.addColumn(EmailAddress.NAME, text())
.addColumn(EmailAddress.EMAIL, text()))

.table(CassandraCustomIdentityTable.TABLE_NAME)
.comment("Hold user custom identities data following JMAP RFC-8621 Identity concept")
.statement(statement => statement
.addPartitionKey(USER, text())
.addClusteringColumn(ID, uuid())
.addColumn(NAME, text())
.addColumn(EMAIL, text())
.addUDTSetColumn(REPLY_TO, SchemaBuilder.frozen(EMAIL_ADDRESS))
.addUDTSetColumn(BCC, SchemaBuilder.frozen(EMAIL_ADDRESS))
.addColumn(TEXT_SIGNATURE, text())
.addColumn(HTML_SIGNATURE, text())
.addColumn(MAY_DELETE, cboolean()))
.build()
}
@@ -0,0 +1,39 @@
/****************************************************************
* 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.cassandra.identity.tables

object CassandraCustomIdentityTable {
val TABLE_NAME: String = "custom_identity"
val USER: String = "user"
val ID: String = "id"
val NAME: String = "name"
val EMAIL: String = "email"
val REPLY_TO: String = "reply_to"
val BCC: String = "bcc"
val TEXT_SIGNATURE: String = "text_signature"
val HTML_SIGNATURE: String = "html_signature"
val MAY_DELETE: String = "may_delete"
val EMAIL_ADDRESS: String = "email_address"

object EmailAddress {
val NAME: String = "name"
val EMAIL: String = "email"
}
}
@@ -0,0 +1,36 @@
/****************************************************************
* 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.cassandra.utils

import com.datastax.driver.core.UDTValue
import org.apache.james.backends.cassandra.init.CassandraTypesProvider
import org.apache.james.jmap.cassandra.identity.tables.CassandraCustomIdentityTable

case class EmailAddressTupleUtil(typesProvider: CassandraTypesProvider) {
def createEmailAddressUDT(name: Option[String], email: String): UDTValue = {
val value = typesProvider.getDefinedUserType(CassandraCustomIdentityTable.EMAIL_ADDRESS)
.newValue()
.setString(CassandraCustomIdentityTable.EmailAddress.EMAIL, email)

name.map(name => value.setString(CassandraCustomIdentityTable.EmailAddress.NAME, name))

value
}
}
@@ -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.cassandra.identity;

import org.apache.james.backends.cassandra.CassandraCluster;
import org.apache.james.backends.cassandra.CassandraClusterExtension;
import org.apache.james.backends.cassandra.components.CassandraModule;
import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionModule;
import org.apache.james.jmap.api.identity.CustomIdentityDAO;
import org.apache.james.jmap.api.identity.CustomIdentityDAOContract;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.RegisterExtension;

public class CassandraCustomIdentityTest implements CustomIdentityDAOContract {
private CassandraCustomIdentityDAO testee;

@RegisterExtension
static CassandraClusterExtension cassandraCluster = new CassandraClusterExtension(CassandraModule.aggregateModules(
CassandraSchemaVersionModule.MODULE,
CassandraCustomIdentityModule.MODULE()));

@BeforeEach
void setup(CassandraCluster cassandra) {
testee = new CassandraCustomIdentityDAO(cassandra.getConf(),
cassandra.getTypesProvider());
}

@Override
public CustomIdentityDAO testee() {
return testee;
}
}
Expand Up @@ -34,13 +34,13 @@ import reactor.core.scheduler.Schedulers
import scala.jdk.CollectionConverters._
import scala.util.Try

case class IdentityCreationRequest(name: IdentityName,
case class IdentityCreationRequest(name: Option[IdentityName],
email: MailAddress,
replyTo: Option[List[EmailAddress]],
bcc: Option[List[EmailAddress]],
textSignature: Option[TextSignature],
htmlSignature: Option[HtmlSignature]) {
def asIdentity(id: IdentityId): Identity = Identity(id, name, email, replyTo, bcc, textSignature.getOrElse(TextSignature.DEFAULT), htmlSignature.getOrElse(HtmlSignature.DEFAULT), mayDelete = MayDeleteIdentity(true))
def asIdentity(id: IdentityId): Identity = Identity(id, name.getOrElse(IdentityName.DEFAULT), email, replyTo, bcc, textSignature.getOrElse(TextSignature.DEFAULT), htmlSignature.getOrElse(HtmlSignature.DEFAULT), mayDelete = MayDeleteIdentity(true))
}

trait IdentityUpdate {
Expand Down
Expand Up @@ -23,6 +23,9 @@ import java.util.UUID

import org.apache.james.core.MailAddress

object IdentityName {
val DEFAULT: IdentityName = IdentityName("")
}
case class IdentityName(name: String) extends AnyVal
object TextSignature {
val DEFAULT: TextSignature = TextSignature("")
Expand Down