001/* 002 * Copyright 2016 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.live; 017 018import java.io.IOException; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.commons.lang3.exception.ExceptionUtils; 025import org.quartz.JobDataMap; 026import org.quartz.JobExecutionContext; 027 028import org.ametys.core.schedule.Schedulable; 029import org.ametys.core.schedule.progression.ContainerProgressionTracker; 030import org.ametys.core.ui.mail.StandardMailBodyHelper; 031import org.ametys.core.util.HttpUtils; 032import org.ametys.core.util.JSONUtils; 033import org.ametys.plugins.core.schedule.Scheduler; 034import org.ametys.runtime.config.Config; 035import org.ametys.runtime.i18n.I18nizableText; 036import org.ametys.web.repository.site.Site; 037import org.ametys.web.repository.site.SiteManager; 038 039/** 040 * A {@link Schedulable} job which rebuild and populate the live workspace of a site. 041 */ 042public class RebuildLiveSiteWorkspaceSchedulable extends AbstractRebuildLiveWorkspaceSchedulable 043{ 044 /** The key for the site to rebuild the live workspace */ 045 protected static final String JOBDATAMAP_SITE_KEY = "siteName"; 046 047 private static final String __JOBDATAMAP_SITE_KEY = Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_SITE_KEY; 048 049 /** The site amanger */ 050 protected SiteManager _siteManager; 051 052 /** The utils for JSON */ 053 protected JSONUtils _jsonUtils; 054 055 @Override 056 public void service(ServiceManager manager) throws ServiceException 057 { 058 super.service(manager); 059 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 060 _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE); 061 } 062 063 @Override 064 protected void _rebuildLiveWorkspace(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception 065 { 066 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 067 String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY); 068 Site site = _getSite(siteAsMap); 069 070 _rebuildLiveComponent.rebuildLive(site, progressionTracker); 071 } 072 073 @Override 074 protected boolean _isMailBodyInHTML(JobExecutionContext context) throws Exception 075 { 076 return true; 077 } 078 079 @Override 080 protected I18nizableText _getSuccessMailSubject(JobExecutionContext context) 081 { 082 String siteTitle = _getSiteTitle(context); 083 return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_SUCCESS_MAIL_SUBJECT", List.of(siteTitle)); 084 } 085 086 @Override 087 protected String _getSuccessMailBody(JobExecutionContext context, String language) 088 { 089 String siteTitle = _getSiteTitle(context); 090 091 try 092 { 093 return StandardMailBodyHelper.newHTMLBody() 094 .withTitle(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_SUCCESS_MAIL_BODY_TITLE", List.of(siteTitle))) 095 .withMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_SUCCESS_MAIL_BODY", List.of(siteTitle))) 096 .withLink(_getSiteUrl(context), new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_SUCCESS_MAIL_BODY_LINK")) 097 .withLanguage(language) 098 .build(); 099 } 100 catch (IOException e) 101 { 102 getLogger().warn("Failed to build HTML email body for rebuild live result. Fallback to no wrapped email", e); 103 return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_SUCCESS_MAIL_BODY", List.of(siteTitle)), language); 104 } 105 } 106 107 @Override 108 protected I18nizableText _getErrorMailSubject(JobExecutionContext context) 109 { 110 String siteTitle = _getSiteTitle(context); 111 return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_SUBJECT", List.of(siteTitle)); 112 } 113 114 @Override 115 protected String _getErrorMailBody(JobExecutionContext context, String language, Throwable throwable) 116 { 117 String siteTitle = _getSiteTitle(context); 118 119 String cmsUrl = HttpUtils.sanitize(Config.getInstance().getValue("cms.url")); 120 String url = cmsUrl + "/_admin/"; 121 122 String error = ExceptionUtils.getStackTrace(throwable); 123 124 try 125 { 126 return StandardMailBodyHelper.newHTMLBody() 127 .withTitle(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_BODY_TITLE", List.of(siteTitle))) 128 .withMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_BODY", List.of(siteTitle, url))) 129 .withDetails(null, error, true) 130 .withLanguage(language) 131 .build(); 132 } 133 catch (IOException e) 134 { 135 getLogger().warn("Failed to build HTML email body for rebuild live result. Fallback to no wrapped email", e); 136 return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_BODY", List.of(siteTitle, url, error)), language); 137 } 138 } 139 140 private String _getSiteTitle(JobExecutionContext context) 141 { 142 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 143 String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY); 144 145 Site site = _getSite(siteAsMap); 146 return site.getTitle() != null ? site.getTitle() : site.getName(); 147 } 148 149 private String _getSiteUrl(JobExecutionContext context) 150 { 151 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 152 String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY); 153 154 Site site = _getSite(siteAsMap); 155 return site.getUrl(); 156 } 157 158 private Site _getSite(String siteAsMap) 159 { 160 Map<String, Object> mapSite = _jsonUtils.convertJsonToMap(siteAsMap); 161 @SuppressWarnings("unchecked") 162 List<String> sites = (List<String>) mapSite.get("sites"); 163 String siteName = sites.get(0); 164 165 return _siteManager.getSite(siteName); 166 } 167}