use of fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile in project muikku by otavanopisto.
the class TranscriptOfRecordsFileController method outputFileToStream.
public void outputFileToStream(TranscriptOfRecordsFile torFile, OutputStream stream) {
String fileUuid = torFile.getFileName();
if (!UUID_PATTERN.matcher(fileUuid).matches()) {
throw new RuntimeException("File name is not a valid UUID");
}
File file = Paths.get(getFileUploadBasePath(), fileUuid).toFile();
try {
FileUtils.copyFile(file, stream);
} catch (IOException e) {
// Wrap with unchecked exception to adhere to StreamingOutput interface
throw new RuntimeException(e);
}
}
use of fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile in project muikku by otavanopisto.
the class TranscriptOfRecordsFileUploadServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (!sessionController.isLoggedIn()) {
sendResponse(resp, "Must be logged in", HttpServletResponse.SC_FORBIDDEN);
return;
}
if (!sessionController.hasEnvironmentPermission(TranscriptofRecordsPermissions.TRANSCRIPT_OF_RECORDS_FILE_UPLOAD)) {
sendResponse(resp, "Insufficient permissions", HttpServletResponse.SC_FORBIDDEN);
return;
}
Part userIdentifierPart = req.getPart("userIdentifier");
if (userIdentifierPart == null) {
sendResponse(resp, "Missing userIdentifier", HttpServletResponse.SC_BAD_REQUEST);
return;
}
String userIdentifier = "";
try (InputStream is = userIdentifierPart.getInputStream()) {
userIdentifier = IOUtils.toString(is, StandardCharsets.UTF_8);
}
SchoolDataIdentifier schoolDataIdentifier = SchoolDataIdentifier.fromId(userIdentifier);
if (schoolDataIdentifier == null) {
sendResponse(resp, "Invalid userIdentifier", HttpServletResponse.SC_BAD_REQUEST);
return;
}
UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(schoolDataIdentifier);
if (userEntity == null) {
sendResponse(resp, "User entity not found", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
Part titlePart = req.getPart("title");
if (titlePart == null) {
sendResponse(resp, "Missing title", HttpServletResponse.SC_BAD_REQUEST);
return;
}
String title = "";
try (InputStream is = titlePart.getInputStream()) {
title = IOUtils.toString(is, StandardCharsets.UTF_8);
}
Part descriptionPart = req.getPart("description");
if (descriptionPart == null) {
sendResponse(resp, "Missing description", HttpServletResponse.SC_BAD_REQUEST);
return;
}
String description = "";
try (InputStream is = descriptionPart.getInputStream()) {
description = IOUtils.toString(is, StandardCharsets.UTF_8);
}
Part uploadPart = req.getPart("upload");
if (uploadPart == null) {
sendResponse(resp, "Missing file", HttpServletResponse.SC_BAD_REQUEST);
return;
}
String contentType = uploadPart.getContentType();
long fileSizeLimit = systemSettingsController.getUploadFileSizeLimit();
if (uploadPart.getSize() > fileSizeLimit) {
sendResponse(resp, "File too large", HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
return;
}
try (InputStream is = uploadPart.getInputStream()) {
TranscriptOfRecordsFile file = transcriptOfRecordsFileController.attachFile(userEntity, is, contentType, title, description);
String result = (new ObjectMapper()).writeValueAsString(file);
sendResponse(resp, result, HttpServletResponse.SC_OK);
}
}
use of fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile in project muikku by otavanopisto.
the class TranscriptofRecordsBackingBean method init.
@RequestAction
public String init() {
if (!sessionController.hasEnvironmentPermission(TranscriptofRecordsPermissions.TRANSCRIPT_OF_RECORDS_VIEW)) {
return NavigationRules.ACCESS_DENIED;
}
Map<String, Grade> grades = new HashMap<>();
List<GradingScale> gradingScales = gradingController.listGradingScales();
for (GradingScale gradingScale : gradingScales) {
List<GradingScaleItem> scaleItems = gradingController.listGradingScaleItems(gradingScale);
for (GradingScaleItem scaleItem : scaleItems) {
String id = StringUtils.join(new String[] { gradingScale.getSchoolDataSource(), gradingScale.getIdentifier(), scaleItem.getSchoolDataSource(), scaleItem.getIdentifier() }, '-');
String grade = scaleItem.getName();
String scale = gradingScale.getName();
Boolean passing = scaleItem.isPassingGrade();
grades.put(id, new Grade(grade, scale, passing));
}
}
try {
this.grades = new ObjectMapper().writeValueAsString(grades);
} catch (JsonProcessingException e) {
logger.log(Level.SEVERE, "Failed to serialize grades", e);
return NavigationRules.INTERNAL_ERROR;
}
UserEntity loggedEntity = sessionController.getLoggedUserEntity();
User user = userController.findUserByDataSourceAndIdentifier(sessionController.getLoggedUserSchoolDataSource(), sessionController.getLoggedUserIdentifier());
studyStartDate = user.getStudyStartDate();
studyTimeEnd = user.getStudyTimeEnd();
studyTimeLeftStr = "";
if (studyTimeEnd != null) {
OffsetDateTime now = OffsetDateTime.now();
Locale locale = sessionController.getLocale();
if (now.isBefore(studyTimeEnd)) {
long studyTimeLeftYears = now.until(studyTimeEnd, ChronoUnit.YEARS);
now = now.plusYears(studyTimeLeftYears);
if (studyTimeLeftYears > 0) {
studyTimeLeftStr += studyTimeLeftYears + " " + localeController.getText(locale, "plugin.records.studyTimeEndShort.y");
}
long studyTimeLeftMonths = now.until(studyTimeEnd, ChronoUnit.MONTHS);
now = now.plusMonths(studyTimeLeftMonths);
if (studyTimeLeftMonths > 0) {
if (studyTimeLeftStr.length() > 0)
studyTimeLeftStr += " ";
studyTimeLeftStr += studyTimeLeftMonths + " " + localeController.getText(locale, "plugin.records.studyTimeEndShort.m");
}
long studyTimeLeftDays = now.until(studyTimeEnd, ChronoUnit.DAYS);
now = now.plusDays(studyTimeLeftDays);
if (studyTimeLeftDays > 0) {
if (studyTimeLeftStr.length() > 0)
studyTimeLeftStr += " ";
studyTimeLeftStr += studyTimeLeftDays + " " + localeController.getText(locale, "plugin.records.studyTimeEndShort.d");
}
}
}
List<TranscriptOfRecordsFile> transcriptOfRecordsFiles;
if (loggedEntity != null) {
transcriptOfRecordsFiles = transcriptOfRecordsFileController.listFiles(loggedEntity);
} else {
transcriptOfRecordsFiles = Collections.emptyList();
}
try {
files = new ObjectMapper().writeValueAsString(transcriptOfRecordsFiles);
} catch (JsonProcessingException e) {
logger.log(Level.SEVERE, "Failed to serialize files", e);
return NavigationRules.INTERNAL_ERROR;
}
return null;
}
use of fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile in project muikku by otavanopisto.
the class GuiderRESTService method deleteTranscriptOfRecordsFile.
@DELETE
@Path("/files/{ID}")
@RESTPermit(GuiderPermissions.GUIDER_DELETE_TORFILE)
public Response deleteTranscriptOfRecordsFile(@PathParam("ID") Long fileId) {
TranscriptOfRecordsFile file = torFileController.findFileById(fileId);
if (file == null) {
return Response.status(Status.NOT_FOUND).entity("file not found").build();
}
torFileController.delete(file);
return Response.status(Status.NO_CONTENT).build();
}
use of fi.otavanopisto.muikku.plugins.transcriptofrecords.model.TranscriptOfRecordsFile in project muikku by otavanopisto.
the class TranscriptofRecordsRESTService method getFileContent.
@GET
@Path("/files/{ID}/content")
@RESTPermit(handling = Handling.INLINE)
@Produces("*/*")
public Response getFileContent(@PathParam("ID") Long fileId) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).entity("Must be logged in").build();
}
UserEntity loggedUserEntity = sessionController.getLoggedUserEntity();
TranscriptOfRecordsFile file = transcriptOfRecordsFileController.findFileById(fileId);
if (file == null) {
return Response.status(Status.NOT_FOUND).entity("File not found").build();
}
boolean isLoggedUser = Objects.equals(file.getUserEntityId(), loggedUserEntity.getId());
if (!isLoggedUser) {
return Response.status(Status.FORBIDDEN).entity("Not your file").build();
}
StreamingOutput output = s -> transcriptOfRecordsFileController.outputFileToStream(file, s);
String contentType = file.getContentType();
return Response.ok().type(contentType).entity(output).build();
}
Aggregations