001/* 002 * Copyright 2023 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.site; 017 018import java.util.List; 019import java.util.Map; 020import java.util.Optional; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025import org.apache.commons.lang3.StringUtils; 026import org.apache.commons.lang3.tuple.Pair; 027 028import org.ametys.core.ObservationConstants; 029import org.ametys.core.observation.AsyncObserver; 030import org.ametys.core.observation.Event; 031import org.ametys.core.user.UserIdentity; 032import org.ametys.runtime.plugin.component.AbstractLogEnabled; 033import org.ametys.runtime.servlet.RuntimeServlet; 034import org.ametys.web.cache.FOCommHelper; 035 036/** 037 * Observer that transmit maintenance mode modifications to the front 038 */ 039public class MaintenanceObserver extends AbstractLogEnabled implements AsyncObserver, Serviceable 040{ 041 private FOCommHelper _foCommHelper; 042 043 public void service(ServiceManager manager) throws ServiceException 044 { 045 _foCommHelper = (FOCommHelper) manager.lookup(FOCommHelper.ROLE); 046 } 047 048 public boolean supports(Event event) 049 { 050 return event.getId().equals(ObservationConstants.EVENT_RUNTIME_MAINTENANCE) 051 && RuntimeServlet.getMaintenanceStatus() != RuntimeServlet.MaintenanceStatus.AUTOMATIC; // Changing to automatic can only happens at startup, and we want to avoid to call the front during startup 052 } 053 054 public int getPriority() 055 { 056 return 0; 057 } 058 059 public void observe(Event event, Map<String, Object> transientVars) throws Exception 060 { 061 List<Pair<String, String>> params = List.of( 062 Pair.of("status", RuntimeServlet.getMaintenanceStatus().toString()), 063 Pair.of("comment", Optional.ofNullable(RuntimeServlet.getMaintenanceStatusForcedInformations()).map(s -> StringUtils.defaultString(s.comment())).orElse("")), 064 Pair.of("initiator", Optional.ofNullable(RuntimeServlet.getMaintenanceStatusForcedInformations()).map(s -> UserIdentity.userIdentityToString(s.initiator())).orElse("")) 065 ); 066 067 _foCommHelper.testWS("/set-maintenance-mode", params); 068 } 069}