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.lang.StringUtils; 025import org.apache.commons.lang3.exception.ExceptionUtils; 026import org.quartz.JobDataMap; 027import org.quartz.JobExecutionContext; 028 029import org.ametys.core.schedule.Schedulable; 030import org.ametys.core.schedule.progression.ContainerProgressionTracker; 031import org.ametys.core.ui.mail.StandardMailBodyHelper; 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) 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 .build(); 098 } 099 catch (IOException e) 100 { 101 getLogger().warn("Failed to build HTML email body for rebuild live result. Fallback to no wrapped email", e); 102 return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_SUCCESS_MAIL_BODY", List.of(siteTitle))); 103 } 104 } 105 106 @Override 107 protected I18nizableText _getErrorMailSubject(JobExecutionContext context) 108 { 109 String siteTitle = _getSiteTitle(context); 110 return new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_SUBJECT", List.of(siteTitle)); 111 } 112 113 @Override 114 protected String _getErrorMailBody(JobExecutionContext context, Throwable throwable) 115 { 116 String siteTitle = _getSiteTitle(context); 117 118 String cmsUrl = StringUtils.stripEnd(StringUtils.removeEndIgnoreCase(Config.getInstance().getValue("cms.url"), "index.html"), "/"); 119 String url = cmsUrl + "/_admin/"; 120 121 String error = ExceptionUtils.getStackTrace(throwable); 122 123 try 124 { 125 return StandardMailBodyHelper.newHTMLBody() 126 .withTitle(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_BODY_TITLE", List.of(siteTitle))) 127 .withMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_BODY", List.of(siteTitle, url))) 128 .withDetails(null, error, true) 129 .build(); 130 } 131 catch (IOException e) 132 { 133 getLogger().warn("Failed to build HTML email body for rebuild live result. Fallback to no wrapped email", e); 134 return _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_BUILDSITE_ERROR_MAIL_BODY", List.of(siteTitle, url, error))); 135 } 136 } 137 138 private String _getSiteTitle(JobExecutionContext context) 139 { 140 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 141 String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY); 142 143 Site site = _getSite(siteAsMap); 144 return site.getTitle() != null ? site.getTitle() : site.getName(); 145 } 146 147 private String _getSiteUrl(JobExecutionContext context) 148 { 149 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 150 String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY); 151 152 Site site = _getSite(siteAsMap); 153 return site.getUrl(); 154 } 155 156 private Site _getSite(String siteAsMap) 157 { 158 Map<String, Object> mapSite = _jsonUtils.convertJsonToMap(siteAsMap); 159 @SuppressWarnings("unchecked") 160 List<String> sites = (List<String>) mapSite.get("sites"); 161 String siteName = sites.get(0); 162 163 return _siteManager.getSite(siteName); 164 } 165}