boolean retryableCommand = false;
try {
logger.log(Level.FINEST, "Response is SSE - about to read events");
closeSse = false;
ProprietaryReader<GfSseEventReceiver> reader = new GfSseEventReceiverProprietaryReader();
GfSseEventReceiver eventReceiver = reader.readFrom(urlConnection.getInputStream(), resultMediaType);
GfSseInboundEvent event;
do {
event = eventReceiver.readEvent();
if (event != null) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Event: {0}", event.getName());
}
if (event.getName() == null) {
//TODO:
SseDumper.getInstance().dump();
throw new IllegalStateException("Event without name. Data: " + event.getData());
}
fireEvent(event.getName(), event);
if (AdminCommandState.EVENT_STATE_CHANGED.equals(event.getName())) {
AdminCommandState acs = event.getData(AdminCommandState.class, MEDIATYPE_JSON);
if (acs.getId() != null) {
instanceId = acs.getId();
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Command instance ID: {0}", instanceId);
}
}
if (acs.getState() == AdminCommandState.State.COMPLETED ||
acs.getState() == AdminCommandState.State.RECORDED ||
acs.getState() == AdminCommandState.State.REVERTED) {
if (acs.getActionReport() != null) {
setActionReport(acs.getActionReport());
}
closeSse = true;
if (!acs.isOutboundPayloadEmpty()) {
logger.log(Level.FINEST, "Romote command holds data. Must load it");
downloadPayloadFromManaged(instanceId);
}
} else if (acs.getState() == AdminCommandState.State.FAILED_RETRYABLE) {
logger.log(Level.INFO, strings.get("remotecommand.failedretryable", acs.getId()));
if (acs.getActionReport() != null) {
setActionReport(acs.getActionReport());
}
closeSse = true;
} else if (acs.getState() == AdminCommandState.State.RUNNING_RETRYABLE) {
logger.log(Level.FINEST, "Command stores checkpoint and is retryable");
retryableCommand = true;
}
}
}
} while (event != null && !eventReceiver.isClosed() && !closeSse);
if (closeSse) {
try { eventReceiver.close(); } catch (Exception exc) {}
}
} catch (IOException ioex) {
if (instanceId != null && "Premature EOF".equals(ioex.getMessage())) {
if (retryableCommand) {
throw new CommandException(strings.get("remotecommand.lostConnection.retryableCommand", new Object[] {instanceId}), ioex);