/** * Publish a given Bundle file * * @param request HttpRequest * @param response HttpResponse * @throws FileUploadException If fails uploading the file * @throws IOException If fails reading the given File content or sending back to the user a * response */ public void uploadBundle(HttpServletRequest request, HttpServletResponse response) throws FileUploadException, IOException { try { if (!APILocator.getLayoutAPI() .doesUserHaveAccessToPortlet("EXT_CONTENT_PUBLISHING_TOOL", getUser())) { response.sendError(401); return; } } catch (DotDataException e1) { Logger.error(RemotePublishAjaxAction.class, e1.getMessage(), e1); response.sendError(401); return; } FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); @SuppressWarnings("unchecked") List<FileItem> items = (List<FileItem>) upload.parseRequest(request); InputStream bundle = items.get(0).getInputStream(); String bundleName = items.get(0).getName(); String bundlePath = ConfigUtils.getBundlePath() + File.separator; String bundleFolder = bundleName.substring(0, bundleName.indexOf(".tar.gz")); String endpointId = getUser().getUserId(); response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); PublishAuditStatus status; try { status = PublishAuditAPI.getInstance().updateAuditTable(endpointId, null, bundleFolder); // Write file on FS FileUtil.writeToFile(bundle, bundlePath + bundleName); if (!status.getStatus().equals(Status.PUBLISHING_BUNDLE)) { new Thread(new PublishThread(bundleName, null, endpointId, status)).start(); } out.print( "<html><head><script>isLoaded = true;</script></head><body><textarea>{'status':'success'}</textarea></body></html>"); } catch (DotPublisherException e) { // TODO Auto-generated catch block out.print( "<html><head><script>isLoaded = true;</script></head><body><textarea>{'status':'error'}</textarea></body></html>"); } }
public static String retIMGURL(String imgUrl) { if (!imgUrl.startsWith(ConfigUtils.getProperty("kaka.qiniu.server"))) { imgUrl = ConfigUtils.getProperty("kaka.qiniu.server") + imgUrl; } return imgUrl; }
@SuppressWarnings("unchecked") public static InputStream buildStream(HTMLPage htmlPage, Identifier identifier, boolean EDIT_MODE) throws DotDataException, DotSecurityException { String folderPath = (!EDIT_MODE) ? "live/" : "working/"; InputStream result; StringBuilder sb = new StringBuilder(); ContentletAPI conAPI = APILocator.getContentletAPI(); Template cmsTemplate = com.dotmarketing.portlets.htmlpages.factories.HTMLPageFactory.getHTMLPageTemplate( htmlPage, EDIT_MODE); if (cmsTemplate == null || !InodeUtils.isSet(cmsTemplate.getInode())) { Logger.error( This.class, "PAGE DOES NOT HAVE A VALID TEMPLATE (template unpublished?) : page id " + htmlPage.getIdentifier() + ":" + identifier.getURI()); } // gets pageChannel for this path java.util.StringTokenizer st = new java.util.StringTokenizer(String.valueOf(identifier.getURI()), "/"); String pageChannel = null; if (st.hasMoreTokens()) { pageChannel = st.nextToken(); } // set the page cache var if (htmlPage.getCacheTTL() > 0 && LicenseUtil.getLevel() > 99) { sb.append("#set($dotPageCacheDate = \"").append(new java.util.Date()).append("\")"); sb.append("#set($dotPageCacheTTL = \"").append(htmlPage.getCacheTTL()).append("\")"); } // set the host variables HTMLPageAPI htmlPageAPI = APILocator.getHTMLPageAPI(); Host host = htmlPageAPI.getParentHost(htmlPage); sb.append("#if(!$doNotParseTemplate)"); sb.append("$velutil.mergeTemplate('") .append(folderPath) .append(host.getIdentifier()) .append(".") .append(Config.getStringProperty("VELOCITY_HOST_EXTENSION")) .append("')"); sb.append(" #end "); // creates the context where to place the variables // Build a context to pass to the page sb.append("#if(!$doNotSetPageInfo)"); sb.append("#set ( $quote = '\"' )"); sb.append("#set ($HTMLPAGE_INODE = \"") .append(String.valueOf(htmlPage.getInode())) .append("\" )"); sb.append("#set ($HTMLPAGE_IDENTIFIER = \"") .append(String.valueOf(htmlPage.getIdentifier())) .append("\" )"); sb.append("#set ($HTMLPAGE_TITLE = \"") .append(UtilMethods.espaceForVelocity(htmlPage.getTitle())) .append("\" )"); sb.append( "#set ($HTMLPAGE_FRIENDLY_NAME = \"" + UtilMethods.espaceForVelocity(htmlPage.getFriendlyName())) .append("\" )"); sb.append("#set ($TEMPLATE_INODE = \"") .append(String.valueOf(cmsTemplate.getInode())) .append("\" )"); sb.append("#set ($HTMLPAGE_META = \"") .append(UtilMethods.espaceForVelocity(htmlPage.getMetadata())) .append("\" )"); sb.append("#set ($HTMLPAGE_META = \"#fixBreaks($HTMLPAGE_META)\")"); sb.append("#set ($HTMLPAGE_DESCRIPTION = \"") .append(UtilMethods.espaceForVelocity(htmlPage.getSeoDescription())) .append("\" )"); sb.append("#set ($HTMLPAGE_DESCRIPTION = \"#fixBreaks($HTMLPAGE_DESCRIPTION)\")"); sb.append("#set ($HTMLPAGE_KEYWORDS = \"") .append(UtilMethods.espaceForVelocity(htmlPage.getSeoKeywords())) .append("\" )"); sb.append("#set ($HTMLPAGE_KEYWORDS = \"#fixBreaks($HTMLPAGE_KEYWORDS)\")"); sb.append("#set ($HTMLPAGE_SECURE = \"") .append(String.valueOf(htmlPage.isHttpsRequired())) .append("\" )"); sb.append("#set ($VTLSERVLET_URI = \"") .append(UtilMethods.encodeURIComponent(identifier.getURI())) .append("\" )"); sb.append("#set ($HTMLPAGE_REDIRECT = \"") .append(UtilMethods.espaceForVelocity(htmlPage.getRedirect())) .append("\" )"); sb.append("#set ($pageTitle = \"") .append(UtilMethods.espaceForVelocity(htmlPage.getTitle())) .append("\" )"); sb.append("#set ($pageChannel = \"").append(pageChannel).append("\" )"); sb.append("#set ($friendlyName = \"") .append(UtilMethods.espaceForVelocity(htmlPage.getFriendlyName())) .append("\" )"); Date moddate = null; if (UtilMethods.isSet(htmlPage.getModDate())) { moddate = htmlPage.getModDate(); } else { moddate = htmlPage.getStartDate(); } moddate = new Timestamp(moddate.getTime()); sb.append("#set ($HTML_PAGE_LAST_MOD_DATE= $date.toDate(\"yyyy-MM-dd HH:mm:ss.SSS\", \"") .append(moddate) .append("\"))"); sb.append("#set ($HTMLPAGE_MOD_DATE= $date.toDate(\"yyyy-MM-dd HH:mm:ss.SSS\", \"") .append(moddate) .append("\"))"); sb.append(" #end "); // get the containers for the page and stick them in context // List identifiers = InodeFactory.getChildrenClass(cmsTemplate, Identifier.class); List<Container> containerList = APILocator.getTemplateAPI() .getContainersInTemplate(cmsTemplate, APILocator.getUserAPI().getSystemUser(), false); Iterator i = containerList.iterator(); while (i.hasNext()) { Container ident = (Container) i.next(); Container c = null; if (EDIT_MODE) { c = (Container) APILocator.getVersionableAPI() .findWorkingVersion( ident.getIdentifier(), APILocator.getUserAPI().getSystemUser(), false); } else { c = (Container) APILocator.getVersionableAPI() .findLiveVersion( ident.getIdentifier(), APILocator.getUserAPI().getSystemUser(), false); } // sets container to load the container file sb.append("#set ($container") .append(ident.getIdentifier()) .append(" = \"") .append(folderPath) .append(ident.getIdentifier()) .append(".") .append(Config.getStringProperty("VELOCITY_CONTAINER_EXTENSION")) .append("\" )"); String sort = (c.getSortContentletsBy() == null) ? "tree_order" : c.getSortContentletsBy(); boolean dynamicContainer = UtilMethods.isSet(c.getLuceneQuery()); int langCounter = 0; List<Contentlet> contentlets = new ArrayList<Contentlet>(); List<Contentlet> contentletsFull = new ArrayList<Contentlet>(); if (!dynamicContainer) { Identifier idenHtmlPage = APILocator.getIdentifierAPI().find(htmlPage); Identifier idenContainer = APILocator.getIdentifierAPI().find(c); // The container doesn't have categories try { contentlets = conAPI.findPageContentlets( idenHtmlPage.getId(), idenContainer.getId(), sort, EDIT_MODE, -1, APILocator.getUserAPI().getSystemUser(), false); if (EDIT_MODE) contentletsFull = contentlets; else contentletsFull = conAPI.findPageContentlets( idenHtmlPage.getId(), idenContainer.getId(), sort, true, -1, APILocator.getUserAPI().getSystemUser(), false); } catch (Exception e) { Logger.error(PageServices.class, "Unable to retrive contentlets on page", e); } Logger.debug( PageServices.class, "HTMLPage= " + htmlPage.getInode() + " Container=" + c.getInode() + " Language=-1 Contentlets=" + contentlets.size()); } // this is to filter the contentlets list removing the repited identifiers if (contentlets.size() > 0) { Set<String> contentletIdentList = new HashSet<String>(); List<Contentlet> contentletsFilter = new ArrayList<Contentlet>(); for (Contentlet cont : contentlets) { if (!contentletIdentList.contains(cont.getIdentifier())) { contentletIdentList.add(cont.getIdentifier()); contentletsFilter.add(cont); } } contentlets = contentletsFilter; } if (contentletsFull.size() > 0) { Set<String> contentletIdentList = new HashSet<String>(); List<Contentlet> contentletsFilter = new ArrayList<Contentlet>(); for (Contentlet cont : contentletsFull) { if (!contentletIdentList.contains(cont.getIdentifier())) { contentletIdentList.add(cont.getIdentifier()); contentletsFilter.add(cont); } } contentletsFull = contentletsFilter; } StringBuilder widgetpree = new StringBuilder(); StringBuilder widgetpreeFull = new StringBuilder(); StringBuilder contentletList = new StringBuilder(); int count = 0; for (Contentlet contentlet : contentlets) { contentletList .append(count == 0 ? "" : ",") .append('"') .append(contentlet.getIdentifier()) .append('"'); if (contentlet.getStructure().getStructureType() == Structure.STRUCTURE_TYPE_WIDGET) { Field field = contentlet.getStructure().getFieldVar("widgetPreexecute"); if (field != null && UtilMethods.isSet(field.getValues())) widgetpree.append(field.getValues().trim()); } if (++count >= c.getMaxContentlets()) break; } StringBuilder contentletListFull = new StringBuilder(); int countFull = 0; for (Contentlet contentlet : contentletsFull) { contentletListFull .append(countFull == 0 ? "" : ",") .append('"') .append(contentlet.getIdentifier()) .append('"'); if (contentlet.getStructure().getStructureType() == Structure.STRUCTURE_TYPE_WIDGET) { Field field = contentlet.getStructure().getFieldVar("widgetPreexecute"); if (field != null && UtilMethods.isSet(field.getValues())) widgetpreeFull.append(field.getValues().trim()); } if (++countFull >= c.getMaxContentlets()) break; } sb.append("#if($request.session.getAttribute(\"tm_date\"))"); sb.append(widgetpreeFull); sb.append("#set ($contentletList") .append(ident.getIdentifier()) .append(" = [") .append(contentletListFull.toString()) .append("] )"); sb.append("#set ($totalSize") .append(ident.getIdentifier()) .append("=") .append(countFull) .append(")"); sb.append("#else "); sb.append(widgetpree); sb.append("#set ($contentletList") .append(ident.getIdentifier()) .append(" = [") .append(contentletList.toString()) .append("] )"); sb.append("#set ($totalSize") .append(ident.getIdentifier()) .append("=") .append(count) .append(")"); sb.append("#end "); langCounter++; } if (htmlPage.isHttpsRequired()) { sb.append(" #if(!$ADMIN_MODE && !$request.isSecure())"); sb.append(" #if($request.getQueryString())"); sb.append( " #set ($REDIRECT_URL = \"https://${request.getServerName()}$request.getAttribute('javax.servlet.forward.request_uri')?$request.getQueryString()\")"); sb.append(" #else "); sb.append( " #set ($REDIRECT_URL = \"https://${request.getServerName()}$request.getAttribute('javax.servlet.forward.request_uri')\")"); sb.append(" #end "); sb.append(" $response.sendRedirect(\"$REDIRECT_URL\")"); sb.append(" #end "); } sb.append("#if($HTMLPAGE_REDIRECT != \"\")"); sb.append(" $response.sendRedirect(\"$HTMLPAGE_REDIRECT\")"); sb.append("#end"); Identifier iden = APILocator.getIdentifierAPI().find(cmsTemplate); sb.append("#if(!$doNotParseTemplate)"); if (cmsTemplate.isDrawed()) { // We have a designed template // Setting some theme variables sb.append("#set ($dotTheme = $templatetool.theme(\"") .append(cmsTemplate.getTheme()) .append("\",\"") .append(host.getIdentifier()) .append("\"))"); sb.append("#set ($dotThemeLayout = $templatetool.themeLayout(\"") .append(cmsTemplate.getInode()) .append("\" ))"); // Merging our template sb.append("$velutil.mergeTemplate(\"$dotTheme.templatePath\")"); } else { sb.append("$velutil.mergeTemplate('") .append(folderPath) .append(iden.getInode()) .append(".") .append(Config.getStringProperty("VELOCITY_TEMPLATE_EXTENSION")) .append("')"); } sb.append("#end"); try { if (Config.getBooleanProperty("SHOW_VELOCITYFILES", false)) { String realFolderPath = (!EDIT_MODE) ? "live" + java.io.File.separator : "working" + java.io.File.separator; String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); String filePath = realFolderPath + identifier.getInode() + "." + Config.getStringProperty("VELOCITY_HTMLPAGE_EXTENSION"); if (velocityRootPath.startsWith("/WEB-INF")) { velocityRootPath = com.liferay.util.FileUtil.getRealPath(velocityRootPath); } velocityRootPath += java.io.File.separator; java.io.BufferedOutputStream tmpOut = new java.io.BufferedOutputStream( new java.io.FileOutputStream( new java.io.File( ConfigUtils.getDynamicVelocityPath() + java.io.File.separator + filePath))); // Specify a proper character encoding OutputStreamWriter out = new OutputStreamWriter(tmpOut, UtilMethods.getCharsetConfiguration()); out.write(sb.toString()); out.flush(); out.close(); tmpOut.close(); } } catch (Exception e) { Logger.error(PageServices.class, e.toString(), e); } try { result = new ByteArrayInputStream(sb.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException e1) { result = new ByteArrayInputStream(sb.toString().getBytes()); Logger.error(ContainerServices.class, e1.getMessage(), e1); } return result; }
/** * Generates and flush an Unpublish bundle for a given bundle id and operation (publish/unpublish) * * @param request HttpRequest * @param response HttpResponse * @throws IOException If fails sending back to the user response information */ public void downloadUnpushedBundle(HttpServletRequest request, HttpServletResponse response) throws IOException { try { if (!APILocator.getLayoutAPI() .doesUserHaveAccessToPortlet("EXT_CONTENT_PUBLISHING_TOOL", getUser())) { response.sendError(401); return; } } catch (DotDataException e1) { Logger.error(RemotePublishAjaxAction.class, e1.getMessage(), e1); response.sendError(401); return; } // Read the parameters Map<String, String> map = getURIParams(); String bundleId = map.get("bundleId"); String paramOperation = map.get("operation"); if (bundleId == null || bundleId.isEmpty()) { Logger.error(this.getClass(), "No Bundle Found with id: " + bundleId); response.sendError(500, "No Bundle Found with id: " + bundleId); return; } // What we want to do with this bundle PushPublisherConfig.Operation operation = PushPublisherConfig.Operation.PUBLISH; if (paramOperation != null && paramOperation.equalsIgnoreCase("unpublish")) { operation = PushPublisherConfig.Operation.UNPUBLISH; } File bundle; String generatedBundleId; try { // Generate the bundle file for this given operation Map<String, Object> bundleData = generateBundle(bundleId, operation); bundle = (File) bundleData.get("file"); generatedBundleId = (String) bundleData.get("id"); } catch (Exception e) { Logger.error(this.getClass(), "Error trying to generate bundle with id: " + bundleId, e); response.sendError(500, "Error trying to generate bundle with id: " + bundleId); return; } response.setContentType("application/x-tgz"); response.setHeader("Content-Disposition", "attachment; filename=" + bundle.getName()); BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(bundle)); byte[] buf = new byte[4096]; int len; while ((len = in.read(buf, 0, buf.length)) != -1) { response.getOutputStream().write(buf, 0, len); } } catch (Exception e) { Logger.error(this.getClass(), "Error reading bundle stream for bundle id: " + bundleId, e); response.sendError(500, "Error reading bundle stream for bundle id: " + bundleId); } finally { try { in.close(); } catch (Exception ex) { Logger.error(this.getClass(), "Error closing Stream for bundle: " + bundleId, ex); } // Clean the just created bundle because on each download we will generate a new bundle file // with a new id in order to avoid conflicts with ids File bundleRoot = BundlerUtil.getBundleRoot(generatedBundleId); File compressedBundle = new File(ConfigUtils.getBundlePath() + File.separator + generatedBundleId + ".tar.gz"); if (compressedBundle.exists()) { compressedBundle.delete(); if (bundleRoot.exists()) { com.liferay.util.FileUtil.deltree(bundleRoot); } } } }
/** * Allow the user to send or try to send again failed and successfully sent bundles, in order to * do that<br> * we send the bundle again to que publisher queue job which will try to remote publish again the * bundle. * * @param request HttpRequest * @param response HttpResponse * @throws IOException If fails sending back to the user a proper response * @throws DotPublisherException If fails retrieving the Bundle related information like elements * on it and statuses * @throws LanguageException If fails using i18 messages */ public void retry(HttpServletRequest request, HttpServletResponse response) throws IOException, DotPublisherException, LanguageException { PublisherAPI publisherAPI = PublisherAPI.getInstance(); PublishAuditAPI publishAuditAPI = PublishAuditAPI.getInstance(); // Read the parameters String bundlesIds = request.getParameter("bundlesIds"); String[] ids = bundlesIds.split(","); StringBuilder responseMessage = new StringBuilder(); for (String bundleId : ids) { if (bundleId.trim().isEmpty()) { continue; } PublisherConfig basicConfig = new PublisherConfig(); basicConfig.setId(bundleId); File bundleRoot = BundlerUtil.getBundleRoot(basicConfig); // Get the audit records related to this bundle PublishAuditStatus status = PublishAuditAPI.getInstance().getPublishAuditStatus(bundleId); String pojo_string = status.getStatusPojo().getSerialized(); PublishAuditHistory auditHistory = PublishAuditHistory.getObjectFromString(pojo_string); // First we need to verify is this bundle is already in the queue job List<PublishQueueElement> foundBundles = publisherAPI.getQueueElementsByBundleId(bundleId); if (foundBundles != null && !foundBundles.isEmpty()) { appendMessage(responseMessage, "publisher_retry.error.already.in.queue", bundleId, true); continue; } // We will be able to retry failed and successfully bundles if (!(status.getStatus().equals(Status.FAILED_TO_PUBLISH) || status.getStatus().equals(Status.SUCCESS))) { appendMessage(responseMessage, "publisher_retry.error.only.failed.publish", bundleId, true); continue; } /* Verify if the bundle exist and was created correctly..., meaning, if there is not a .tar.gz file is because something happened on the creation of the bundle. */ File bundleFile = new File( bundleRoot + File.separator + ".." + File.separator + basicConfig.getId() + ".tar.gz"); if (!bundleFile.exists()) { Logger.error(this.getClass(), "No Bundle with id: " + bundleId + " found."); appendMessage(responseMessage, "publisher_retry.error.not.found", bundleId, true); continue; } if (!BundlerUtil.bundleExists(basicConfig)) { Logger.error( this.getClass(), "No Bundle Descriptor for bundle id: " + bundleId + " found."); appendMessage( responseMessage, "publisher_retry.error.not.descriptor.found", bundleId, true); continue; } try { // Read the bundle to see what kind of configuration we need to apply String bundlePath = ConfigUtils.getBundlePath() + File.separator + basicConfig.getId(); File xml = new File(bundlePath + File.separator + "bundle.xml"); PushPublisherConfig config = (PushPublisherConfig) BundlerUtil.xmlToObject(xml); // We can not retry Received Bundles, just bundles that we are trying to send Boolean sending = sendingBundle(request, config, bundleId); if (!sending) { appendMessage( responseMessage, "publisher_retry.error.cannot.retry.received", bundleId, true); continue; } if (status.getStatus().equals(Status.SUCCESS)) { // Get the bundle Bundle bundle = APILocator.getBundleAPI().getBundleById(bundleId); if (bundle == null) { Logger.error(this.getClass(), "No Bundle with id: " + bundleId + " found."); appendMessage(responseMessage, "publisher_retry.error.not.found", bundleId, true); continue; } bundle.setForcePush(true); APILocator.getBundleAPI().updateBundle(bundle); } // Clean the number of tries, we want to try it again auditHistory.setNumTries(0); publishAuditAPI.updatePublishAuditStatus( config.getId(), status.getStatus(), auditHistory, true); // Get the identifiers on this bundle HashSet<String> identifiers = new HashSet<String>(); List<PublishQueueElement> assets = config.getAssets(); if (config.getLuceneQueries() != null && !config.getLuceneQueries().isEmpty()) { identifiers.addAll(PublisherUtil.getContentIds(config.getLuceneQueries())); } if (assets != null && !assets.isEmpty()) { for (PublishQueueElement asset : assets) { identifiers.add(asset.getAsset()); } } // Now depending of the operation lets add it to the queue job if (config.getOperation().equals(PushPublisherConfig.Operation.PUBLISH)) { publisherAPI.addContentsToPublish( new ArrayList<String>(identifiers), bundleId, new Date(), getUser()); } else { publisherAPI.addContentsToUnpublish( new ArrayList<String>(identifiers), bundleId, new Date(), getUser()); } // Success... appendMessage(responseMessage, "publisher_retry.success", bundleId, false); } catch (Exception e) { Logger.error( this.getClass(), "Error trying to add bundle id: " + bundleId + " to the Publishing Queue.", e); appendMessage(responseMessage, "publisher_retry.error.adding.to.queue", bundleId, true); } } response.getWriter().println(responseMessage.toString()); }