use of fi.foyt.coops.extensions.websocket.ErrorMessage in project muikku by otavanopisto.
the class CoOpsDocumentWebSocket method onMessage.
@OnMessage
public void onMessage(Reader messageReader, Session client, @PathParam("HTMLMATERIALID") String fileId, @PathParam("SESSIONID") String sessionId) throws IOException {
CoOpsSession session = coOpsSessionController.findSessionBySessionId(sessionId);
if (session == null) {
client.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Not Found"));
return;
}
if (!session.getHtmlMaterial().getId().equals(NumberUtils.createLong(fileId))) {
client.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "Session is associated with another fileId"));
return;
}
ObjectMapper objectMapper = new ObjectMapper();
try {
PatchMessage patchMessage;
try {
patchMessage = objectMapper.readValue(messageReader, PatchMessage.class);
} catch (IOException e) {
throw new CoOpsInternalErrorException(e);
}
if (patchMessage == null) {
throw new CoOpsInternalErrorException("Could not parse message");
}
if (!patchMessage.getType().equals("patch")) {
throw new CoOpsInternalErrorException("Unknown message type: " + patchMessage.getType());
}
Patch patch = patchMessage.getData();
coOpsApi.filePatch(fileId, patch.getSessionId(), patch.getRevisionNumber(), patch.getPatch(), patch.getProperties(), patch.getExtensions());
} catch (CoOpsInternalErrorException e) {
client.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, "Internal Error"));
} catch (CoOpsUsageException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 400, e.getMessage())));
} catch (CoOpsNotFoundException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 404, e.getMessage())));
} catch (CoOpsConflictException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchRejected", 409, "Conflict")));
} catch (CoOpsForbiddenException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 500, e.getMessage())));
}
}
use of fi.foyt.coops.extensions.websocket.ErrorMessage in project muikku by otavanopisto.
the class CoOpsDocumentWebSocket method onOpen.
@OnOpen
public void onOpen(final Session client, EndpointConfig endpointConfig, @PathParam("HTMLMATERIALID") String htmlMaterialId, @PathParam("SESSIONID") String sessionId) throws IOException {
synchronized (this) {
//
// TODO: RequestScope is not available on the websockets, switch to ticket system
//
// if (!sessionController.isLoggedIn()) {
// client.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Permission denied"));
// }
//
// UserEntity userEntity = sessionController.getLoggedUserEntity();
//
// EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
//
// if (environmentUser.getRole() == null || environmentUser.getRole().getArchetype() == EnvironmentRoleArchetype.STUDENT) {
// client.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Permission denied"));
// }
CoOpsSession session = coOpsSessionController.findSessionBySessionId(sessionId);
if (session == null) {
client.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Not Found"));
return;
}
if (!session.getHtmlMaterial().getId().equals(NumberUtils.createLong(htmlMaterialId))) {
client.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "Session is associated with another fileId"));
return;
}
Map<String, Session> sessions = fileClients.get(htmlMaterialId);
if (sessions == null) {
fileClients.put(htmlMaterialId, new HashMap<String, Session>());
}
fileClients.get(htmlMaterialId).put(client.getId(), client);
coOpsSessionController.updateSessionType(session, CoOpsSessionType.WS);
HtmlMaterial htmlMaterial = session.getHtmlMaterial();
Long currentRevisionNumber = htmlMaterial.getRevisionNumber();
if (session.getJoinRevision() < currentRevisionNumber) {
ObjectMapper objectMapper = new ObjectMapper();
List<Patch> patches;
try {
patches = coOpsApi.fileUpdate(session.getHtmlMaterial().getId().toString(), session.getSessionId(), session.getJoinRevision());
for (Patch patch : patches) {
sendPatch(client, patch);
}
} catch (CoOpsInternalErrorException e) {
client.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, "Internal Error"));
} catch (CoOpsUsageException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 400, e.getMessage())));
} catch (CoOpsNotFoundException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 404, e.getMessage())));
} catch (CoOpsForbiddenException e) {
client.getAsyncRemote().sendText(objectMapper.writeValueAsString(new ErrorMessage("patchError", 500, e.getMessage())));
}
}
}
}
Aggregations