Commit 35049bb6 authored by Yakshit Jain's avatar Yakshit Jain
Browse files

Single Class Proposal

parent 448b3127
File added
{
"java.configuration.updateBuildConfiguration": "interactive"
}
\ No newline at end of file
File added
File added
File added
package com.crio.messaging;
public class MessageController {
// This class contains a list of methods that will be called from
// the Android app through a REST API server.
// For simplicity, imagine these functions will be called directly
// from your Android app.
// Example: If you click send message in your Android app,
// sendText() function will be called.
public static void send(Message message) {
public class AndroidHandler {
public static void sendText(Message message) {
// 1. Discard empty strings.
if (message.getTextMessageContent().isEmpty()){
try {
throw new Exception("Cannot Send Empty Message!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 3. Store the Message Safely.
save(message);
// 4.Deliver the message.
deliverText(message);
}
public static void sendImage(Message message) {
// 1. Discard empty strings.
if (message.getMessageContent().isEmpty()){
if (message.getImageMessageContent().isEmpty()){
try {
throw new Exception("Cannot send empty string");
throw new Exception("Cannot Send Empty Image!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
......@@ -17,7 +43,7 @@ public class MessageController {
save(message);
// 4.Deliver the message.
deliver(message);
deliverImage(message);
}
......@@ -27,15 +53,15 @@ public class MessageController {
System.out.println(message + " stored successfully.");
}
private static void deliver(Message message) {
private static void deliverText(Message message) {
// Logic to actually send the message to the user. It may happen through some queueing mechanism.
// Out of syllabus for this exercise :')
// # If the message is too large, don't deliver the message directly.
// For now, drop the message, in future it can be sent as a link to a storage bucket.
if(message.getMessageContentSize() > 100){
if(message.getTextMessageContentSize() > 100){
try {
throw new Exception("Message too large to send >" + message.getMessageContentSize() + " 100 bytes");
throw new Exception("Message too large to send >" + message.getTextMessageContentSize()+ " 100 bytes");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
......@@ -44,5 +70,11 @@ public class MessageController {
System.out.println(message + " delivered successfully.");
}
private static void deliverImage(Message message) {
// Logic to actually send the message to the user. It may happen through some queueing mechanism.
// Out of syllabus for this exercise :')
System.out.println("Image" + message + " delivered successfully.");
}
}
package com.crio.messaging;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
public class Main {
public static void main(String[] args) {
// Call the android/iOS Controller code to send a message.
// Call the android/iOS Handler code to send a message.
// In real world, these functions will be called from your REST API server methods.
// Don't worry about them for now - let's live in a simple world :)
// Ankur sends a message to Abhinaya
Message message1 = new Message("SENDER_Ankur","RECEIVER_Abhinaya","Hello, Have you checked out https://blog.crio.do?");
MessageController.send(message1);
// Ankur sends a text message to Abhinaya
Message message1 = new Message();
message1.setSenderId("SENDER_Ankur");
message1.setReceiverId("RECEIVER_Abhinaya");
message1.setTextMessageContent("Hello, Have you checked out https://blog.crio.do?");
AndroidHandler.sendText(message1);
// Aman sends a message to Abhiskek
Message message2 = new Message("SENDER_Aman","RECEIVER_Abhiskek","Hello, Hello, Enjoying Learn By Doing");
MessageController.send(message2);
Message message2 = new Message();
message2.setSenderId("SENDER_Aman");
message2.setReceiverId("RECEIVER_Abhiskek");
String imagePath = getFilePathFromResource("CrioLogo.png");
message2.setImageMessageContent(imageToBase64StringConversion(imagePath), "1080x1920","Some Random Image MetaData");
AndroidHandler.sendImage(message2);
}
public static String imageToBase64StringConversion(String filePath) {
byte[] fileContent;
try {
fileContent = Files.readAllBytes(Paths.get(filePath));
return Base64.getEncoder().encodeToString(fileContent);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static String getFilePathFromResource(String fileName){
return Main.class.getClassLoader().getResource(fileName).getPath();
}
}
......@@ -2,44 +2,90 @@ package com.crio.messaging;
// Message class contains all message related variables/functions.
public class Message {
//Common fields
private String messageId;
private String senderId;
private String receiverId;
private String messageContent;
private MessageType messageType;
// Some Common fields, can be extended to many more.
private MessageStatus messageStatus;
public Message(String senderId, String receiverId, String messageContent) {
this.senderId = senderId;
this.receiverId = receiverId;
this.messageContent = messageContent;
//Fields related to text message.
private String textMessageContent;
//Fields related to image message.
private String imageMessageContent;
private String imageResolution;
private String imageMetaData;
//Fields relate to voice message.
private String voiceMessageContent;
private Integer durationInSec;
private Integer voiceQualityInKbps;
public Message(){
this.messageStatus = MessageStatus.SENT;
}
public String getSenderId() {
return senderId;
}
public void setSenderId(String senderId) {
this.senderId = senderId;
}
public String getReceiverId() {
return receiverId;
}
public String getMessageContent() {
return messageContent;
public void setReceiverId(String receiverId) {
this.receiverId = receiverId;
}
public MessageStatus getMessageStatus() {
return messageStatus;
}
public void setMessageStatus(MessageStatus messageStatus) {
this.messageStatus = messageStatus;
}
public String getTextMessageContent() {
return textMessageContent;
}
public void setTextMessageContent(String textMessageContent) {
this.messageType = MessageType.TEXT;
this.textMessageContent = textMessageContent;
}
public String getImageMessageContent() {
return imageMessageContent;
}
public void setImageMessageContent(String imageMessageContent, String imageResolution, String imageMetaData) {
this.messageType = MessageType.IMAGE;
this.imageMessageContent = imageMessageContent;
this.imageResolution = imageResolution;
this.imageMetaData = imageMetaData;
}
public String getVoiceMessageContent() {
return voiceMessageContent;
}
public void setVoiceMessageContent(String voiceMessageContent, Integer durationInSec, Integer voiceQualityInKbps) {
this.messageType = MessageType.VOICE;
this.voiceMessageContent = voiceMessageContent;
this.durationInSec = durationInSec;
this.voiceQualityInKbps = voiceQualityInKbps;
}
public int getMessageContentSize(){
return messageContent.length();
public int getTextMessageContentSize(){
return textMessageContent.length();
}
@Override
public String toString() {
return "Message [messageContent=" + messageContent + ", messageId=" + messageId + ", messageStatus="
+ messageStatus + ", receiverId=" + receiverId + ", senderId=" + senderId + "]";
return "Message [messageType=" + messageType + ", durationInSec=" + durationInSec + ", imageMessageContent=" + imageMessageContent
+ ", imageMetaData=" + imageMetaData + ", imageResolution=" + imageResolution + ", messageStatus="
+ messageStatus + ", receiverId=" + receiverId + ", senderId="
+ senderId + ", textMessageContent=" + textMessageContent + ", voiceMessageContent="
+ voiceMessageContent + ", voiceQualityInKbps=" + voiceQualityInKbps + "]";
}
}
package com.crio.messaging;
public enum MessageType {
TEXT,
IMAGE,
VOICE
}
messaging/src/main/resources/CrioLogo.png

1.35 KB

Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment