diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml index f552c5aebf..9c18cec6e1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml @@ -145,6 +145,11 @@ + + com.squareup.okhttp3 + okhttp + 4.10.0 + org.testng testng diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BatchResponse.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BatchResponse.java new file mode 100644 index 0000000000..8858ef085d --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BatchResponse.java @@ -0,0 +1,52 @@ +package org.wso2.carbon.device.mgt.jaxrs.beans; + +import okhttp3.Request; +import okhttp3.Response; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +public class BatchResponse implements Serializable { + private List responses = new ArrayList<>(); + private boolean success; + private List failedResponses = new ArrayList<>(); + private List succeededResponses = new ArrayList<>(); + + private List failedRequests = new ArrayList<>(); + + public void setResponses(List responses) { this.responses = responses; } + + public List getResponses() { return this.responses; } + + public void setFailedResponses(List failedResponses) { this.failedResponses = failedResponses; } + + public List getFailedResponses() { return this.failedResponses; } + + public void setSucceededResponses(List succeededResponses) { this.succeededResponses = succeededResponses; } + + public List getSucceededResponses() { return this.succeededResponses; } + + public void setFailedRequests(List failedRequests) { this.failedRequests = failedRequests; } + + public List getFailedRequests() { return this.failedRequests; } + + public void setSuccess(boolean success) { this.success = success; } + + public boolean getSuccess() { return this.success; } + + public void addResponse(Response response) { + this.responses.add(response); + if(response.isSuccessful()) { + this.succeededResponses.add(response); + } else { + this.failedResponses.add(response); + } + } + + public void addRequest(Request request) { + this.failedRequests.add(request); + } + + public BatchResponse() {} +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java index d7462993a9..d84c9dd16e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java @@ -59,6 +59,8 @@ public class Constants { public static final String IOS = "ios"; public static final String WINDOWS = "windows"; + public static final int MAX_REQUEST_FOR_PUBLISHER = 50; + public final class OperationStatus { private OperationStatus () { throw new AssertionError(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/RESTEndpointHandler.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/RESTEndpointHandler.java new file mode 100644 index 0000000000..acacd71ff7 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/RESTEndpointHandler.java @@ -0,0 +1,44 @@ +package org.wso2.carbon.device.mgt.jaxrs.util; + +import okhttp3.*; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.jaxrs.beans.BatchResponse; + +import java.io.IOException; +import java.util.List; + +public class RESTEndpointHandler { + private static final Log logger = LogFactory.getLog(RESTEndpointHandler.class); + + // executes batch of requests to hosts indicate by requests + public BatchResponse batchExecute(List batch, int maxRequestsPerHost) { + BatchResponse batchResponse = new BatchResponse(); + OkHttpClient client = new OkHttpClient(); + client.dispatcher().setMaxRequestsPerHost(maxRequestsPerHost); + batch.forEach(request -> { + client.newCall(request).enqueue(new Callback() { + @Override + public void onFailure(Call call, IOException e) { + batchResponse.addRequest(call.request()); + String msg = "Failed to execute request to "+call.request().url(); + logger.error(msg, e); + } + + @Override + public void onResponse(Call call, Response response) { + batchResponse.addResponse(response); + } + }); + }); + if(logger.isDebugEnabled()) { + logger.info( + "Batch execution is completed."+ + "Executed : "+batch.size()+"\n"+ + "Failed : "+batchResponse.getFailedRequests().size()+"\n"+ + "Succeeded :"+batchResponse.getResponses().size() + ); + } + return batchResponse; + } +}