@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String domain = request.getHeader("domain");
WaveId waveId = WaveId.deserialise(request.getHeader("waveId"));
WaveletId waveletId = WaveletId.deserialise(request.getHeader("waveletId"));
final StringWriter error = new StringWriter();
boolean somethingProcessed = false;
boolean somethingSkipped = false;
try {
JSONObject exp = new JSONObject(readToString(request.getReader()));
JSONArray rawDeltas = exp.getJSONObject("data").getJSONArray("rawDeltas");
if (rawDeltas.length() != 0) {
List<ProtocolAppliedWaveletDelta> deltas = new LinkedList<ProtocolAppliedWaveletDelta>();
for (int i = 0; i < rawDeltas.length(); i++) {
deltas.add(ProtocolAppliedWaveletDelta.parseFrom(Base64.decode(rawDeltas.getString(i))));
}
WaveletName waveletName = WaveletName.of(waveId, waveletId);
LocalWaveletContainerImpl wavelet = (LocalWaveletContainerImpl) waveMap.getLocalWavelet(waveletName);
Set<ParticipantId> participants = new HashSet<ParticipantId>();
if (wavelet != null) {
participants.addAll(wavelet.accessSnapshot().getParticipants());
}
for (ProtocolAppliedWaveletDelta appliedDelta : deltas) {
ProtocolWaveletDelta delta = ProtocolWaveletDelta.parseFrom(
appliedDelta.getSignedOriginalDelta().getDelta());
long currentVersion = 0;
if (wavelet != null) {
currentVersion = wavelet.getCurrentVersion().getVersion();
}
if (currentVersion == delta.getHashedVersion().getVersion()) {
ProtocolWaveletDelta newDelta = convertDelta(delta, domain, wavelet, waveletName,
participants);
waveletProvider.submitRequest(waveletName, newDelta,
new WaveletProvider.SubmitRequestListener() {
@Override
public void onSuccess(int operationsApplied, HashedVersion hashedVersionAfterApplication,
long applicationTimestamp) {
}
@Override
public void onFailure(String errorMessage) {
error.write(errorMessage);
}
});
if (error.getBuffer().length() != 0) {
break;
}
if (wavelet == null) {
wavelet = (LocalWaveletContainerImpl) waveMap.getLocalWavelet(waveletName);
}
somethingProcessed = true;
} else {
somethingSkipped = true;
}
}
}
} catch (Exception ex) {
LOG.log(Level.SEVERE, "waveId " + waveId.toString() + ", waveletId " + waveletId.toString(), ex);
throw new IOException(ex);
}
response.setStatus(HttpServletResponse.SC_OK);
if (error.getBuffer().length() != 0) {
response.getOutputStream().write(("error : " + error.getBuffer()).getBytes());