-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature (DX): Simplify Recipient usage related to IdentifiedBy
- Loading branch information
Showing
16 changed files
with
219 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
client/src/main/com/sinch/sdk/domains/conversation/adapters/RecipientMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.sinch.sdk.domains.conversation.adapters; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import com.sinch.sdk.core.utils.databind.Mapper; | ||
import com.sinch.sdk.domains.conversation.models.v1.ChannelRecipientIdentities; | ||
import com.sinch.sdk.domains.conversation.models.v1.ContactId; | ||
import com.sinch.sdk.domains.conversation.models.v1.Recipient; | ||
import java.io.IOException; | ||
import java.util.logging.Logger; | ||
|
||
public class RecipientMapper extends StdSerializer<Recipient> { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(RecipientMapper.class.getName()); | ||
|
||
public RecipientMapper() { | ||
this(null); | ||
} | ||
|
||
public RecipientMapper(Class<Recipient> t) { | ||
super(t); | ||
} | ||
|
||
public static void initMapper() { | ||
SimpleModule module = new SimpleModule(); | ||
module.addSerializer(Recipient.class, new RecipientMapper()); | ||
Mapper.getInstance().registerModule(module); | ||
} | ||
|
||
@Override | ||
public void serialize(Recipient value, JsonGenerator jgen, SerializerProvider provider) | ||
throws IOException { | ||
|
||
/* TODO: To avoid recursive calls we should be able to add a rootName at class level (not for global mapper) | ||
* Need to be enhanced in the future: one solution https://github.com/FasterXML/jackson-databind/issues/1022 | ||
*/ | ||
ObjectMapper mapper = (ObjectMapper) jgen.getCodec(); | ||
|
||
jgen.writeStartObject(); | ||
if (value instanceof ContactId) { | ||
// hardcoded value: regression test have to be used to ensure proper validation | ||
jgen.writeFieldName("contact_id"); | ||
String stringValue = mapper.writeValueAsString(((ContactId) value).getContactId()); | ||
jgen.writeRawValue(stringValue); | ||
} else if (value instanceof ChannelRecipientIdentities) { | ||
// hardcoded value: regression test have to be used to ensure proper validation | ||
jgen.writeFieldName("identified_by"); | ||
jgen.writeStartObject(); | ||
jgen.writeFieldName("channel_identities"); | ||
String stringValue = | ||
mapper.writeValueAsString(((ChannelRecipientIdentities) value).toArray()); | ||
jgen.writeRawValue(stringValue); | ||
jgen.writeEndObject(); | ||
} else { | ||
LOGGER.severe("Unexpected class '" + value.getClass() + "'"); | ||
} | ||
jgen.writeEndObject(); | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
client/src/main/com/sinch/sdk/domains/conversation/adapters/contact/IdentifiedByMapper.java
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
client/src/main/com/sinch/sdk/domains/conversation/models/v1/ChannelRecipientIdentities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.sinch.sdk.domains.conversation.models.v1; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
public class ChannelRecipientIdentities extends ArrayList<ChannelRecipientIdentity> | ||
implements com.sinch.sdk.domains.conversation.models.v1.Recipient { | ||
|
||
public ChannelRecipientIdentities() { | ||
super(); | ||
} | ||
|
||
public ChannelRecipientIdentities(Collection<ChannelRecipientIdentity> collection) { | ||
super(collection); | ||
} | ||
|
||
public static ChannelRecipientIdentities of(ChannelRecipientIdentity... elements) { | ||
return new ChannelRecipientIdentities(Arrays.asList(elements)); | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
Collection<ChannelRecipientIdentity> identities; | ||
|
||
public Builder setRecipientIdentities(Collection<ChannelRecipientIdentity> identities) { | ||
this.identities = identities; | ||
return this; | ||
} | ||
|
||
public ChannelRecipientIdentities build() { | ||
return new ChannelRecipientIdentities(identities); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.