use of fi.foyt.coops.CoOpsNotFoundException in project muikku by otavanopisto.
the class CoOpsApiImpl method fileUpdate.
public List<Patch> fileUpdate(String fileId, String sessionId, Long revisionNumber) throws CoOpsNotFoundException, CoOpsInternalErrorException, CoOpsUsageException, CoOpsForbiddenException {
CoOpsSession session = coOpsSessionController.findSessionBySessionId(sessionId);
if (session == null) {
throw new CoOpsUsageException("Invalid session id");
}
if (revisionNumber == null) {
throw new CoOpsUsageException("revisionNumber parameter is missing");
}
HtmlMaterial htmlMaterial = findFile(fileId);
if (htmlMaterial == null) {
throw new CoOpsNotFoundException();
}
List<Patch> updateResults = new ArrayList<>();
List<HtmlMaterialRevision> htmlMaterialRevisions = htmlMaterialController.listRevisionsAfter(htmlMaterial, revisionNumber);
if (!htmlMaterialRevisions.isEmpty()) {
for (HtmlMaterialRevision htmlMaterialRevision : htmlMaterialRevisions) {
String patch = htmlMaterialRevision.getData();
Map<String, String> properties = null;
Map<String, Object> extensions = null;
List<HtmlMaterialRevisionProperty> revisionProperties = htmlMaterialController.listRevisionProperties(htmlMaterialRevision);
if (revisionProperties.size() > 0) {
properties = new HashMap<>();
for (HtmlMaterialRevisionProperty revisionProperty : revisionProperties) {
properties.put(revisionProperty.getKey(), revisionProperty.getValue());
}
}
List<HtmlMaterialRevisionExtensionProperty> revisionExtensionProperties = htmlMaterialController.listRevisionExtensionProperties(htmlMaterialRevision);
if (revisionExtensionProperties.size() > 0) {
extensions = new HashMap<>();
for (HtmlMaterialRevisionExtensionProperty revisionExtensionProperty : revisionExtensionProperties) {
extensions.put(revisionExtensionProperty.getKey(), revisionExtensionProperty.getValue());
}
}
if (patch != null) {
updateResults.add(new Patch(htmlMaterialRevision.getSessionId(), htmlMaterialRevision.getRevision(), htmlMaterialRevision.getChecksum(), patch, properties, extensions));
} else {
updateResults.add(new Patch(htmlMaterialRevision.getSessionId(), htmlMaterialRevision.getRevision(), null, null, properties, extensions));
}
}
}
return updateResults;
}
use of fi.foyt.coops.CoOpsNotFoundException in project muikku by otavanopisto.
the class CoOpsApiImpl method findFile.
private HtmlMaterial findFile(String fileId) throws CoOpsUsageException, CoOpsNotFoundException {
if (!StringUtils.isNumeric(fileId)) {
throw new CoOpsUsageException("fileId must be a number");
}
Long id = NumberUtils.createLong(fileId);
if (id == null) {
throw new CoOpsUsageException("fileId must be a number");
}
HtmlMaterial file = htmlMaterialController.findHtmlMaterialById(id);
if (file == null) {
throw new CoOpsNotFoundException();
}
return file;
}
use of fi.foyt.coops.CoOpsNotFoundException in project muikku by otavanopisto.
the class HtmlMaterialRESTService method publishMaterial.
@POST
@Path("/{id}/publish/")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response publishMaterial(@PathParam("id") Long id, HtmlRestMaterialPublish entity) {
if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.MANAGE_MATERIALS)) {
return Response.status(Status.FORBIDDEN).entity("Permission denied").build();
}
HtmlMaterial htmlMaterial = htmlMaterialController.findHtmlMaterialById(id);
if (htmlMaterial == null) {
return Response.status(Status.NOT_FOUND).build();
}
if (!htmlMaterial.getRevisionNumber().equals(entity.getFromRevision())) {
return Response.status(Status.CONFLICT).entity(new HtmlRestMaterialPublishError(HtmlRestMaterialPublishError.Reason.CONCURRENT_MODIFICATIONS)).build();
}
try {
File fileRevision = coOpsApi.fileGet(id.toString(), entity.getToRevision());
if (fileRevision == null) {
return Response.status(Status.NOT_FOUND).build();
}
htmlMaterialController.updateHtmlMaterialToRevision(htmlMaterial, fileRevision.getContent(), entity.getToRevision(), false, entity.getRemoveAnswers() != null ? entity.getRemoveAnswers() : false);
} catch (WorkspaceMaterialContainsAnswersExeption e) {
return Response.status(Status.CONFLICT).entity(new HtmlRestMaterialPublishError(HtmlRestMaterialPublishError.Reason.CONTAINS_ANSWERS)).build();
} catch (CoOpsNotImplementedException | CoOpsNotFoundException | CoOpsUsageException | CoOpsInternalErrorException | CoOpsForbiddenException e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
return Response.noContent().build();
}
use of fi.foyt.coops.CoOpsNotFoundException 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.CoOpsNotFoundException in project muikku by otavanopisto.
the class CoOpsApiImpl method fileGet.
public File fileGet(String fileId, Long revisionNumber) throws CoOpsNotImplementedException, CoOpsNotFoundException, CoOpsUsageException, CoOpsInternalErrorException, CoOpsForbiddenException {
HtmlMaterial htmlMaterial = findFile(fileId);
if (htmlMaterial == null) {
throw new CoOpsNotFoundException();
}
if (revisionNumber != null) {
String data = htmlMaterialController.getRevisionHtml(htmlMaterial, revisionNumber);
Map<String, String> properties = htmlMaterialController.getRevisionProperties(htmlMaterial, revisionNumber);
return new File(revisionNumber, data, htmlMaterial.getContentType(), properties);
} else {
Long maxRevisionNumber = htmlMaterialController.lastHtmlMaterialRevision(htmlMaterial);
String data = htmlMaterialController.getRevisionHtml(htmlMaterial, maxRevisionNumber);
Map<String, String> properties = htmlMaterialController.getRevisionProperties(htmlMaterial, maxRevisionNumber);
return new File(maxRevisionNumber, data, htmlMaterial.getContentType(), properties);
}
}
Aggregations