com.cleeng.api.BatchAsyncRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cleeng-java-sdk Show documentation
Show all versions of cleeng-java-sdk Show documentation
When you are building your website or application with Java, you can use the Cleeng Java SDK. It simplifies the use of the Cleeng API as it handles user login and content access, and deals with all JSON-RPC specific code.
package com.cleeng.api;
import com.cleeng.api.domain.BatchResponse;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsonrpc.JSONRPCMessage;
import org.jsonrpc.JSONRPCRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class BatchAsyncRequest extends AsyncRequestCallback {
private static final Logger logger = LogManager.getLogger(BatchAsyncRequest.class);
private List requests;
private ResponseMapper mapper = new ResponseMapper();
private int count = 0;
public BatchAsyncRequest() {
super(BatchResponse.class);
this.requests = new ArrayList();
}
public BatchAsyncRequest(List requests) {
super(BatchResponse.class);
this.requests = requests;
}
public void addRequest(JSONRPCRequest request) {
request.id = Integer.toString(this.count);
this.requests.add(request);
this.count++;
}
public List getRequests() {
return this.requests;
}
public BatchResponse getResponse() {
final BatchResponse batchResponse = new BatchResponse();
final JsonParser parser = new JsonParser();
final JsonArray o = parser.parse(this._response).getAsJsonArray();
for (int i = 0; i < o.size(); i++) {
JsonElement element = o.get(i);
for (int j = 0; j < this.requests.size(); j++) {
JSONRPCRequest r = (JSONRPCRequest) this.requests.get(j);
if (element.isJsonObject()) {
JsonObject res = element.getAsJsonObject();
if (res.get("id").getAsString().equals(r.id)) {
String responseTypeName = this.mapper.map(r.method);
if (responseTypeName != null) {
try {
logger.info("Processing " + responseTypeName);
Serializable payload = (Serializable) this.gson.fromJson(res, Class.forName(responseTypeName));
batchResponse.responses.add(payload);
} catch (ClassNotFoundException e) {
logger.error("Class not found " + e);
}
} else {
logger.warn("Mapper did not contain a response type for " + r.getClass().getTypeName());
}
}
}
}
}
return batchResponse;
}
}