package gx.realtime;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;

import gx.browserchannel.message.DataMessage;
import gx.browserchannel.message.MessageEvent;
import gx.browserchannel.message.MessageHandler;

public class RealtimeMessageHandler implements MessageHandler {

    @Override
    public void receive(MessageEvent e) {
        // Return on invalid data
        if(!(e.getMessage() instanceof DataMessage))
            return;

        // Parse data as event
        DataMessage data = (DataMessage) e.getMessage();
        JsonParser parser = data.getContent().traverse();
        parser.setCodec(new ObjectMapper());
        Event event = null;
        try {
            event = parser.readValueAs(Event.class);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        // Handle events
        if(event instanceof CollaboratorJoinedEvent) {
            Collaborator user = ((CollaboratorJoinedEvent)event).getCollaborator();
            System.out.println("Collaborator joined event: " + user.getDisplayName());
        }
    }
}

@@ -260,325 +260,1082 @@
 er;%0A
-%0Apublic class RealtimeMessageHandler implements MessageHandler %7B%0A%0A    @Override%0A    public void receive(MessageEvent e) %7B%0A        // Return on invalid data%0A        if(!(e.getMessage() instanceof DataMessage))%0A            return;%0A%0A        // Parse data as event%0A        DataMessage data = (DataMessage) e.getMessage();
+import org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0A%0Apublic class RealtimeMessageHandler implements MessageHandler %7B%0A%0A    private static Logger logger = LogManager.getLogger(MessageHandler.class);%0A%0A    @Override%0A    public void receive(MessageEvent e) %7B%0A        logger.debug(%22Received messageEvent %7B%7D%22, e);%0A        logger.debug(%22with message %7B%7D%22, e.getMessage());%0A%0A        // Return on invalid data%0A        if(!(e.getMessage() instanceof DataMessage))%0A            return;%0A%0A        // Parse data as event%0A        DataMessage data = (DataMessage) e.getMessage();%0A        Event event = parseDataMessage(data);%0A%0A        // Handle events%0A        if(event instanceof CollaboratorJoinedEvent) %7B%0A            Collaborator user = ((CollaboratorJoinedEvent)event).getCollaborator();%0A            System.out.println(%22Collaborator joined event: %22 + user.getDisplayName());%0A        %7D%0A    %7D%0A%0A    /**%0A     * Parses a given DataMessage into an Event object.%0A     * @param data%0A     * @return%0A     */%0A    protected Event parseDataMessage(DataMessage data)%0A    %7B
 %0A   
@@ -1606,25 +1606,24 @@
       %7D%0A
-%0A
         
 // Handl
@@ -1618,265 +1618,27 @@
     
-// Handle events%0A        if(event instanceof CollaboratorJoinedEvent) %7B%0A            Collaborator user = ((CollaboratorJoinedEvent)event).getCollaborator();%0A            System.out.println(%22Collaborator joined event: %22 + user.getDisplayName());%0A        %7D%0A    %7D
+return event;%0A    %7D%0A
 %0A%7D%0A

Split up the receive method into a message parser and handler for easier testing