Make Http Client closable for enrollment app install

revert-dabc3590
lasanthaDLPDS 5 years ago
parent d853a709c7
commit b12b6865b7

@ -369,5 +369,9 @@
<artifactId>org.wso2.carbon.device.application.mgt.common</artifactId> <artifactId>org.wso2.carbon.device.application.mgt.common</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -41,12 +41,15 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonNull; import com.google.gson.JsonNull;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONArray;
import org.json.JSONObject;
import org.wso2.carbon.analytics.api.AnalyticsDataAPI; import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil; import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil;
import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse; import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse;
@ -97,9 +100,9 @@ import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
import org.apache.http.impl.client.CloseableHttpClient;
import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolation;
import javax.ws.rs.core.MediaType;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -673,8 +676,6 @@ public class AndroidDeviceUtils {
public static void installEnrollmentApplications(ProfileFeature feature, DeviceIdentifier deviceIdentifier) public static void installEnrollmentApplications(ProfileFeature feature, DeviceIdentifier deviceIdentifier)
throws PolicyManagementException { throws PolicyManagementException {
String uuid = ""; String uuid = "";
HttpClient httpClient;
PostMethod request;
try { try {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
ApiApplicationKey apiApplicationKey = OAuthUtils.getClientCredentials(tenantDomain); ApiApplicationKey apiApplicationKey = OAuthUtils.getClientCredentials(tenantDomain);
@ -688,21 +689,25 @@ public class AndroidDeviceUtils {
AndroidConstants.ApplicationInstall.ENROLLMENT_APP_INSTALL_URL; AndroidConstants.ApplicationInstall.ENROLLMENT_APP_INSTALL_URL;
JsonElement appListElement = new JsonParser().parse(feature.getContent().toString()).getAsJsonObject() JsonElement appListElement = new JsonParser().parse(feature.getContent().toString()).getAsJsonObject()
.get(AndroidConstants.ApplicationInstall.ENROLLMENT_APP_INSTALL_CODE); .get(AndroidConstants.ApplicationInstall.ENROLLMENT_APP_INSTALL_CODE);
String payload = "[{\"id\":\"" + deviceIdentifier.getId() + "\", \"type\":\""
+ deviceIdentifier.getType() + "\"}]"; JSONObject deviceObject = new JSONObject();
StringRequestEntity requestEntity = new StringRequestEntity(payload, MediaType.APPLICATION_JSON deviceObject.put("id", deviceIdentifier.getId());
, AndroidConstants.ApplicationInstall.ENCODING); deviceObject.put("type", deviceIdentifier.getType());
JSONArray payload = new JSONArray();
payload.put(deviceObject);
StringEntity requestEntity = new StringEntity(payload.toString(), ContentType.APPLICATION_JSON);
JsonArray appListArray = appListElement.getAsJsonArray(); JsonArray appListArray = appListElement.getAsJsonArray();
for (JsonElement appElement : appListArray) { for (JsonElement appElement : appListArray) {
uuid = appElement.getAsJsonObject(). uuid = appElement.getAsJsonObject().
get(AndroidConstants.ApplicationInstall.ENROLLMENT_APP_INSTALL_UUID).getAsString(); get(AndroidConstants.ApplicationInstall.ENROLLMENT_APP_INSTALL_UUID).getAsString();
requestUrl = requestUrl.replace("{uuid}", uuid); try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
httpClient = new HttpClient(); HttpPost postRequest = new HttpPost(requestUrl.replace("{uuid}", uuid));
request = new PostMethod(requestUrl); postRequest.setHeader(AndroidConstants.ApplicationInstall.AUTHORIZATION,
request.addRequestHeader(AndroidConstants.ApplicationInstall.AUTHORIZATION AndroidConstants.ApplicationInstall.AUTHORIZATION_HEADER_VALUE + tokenInfo
, AndroidConstants.ApplicationInstall.AUTHORIZATION_HEADER_VALUE + tokenInfo.getAccessToken()); .getAccessToken());
request.setRequestEntity(requestEntity); postRequest.setEntity(requestEntity);
httpClient.executeMethod(request); httpClient.execute(postRequest);
}
} }
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Error while accessing user store for user with Android device id: " + String msg = "Error while accessing user store for user with Android device id: " +

@ -1020,6 +1020,11 @@
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>${httpclient.version}</version> <version>${httpclient.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apache.http.client.version}</version>
</dependency>
<dependency> <dependency>
<groupId>commons-collections</groupId> <groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId> <artifactId>commons-collections</artifactId>
@ -1334,6 +1339,7 @@
<httpclient.version>4.3.1.wso2v2</httpclient.version> <httpclient.version>4.3.1.wso2v2</httpclient.version>
<httpclient.version.range>[4.3.1, 5.0.0)</httpclient.version.range> <httpclient.version.range>[4.3.1, 5.0.0)</httpclient.version.range>
<httpcore.version>4.3.3.wso2v1</httpcore.version> <httpcore.version>4.3.3.wso2v1</httpcore.version>
<apache.http.client.version>4.5.6</apache.http.client.version>
<!-- Nimbus Jose--> <!-- Nimbus Jose-->
<nimbus.orbit.version>2.26.1.wso2v3</nimbus.orbit.version> <nimbus.orbit.version>2.26.1.wso2v3</nimbus.orbit.version>

Loading…
Cancel
Save