Search in sources :

Example 16 with HtmlMaterial

use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.

the class HtmlMaterialDAO method create.

public HtmlMaterial create(String title, String html, String contentType, Long revisionNumber, HtmlMaterial originMaterial, String license, MaterialViewRestrict viewRestrict) {
    HtmlMaterial htmlMaterial = new HtmlMaterial();
    htmlMaterial.setHtml(html);
    htmlMaterial.setTitle(title);
    htmlMaterial.setOriginMaterial(originMaterial);
    htmlMaterial.setContentType(contentType);
    htmlMaterial.setRevisionNumber(revisionNumber);
    htmlMaterial.setLicense(license);
    htmlMaterial.setViewRestrict(viewRestrict);
    return persist(htmlMaterial);
}
Also used : HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)

Example 17 with HtmlMaterial

use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial 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);
    }
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) ArrayList(java.util.ArrayList) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) Material(fi.otavanopisto.muikku.plugins.material.model.Material) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) BinaryMaterial(fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial) BinaryMaterial(fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)

Example 18 with HtmlMaterial

use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.

the class DeusNexMachinaRESTService method cleanMaterials.

private void cleanMaterials(List<WorkspaceNode> nodes) {
    for (WorkspaceNode node : nodes) {
        if (node.getType() != WorkspaceNodeType.MATERIAL) {
            cleanMaterials(workspaceMaterialController.listWorkspaceNodesByParent(node));
        } else {
            WorkspaceMaterial workspaceMaterial = (WorkspaceMaterial) node;
            Material material = workspaceMaterialController.getMaterialForWorkspaceMaterial(workspaceMaterial);
            if ("html".equals(material.getType())) {
                logger.info("Cleaning html material " + material.getId());
                htmlMaterialCleaner.cleanMaterial((HtmlMaterial) material, workspaceMaterial);
            }
        }
    }
}
Also used : Material(fi.otavanopisto.muikku.plugins.material.model.Material) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)

Example 19 with HtmlMaterial

use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.

the class AcceptanceTestsRESTService method createWorkspaceMaterial.

@POST
@Path("/workspaces/{WORKSPACEID}/htmlmaterials")
@RESTPermit(handling = Handling.UNSECURED)
public Response createWorkspaceMaterial(fi.otavanopisto.muikku.atests.WorkspaceHtmlMaterial payload) {
    if (payload.getParentId() == null) {
        return Response.status(Status.BAD_REQUEST).entity("Mandatory parentId is missing").build();
    }
    HtmlMaterial htmlMaterial = htmlMaterialController.createHtmlMaterial(payload.getTitle(), payload.getHtml(), payload.getContentType(), payload.getRevisionNumber(), payload.getLicense());
    WorkspaceNode parent = workspaceMaterialController.findWorkspaceNodeById(payload.getParentId());
    if (parent == null) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid parentId").build();
    }
    WorkspaceMaterial workspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(parent, htmlMaterial);
    String assignmentType = payload.getAssignmentType();
    if (StringUtils.isNotBlank(assignmentType)) {
        WorkspaceMaterialAssignmentType workspaceMaterialAssignmentType = WorkspaceMaterialAssignmentType.valueOf(assignmentType);
        if (workspaceMaterialAssignmentType == null) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid assignmentType '%s'", assignmentType)).build();
        }
        workspaceMaterialController.updateWorkspaceMaterialAssignmentType(workspaceMaterial, workspaceMaterialAssignmentType);
    }
    return Response.ok(createRestEntity(workspaceMaterial, htmlMaterial)).build();
}
Also used : HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) POST(javax.ws.rs.POST)

Example 20 with HtmlMaterial

use of fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial in project muikku by otavanopisto.

the class WorkspaceMaterialController method ensureWorkspaceHelpPageExists.

public WorkspaceMaterial ensureWorkspaceHelpPageExists(WorkspaceEntity workspace) {
    WorkspaceFolder helpPageFolder = findWorkspaceHelpPageFolder(workspace);
    if (helpPageFolder == null) {
        helpPageFolder = createWorkspaceHelpPageFolder(workspace);
    }
    WorkspaceMaterial helpPageMaterial = null;
    List<WorkspaceMaterial> helpPageMaterials = listWorkspaceMaterialsByParent(helpPageFolder);
    if (helpPageMaterials.isEmpty()) {
        String title = localeController.getText(sessionController.getLocale(), "plugin.workspace.helpPage.title");
        String license = null;
        HtmlMaterial htmlMaterial = htmlMaterialController.createHtmlMaterial(title, "", "text/html", 0l, license);
        helpPageMaterial = createWorkspaceMaterial(helpPageFolder, htmlMaterial);
    } else {
        helpPageMaterial = helpPageMaterials.get(0);
    }
    return helpPageMaterial;
}
Also used : HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)

Aggregations

HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)29 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)14 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)10 CoOpsNotFoundException (fi.foyt.coops.CoOpsNotFoundException)7 CoOpsUsageException (fi.foyt.coops.CoOpsUsageException)7 Material (fi.otavanopisto.muikku.plugins.material.model.Material)7 Path (javax.ws.rs.Path)7 CoOpsInternalErrorException (fi.foyt.coops.CoOpsInternalErrorException)6 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)5 IOException (java.io.IOException)5 CoOpsForbiddenException (fi.foyt.coops.CoOpsForbiddenException)4 CoOpsNotImplementedException (fi.foyt.coops.CoOpsNotImplementedException)4 File (fi.foyt.coops.model.File)4 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)4 CoOpsSession (fi.otavanopisto.muikku.plugins.material.coops.model.CoOpsSession)4 WorkspaceMaterialContainsAnswersExeption (fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption)4 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)4 Patch (fi.foyt.coops.model.Patch)3 BinaryMaterial (fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial)3 WorkspaceMaterialAssignmentType (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType)3