-
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 (Conversation/ContactMessage): Synch ContactMessage with upda…
…ted specs
- Loading branch information
Showing
54 changed files
with
1,389 additions
and
753 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
92 changes: 86 additions & 6 deletions
92
...ain/com/sinch/sdk/domains/conversation/api/v1/adapters/messages/ContactMessageMapper.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 |
---|---|---|
@@ -1,38 +1,118 @@ | ||
package com.sinch.sdk.domains.conversation.api.v1.adapters.messages; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
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.messages.ContactMessage; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.ContactMessage.Builder; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.ContactMessageBody; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.ContactMessageImpl; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.internal.ContactMessageInternal; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.internal.ContactMessageInternalImpl; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.types.channelspecific.ChannelSpecificContactMessage; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.types.choiceresponse.ChoiceResponseMessage; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.types.fallback.FallbackMessage; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.types.location.LocationMessage; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.types.media.MediaMessage; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.types.mediacard.MediaCardMessage; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.types.productresponse.ProductResponseMessage; | ||
import com.sinch.sdk.domains.conversation.models.v1.messages.types.text.TextMessage; | ||
import java.io.IOException; | ||
import java.util.logging.Logger; | ||
|
||
public class ContactMessageMapper { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(ContactMessageMapper.class.getName()); | ||
|
||
public static void initMapper() { | ||
SimpleModule module = | ||
new SimpleModule().addDeserializer(ContactMessage.class, new Deserializer()); | ||
new SimpleModule() | ||
.addSerializer(ContactMessage.class, new Serializer()) | ||
.addDeserializer(ContactMessage.class, new Deserializer()); | ||
Mapper.getInstance().registerModule(module); | ||
} | ||
|
||
static class Deserializer extends StdDeserializer<ContactMessage> { | ||
static class Deserializer extends StdDeserializer<ContactMessage<?>> { | ||
|
||
public Deserializer() { | ||
this(null); | ||
} | ||
|
||
public Deserializer(Class<ContactMessage> vc) { | ||
public Deserializer(Class<ContactMessage<?>> vc) { | ||
super(vc); | ||
} | ||
|
||
@Override | ||
public ContactMessage deserialize(JsonParser jp, DeserializationContext ctxt) | ||
public ContactMessage<?> deserialize(JsonParser jp, DeserializationContext ctxt) | ||
throws IOException { | ||
|
||
Object deserialized = jp.readValueAs(ContactMessageInternalImpl.class).getActualInstance(); | ||
return (ContactMessage) deserialized; | ||
ContactMessageInternalImpl deserialized = | ||
(ContactMessageInternalImpl) jp.readValueAs(ContactMessageInternal.class); | ||
Builder<ContactMessageBody> internal = ContactMessage.builder(); | ||
|
||
deserialized.channelSpecificMessage().ifPresent(internal::setBody); | ||
deserialized.choiceResponseMessage().ifPresent(internal::setBody); | ||
deserialized.fallbackMessage().ifPresent(internal::setBody); | ||
deserialized.locationMessage().ifPresent(internal::setBody); | ||
deserialized.mediaCardMessage().ifPresent(internal::setBody); | ||
deserialized.mediaMessage().ifPresent(internal::setBody); | ||
deserialized.productResponseMessage().ifPresent(internal::setBody); | ||
deserialized.textMessage().ifPresent(internal::setBody); | ||
deserialized.replyTo().ifPresent(internal::setReplyTo); | ||
|
||
return internal.build(); | ||
} | ||
} | ||
|
||
static class Serializer extends StdSerializer<ContactMessage> { | ||
|
||
public Serializer() { | ||
this(null); | ||
} | ||
|
||
public Serializer(Class<ContactMessage> t) { | ||
super(t); | ||
} | ||
|
||
@Override | ||
public void serialize(ContactMessage raw, JsonGenerator jgen, SerializerProvider provider) | ||
throws IOException { | ||
|
||
ContactMessageImpl<?> value = (ContactMessageImpl<?>) raw; | ||
ContactMessageInternal.Builder internal = ContactMessageInternal.builder(); | ||
|
||
value.replyTo().ifPresent(internal::setReplyTo); | ||
value | ||
.body() | ||
.ifPresent( | ||
message -> { | ||
if (message instanceof ChannelSpecificContactMessage) { | ||
internal.setChannelSpecificMessage((ChannelSpecificContactMessage) message); | ||
} else if (message instanceof ChoiceResponseMessage) { | ||
internal.setChoiceResponseMessage((ChoiceResponseMessage) message); | ||
} else if (message instanceof FallbackMessage) { | ||
internal.setFallbackMessage((FallbackMessage) message); | ||
} else if (message instanceof LocationMessage) { | ||
internal.setLocationMessage((LocationMessage) message); | ||
} else if (message instanceof MediaCardMessage) { | ||
internal.setMediaCardMessage((MediaCardMessage) message); | ||
} else if (message instanceof MediaMessage) { | ||
internal.setMediaMessage((MediaMessage) message); | ||
} else if (message instanceof ProductResponseMessage) { | ||
internal.setProductResponseMessage((ProductResponseMessage) message); | ||
} else if (message instanceof TextMessage) { | ||
internal.setTextMessage((TextMessage) message); | ||
} else { | ||
LOGGER.severe("Unexpected class '" + message.getClass() + "'"); | ||
} | ||
}); | ||
|
||
jgen.writeObject(internal.build()); | ||
} | ||
} | ||
} |
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
56 changes: 55 additions & 1 deletion
56
client/src/main/com/sinch/sdk/domains/conversation/models/v1/messages/ContactMessage.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 |
---|---|---|
@@ -1,3 +1,57 @@ | ||
package com.sinch.sdk.domains.conversation.models.v1.messages; | ||
|
||
public interface ContactMessage extends ConversationMessageBody {} | ||
/** | ||
* Message originating from a Contact | ||
* | ||
* @param <T> Body type for message | ||
* @since _NEXT_VERSION_ | ||
*/ | ||
public interface ContactMessage<T extends ContactMessageBody> extends ConversationMessageBody { | ||
|
||
/** | ||
* Get message body | ||
* | ||
* @return Message body | ||
*/ | ||
T getBody(); | ||
|
||
/** | ||
* @see ReplyTo | ||
* @return reply to | ||
*/ | ||
ReplyTo getReplyTo(); | ||
|
||
/** | ||
* Getting Builder | ||
* | ||
* @return New Builder instance | ||
* @since 1.0 | ||
*/ | ||
static <T extends ContactMessageBody> Builder<T> builder() { | ||
return new ContactMessageImpl.Builder<>(); | ||
} | ||
|
||
/** Dedicated Builder */ | ||
interface Builder<T extends ContactMessageBody> { | ||
|
||
/** | ||
* see getter | ||
* | ||
* @param message see getter | ||
* @return Current builder | ||
* @see #getBody() | ||
*/ | ||
Builder<T> setBody(T message); | ||
|
||
/** | ||
* see getter | ||
* | ||
* @param replyTo see getter | ||
* @return Current builder | ||
* @see #getReplyTo() | ||
*/ | ||
Builder<T> setReplyTo(ReplyTo replyTo); | ||
|
||
ContactMessage<T> build(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...nt/src/main/com/sinch/sdk/domains/conversation/models/v1/messages/ContactMessageBody.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,3 @@ | ||
package com.sinch.sdk.domains.conversation.models.v1.messages; | ||
|
||
public interface ContactMessageBody {} |
57 changes: 57 additions & 0 deletions
57
...nt/src/main/com/sinch/sdk/domains/conversation/models/v1/messages/ContactMessageImpl.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,57 @@ | ||
package com.sinch.sdk.domains.conversation.models.v1.messages; | ||
|
||
import com.sinch.sdk.core.models.OptionalValue; | ||
|
||
public class ContactMessageImpl<T extends ContactMessageBody> implements ContactMessage<T> { | ||
|
||
private final OptionalValue<T> message; | ||
|
||
private final OptionalValue<ReplyTo> replyTo; | ||
|
||
public ContactMessageImpl(OptionalValue<T> message, OptionalValue<ReplyTo> replyTo) { | ||
this.message = message; | ||
this.replyTo = replyTo; | ||
} | ||
|
||
public T getBody() { | ||
return message.orElse(null); | ||
} | ||
|
||
public OptionalValue<T> body() { | ||
return message; | ||
} | ||
|
||
public ReplyTo getReplyTo() { | ||
return replyTo.orElse(null); | ||
} | ||
|
||
public OptionalValue<ReplyTo> replyTo() { | ||
return replyTo; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ContactMessageImpl{" + "message=" + message + ", replyTo=" + replyTo + '}'; | ||
} | ||
|
||
/** Dedicated Builder */ | ||
static class Builder<T extends ContactMessageBody> implements ContactMessage.Builder<T> { | ||
|
||
OptionalValue<T> message = OptionalValue.empty(); | ||
OptionalValue<ReplyTo> replyTo = OptionalValue.empty(); | ||
|
||
public Builder<T> setBody(T message) { | ||
this.message = OptionalValue.of(message); | ||
return this; | ||
} | ||
|
||
public Builder<T> setReplyTo(ReplyTo replyTo) { | ||
this.replyTo = OptionalValue.of(replyTo); | ||
return this; | ||
} | ||
|
||
public ContactMessage<T> build() { | ||
return new ContactMessageImpl<>(message, replyTo); | ||
} | ||
} | ||
} |
Oops, something went wrong.