Merge branch 'master' into 'master'

Make Http Client closable for enrollment app install

See merge request entgra/carbon-device-mgt-plugins!108
revert-dabc3590
Saad Sahibjan 5 years ago
commit 68600e2473

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

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

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

Loading…
Cancel
Save