Skip to content
Snippets Groups Projects
Commit e06b2813 authored by Daniel Gerhardt's avatar Daniel Gerhardt
Browse files

Implement maintenance mode system

Classes can register reasons for maintenance. While at least one reason
is set, all requests are blocked and responded to with a 503 status
code.
parent b6f3e7dd
1 merge request!89Foundation for development of version 3.0
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2018 The ARSnova Team
*
* ARSnova Backend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARSnova Backend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.thm.arsnova.services;
import java.util.Map;
public interface StatusService {
void putMaintenanceReason(Class<?> type, String reason);
void removeMaintenanceReason(Class<?> type);
Map<Class<?>, String> getMaintenanceReasons();
boolean isMaintenanceActive();
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2018 The ARSnova Team
*
* ARSnova Backend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARSnova Backend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.thm.arsnova.services;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* Keeps track of maintenance reasons registered by other components. While at least one maintenance reason is active,
* API access is blocked by {@link de.thm.arsnova.web.MaintenanceModeFilter}.
*
* @author Daniel Gerhardt
*/
@Service
public class StatusServiceImpl implements StatusService {
private final Map<Class<?>, String> maintenanceReasons = new HashMap<>();
@Override
public void putMaintenanceReason(final Class<?> type, final String reason) {
maintenanceReasons.put(type, reason);
}
@Override
public void removeMaintenanceReason(final Class<?> type) {
maintenanceReasons.remove(type);
}
@Override
public Map<Class<?>, String> getMaintenanceReasons() {
return maintenanceReasons;
}
@Override
public boolean isMaintenanceActive() {
return !maintenanceReasons.isEmpty();
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2018 The ARSnova Team
*
* ARSnova Backend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARSnova Backend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.thm.arsnova.web;
import de.thm.arsnova.services.StatusService;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Blocks API requests while maintenance reasons are active.
*
* @author Daniel Gerhardt
*/
@Component
public class MaintenanceModeFilter extends OncePerRequestFilter {
private StatusService statusService;
public MaintenanceModeFilter(final StatusService statusService) {
this.statusService = statusService;
}
@Override
protected void doFilterInternal(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse, final FilterChain filterChain) throws ServletException, IOException {
if (statusService.isMaintenanceActive()) {
httpServletResponse.setStatus(503);
return;
}
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
}
......@@ -79,6 +79,16 @@
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>maintenanceModeFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>maintenanceModeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment