1 package org.jahia.engines.importexport; 2 3 import org.apache.log4j.Logger; 4 import org.jahia.content.ContentObject; 5 import org.jahia.content.ContentObjectKey; 6 import org.jahia.data.JahiaData; 7 import org.jahia.engines.EngineToolBox; 8 import org.jahia.engines.JahiaEngine; 9 import org.jahia.exceptions.JahiaException; 10 import org.jahia.params.ParamBean; 11 import org.jahia.registries.ServicesRegistry; 12 import org.jahia.services.importexport.ImportExportService; 13 import org.jahia.services.sites.SiteLanguageSettings; 14 import org.jahia.services.sites.JahiaSite; 15 import org.jahia.services.version.EntryLoadRequest; 16 17 import javax.servlet.ServletOutputStream ; 18 import javax.servlet.http.HttpServletResponse ; 19 import java.net.HttpURLConnection ; 20 import java.util.*; 21 22 29 public class ExportEngine implements JahiaEngine { 30 private static Logger logger = Logger.getLogger(ExportEngine.class); 31 public static final String ENGINE_NAME = "export"; 32 33 private static ExportEngine instance; 34 35 private EngineToolBox toolBox; 36 37 public ExportEngine() { 38 logger.debug("Starting Import Export Engine"); 39 toolBox = EngineToolBox.getInstance(); 40 } 41 42 45 public static synchronized ExportEngine getInstance () { 46 if (instance == null) { 47 instance = new ExportEngine (); 48 } 49 return instance; 50 } 51 52 53 public boolean authoriseRender(ParamBean processingContext) { 54 return true; 55 } 56 57 public String getName() { 58 return ENGINE_NAME; 59 } 60 61 public String renderLink(ParamBean processingContext, Object theObj) throws JahiaException { 62 return processingContext.composeEngineUrl (ENGINE_NAME); 64 } 65 66 public boolean needsJahiaData(ParamBean processingContext) { 67 return false; 68 } 69 70 public void handleActions(ParamBean processingContext, JahiaData jData) throws JahiaException { 71 try { 72 Map engineMap = new HashMap(); 73 74 export(processingContext); 75 engineMap.put (JahiaEngine.RENDER_TYPE_PARAM, new Integer (-1)); 76 77 toolBox.displayScreen(processingContext, (HashMap) engineMap); 78 } catch (Exception e) { 79 throw new JahiaException("Exception occured","Exception occured",JahiaException.ENGINE_ERROR,JahiaException.ERROR_SEVERITY,e); 80 } 81 } 82 83 private void export(ParamBean processingContext) throws Exception { 84 HttpServletResponse resp = processingContext.getRealResponse(); 85 ServletOutputStream outputStream = resp.getOutputStream(); 86 resp.setHeader("Expires", "Thu, 01 Jan 1970 00:00:00 GMT"); 88 resp.setHeader("Pragma", "no-cache"); 89 resp.setHeader("Cache-Control", "no-cache"); 90 91 Map params = new HashMap(); 92 93 params.put(ImportExportService.VIEW_CONTENT, new Boolean (!"false".equals(processingContext.getParameter("viewContent")))); 94 params.put(ImportExportService.VIEW_VERSION, new Boolean ("true".equals(processingContext.getParameter("viewVersion")))); 95 params.put(ImportExportService.VIEW_ACL, new Boolean (!"false".equals(processingContext.getParameter("viewAcl")))); 96 params.put(ImportExportService.VIEW_METADATA, new Boolean (!"false".equals(processingContext.getParameter("viewMetadata")))); 97 params.put(ImportExportService.VIEW_JAHIALINKS, new Boolean (!"false".equals(processingContext.getParameter("viewLinks")))); 98 boolean enforceLanguage = "true".equals(processingContext.getParameter("enforceLanguage")); 99 100 ImportExportService ie = ServicesRegistry.getInstance().getImportExportService(); 101 102 if ("all".equals(processingContext.getParameter("exportformat"))) { 103 if (!processingContext.getUser().isRoot()) { 104 resp.setStatus(HttpURLConnection.HTTP_UNAUTHORIZED); 105 return; 106 } 107 108 resp.setContentType("application/zip"); 109 params.put(ImportExportService.INCLUDE_ALL_FILES, Boolean.TRUE); 110 params.put(ImportExportService.INCLUDE_TEMPLATES, Boolean.TRUE); 111 params.put(ImportExportService.INCLUDE_SITE_INFOS, Boolean.TRUE); 112 113 ie.exportAll(outputStream, params, processingContext); 114 outputStream.close(); 115 return; 116 } else if ("site".equals(processingContext.getParameter("exportformat"))) { 117 if (!processingContext.getUser().isRoot()) { 118 resp.setStatus(HttpURLConnection.HTTP_UNAUTHORIZED); 119 return; 120 } 121 122 String sitekey = processingContext.getParameter("site"); 123 JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService().getSiteByKey(sitekey); 124 125 resp.setContentType("application/zip"); 126 params.put(ImportExportService.INCLUDE_ALL_FILES, Boolean.TRUE); 127 params.put(ImportExportService.INCLUDE_TEMPLATES, Boolean.TRUE); 128 params.put(ImportExportService.INCLUDE_SITE_INFOS, Boolean.TRUE); 129 130 ie.exportSite(site, outputStream, processingContext, params); 131 outputStream.close(); 132 return; 133 } else if ("filesacl".equals(processingContext.getParameter("exportformat"))) { 134 resp.setContentType("text/xml"); 135 136 ie.exportAllFilesAcl(outputStream, processingContext); 137 outputStream.close(); 138 return; 139 } 140 141 String key = processingContext.getParameter("key"); 142 ContentObject object; 143 if (key != null) { 144 object = (ContentObject)ContentObject.getInstance(ContentObjectKey.getInstance(key)); 145 } else { 146 object = processingContext.getSite().getHomeContentPage(); 147 } 148 149 if (object == null) { 150 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND); 151 return; 152 } 153 154 if (!object.checkReadAccess(processingContext.getUser())) { 155 resp.setStatus(HttpURLConnection.HTTP_UNAUTHORIZED); 156 return; 157 } 158 159 Vector siteLanguageSettings = processingContext.getSite ().getLanguageSettings (); 160 Set languageCodes = new HashSet(); 161 if (siteLanguageSettings != null) { 162 for (int i = 0; i < siteLanguageSettings.size (); i++) { 163 SiteLanguageSettings curSetting = (SiteLanguageSettings) 164 siteLanguageSettings.elementAt (i); 165 if (curSetting.isActivated ()) { 166 languageCodes.add(curSetting.getCode()); 167 } 168 } 169 } 170 171 if (enforceLanguage && processingContext.getParameter("lang") != null ) { 172 if (!processingContext.getParameter("lang").equals(processingContext.getLocale().toString())) { 173 resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND); 174 return; 175 } 176 } 177 178 if ("diff".equals(processingContext.getParameter("exporttype"))) { 180 params.put(ImportExportService.FROM, EntryLoadRequest.CURRENT); 181 params.put(ImportExportService.TO, EntryLoadRequest.STAGED); 182 } else if ("staging".equals(processingContext.getParameter("exporttype"))) { 183 params.put(ImportExportService.TO, EntryLoadRequest.STAGED); 184 } 185 186 if ("zipfiles".equals(processingContext.getParameter("exportformat"))) { 187 resp.setContentType("application/zip"); 188 params.put(ImportExportService.INCLUDE_FILES, Boolean.TRUE); 189 ie.exportZip(object, languageCodes, outputStream, processingContext, params); 190 } else if ("zipallfiles".equals(processingContext.getParameter("exportformat"))) { 191 resp.setContentType("application/zip"); 192 params.put(ImportExportService.INCLUDE_ALL_FILES, Boolean.TRUE); 193 ie.exportZip(object, languageCodes, outputStream, processingContext, params); 194 } else if ("zipnofiles".equals(processingContext.getParameter("exportformat"))) { 195 resp.setContentType("application/zip"); 196 ie.exportZip(object, languageCodes, outputStream, processingContext, params); 197 } else { 198 resp.setContentType("text/xml"); 199 ie.exportFile(object, processingContext.getLocale().toString(), outputStream, processingContext, params); 200 } 201 outputStream.close(); 202 } 203 } 204 | Popular Tags |