Created
July 11, 2013 13:47
-
-
Save awartoft/5975583 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.cyant.chat.protocol; | |
import com.cyant.chat.CustomerEvents; | |
import com.cyant.chat.events.customer.InitiateEvent; | |
import com.cyant.chat.modal.CustomerUser; | |
import com.cyant.chat.modal.Organisation; | |
import com.cyant.chat.protocol.listener.CustomerActivity; | |
import com.cyant.chat.service.OrganisationService; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.github.fge.jackson.JsonLoader; | |
import com.google.inject.Inject; | |
import org.atmosphere.config.service.ManagedService; | |
import org.atmosphere.config.service.Post; | |
import org.atmosphere.config.service.Ready; | |
import org.atmosphere.cpr.AtmosphereResource; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.io.IOException; | |
@ManagedService(path = "/customer") | |
public class CustomerProtocol | |
{ | |
private final Logger logger = LoggerFactory.getLogger(CustomerProtocol.class); | |
private final ObjectMapper mapper = new ObjectMapper(); | |
private final OrganisationService organisationService; | |
@Inject | |
public CustomerProtocol(OrganisationService organisationService) | |
{ | |
this.organisationService = organisationService; | |
} | |
@Ready | |
public void onReady(final AtmosphereResource resource) | |
{ | |
// track users activity | |
resource.addEventListener(new CustomerActivity()); | |
} | |
@Post | |
public void onMessage(AtmosphereResource resource) throws IOException | |
{ | |
JsonNode json = JsonLoader.fromString(resource.getRequest().getReader().readLine()); | |
// parse the information | |
final String eventName = json.get("event").asText(); | |
final JsonNode data = json.get("data"); | |
final CustomerUser user = (CustomerUser) resource.session().getAttribute("user"); | |
final CustomerEvents event = CustomerEvents.valueOf(eventName.toUpperCase()); | |
switch (event) | |
{ | |
case CONNECT: | |
initialize(resource, data); | |
break; | |
} | |
} | |
/** | |
* Initialize customer | |
* | |
* Must be the first method to be called when a customer connects, sets up the customer user and connects them | |
* to the given organisation. And notifies all the operators about them. | |
* | |
* @param resource the resource used to represent the connection | |
* @param data Contains the rest of the information about the user | |
*/ | |
public void initialize(final AtmosphereResource resource, final JsonNode data) | |
{ | |
logger.debug("CustomerProtocol::initialize {}", data); | |
try { | |
InitiateEvent event = mapper.readValue(data.traverse(), InitiateEvent.class); | |
Organisation organisation = organisationService.getById(event.getAppId()); | |
if (organisation == null) { | |
throw new RuntimeException("Organisation was not found exception"); | |
} | |
CustomerUser user = organisation.getCustomer(event.getUniqueId()); | |
if (user == null) { | |
user = new CustomerUser(event.getUniqueId()); | |
organisation.addCustomer(user); | |
} | |
resource.session().setAttribute("user", user); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment