use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class GuiderRESTService method getWorkspaceAssessmentsStudyProgressAnalysis.
@GET
@Path("/workspaces/{WORKSPACEENTITYID}/activity")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response getWorkspaceAssessmentsStudyProgressAnalysis(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId) {
SchoolDataIdentifier userIdentifier = sessionController.getLoggedUser();
if (userIdentifier == null) {
return Response.status(Status.BAD_REQUEST).entity("Invalid userIdentifier").build();
}
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.NOT_FOUND).entity("WorkspaceEntity not found").build();
}
GuiderStudentWorkspaceActivity activity = guiderController.getStudentWorkspaceActivity(workspaceEntity, userIdentifier);
if (activity == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(String.format("Failed to analyze assignments progress for student %s in workspace %d", userIdentifier, workspaceEntity.getId())).build();
}
WorkspaceAssessmentState assessmentState = new WorkspaceAssessmentState(WorkspaceAssessmentState.UNASSESSED);
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceAndUserIdentifier(workspaceEntity, userIdentifier);
if (workspaceUserEntity != null && workspaceUserEntity.getWorkspaceUserRole().getArchetype() == WorkspaceRoleArchetype.STUDENT) {
assessmentState = assessmentRequestController.getWorkspaceAssessmentState(workspaceUserEntity);
}
return Response.ok(toRestModel(activity, assessmentState)).build();
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class AbstractWorkspaceBackingBean method checkAccess.
@RequestAction
public String checkAccess() {
String urlName = getWorkspaceUrlName();
if (StringUtils.isBlank(urlName)) {
return NavigationRules.NOT_FOUND;
}
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityByUrlName(urlName);
if (workspaceEntity == null) {
return NavigationRules.NOT_FOUND;
}
switch(workspaceEntity.getAccess()) {
case ANYONE:
break;
case LOGGED_IN:
if (!sessionController.isLoggedIn()) {
return navigationController.requireLogin();
}
break;
case MEMBERS_ONLY:
if (!sessionController.isLoggedIn()) {
return navigationController.requireLogin();
}
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findActiveWorkspaceUserByWorkspaceEntityAndUserIdentifier(workspaceEntity, sessionController.getLoggedUser());
if (workspaceUserEntity == null) {
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.ACCESS_MEMBERS_ONLY_WORKSPACE, workspaceEntity)) {
return NavigationRules.ACCESS_DENIED;
}
}
break;
}
return null;
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class MaterialAttachmentUploadServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String materialUrl = req.getPathInfo();
if (StringUtils.isBlank(materialUrl)) {
sendResponse(resp, "Missing material path", HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (!sessionController.isLoggedIn()) {
sendResponse(resp, "Unauthorized", HttpServletResponse.SC_UNAUTHORIZED);
return;
}
Part file = req.getPart("upload");
if (file == null) {
sendResponse(resp, "Missing file", HttpServletResponse.SC_BAD_REQUEST);
return;
}
long fileSizeLimit = systemSettingsController.getUploadFileSizeLimit();
if (file.getSize() > fileSizeLimit) {
sendResponse(resp, "File too large", HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
return;
}
WorkspaceMaterial parentWorkspaceMaterial = workspaceMaterialController.findWorkspaceMaterialByRootPath(materialUrl);
if (parentWorkspaceMaterial == null) {
sendResponse(resp, "Material not found", HttpServletResponse.SC_NOT_FOUND);
return;
}
WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(parentWorkspaceMaterial);
if (workspaceRootFolder == null) {
sendResponse(resp, "Workspace root folder not found", HttpServletResponse.SC_NOT_FOUND);
return;
}
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceRootFolder.getWorkspaceEntityId());
if (workspaceEntity == null) {
sendResponse(resp, "Workspace entity not found", HttpServletResponse.SC_NOT_FOUND);
return;
}
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_MATERIALS, workspaceEntity)) {
sendResponse(resp, "Forbidden", HttpServletResponse.SC_FORBIDDEN);
return;
}
HtmlMaterial parentMaterial = htmlMaterialController.findHtmlMaterialById(parentWorkspaceMaterial.getMaterialId());
if (parentMaterial == null) {
sendResponse(resp, "Parent material is not html material", HttpServletResponse.SC_BAD_REQUEST);
return;
}
String license = null;
BinaryMaterial uploadedMaterial = binaryMaterialController.createBinaryMaterial(file.getSubmittedFileName(), file.getContentType(), IOUtils.toByteArray(file.getInputStream()), license);
String uploadedUrl = null;
List<WorkspaceMaterial> parentWorkspaceMaterials = workspaceMaterialController.listWorkspaceMaterialsByMaterial(parentMaterial);
for (WorkspaceMaterial sharedWorkspaceMaterial : parentWorkspaceMaterials) {
WorkspaceMaterial uploadedWorkspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(sharedWorkspaceMaterial, uploadedMaterial);
if (sharedWorkspaceMaterial.getId().equals(parentWorkspaceMaterial.getId())) {
uploadedUrl = uploadedWorkspaceMaterial.getUrlName();
}
}
if (StringUtils.isBlank(uploadedUrl)) {
sendResponse(resp, "Could not resolve uploaded file url", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
UploadMeta uploadMeta = new UploadMeta(file.getName(), 1, uploadedUrl);
resp.setContentType("application/json");
ServletOutputStream servletOutputStream = resp.getOutputStream();
try {
(new ObjectMapper()).writeValue(servletOutputStream, uploadMeta);
} finally {
servletOutputStream.flush();
}
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class WorkspaceBinaryMaterialServlet method process.
private void process(HttpServletRequest request, HttpServletResponse response, boolean serveContent) throws ServletException, IOException {
String workspaceUrl = request.getParameter("workspaceUrlName");
String materialPath = request.getParameter("workspaceMaterialUrlName");
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityByUrlName(workspaceUrl);
if (workspaceEntity == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialByWorkspaceEntityAndPath(workspaceEntity, materialPath);
if (workspaceMaterial == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
Material material = workspaceMaterialController.getMaterialForWorkspaceMaterial(workspaceMaterial);
int materialSize = material instanceof BinaryMaterial ? ((BinaryMaterial) material).getContent().length : material instanceof HtmlMaterial ? ((HtmlMaterial) material).getHtml().length() : 0;
String eTag = DigestUtils.md5Hex(material.getTitle() + ':' + material.getId() + ':' + materialSize + ':' + material.getVersion());
response.setHeader("ETag", eTag);
String ifNoneMatch = request.getHeader("If-None-Match");
if (!StringUtils.equals(ifNoneMatch, eTag)) {
response.setStatus(HttpServletResponse.SC_OK);
if (material instanceof BinaryMaterial) {
BinaryMaterial binaryMaterial = (BinaryMaterial) material;
byte[] data = binaryMaterial.getContent();
// Byte range support
List<Range> ranges = new ArrayList<Range>();
String range = request.getHeader("Range");
if (range != null) {
if (!range.matches("^bytes=\\d*-\\d*(,\\d*-\\d*)*$")) {
response.setHeader("Content-Range", "bytes */" + data.length);
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
for (String part : range.substring(6).split(",")) {
String startStr = StringUtils.substringBefore(part, "-");
String endStr = StringUtils.substringAfter(part, "-");
int start = NumberUtils.isDigits(startStr) ? NumberUtils.toInt(startStr) : -1;
int end = NumberUtils.isDigits(endStr) ? NumberUtils.toInt(endStr) : -1;
if (start == -1) {
start = data.length - end;
end = data.length - 1;
} else if (end == -1 || end > data.length - 1) {
end = data.length - 1;
}
if (start > end) {
response.setHeader("Content-Range", "bytes */" + data.length);
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
ranges.add(new Range(start, end, data.length));
}
}
response.setHeader("Accept-Ranges", "bytes");
response.setContentType(binaryMaterial.getContentType());
try {
if (ranges.isEmpty()) {
// Entire file
if (serveContent) {
response.setHeader("Content-Length", String.valueOf(data.length));
response.getOutputStream().write(data);
}
} else if (ranges.size() == 1) {
// Single byte range
Range r = ranges.get(0);
response.setHeader("Content-Range", String.format("bytes %d-%d/%d", r.start, r.end, r.total));
response.setHeader("Content-Length", String.valueOf(r.length));
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
if (serveContent) {
response.getOutputStream().write(data, r.start, r.length);
}
} else {
// Multiple byte ranges
response.setContentType("multipart/byteranges; boundary=MULTIPART_BYTERANGES");
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
if (serveContent) {
for (Range r : ranges) {
response.getOutputStream().println();
response.getOutputStream().println("--MULTIPART_BYTERANGES");
response.getOutputStream().println(String.format("Content-Type: %s", binaryMaterial.getContentType()));
response.getOutputStream().println(String.format("Content-Range: bytes %d-%d/%d", r.start, r.end, r.total));
response.getOutputStream().write(data, r.start, r.length);
}
response.getOutputStream().println();
response.getOutputStream().println("--MULTIPART_BYTERANGES--");
}
}
} finally {
response.getOutputStream().flush();
}
} else if (material instanceof HtmlMaterial) {
HtmlMaterial htmlMaterial = (HtmlMaterial) material;
byte[] data = htmlMaterial.getHtml().getBytes("UTF-8");
response.setContentLength(data.length);
response.setContentType("text/html; charset=UTF-8");
try {
response.getOutputStream().write(data);
} finally {
response.getOutputStream().flush();
}
}
} else {
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceEntity in project muikku by otavanopisto.
the class WorkspaceFrontPageManagementBackingBean method init.
@RequestAction
public String init() {
String urlName = getWorkspaceUrlName();
if (StringUtils.isBlank(urlName)) {
return NavigationRules.NOT_FOUND;
}
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityByUrlName(urlName);
if (workspaceEntity == null) {
return NavigationRules.NOT_FOUND;
}
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.MANAGE_WORKSPACE_FRONTPAGE, workspaceEntity)) {
return NavigationRules.ACCESS_DENIED;
}
rootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceEntity(workspaceEntity);
workspaceEntityId = workspaceEntity.getId();
workspaceBackingBean.setWorkspaceUrlName(urlName);
workspaceName = workspaceBackingBean.getWorkspaceName();
try {
WorkspaceMaterial frontPage = workspaceMaterialController.ensureWorkspaceFrontPageExists(workspaceEntity);
contentNodes = Arrays.asList(workspaceMaterialController.createContentNode(frontPage));
} catch (WorkspaceMaterialException e) {
logger.log(Level.SEVERE, "Error loading materials", e);
return NavigationRules.INTERNAL_ERROR;
}
materialsBaseUrl = String.format("/workspace/%s/materials", workspaceUrlName);
return null;
}
Aggregations