Created
July 1, 2013 14:46
-
-
Save awartoft/5901457 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.Organisation; | |
import com.cyant.chat.event.ErrorEvent; | |
import com.cyant.chat.user.CustomerUser; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.github.fge.jackson.JsonLoader; | |
import org.atmosphere.cache.UUIDBroadcasterCache; | |
import org.atmosphere.client.TrackMessageSizeInterceptor; | |
import org.atmosphere.config.service.AtmosphereHandlerService; | |
import org.atmosphere.cpr.AtmosphereResponse; | |
import org.atmosphere.interceptor.AtmosphereResourceLifecycleInterceptor; | |
import org.atmosphere.interceptor.BroadcastOnPostAtmosphereInterceptor; | |
import org.atmosphere.interceptor.HeartbeatInterceptor; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.io.IOException; | |
@AtmosphereHandlerService( | |
path="/customer", | |
broadcasterCache = UUIDBroadcasterCache.class, | |
interceptors = { | |
AtmosphereResourceLifecycleInterceptor.class, | |
BroadcastOnPostAtmosphereInterceptor.class, | |
TrackMessageSizeInterceptor.class, | |
HeartbeatInterceptor.class | |
} | |
) | |
public class Customer extends AbstractProtocol | |
{ | |
private final Logger logger = LoggerFactory.getLogger(Operator.class); | |
/** | |
* Events that can occur from the user | |
*/ | |
private enum Event { | |
INIT, | |
MESSAGE, | |
TYPE, | |
OPEN_CONVERSATION, | |
CLOSE_CONVERSATION, | |
MINIMIZE_CONVERSATION, | |
} | |
@Override | |
public void onMessage(AtmosphereResponse response, String message) throws IOException | |
{ | |
logger.debug("CustomerProtocol::onMessage", message); | |
JsonNode json = JsonLoader.fromString(message); | |
try { | |
Event event = Event.valueOf(json.get("event").asText().toUpperCase()); | |
JsonNode data = json.get("data"); | |
switch (event) | |
{ | |
case INIT: | |
initialize(response, data); | |
break; | |
} | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Create a new customer user and add them to an organisation | |
* | |
* @param response user response object | |
* @param data event data | |
*/ | |
private void initialize(AtmosphereResponse response, JsonNode data) | |
{ | |
final int appId = data.get("appId").asInt(); | |
Organisation organisation = kernel.getOrganisation(appId); | |
if (organisation == null) { | |
response.write(ErrorEvent.organisationNotFound()); | |
return; | |
} | |
try { | |
ObjectMapper mapper = new ObjectMapper(); | |
CustomerUser user = mapper.readValue(data.get("data").traverse(), CustomerUser.class); | |
user.setResource(response.resource()); | |
organisation.addCustomer(user); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment