revert Improvements for the reporting data publishing
pull/374/head
Pahansith Gunathilake 6 months ago
parent 0071e13692
commit ea53734901

@ -19,7 +19,6 @@
package io.entgra.device.mgt.core.device.mgt.core.app.mgt; package io.entgra.device.mgt.core.device.mgt.core.app.mgt;
import com.google.gson.Gson; import com.google.gson.Gson;
import io.entgra.device.mgt.core.device.mgt.core.report.mgt.ReportingPublisherManager;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -287,11 +286,8 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem
deviceDetailsWrapper.setTenantId(tenantId); deviceDetailsWrapper.setTenantId(tenantId);
deviceDetailsWrapper.setDevice(device); deviceDetailsWrapper.setDevice(device);
deviceDetailsWrapper.setApplications(newApplications); deviceDetailsWrapper.setApplications(newApplications);
ReportingPublisherManager reportingManager = new ReportingPublisherManager(); HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(),
reportingManager.publishData(deviceDetailsWrapper, DeviceManagementConstants reportingHost + DeviceManagementConstants.Report.APP_USAGE_ENDPOINT);
.Report.APP_USAGE_ENDPOINT);
/*HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(),
reportingHost + DeviceManagementConstants.Report.APP_USAGE_ENDPOINT);*/
} }
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {

@ -18,7 +18,6 @@
package io.entgra.device.mgt.core.device.mgt.core.device.details.mgt.impl; package io.entgra.device.mgt.core.device.mgt.core.device.details.mgt.impl;
import io.entgra.device.mgt.core.device.mgt.core.report.mgt.ReportingPublisherManager;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -30,6 +29,7 @@ import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceDetailsW
import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceInfo; import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceInfo;
import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceLocation; import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceLocation;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.EventPublishingException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException;
@ -203,8 +203,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
getDeviceManagementProvider().getDevice(deviceIdentifier, false); getDeviceManagementProvider().getDevice(deviceIdentifier, false);
DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper();
deviceDetailsWrapper.setEvents(payload); deviceDetailsWrapper.setEvents(payload);
publishEvents(device, deviceDetailsWrapper, eventType); return publishEvents(device, deviceDetailsWrapper, eventType);
return 201;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Event publishing error. Could not get device " + deviceId; String msg = "Event publishing error. Could not get device " + deviceId;
@ -218,7 +217,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
* @param device Device that is sending event * @param device Device that is sending event
* @param deviceDetailsWrapper Payload to send(example, deviceinfo, applist, raw events) * @param deviceDetailsWrapper Payload to send(example, deviceinfo, applist, raw events)
*/ */
private void publishEvents(Device device, DeviceDetailsWrapper deviceDetailsWrapper, String private int publishEvents(Device device, DeviceDetailsWrapper deviceDetailsWrapper, String
eventType) { eventType) {
String reportingHost = HttpReportingUtil.getReportingHost(); String reportingHost = HttpReportingUtil.getReportingHost();
if (!StringUtils.isBlank(reportingHost) if (!StringUtils.isBlank(reportingHost)
@ -253,9 +252,9 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
String eventUrl = reportingHost + DeviceManagementConstants.Report String eventUrl = reportingHost + DeviceManagementConstants.Report
.REPORTING_CONTEXT + DeviceManagementConstants.URL_SEPERATOR + eventType; .REPORTING_CONTEXT + DeviceManagementConstants.URL_SEPERATOR + eventType;
ReportingPublisherManager reportingManager = new ReportingPublisherManager(); return HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), eventUrl);
reportingManager.publishData(deviceDetailsWrapper, eventUrl); } catch (EventPublishingException e) {
//return HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), eventUrl); log.error("Error occurred while sending events", e);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
log.error("Error occurred while getting group list", e); log.error("Error occurred while getting group list", e);
} catch (UserStoreException e) { } catch (UserStoreException e) {
@ -271,6 +270,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
+ DeviceManagerUtil.getTenantId()); + DeviceManagerUtil.getTenantId());
} }
} }
return 0;
} }
@Override @Override

@ -1,82 +0,0 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.device.mgt.core.device.mgt.core.report.mgt;
import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceDetailsWrapper;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.EventPublishingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HTTP;
import java.io.IOException;
import java.net.ConnectException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ReportingPublisherManager {
private static final Log log = LogFactory.getLog(ReportingPublisherManager.class);
private final static ExecutorService executorService;
private DeviceDetailsWrapper payload;
private String endpoint;
private static final PoolingHttpClientConnectionManager poolingManager;
static {
executorService = Executors.newFixedThreadPool(10); //todo make this configurable
poolingManager = new PoolingHttpClientConnectionManager();
poolingManager.setMaxTotal(10); //todo make this configurable
poolingManager.setDefaultMaxPerRoute(10);
}
public void publishData(DeviceDetailsWrapper deviceDetailsWrapper, String eventUrl) {
this.payload = deviceDetailsWrapper;
this.endpoint = eventUrl;
executorService.submit(new ReportingPublisher());
}
private class ReportingPublisher implements Runnable {
@Override
public void run() {
try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(poolingManager).build()) {
HttpPost apiEndpoint = new HttpPost(endpoint);
apiEndpoint.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
StringEntity requestEntity = new StringEntity(payload.getJSONString(), ContentType.APPLICATION_JSON);
apiEndpoint.setEntity(requestEntity);
HttpResponse response = client.execute(apiEndpoint);
int statusCode = response.getStatusLine().getStatusCode();
if (log.isDebugEnabled()) {
log.debug("Published data to the reporting backend: " + endpoint + ", Response code: " + statusCode);
}
} catch (ConnectException e) {
String message = "Connection refused while publishing reporting data to the API: " + endpoint;
log.error(message, e);
} catch (IOException e) {
String message = "Error occurred when publishing reporting data to the API: " + endpoint;
log.error(message, e);
}
}
}
}

@ -27,7 +27,6 @@ import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HTTP;
import org.json.JSONObject; import org.json.JSONObject;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.EventPublishingException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.EventPublishingException;
@ -50,6 +49,24 @@ public class HttpReportingUtil {
return System.getProperty(DeviceManagementConstants.Report.REPORTING_EVENT_HOST); return System.getProperty(DeviceManagementConstants.Report.REPORTING_EVENT_HOST);
} }
public static int invokeApi(String payload, String endpoint) throws EventPublishingException {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost apiEndpoint = new HttpPost(endpoint);
apiEndpoint.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
StringEntity requestEntity = new StringEntity(
payload, ContentType.APPLICATION_JSON);
apiEndpoint.setEntity(requestEntity);
HttpResponse response = client.execute(apiEndpoint);
return response.getStatusLine().getStatusCode();
} catch (ConnectException e) {
log.error("Connection refused to API endpoint: " + endpoint, e);
return HttpStatus.SC_SERVICE_UNAVAILABLE;
} catch (IOException e) {
throw new EventPublishingException("Error occurred when " +
"invoking API. API endpoint: " + endpoint, e);
}
}
public static boolean isPublishingEnabledForTenant() { public static boolean isPublishingEnabledForTenant() {
Object configuration = DeviceManagerUtil.getConfiguration(IS_EVENT_PUBLISHING_ENABLED); Object configuration = DeviceManagerUtil.getConfiguration(IS_EVENT_PUBLISHING_ENABLED);

Loading…
Cancel
Save