Created
July 1, 2013 14:45
-
-
Save awartoft/5901450 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.OperatorUser; | |
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.config.service.ManagedService; | |
import org.atmosphere.cpr.AtmosphereResource; | |
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="/operator", | |
broadcasterCache = UUIDBroadcasterCache.class, | |
interceptors = { | |
AtmosphereResourceLifecycleInterceptor.class, | |
BroadcastOnPostAtmosphereInterceptor.class, | |
TrackMessageSizeInterceptor.class, | |
HeartbeatInterceptor.class | |
} | |
) | |
public class Operator extends AbstractProtocol | |
{ | |
private final Logger logger = LoggerFactory.getLogger(Operator.class); | |
private enum Event | |
{ | |
INIT, | |
OPEN_CONVERSATION, | |
CLOSE_CONVERSATION | |
} | |
@Override | |
public void onMessage(AtmosphereResponse response, String message) throws IOException | |
{ | |
JsonNode json = JsonLoader.fromString(message); | |
try { | |
final Event event = Event.valueOf(json.get("event").asText().toUpperCase()); | |
switch (event) | |
{ | |
case INIT: | |
initialize(response, json.get("data")); | |
break; | |
case OPEN_CONVERSATION: | |
// todo: implement | |
break; | |
case CLOSE_CONVERSATION: | |
// todo: implement | |
break; | |
} | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void initialize(AtmosphereResponse response, JsonNode data) throws IOException | |
{ | |
final int appId = data.get("appId").asInt(); | |
final String appSecret = data.get("appSecret").asText(); | |
final Organisation organisation = kernel.getOrganisation(appId); | |
if (organisation == null) { | |
response.write(new ErrorEvent("Organisation was not found").toString()); | |
return; | |
} | |
ObjectMapper mapper = new ObjectMapper(); | |
OperatorUser operator = mapper.readValue(data.get("data").traverse(), OperatorUser.class); | |
operator.setResource(response.resource()); | |
organisation.addOperator(operator); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment