use of fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession in project muikku by otavanopisto.
the class CoOpsSessionDAO method listByAccessedBeforeAndTypeAndClosed.
public List<CoOpsSession> listByAccessedBeforeAndTypeAndClosed(Date accessed, CoOpsSessionType type, Boolean closed) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<CoOpsSession> criteria = criteriaBuilder.createQuery(CoOpsSession.class);
Root<CoOpsSession> root = criteria.from(CoOpsSession.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(CoOpsSession_.closed), closed), criteriaBuilder.equal(root.get(CoOpsSession_.type), type), criteriaBuilder.lessThan(root.get(CoOpsSession_.accessed), accessed)));
return entityManager.createQuery(criteria).getResultList();
}
use of fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession 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.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession in project muikku by otavanopisto.
the class CoOpsSessionController method createSession.
public CoOpsSession createSession(HtmlMaterial htmlMaterial, UserEntity userEntity, String sessionId, Long joinRevision, String algorithm) {
CoOpsSession session = coOpsSessionDAO.create(htmlMaterial, userEntity != null ? userEntity.getId() : null, sessionId, CoOpsSessionType.REST, joinRevision, algorithm, Boolean.FALSE, new Date());
sessionOpenEvent.fire(new CoOpsSessionOpenEvent(session.getSessionId()));
return session;
}
use of fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession 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())));
}
}
}
}
use of fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession in project muikku by otavanopisto.
the class CoOpsDocumentWebSocket method onClose.
@OnClose
public void onClose(final Session session, CloseReason closeReason, @PathParam("HTMLMATERIALID") String fileId, @PathParam("SESSIONID") String sessionId) {
synchronized (this) {
fileClients.get(fileId).remove(session.getId());
CoOpsSession coOpsSession = coOpsSessionController.findSessionBySessionId(sessionId);
if (coOpsSession != null) {
closeSession(coOpsSession);
}
}
}
Aggregations