use of org.json.simple.JSONStreamAware in project elastic-core-maven by OrdinaryDude.
the class MessageEncoder method push.
public static long push(IComputationAttachment work, String secretPhrase) throws NxtException, IOException {
Appendix.PrunablePlainMessage[] messages = MessageEncoder.encodeAttachment(work);
JSONStreamAware[] individual_txs = MessageEncoder.encodeTransactions(messages, secretPhrase);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
for (int i = 0; i < individual_txs.length; ++i) {
individual_txs[i].writeJSONString(pw);
}
StringBuffer sb = sw.getBuffer();
return MessageEncoder.pushThemAll(individual_txs);
}
use of org.json.simple.JSONStreamAware in project elastic-core-maven by OrdinaryDude.
the class MessageEncryptionTest method encryptText.
@Test
public void encryptText() {
JSONStreamAware json = JSONResponses.INCORRECT_ALIAS;
EncryptedData encryptedData = encrypt(Convert.toBytes(json.toString()));
Assert.assertEquals(json.toString(), Convert.toString(decrypt(encryptedData)));
}
use of org.json.simple.JSONStreamAware in project elastic-core-maven by OrdinaryDude.
the class User method send.
synchronized void send(JSONStreamAware response) {
if (asyncContext == null) {
if (isInactive) {
// user not seen recently, no responses should be collected
return;
}
if (pendingResponses.size() > 1000) {
pendingResponses.clear();
// stop collecting responses for this user
isInactive = true;
if (secretPhrase == null) {
// but only completely remove users that don't have unlocked accounts
Users.remove(this);
}
return;
}
pendingResponses.offer(response);
} else {
JSONArray responses = new JSONArray();
JSONStreamAware pendingResponse;
while ((pendingResponse = pendingResponses.poll()) != null) {
responses.add(pendingResponse);
}
responses.add(response);
JSONObject combinedResponse = new JSONObject();
combinedResponse.put("responses", responses);
asyncContext.getResponse().setContentType("text/plain; charset=UTF-8");
try (Writer writer = asyncContext.getResponse().getWriter()) {
combinedResponse.writeJSONString(writer);
} catch (IOException e) {
Logger.logMessage("Error sending response to user", e);
}
asyncContext.complete();
asyncContext = null;
}
}
Aggregations