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);
}
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);
}
}
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);
}
}
}
}
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();
}
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;
}
Aggregations