seconds = MAX_NUMBER_OF_SECONDS;
} else if (seconds < 0) {
seconds = DEFAULT_NUMBER_OF_SECONDS;
}
ReplicationTrigger replicationTrigger = request.getResource().adaptTo(ReplicationTrigger.class);
// setup SSE headers
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Connection", "keep-alive");
// needed to allow e.g. the JavaScript EventSource API to make a call from author to this server and listen for the events
// response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); // allowed origins should be explicitly configured
// response.setHeader("Access-Control-Allow-Credentials", "true");
final PrintWriter writer = response.getWriter();
ReplicationRequestHandler replicationRequestHandler = new ReplicationRequestHandler() {
public void handle(@Nonnull ReplicationRequest request) {
writeEvent(writer, request);
}
};
try {
replicationTrigger.register(replicationRequestHandler);
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
log.error("thread interrupted", e);
}
replicationTrigger.unregister(replicationRequestHandler);
} catch (ReplicationTriggerException e) {
response.setStatus(400);
response.getWriter().write("error while (un)registering trigger " + e.toString());
}