Search in sources :

Example 36 with WorkspaceMaterial

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.

the class WorkspaceRESTService method createWorkspaceMaterialReply.

@POST
// @Path ("/workspaces/{WORKSPACEENTITYID:[0-9]*}/materials/{WORKSPACEMATERIALID:[0-9]*}/replies")
@Path("/workspaces/{WORKSPACEENTITYID}/materials/{WORKSPACEMATERIALID}/replies")
@RESTPermitUnimplemented
public Response createWorkspaceMaterialReply(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("WORKSPACEMATERIALID") Long workspaceMaterialId, WorkspaceMaterialReply payload) {
    if (payload == null) {
        return Response.status(Status.BAD_REQUEST).entity("Payload is missing").build();
    }
    if (payload.getState() == null) {
        return Response.status(Status.BAD_REQUEST).entity("State is missing").build();
    }
    UserEntity loggedUser = sessionController.getLoggedUserEntity();
    if (loggedUser == null) {
        return Response.status(Status.UNAUTHORIZED).entity("Unauthorized").build();
    }
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).entity("Could not find workspace entity").build();
    }
    WorkspaceMaterial workspaceMaterial = workspaceMaterialController.findWorkspaceMaterialById(workspaceMaterialId);
    if (workspaceMaterial == null) {
        return Response.status(Status.NOT_FOUND).entity("Could not find workspace material").build();
    }
    WorkspaceRootFolder workspaceRootFolder = workspaceMaterialController.findWorkspaceRootFolderByWorkspaceNode(workspaceMaterial);
    if (workspaceRootFolder == null) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Could not find workspace root folder").build();
    }
    if (!workspaceRootFolder.getWorkspaceEntityId().equals(workspaceEntity.getId())) {
        return Response.status(Status.BAD_REQUEST).entity("Invalid workspace material id or workspace entity id").build();
    }
    fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply workspaceMaterialReply = workspaceMaterialReplyController.createWorkspaceMaterialReply(workspaceMaterial, payload.getState(), loggedUser);
    return Response.ok(createRestModel(workspaceMaterialReply)).build();
}
Also used : WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)

Example 37 with WorkspaceMaterial

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial 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 38 with WorkspaceMaterial

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.

the class TaskRelativeWorkspaceMaterialResources method cleanElement.

@Override
protected void cleanElement(Element element) {
    boolean resourceConflict = false;
    // TODO: Treats all relative links as immediate child resources of this material ONLY
    String elementName = element.getTagName();
    if ("img".equals(elementName) || "source".equals(elementName) || "embed".equals(elementName) || "object".equals(elementName)) {
        String attributeName = "object".equals(elementName) ? "data" : "src";
        String src = element.getAttribute(attributeName);
        if (StringUtils.startsWith(src, "/workspace/")) {
            logger.info(String.format("Processing resource %s", src));
            // Resolve WorkspaceMaterial and Material the link points to
            WorkspaceMaterial resourceWorkspaceMaterial = workspaceMaterialController.findWorkspaceMaterialByRootPath(src);
            if (resourceWorkspaceMaterial != null) {
                String resourceUrlName = resourceWorkspaceMaterial.getUrlName();
                Material resourceMaterial = resourceWorkspaceMaterial == null ? null : workspaceMaterialController.getMaterialForWorkspaceMaterial(resourceWorkspaceMaterial);
                if (resourceMaterial != null) {
                    // Ensure that all workspace materials using this HTML material will have the link target as a child resource
                    Material htmlMaterial = workspaceMaterialController.getMaterialForWorkspaceMaterial(getWorkspaceMaterial());
                    List<WorkspaceMaterial> workspaceHtmlMaterials = workspaceMaterialController.listWorkspaceMaterialsByMaterial(htmlMaterial);
                    for (WorkspaceMaterial workspaceHtmlMaterial : workspaceHtmlMaterials) {
                        WorkspaceMaterial childMaterial = workspaceMaterialController.findWorkspaceMaterialByParentAndUrlName(workspaceHtmlMaterial, resourceUrlName);
                        if (childMaterial == null) {
                            childMaterial = workspaceMaterialController.createWorkspaceMaterial(workspaceHtmlMaterial, resourceMaterial, resourceUrlName, null, null);
                            logger.info(String.format("Created resource %d with relative url %s", childMaterial.getId(), resourceUrlName));
                        } else {
                            resourceConflict = !resourceMaterial.getId().equals(childMaterial.getMaterialId());
                            if (resourceConflict) {
                                logger.warning(String.format("Skipping resource because WorkspaceMaterial %d has conflicting child resource %s", workspaceHtmlMaterial.getId(), resourceUrlName));
                                break;
                            }
                        }
                    }
                    if (!resourceConflict) {
                        element.setAttribute(attributeName, resourceUrlName);
                        markModified();
                    }
                } else {
                    logger.warning("Resource does not resolve to Material");
                }
            } else {
                logger.warning("Resource does not resolve to WorkspaceMaterial");
            }
        }
    }
}
Also used : Material(fi.otavanopisto.muikku.plugins.material.model.Material) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)

Example 39 with WorkspaceMaterial

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.

the class TaskBase64Image method cleanElement.

@Override
protected void cleanElement(Element element) {
    if ("img".equals(element.getTagName())) {
        String src = element.getAttribute("src");
        if (StringUtils.startsWith(src, "data:") && StringUtils.contains(src, ";base64,")) {
            logger.info("Converting base64 image");
            try {
                String contentType = StringUtils.substringBetween(src, "data:", ";base64,");
                byte[] data = Base64.decodeBase64(StringUtils.substringAfter(src, ";base64,"));
                String prefix = null;
                if (StringUtils.contains(contentType, "jpg") || StringUtils.contains(contentType, "jpeg")) {
                    prefix = "jpg";
                } else if (StringUtils.contains(contentType, "gif")) {
                    prefix = "gif";
                } else if (StringUtils.contains(contentType, "png")) {
                    prefix = "png";
                } else {
                    logger.log(Level.WARNING, String.format("Image of content type %s prefixed as %s", contentType, prefix));
                }
                String name = prefix == null ? String.format("img%s", StringUtils.leftPad(++imageCounter + "", 3, '0')) : String.format("img%s.%s", StringUtils.leftPad(++imageCounter + "", 3, '0'), prefix);
                String license = null;
                BinaryMaterial material = binaryMaterialController.createBinaryMaterial(name, contentType, data, license);
                WorkspaceMaterial workspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(getWorkspaceMaterial(), material);
                String workspaceUrl = StringUtils.prependIfMissing(workspaceMaterialController.getCompletePath(workspaceMaterial), "/");
                logger.info(String.format("Image converted to %s", workspaceUrl));
                element.setAttribute("src", workspaceUrl);
                markModified();
            } catch (Exception e) {
                logger.log(Level.SEVERE, String.format("Failed to fix image from %s", src), e);
            }
        }
    }
}
Also used : BinaryMaterial(fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)

Example 40 with WorkspaceMaterial

use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial in project muikku by otavanopisto.

the class TaskM2Image method cleanElement.

@Override
protected void cleanElement(Element element) {
    boolean needsModify = false;
    if ("img".equals(element.getTagName())) {
        String src = element.getAttribute("src");
        if (StringUtils.startsWith(src, "https://muikku.otavanopisto.fi/fi")) {
            needsModify = true;
        } else if (StringUtils.startsWith(src, "http://muikku.otavanopisto.fi/fi")) {
            needsModify = true;
        } else if (StringUtils.startsWith(src, "muikku.otavanopisto.fi/fi")) {
            src = "http://" + src;
            needsModify = true;
        }
        if (needsModify) {
            try {
                logger.info(String.format("Fixing image from %s", src));
                String m2Url = pluginSettingsController.getPluginSetting("deus-nex-machina", "m2.url");
                String m2User = pluginSettingsController.getPluginSetting("deus-nex-machina", "m2.user");
                String m2Pass = pluginSettingsController.getPluginSetting("deus-nex-machina", "m2.pass");
                String m2Auth = pluginSettingsController.getPluginSetting("deus-nex-machina", "m2.auth");
                String cookie = null;
                URL authUrl = new URL(String.format("%s?u=%s&p=%s&c=%s", m2Url, m2User, m2Pass, m2Auth));
                URLConnection authUrlConnection = authUrl.openConnection();
                authUrlConnection.connect();
                try {
                    String headerName = null;
                    for (int i = 1; (headerName = authUrlConnection.getHeaderFieldKey(i)) != null; i++) {
                        if (StringUtils.equals(headerName, "Set-Cookie")) {
                            if (StringUtils.contains(authUrlConnection.getHeaderField(i), "IxAuth")) {
                                cookie = authUrlConnection.getHeaderField(i);
                                break;
                            }
                        }
                    }
                    if (cookie != null) {
                        URL imgUrl = new URL(src);
                        URLConnection imgUrlConnection = imgUrl.openConnection();
                        imgUrlConnection.setRequestProperty("Cookie", cookie);
                        imgUrlConnection.connect();
                        String contentType = imgUrlConnection.getContentType();
                        if (StringUtils.startsWith(contentType, "image/")) {
                            String prefix = null;
                            if (StringUtils.contains(contentType, "jpg") || StringUtils.contains(contentType, "jpeg")) {
                                prefix = "jpg";
                            } else if (StringUtils.contains(contentType, "gif")) {
                                prefix = "gif";
                            } else if (StringUtils.contains(contentType, "png")) {
                                prefix = "png";
                            } else {
                                prefix = StringUtils.substringAfter(contentType, "image/");
                                logger.log(Level.WARNING, String.format("Image of content type %s prefixed as %s", contentType, prefix));
                            }
                            InputStream is = imgUrlConnection.getInputStream();
                            byte[] data = {};
                            try {
                                data = IOUtils.toByteArray(is);
                            } finally {
                                is.close();
                            }
                            String name = String.format("img%s.%s", StringUtils.leftPad(++imageCounter + "", 3, '0'), prefix);
                            String license = null;
                            BinaryMaterial material = binaryMaterialController.createBinaryMaterial(name, contentType, data, license);
                            WorkspaceMaterial workspaceMaterial = workspaceMaterialController.createWorkspaceMaterial(getWorkspaceMaterial(), material);
                            String workspaceUrl = StringUtils.prependIfMissing(workspaceMaterialController.getCompletePath(workspaceMaterial), "/");
                            logger.info(String.format("Image converted to %s", workspaceUrl));
                            element.setAttribute("src", workspaceUrl);
                            markModified();
                        } else {
                            logger.log(Level.SEVERE, String.format("Skipping image due to questionable content type %s", contentType));
                        }
                    } else {
                        logger.log(Level.SEVERE, "Cookie get fail");
                    }
                } finally {
                    URL logoutUrl = new URL(String.format("%s", m2Url));
                    URLConnection logoutUrlConnection = logoutUrl.openConnection();
                    logoutUrlConnection.connect();
                }
            } catch (Exception e) {
                logger.log(Level.SEVERE, String.format("Failed to fix image from %s", src), e);
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) BinaryMaterial(fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial) URL(java.net.URL) URLConnection(java.net.URLConnection) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)

Aggregations

WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)66 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)24 Path (javax.ws.rs.Path)24 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)18 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)17 HtmlMaterial (fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial)16 WorkspaceNode (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode)15 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)14 WorkspaceRootFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder)14 GET (javax.ws.rs.GET)14 Material (fi.otavanopisto.muikku.plugins.material.model.Material)13 ArrayList (java.util.ArrayList)11 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)7 WorkspaceMaterialEvaluation (fi.otavanopisto.muikku.plugins.evaluation.model.WorkspaceMaterialEvaluation)6 BinaryMaterial (fi.otavanopisto.muikku.plugins.material.model.BinaryMaterial)6 WorkspaceFolder (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder)6 WorkspaceMaterialReply (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReply)6 POST (javax.ws.rs.POST)6 SupplementationRequest (fi.otavanopisto.muikku.plugins.evaluation.model.SupplementationRequest)5 RestSupplementationRequest (fi.otavanopisto.muikku.plugins.evaluation.rest.model.RestSupplementationRequest)5