@Button(list = "configuration", key = "update.configuration", order = 1, type = Button.TYPE_PRIMARY) @RequiresPermissions(level = AccessLevel.DEVELOP) @RequiresAuthentication public Resolution updateConfiguration() throws IOException { prepareConfigurationForms(); readPageConfigurationFromRequest(); boolean valid = validatePageConfiguration(); if (valid) { updatePageConfiguration(); SessionMessages.addInfoMessage(ElementsThreadLocals.getText("configuration.updated.successfully")); return cancel(); } else { return new ForwardResolution("/m/pageactions/text/configure.jsp"); } }
/** * Computes the File object used to store this page's content. The default implementation * looks for a file with a fixed name in this page's directory, computed by calling * {@link TextAction#computeTextFileName()}. * @see TextAction#computeTextFileName() * @return the document File. */ protected File locateTextFile() { return new File(pageInstance.getDirectory(), computeTextFileName()); }
protected String processAttachmentUrls(String content) { String baseUrl = StringEscapeUtils.escapeHtml(generateViewAttachmentUrl("").replace("?", "\\?")); String patternString = "([^\\s\"]+)\\s*=\\s*\"\\s*" + baseUrl + "([^\"]+)\""; Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(content); int lastEnd = 0; StringBuilder sb = new StringBuilder(); while (matcher.find()) { String hrefAttribute = matcher.group(1); String attachmentId = matcher.group(2); sb.append(content.substring(lastEnd, matcher.start())); sb.append("portofino:attachment=\"").append(attachmentId).append("\" "); sb.append("portofino:hrefAttribute=\"").append(hrefAttribute).append("\""); lastEnd = matcher.end(); } sb.append(content.substring(lastEnd)); return sb.toString(); }
/** * Loads the content of the page from a file (see {@link TextAction#locateTextFile()}). * Assigns the {@link TextAction#content} field. * @throws IOException if there's a problem loading the content. */ protected void loadContent() throws IOException { textFile = locateTextFile(); try { content = FileUtils.readFileToString(textFile, CONTENT_ENCODING); content = processContentBeforeView(content); } catch (FileNotFoundException e) { content = EMPTY_STRING; logger.debug("Content file not found. Content set to empty.", e); } }
/** * Processes the content before saving it, in order to encode internal links and attachments, and * removing potentially problematic characters. * @param content the original content. * @return the processed content. */ protected String processContentBeforeSave(String content) { content = processAttachmentUrls(content); content = processLocalUrls(content); content = Util.replaceBadUnicodeCharactersWithHtmlEntities(content); return content; }
/** * Saves the content of the page to a file (see {@link TextAction#locateTextFile()}). * @throws IOException if there's a problem saving the content. */ protected void saveContent() throws IOException { if (content == null) { content = EMPTY_STRING; } content = processContentBeforeSave(content); byte[] contentByteArray = content.getBytes(CONTENT_ENCODING); textFile = locateTextFile(); // copy the data FileOutputStream fileOutputStream = new FileOutputStream(textFile); try { long size = IOUtils.copyLarge( new ByteArrayInputStream(contentByteArray), fileOutputStream); } catch (IOException e) { logger.error("Could not save content", e); throw e; } finally { fileOutputStream.close(); } logger.info("Content saved to: {}", textFile.getAbsolutePath()); }
@Button(list = "manage-attachments", key = "ok", order = 1, type = Button.TYPE_PRIMARY) @RequiresPermissions(level = AccessLevel.VIEW, permissions = { PERMISSION_EDIT }) public Resolution saveAttachments() { if(downloadable == null) { downloadable = new String[0]; } if(textConfiguration == null) { textConfiguration = new TextConfiguration(); } for(Attachment attachment : textConfiguration.getAttachments()) { boolean contained = ArrayUtils.contains(downloadable, attachment.getId()); attachment.setDownloadable(contained); } saveConfiguration(textConfiguration); return cancel(); }
protected void commonUploadAttachment() throws IOException { logger.debug("Uploading attachment"); viewAttachmentUrl = null; InputStream attachmentStream = upload.getInputStream(); String attachmentId = RandomUtil.createRandomId(); File dataFile = RandomUtil.getCodeFile( pageInstance.getDirectory(), ATTACHMENT_FILE_NAME_PATTERN, attachmentId); // copy the data FileOutputStream fileOutputStream = new FileOutputStream(dataFile); IOUtils.copyLarge(attachmentStream, fileOutputStream); if(textConfiguration == null) { textConfiguration = new TextConfiguration(); } Attachment attachment = TextLogic.createAttachment( textConfiguration, attachmentId, upload.getFileName(), upload.getContentType(), upload.getSize()); attachment.setDownloadable(uploadDownloadable); viewAttachmentUrl = generateViewAttachmentUrl(attachmentId); saveConfiguration(textConfiguration); logger.info("Attachment uploaded: " + upload.getFileName() + " (" + attachmentId + ")"); IOUtils.closeQuietly(attachmentStream); IOUtils.closeQuietly(fileOutputStream); upload.delete(); logger.debug("Upload resources cleaned"); }
@Button(list = "edit-content", key = "update", order = 1, type = Button.TYPE_PRIMARY) @RequiresPermissions(level = AccessLevel.VIEW, permissions = { PERMISSION_EDIT }) public Resolution updateContent() { title = context.getRequest().getParameter("title"); title = StringUtils.trimToNull(title); if (title == null) { SessionMessages.addErrorMessage(ElementsThreadLocals.getText("title.cannot.be.empty")); return new ForwardResolution("/m/pageactions/text/edit-content.jsp"); } description = context.getRequest().getParameter("description"); description = StringUtils.trimToNull(description); Page page = pageInstance.getPage(); page.setTitle(title); page.setDescription(description); try { DispatcherLogic.savePage(pageInstance.getDirectory(), page); saveContent(); SessionMessages.addInfoMessage(ElementsThreadLocals.getText("object.updated.successfully")); } catch (Exception e) { logger.error("Could not save content for page " + pageInstance.getPath(), e); SessionMessages.addInfoMessage(ElementsThreadLocals.getText("update.failed")); } return cancel(); }
@GET @Produces(MediaType.TEXT_HTML) public Response downloadContent() { try { loadContent(); return Response.ok(content).build(); } catch (IOException e) { logger.error("Could not load content", e); return Response.serverError().entity(e).build(); } }
@RequiresPermissions(level = AccessLevel.VIEW, permissions = { PERMISSION_EDIT }) @RequiresAuthentication public Resolution uploadAttachmentFromCKEditor() { try { uploadDownloadable = false; commonUploadAttachment(); message = null; } catch (IOException e) { message = "File upload failed!"; logger.error("Upload failed", e); } return new ForwardResolution( "/m/pageactions/text/upload-attachment.jsp"); }
public List<Attachment> getDownloadableAttachments() { List<Attachment> downloadableAttachments = new ArrayList<Attachment>(); for(Attachment attachment : getTextConfiguration().getAttachments()) { if(attachment.isDownloadable()) { downloadableAttachments.add(attachment); } } return downloadableAttachments; } }
@Button(list = "pageHeaderButtons", titleKey = "configure", order = 1, icon = Button.ICON_WRENCH, group = "pageHeaderButtons") @RequiresPermissions(level = AccessLevel.EDIT) @RequiresAuthentication public Resolution configurePage() { prepareConfigurationForms(); return new ForwardResolution("/m/pageactions/text/configure.jsp"); }
@DefaultHandler public Resolution execute() throws IOException { loadContent(); if (StringUtils.isEmpty(content)) { content = "<em>Empty content. To add content, configure this page.</em>"; } return new ForwardResolution("/m/pageactions/text/read.jsp"); }
@Button(list = "manage-attachments-upload", key = "text.attachment.upload", order = 1 , icon=Button.ICON_UPLOAD ) @RequiresPermissions(level = AccessLevel.VIEW, permissions = { PERMISSION_EDIT }) @RequiresAuthentication public Resolution uploadAttachment() { if (upload == null) { SessionMessages.addWarningMessage(ElementsThreadLocals.getText("text.attachment.noFileSelected")); } else { try { commonUploadAttachment(); SessionMessages.addInfoMessage(ElementsThreadLocals.getText("text.attachment.uploadSuccessful")); } catch (IOException e) { logger.error("Upload failed", e); SessionMessages.addErrorMessage(ElementsThreadLocals.getText("text.attachment.uploadFailed")); } } return new RedirectResolution(context.getActionPath()) .addParameter("manageAttachments") .addParameter("returnUrl", returnUrl); }
@Button(list = "pageHeaderButtons", titleKey = "edit", order = 2, icon = Button.ICON_EDIT, group = "pageHeaderButtons") @RequiresPermissions(level = AccessLevel.VIEW, permissions = { PERMISSION_EDIT }) @RequiresAuthentication public Resolution configure() { title = pageInstance.getPage().getTitle(); description = pageInstance.getPage().getDescription(); try { loadContent(); logger.debug("Edit content: {}", textFile.getAbsolutePath()); } catch (IOException e) { logger.error("Could not load content", e); SessionMessages.addErrorMessage("Could not load content: " + e); } return new ForwardResolution("/m/pageactions/text/edit-content.jsp"); }
protected String restoreAttachmentUrls(String content) { Pattern pattern = Pattern.compile(PORTOFINO_ATTACHMENT_PATTERN); Matcher matcher = pattern.matcher(content); int lastEnd = 0; StringBuilder sb = new StringBuilder(); while (matcher.find()) { String attachmentId = matcher.group(1); //Default to src for old texts String hrefAttribute = (matcher.groupCount() >= 3 && matcher.group(3) != null) ? matcher.group(3) : "src"; sb.append(content.substring(lastEnd, matcher.start())) .append(hrefAttribute).append("=\"") .append(StringEscapeUtils.escapeHtml(generateViewAttachmentUrl(attachmentId))) .append("\""); lastEnd = matcher.end(); } sb.append(content.substring(lastEnd)); return sb.toString(); }