|
| 1 | +package com.dotcms.enterprise.publishing.staticpublishing; |
| 2 | + |
| 3 | +import com.dotcms.enterprise.LicenseUtil; |
| 4 | +import com.dotcms.enterprise.license.LicenseLevel; |
| 5 | +import com.dotcms.publisher.business.DotPublisherException; |
| 6 | +import com.dotcms.publisher.business.PublisherAPI; |
| 7 | +import com.dotcms.publisher.pusher.PushPublisherConfig; |
| 8 | +import com.dotcms.publisher.pusher.wrapper.FolderWrapper; |
| 9 | +import com.dotcms.publishing.*; |
| 10 | +import com.dotcms.publishing.output.BundleOutput; |
| 11 | +import com.dotmarketing.beans.Host; |
| 12 | +import com.dotmarketing.business.APILocator; |
| 13 | +import com.dotmarketing.business.UserAPI; |
| 14 | +import com.dotmarketing.exception.DotDataException; |
| 15 | +import com.dotmarketing.exception.DotSecurityException; |
| 16 | +import com.dotmarketing.portlets.contentlet.business.ContentletAPI; |
| 17 | +import com.dotmarketing.portlets.folders.business.FolderAPI; |
| 18 | +import com.dotmarketing.portlets.folders.model.Folder; |
| 19 | +import com.dotmarketing.portlets.languagesmanager.business.LanguageAPI; |
| 20 | +import com.dotmarketing.util.Config; |
| 21 | +import com.dotmarketing.util.Logger; |
| 22 | +import com.dotmarketing.util.PushPublishLogger; |
| 23 | +import com.liferay.portal.model.User; |
| 24 | + |
| 25 | +import java.io.File; |
| 26 | +import java.io.FileFilter; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Set; |
| 32 | +/** |
| 33 | + * This bundler will generate the corresponding folder tree for the bundle when trying to Static Publish a folder |
| 34 | + */ |
| 35 | +public class StaticFolderBundler implements IBundler { |
| 36 | + private PublisherConfig config; |
| 37 | + private User systemUser; |
| 38 | + ContentletAPI contentletAPI = null; |
| 39 | + UserAPI userAPI = null; |
| 40 | + com.dotcms.publisher.business.PublisherAPI publisherAPI = null; |
| 41 | + FolderAPI folderAPI = APILocator.getFolderAPI(); |
| 42 | + LanguageAPI languageAPI = APILocator.getLanguageAPI(); |
| 43 | + |
| 44 | + public final static String FOLDER_EXTENSION = ".folder.xml" ; |
| 45 | + |
| 46 | + @Override |
| 47 | + public String getName() { |
| 48 | + return "Static Folder bundler"; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void setConfig(PublisherConfig pc) { |
| 53 | + config = pc; |
| 54 | + contentletAPI = APILocator.getContentletAPI(); |
| 55 | + userAPI = APILocator.getUserAPI(); |
| 56 | + publisherAPI = PublisherAPI.getInstance(); |
| 57 | + |
| 58 | + try { |
| 59 | + systemUser = userAPI.getSystemUser(); |
| 60 | + } catch (DotDataException e) { |
| 61 | + Logger.fatal(StaticFolderBundler.class,e.getMessage(),e); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void setPublisher(IPublisher publisher) { |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void generate(final BundleOutput output, final BundlerStatus status) |
| 71 | + throws DotBundleException { |
| 72 | + if(LicenseUtil.getLevel() < LicenseLevel.PROFESSIONAL.level) |
| 73 | + throw new RuntimeException("need an enterprise pro license to run this bundler"); |
| 74 | + |
| 75 | + Set<String> folders = (Set<String>) config.get(PublisherConfig.Config.FOLDERS.name()); |
| 76 | + |
| 77 | + try { |
| 78 | + for (String folder : folders) { |
| 79 | + final long defaultLanguage = this.languageAPI.getDefaultLanguage().getId(); |
| 80 | + |
| 81 | + for (final Long langId : getSortedConfigLanguages(this.config, defaultLanguage)) { |
| 82 | + |
| 83 | + writeFolderTree(output, folder, langId); |
| 84 | + } |
| 85 | + } |
| 86 | + } catch (Exception e) { |
| 87 | + status.addFailure(); |
| 88 | + |
| 89 | + throw new DotBundleException(this.getClass().getName() + " : " + "generate()" |
| 90 | + + e.getMessage() + ": Unable to pull content", e); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + private void writeFolderTree(BundleOutput bundleOutput, String idFolder, Long languageId) |
| 98 | + throws DotDataException, DotSecurityException |
| 99 | + { |
| 100 | + //Get Folder tree |
| 101 | + Folder folder = folderAPI.find(idFolder, systemUser, false); |
| 102 | + String folderName = folder.getName(); |
| 103 | + List<String> path = new ArrayList<>(); |
| 104 | + |
| 105 | + Host site = folder.getHost(); |
| 106 | + while(folder != null && !folder.isSystemFolder()) { |
| 107 | + path.add(folder.getName()); |
| 108 | + folder = folderAPI.findParentFolder(folder, systemUser, false); |
| 109 | + } |
| 110 | + |
| 111 | + if(path.size() > 0) { |
| 112 | + Collections.reverse(path); |
| 113 | + StringBuilder b = new StringBuilder(File.separator); |
| 114 | + for (String f : path) { |
| 115 | + b.append(f); |
| 116 | + b.append(File.separator); |
| 117 | + |
| 118 | + // exclude other folders but the one being pushed, when unpublishing |
| 119 | + if(config.getOperation().equals(PushPublisherConfig.Operation.UNPUBLISH) && !f.equals(folderName)) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + String myFolderUrl = File.separator + "live" + File.separator + site.getHostname() + File.separator + languageId + |
| 125 | + b.toString(); |
| 126 | + bundleOutput.mkdirs(myFolderUrl); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if(Config.getBooleanProperty("PUSH_PUBLISHING_LOG_DEPENDENCIES", false)) { |
| 131 | + PushPublishLogger.log(getClass(), "Folder bundled for pushing. Operation: "+config.getOperation()+", Id: "+ idFolder, config.getId()); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public FileFilter getFileFilter(){ |
| 137 | + return new StaticFolderBundler.FolderBundlerFilter(); |
| 138 | + } |
| 139 | + |
| 140 | + public class FolderBundlerFilter implements FileFilter{ |
| 141 | + |
| 142 | + @Override |
| 143 | + public boolean accept(File pathname) { |
| 144 | + |
| 145 | + return (pathname.isDirectory() || pathname.getName().endsWith(FOLDER_EXTENSION)); |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments