package org.stagemonitor.alerting.alerter;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.stagemonitor.alerting.AlertingPlugin;
import org.stagemonitor.alerting.incident.IncidentRepository;
import org.stagemonitor.core.Stagemonitor;
import org.stagemonitor.core.util.JsonUtils;

@WebServlet(urlPatterns = "/stagemonitor/incidents")
public class IncidentServlet extends HttpServlet {

	private final IncidentRepository incidentRepository;

	public IncidentServlet() {
		this(Stagemonitor.getConfiguration(AlertingPlugin.class));
	}

	public IncidentServlet(AlertingPlugin alertingPlugin) {
		this.incidentRepository = alertingPlugin.getIncidentRepository();
	}

	/**
	 * GET /stagemonitor/incidents
	 *
	 * Returns all current incidents
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		JsonUtils.writeJsonToOutputStream(incidentRepository.getAllIncidents(), resp.getOutputStream());
	}

}

@@ -57,32 +57,62 @@
 io.IOException;%0A
+import java.util.Collections;%0A
 import javax.ser
@@ -126,32 +126,32 @@
 rvletException;%0A
-
 import javax.ser
@@ -635,45 +635,37 @@
 nal 
-IncidentRepository incidentRepository
+AlertingPlugin alertingPlugin
 ;%0A%0A%09
@@ -824,67 +824,39 @@
 his.
-incidentRepository = alertingPlugin.getIncidentRepository()
+alertingPlugin = alertingPlugin
 ;%0A%09%7D
@@ -1065,104 +1065,325 @@
 %7B%0A%09%09
-JsonUtils.writeJsonToOutputStream(incidentRepository.getAllIncidents(), resp.getOutputStream());
+IncidentRepository incidentRepository = alertingPlugin.getIncidentRepository();%0A%09%09if (incidentRepository != null) %7B%0A%09%09%09JsonUtils.writeJsonToOutputStream(incidentRepository.getAllIncidents(), resp.getOutputStream());%0A%09%09%7D else %7B%0A%09%09%09JsonUtils.writeJsonToOutputStream(Collections.emptyList(), resp.getOutputStream());%0A%09%09%7D
 %0A%09%7D%0A

Return empty JSON array if IncidentRepository is not yet initialized