From e5963c79fdb714cb1c068da81a26ce6629419049 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Fri, 20 Sep 2019 06:48:11 +0530 Subject: [PATCH 1/9] Override toString --- .../carbon/device/mgt/jaxrs/beans/analytics/AttributeType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AttributeType.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AttributeType.java index 32302986e7..131904671a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AttributeType.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AttributeType.java @@ -28,4 +28,4 @@ public enum AttributeType { public String toString() { return super.toString().toLowerCase(); } -} \ No newline at end of file +} From f660bf9edd409136c7b172870b3a0c624def0d98 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Fri, 20 Sep 2019 10:37:07 +0530 Subject: [PATCH 2/9] Add ApiParams to all the parameters --- .../AnalyticsArtifactsManagementService.java | 82 ++++++++++++------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/AnalyticsArtifactsManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/AnalyticsArtifactsManagementService.java index 260ec66a11..871042bf53 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/AnalyticsArtifactsManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/AnalyticsArtifactsManagementService.java @@ -17,6 +17,7 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.api; +import io.swagger.annotations.ApiParam; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; @@ -91,17 +92,17 @@ import javax.ws.rs.core.Response; } ) -@Path("/analytics/artifacts") @Api(value = "Analytics Artifacts Management", description = "This API corresponds to services" + " related to Analytics Artifacts management") +@Path("/analytics/artifacts") +@Consumes(MediaType.APPLICATION_JSON) public interface AnalyticsArtifactsManagementService { @POST - @Consumes(MediaType.APPLICATION_JSON) @Path("/stream/{id}") @ApiOperation( httpMethod = "POST", - value = "Create Event Stream Artifact as String", + value = "Create Event Stream Artifact through a String argument.", notes = "Deploy a Json Stream Artifact in Analytics server.", tags = "Analytics Artifacts Management", extensions = { @@ -143,16 +144,20 @@ public interface AnalyticsArtifactsManagementService { response = ErrorResponse.class) } ) - Response deployEventDefinitionAsString(@PathParam("id") String id, - @QueryParam("isEdited") boolean isEdited, - @Valid EventStream stream); + Response deployEventDefinitionAsString( + @ApiParam(name = "id", value = "Stream id(name:version).") + @PathParam("id") String id, + @ApiParam(name = "isEdited", value = "This stream is being edited or created.") + @QueryParam("isEdited") boolean isEdited, + @ApiParam(name = "stream", value = "Add the data to complete the EventStream object.", + required = true) + @Valid EventStream stream); @POST - @Consumes(MediaType.APPLICATION_JSON) @Path("/stream") @ApiOperation( httpMethod = "POST", - value = "Create Event Stream Artifact as DTO", + value = "Create Event Stream Artifact through a DTO class.", notes = "Deploy a Json Stream Artifact in Analytics server.", tags = "Analytics Artifacts Management", extensions = { @@ -194,14 +199,16 @@ public interface AnalyticsArtifactsManagementService { response = ErrorResponse.class) } ) - Response deployEventDefinitionAsDto(@Valid EventStream stream); + Response deployEventDefinitionAsDto( + @ApiParam(name = "stream", value = "Add the data to complete the EventStream object.", + required = true) + @Valid EventStream stream); @POST - @Consumes(MediaType.APPLICATION_JSON) @Path("/receiver/{name}") @ApiOperation( httpMethod = "POST", - value = "Create Event Receiver Artifact as String", + value = "Create Event Receiver Artifact through a String argument.", notes = "Deploy a XML Event Receiver Artifact in Analytics server.", tags = "Analytics Artifacts Management", extensions = { @@ -244,16 +251,20 @@ public interface AnalyticsArtifactsManagementService { response = ErrorResponse.class) } ) - Response deployEventReceiverAsString(@PathParam("name") String name, - @QueryParam("isEdited") boolean isEdited, - @Valid EventReceiver receiver); + Response deployEventReceiverAsString( + @ApiParam(name = "name", value = "Receiver name.") + @PathParam("name") String name, + @ApiParam(name = "isEdited", value = "This stream is being edited or created.") + @QueryParam("isEdited") boolean isEdited, + @ApiParam(name = "receiver", value = "Add the data to complete the EventReceiver object.", + required = true) + @Valid EventReceiver receiver); @POST - @Consumes(MediaType.APPLICATION_JSON) @Path("/receiver") @ApiOperation( httpMethod = "POST", - value = "Create Event Receiver Artifact as DTO", + value = "Create Event Receiver Artifact through a DTO class.", notes = "Deploy a JSON Event Receiver Artifact in Analytics server.", tags = "Analytics Artifacts Management", extensions = { @@ -296,14 +307,16 @@ public interface AnalyticsArtifactsManagementService { response = ErrorResponse.class) } ) - Response deployEventReceiverAsDto(@Valid Adapter receiver); + Response deployEventReceiverAsDto( + @ApiParam(name = "receiver", value = "Add the data to complete the Adapter object.", + required = true) + @Valid Adapter receiver); @POST - @Consumes(MediaType.APPLICATION_JSON) @Path("/publisher/{name}") @ApiOperation( httpMethod = "POST", - value = "Create Event Publisher Artifact as String", + value = "Create Event Publisher Artifact through a String argument.", notes = "Deploy a XML Event Publisher Artifact in Analytics server.", tags = "Analytics Artifacts Management", extensions = { @@ -346,16 +359,20 @@ public interface AnalyticsArtifactsManagementService { response = ErrorResponse.class) } ) - Response deployEventPublisherAsString(@PathParam("name") String name, - @QueryParam("isEdited") boolean isEdited, - @Valid EventPublisher publisher); + Response deployEventPublisherAsString( + @ApiParam(name = "name", value = "Publisher name.") + @PathParam("name") String name, + @ApiParam(name = "isEdited", value = "This stream is being edited or created.") + @QueryParam("isEdited") boolean isEdited, + @ApiParam(name = "publisher", value = "Add the data to complete the EventPublisher object.", + required = true) + @Valid EventPublisher publisher); @POST - @Consumes(MediaType.APPLICATION_JSON) @Path("/publisher") @ApiOperation( httpMethod = "POST", - value = "Create Event Publisher Artifact as DTO", + value = "Create Event Publisher Artifact through a DTO class.", notes = "Deploy a JSON Event Publisher Artifact in Analytics server.", tags = "Analytics Artifacts Management", extensions = { @@ -398,15 +415,17 @@ public interface AnalyticsArtifactsManagementService { response = ErrorResponse.class) } ) - Response deployEventPublisherAsDto(@Valid Adapter publisher); + Response deployEventPublisherAsDto( + @ApiParam(name = "publisher", value = "Add the data to complete the Adapter object.", + required = true) + @Valid Adapter publisher); @POST - @Consumes(MediaType.APPLICATION_JSON) @Path("/siddhi-script/{name}") @ApiOperation( httpMethod = "POST", - value = "Create Siddhi Script Artifact as String", - notes = "Deploy a SiddhiQL Siddhi script Artifact in Analytics server.", + value = "Create Siddhi Script Artifact through a String argument.", + notes = "Deploy a SiddhiQL script Artifact in Analytics server.", tags = "Analytics Artifacts Management", extensions = { @Extension(properties = { @@ -449,6 +468,11 @@ public interface AnalyticsArtifactsManagementService { } ) Response deploySiddhiExecutableScript( - @PathParam("name") String name, @QueryParam("isEdited") boolean isEdited, + @ApiParam(name = "name", value = "Siddhi Executable Script name.") + @PathParam("name") String name, + @ApiParam(name = "isEdited", value = "This stream is being edited or created.") + @QueryParam("isEdited") boolean isEdited, + @ApiParam(name = "plan", value = "Add the data to complete the SiddhiExecutionPlan object.", + required = true) @Valid SiddhiExecutionPlan plan); } From da2d95e7ace85aa33dcd76ebd8b92fa0a9353652 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Thu, 10 Oct 2019 17:37:05 +0530 Subject: [PATCH 3/9] Change event attribute mapping logic and error handling Event attribute mapping logic was moved to service layer from the api layer and a created a meaningful error message. --- .../jaxrs/beans/analytics/EventStream.java | 12 +- ...alyticsArtifactsManagementServiceImpl.java | 162 +++++++++--------- 2 files changed, 84 insertions(+), 90 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java index 17100e75f2..1fc05ba31b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java @@ -17,10 +17,9 @@ */ package org.wso2.carbon.device.mgt.jaxrs.beans.analytics; -import com.sun.istack.NotNull; import io.swagger.annotations.ApiModelProperty; -import org.hibernate.validator.constraints.NotEmpty; +import java.util.ArrayList; import java.util.List; /** @@ -37,12 +36,13 @@ public class EventStream { @ApiModelProperty(value = "Stream description") private String description; @ApiModelProperty(value = "Meta attribute list") - private List metaData; + private List metaData = new ArrayList<>(); @ApiModelProperty(value = "Correlation attribute list") - private List correlationData; + private List correlationData = new ArrayList<>(); @ApiModelProperty(value = "Payload attribute list") - private List payloadData; - @ApiModelProperty(value = "Stream definition") @NotNull @NotEmpty + private List payloadData = new ArrayList<>(); + + @ApiModelProperty(value = "Stream definition") private String definition; public String getName() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java index 21583f0f28..72266166eb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.Attribute; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AdapterMappingConfiguration; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.MappingProperty; @@ -27,6 +28,7 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.MessageFormat; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.SiddhiExecutionPlan; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventPublisher; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventReceiver; +import org.wso2.carbon.device.mgt.jaxrs.exception.BadRequestException; import org.wso2.carbon.device.mgt.jaxrs.exception.ErrorDTO; import org.wso2.carbon.device.mgt.jaxrs.exception.InvalidExecutionPlanException; import org.wso2.carbon.device.mgt.jaxrs.service.api.AnalyticsArtifactsManagementService; @@ -58,7 +60,6 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; -import java.nio.charset.StandardCharsets; import java.rmi.RemoteException; import java.util.List; @@ -67,14 +68,10 @@ import java.util.List; * siddhi scripts to the Analytics server as Artifacts */ @Path("/analytics/artifacts") -public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifactsManagementService { +public class AnalyticsArtifactsManagementServiceImpl + implements AnalyticsArtifactsManagementService { private static final Log log = LogFactory.getLog(AnalyticsArtifactsManagementServiceImpl.class); - /** - * @param stream EventStream object with the properties of the stream - * @return A status code depending on the code result - * Function - Used to deploy stream as an artifact using a String - */ @Override @POST @Path("/stream/{id}") @@ -84,7 +81,7 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); EventStreamAdminServiceStub eventStreamAdminServiceStub = null; try { - String streamDefinition = new String(stream.getDefinition().getBytes(), StandardCharsets.UTF_8); + String streamDefinition = stream.getDefinition(); eventStreamAdminServiceStub = DeviceMgtAPIUtils.getEventStreamAdminServiceStub(); if (!isEdited) { eventStreamAdminServiceStub.addEventStreamDefinitionAsString(streamDefinition); @@ -112,34 +109,14 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac } } - /** - * @param stream EventStream object with the properties of the stream - * @return A status code depending on the code result - * Function - Used to deploy stream as an artifact using a DTO - */ @Override @POST @Path("/stream") public Response deployEventDefinitionAsDto(@Valid EventStream stream) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); - // Categorize attributes to three lists depending on their type - List metaData = stream.getMetaData(); - List payloadData = stream.getPayloadData(); - List correlationData = stream.getCorrelationData(); - try { - /* Conditions - * - At least one list should always be not null - */ - if (metaData == null && correlationData == null && payloadData == null) { - log.error("Invalid payload: event attributes"); - return Response.status(Response.Status.BAD_REQUEST).build(); - - } else { - // Publish the event stream - publishStream(stream, metaData, correlationData, payloadData); - return Response.ok().build(); - } + publishStream(stream); + return Response.ok().build(); } catch (AxisFault e) { log.error("Failed to create event definitions for tenantDomain: " + tenantDomain, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); @@ -155,13 +132,6 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac } } - /** - * @param name Receiver name - * @param isEdited If receiver is created or edited - * @param receiver Receiver object with the properties of the receiver - * @return A status code depending on the code result - * Function - Used to deploy receiver as an artifact using a String - */ @Override @POST @Path("/receiver/{name}") @@ -194,11 +164,6 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac return Response.ok().entity(name).build(); } - /** - * @param receiver Receiver object with the properties of the receiver - * @return A status code depending on the code result - * Function - Used to deploy receiver as an artifact using a DTO - */ @Override @POST @Path("/receiver") @@ -260,13 +225,6 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac } } - /** - * @param name Publisher name - * @param isEdited If receiver is created or edited - * @param publisher Publisher object with the properties of the publisher - * @return A status code depending on the code result - * Function - Used to deploy publisher as an artifact using a String - */ @Override @POST @Path("/publisher/{name}") @@ -301,11 +259,6 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac return Response.ok().entity(publisher).build(); } - /** - * @param publisher Publisher object with the properties of the publisher - * @return A status code depending on the code result - * Function - Used to deploy publisher as an artifact using a DTO - */ @Override @POST @Path("/publisher") @@ -367,13 +320,6 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac } } - /** - * @param name Siddhi plan name - * @param isEdited If receiver is created or edited - * @param plan Siddhi plan definition - * @return a status code depending on the code execution - * Function - Used to deploy Siddhi script as an artifact using a String - */ @Override @POST @Path("/siddhi-script/{name}") @@ -403,17 +349,25 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac } /** + * Set data to a stream dto and publish dto using a stub + * * @param stream Stream definition - * @param metaData Meta attributes of the stream - * @param correlationData Correlation attributes of the stream - * @param payloadData Payload attributes of the stream - * @throws RemoteException exception that may occur during a remote method call - * @throws UserStoreException exception that may occur during JWT token generation - * @throws JWTClientException exception that may occur during connecting to client store + * @throws RemoteException Exception that may occur during a remote method call + * @throws UserStoreException Exception that may occur during JWT token generation + * @throws JWTClientException Exception that may occur during connecting to client store */ - private void publishStream(EventStream stream, List metaData, - List correlationData, List payloadData) + private void publishStream(EventStream stream) throws RemoteException, UserStoreException, JWTClientException { + List metaData = stream.getMetaData(); + List payloadData = stream.getPayloadData(); + List correlationData = stream.getCorrelationData(); + if (metaData.isEmpty() && correlationData.isEmpty() && payloadData.isEmpty()) { + String errMsg = "Invalid payload: event mapping attributes invalid!!!"; + ErrorResponse errorResponse = new ErrorResponse(); + errorResponse.setMessage(errMsg); + log.error(errMsg); + throw new BadRequestException(errorResponse); + } EventStreamAdminServiceStub eventStreamAdminServiceStub = DeviceMgtAPIUtils.getEventStreamAdminServiceStub(); try { @@ -438,6 +392,8 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac } /** + * Set data to a receiver dto and publish dto using a stub + * * @param receiverName Receiver name * @param adapterType Receiver type * @param adapterProperties Receiver properties @@ -449,9 +405,9 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac * @param metaMappingProperties Receiver meta attribute mapping * @param messageFormat Receiver mapping format * @param eventStreamWithVersion Attached stream - * @throws RemoteException exception that may occur during a remote method call - * @throws UserStoreException exception that may occur during JWT token generation - * @throws JWTClientException exception that may occur during connecting to client store + * @throws RemoteException Exception that may occur during a remote method call + * @throws UserStoreException Exception that may occur during JWT token generation + * @throws JWTClientException Exception that may occur during connecting to client store */ private void publishReceiver(String receiverName, String adapterType, List adapterProperties, boolean customMapping, @@ -525,6 +481,8 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac } /** + * Set data to a publisher dto and publish dto using a stub + * * @param publisherName Publisher name * @param adapterType Publisher type * @param adapterProperties Publisher properties @@ -534,9 +492,9 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac * @param metaMappingProperties Publisher meta attribute mapping * @param messageFormat Publisher mapping format * @param eventStreamWithVersion Attached stream - * @throws RemoteException exception that may occur during a remote method call - * @throws UserStoreException exception that may occur during JWT token generation - * @throws JWTClientException exception that may occur during connecting to client store + * @throws RemoteException Exception that may occur during a remote method call + * @throws UserStoreException Exception that may occur during JWT token generation + * @throws JWTClientException Exception that may occur during connecting to client store */ private void publishPublisher(String publisherName, String adapterType, List adapterProperties, @@ -611,13 +569,15 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac } /** - * @param name plan name - * @param isEdited is plan edited - * @param plan plan data - * @throws RemoteException exception that may occur during a remote method call - * @throws UserStoreException exception that may occur during JWT token generation - * @throws JWTClientException exception that may occur during connecting to client store - * @throws InvalidExecutionPlanException exception that may occur if execution plan validation fails + * Publish a siddhi execution plan using a stub + * + * @param name Plan name + * @param isEdited Is plan edited + * @param plan Plan data + * @throws RemoteException Exception that may occur during a remote method call + * @throws UserStoreException Exception that may occur during JWT token generation + * @throws JWTClientException Exception that may occur during connecting to client store + * @throws InvalidExecutionPlanException Exception that may occur if execution plan validation fails */ private void publishSiddhiExecutionPlan(String name, boolean isEdited, String plan) @@ -646,6 +606,12 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac } } + /** + * This will set payload of event attributes to the DTO + * + * @param attributes list of event attributes + * @return DTO with all the event attributes + */ private EventStreamAttributeDto[] addEventAttributesToDto(List attributes) { EventStreamAttributeDto[] eventStreamAttributeDtos = new EventStreamAttributeDto[attributes.size()]; for (int i = 0; i < attributes.size(); i++) { @@ -657,7 +623,12 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac return eventStreamAttributeDtos; } - + /** + * This will set payload of receiver attributes to the DTO + * + * @param adapterProperties List of receiver attributes + * @return DTO with all the receiver attributes + */ private BasicInputAdapterPropertyDto[] addReceiverConfigToDto( List adapterProperties) { BasicInputAdapterPropertyDto[] basicInputAdapterPropertyDtos @@ -671,6 +642,12 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac return basicInputAdapterPropertyDtos; } + /** + * This will set payload of receiver mapping attributes to the DTO + * + * @param mapProperties List of receiver mapping attributes + * @return DTO with all the receiver mapping attributes + */ private EventMappingPropertyDto[] addReceiverMappingToDto(List mapProperties) { EventMappingPropertyDto[] eventMappingPropertyDtos = new EventMappingPropertyDto[mapProperties.size()]; for (int i = 0; i < mapProperties.size(); i++) { @@ -683,6 +660,12 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac return eventMappingPropertyDtos; } + /** + * This will set payload of publisher attributes to the DTO + * + * @param adapterProperties List of publisher attributes + * @return DTO with all the publisher attributes + */ private BasicOutputAdapterPropertyDto[] addPublisherConfigToDto( List adapterProperties) { BasicOutputAdapterPropertyDto[] basicOutputAdapterPropertyDtos = @@ -697,8 +680,14 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac return basicOutputAdapterPropertyDtos; } + /** + * This will set payload of publisher mapping attributes to the DTO + * + * @param mapProperties List of publisher mapping attributes + * @return DTO with all the publisher mapping attributes + */ private org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] addPublisherMappingToDto - (List mapProperties) { + (List mapProperties) { org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] eventMappingPropertyDtos = new org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[mapProperties.size()]; for (int i = 0; i < mapProperties.size(); i++) { @@ -712,6 +701,11 @@ public class AnalyticsArtifactsManagementServiceImpl implements AnalyticsArtifac return eventMappingPropertyDtos; } + /** + * Clean Service client in the stub + * + * @param stub Stud that needs to be cleaned + */ private void cleanup(Stub stub) { if (stub != null) { try { From e643db08d1e52800f95e0d4f02c3f4ec71a02c47 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Fri, 11 Oct 2019 09:21:51 +0530 Subject: [PATCH 4/9] Change event attribute mapping logic and error handling Event attribute mapping logic was moved to service layer from the api layer and a created a meaningful error message. --- .../impl/AnalyticsArtifactsManagementServiceImpl.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java index 72266166eb..443177d18a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java @@ -607,18 +607,18 @@ public class AnalyticsArtifactsManagementServiceImpl } /** - * This will set payload of event attributes to the DTO + * This will set payload of event attribute's mapping to the DTO * * @param attributes list of event attributes * @return DTO with all the event attributes */ private EventStreamAttributeDto[] addEventAttributesToDto(List attributes) { EventStreamAttributeDto[] eventStreamAttributeDtos = new EventStreamAttributeDto[attributes.size()]; - for (int i = 0; i < attributes.size(); i++) { + for (Attribute attribute : attributes) { EventStreamAttributeDto eventStreamAttributeDto = new EventStreamAttributeDto(); - eventStreamAttributeDto.setAttributeName(attributes.get(i).getName()); - eventStreamAttributeDto.setAttributeType(attributes.get(i).getType().toString()); - eventStreamAttributeDtos[i] = eventStreamAttributeDto; + eventStreamAttributeDto.setAttributeName(attribute.getName()); + eventStreamAttributeDto.setAttributeType(attribute.getType().toString()); + } return eventStreamAttributeDtos; } From fe4e857639a64a8134d9fce3a14d31c13cb2566c Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Fri, 11 Oct 2019 10:00:54 +0530 Subject: [PATCH 5/9] Change exception codes of device stream events --- ...alyticsArtifactsManagementServiceImpl.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java index 443177d18a..ecad0b7bd5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java @@ -86,24 +86,27 @@ public class AnalyticsArtifactsManagementServiceImpl if (!isEdited) { eventStreamAdminServiceStub.addEventStreamDefinitionAsString(streamDefinition); } else { - // Find and edit stream if (eventStreamAdminServiceStub.getStreamDetailsForStreamId(id) != null) { eventStreamAdminServiceStub.editEventStreamDefinitionAsString(streamDefinition, id); } } return Response.ok().build(); } catch (AxisFault e) { - log.error("Failed to create event definitions for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (RemoteException e) { - log.error("Failed to connect with the remote services for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the remote services for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (JWTClientException e) { - log.error("Failed to generate jwt token for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to generate jwt token for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (UserStoreException e) { - log.error("Failed to connect with the user store for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the user store for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } finally { cleanup(eventStreamAdminServiceStub); } @@ -118,17 +121,21 @@ public class AnalyticsArtifactsManagementServiceImpl publishStream(stream); return Response.ok().build(); } catch (AxisFault e) { - log.error("Failed to create event definitions for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (RemoteException e) { - log.error("Failed to connect with the remote services for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the remote services for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (JWTClientException e) { - log.error("Failed to generate jwt token for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to generate jwt token for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (UserStoreException e) { - log.error("Failed to connect with the user store for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the user store for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } } @@ -351,7 +358,7 @@ public class AnalyticsArtifactsManagementServiceImpl /** * Set data to a stream dto and publish dto using a stub * - * @param stream Stream definition + * @param stream Stream definition * @throws RemoteException Exception that may occur during a remote method call * @throws UserStoreException Exception that may occur during JWT token generation * @throws JWTClientException Exception that may occur during connecting to client store From d885707046c5e6725c91ef1deb38d810e2a1eda3 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Sun, 13 Oct 2019 12:37:29 +0530 Subject: [PATCH 6/9] Refactor code in a reusable way and change response messages Removed repeated classes and implemented code to already existing classes and divided code into sub methods to limit repetitive code and increase readability. And the response messages were change in order to give users a better understating of the responses they get when calling the api endpoints. --- .../mgt/jaxrs/beans/analytics/Adapter.java | 17 +- .../AdapterMappingConfiguration.java | 12 +- .../jaxrs/beans/analytics/AdapterType.java | 30 -- .../jaxrs/beans/analytics/EventPublisher.java | 34 -- .../jaxrs/beans/analytics/EventReceiver.java | 34 -- .../jaxrs/beans/analytics/EventStream.java | 2 +- .../jaxrs/beans/analytics/TransportType.java | 7 +- .../InvalidExecutionPlanException.java | 4 +- .../AnalyticsArtifactsManagementService.java | 6 +- ...alyticsArtifactsManagementServiceImpl.java | 505 +++++++++--------- 10 files changed, 271 insertions(+), 380 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterType.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventPublisher.java delete mode 100755 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventReceiver.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/Adapter.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/Adapter.java index f3d3bcb639..f11c65efcc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/Adapter.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/Adapter.java @@ -29,10 +29,13 @@ public class Adapter { @ApiModelProperty(value = "Attached stream name:version") private String eventStreamWithVersion; @ApiModelProperty(value = "Adapter type") - private AdapterType adapterType; + private TransportType adapterType; @ApiModelProperty(value = "Adapter main configurations") private AdapterConfiguration adapterConfiguration; + @ApiModelProperty(value = "Adapter definition", notes = "use only when creating adapter as a String") + private String definition; + public String getAdapterName() { return adapterName; } @@ -49,11 +52,11 @@ public class Adapter { this.eventStreamWithVersion = eventStreamWithVersion; } - public AdapterType getAdapterType() { + public TransportType getAdapterType() { return adapterType; } - public void setAdapterType(AdapterType adapterType) { + public void setAdapterType(TransportType adapterType) { this.adapterType = adapterType; } @@ -65,4 +68,12 @@ public class Adapter { AdapterConfiguration adapterConfiguration) { this.adapterConfiguration = adapterConfiguration; } + + public String getDefinition() { + return definition; + } + + public void setDefinition(String definition) { + this.definition = definition; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java index 3984b23065..a9894f459e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java @@ -18,6 +18,8 @@ package org.wso2.carbon.device.mgt.jaxrs.beans.analytics; import io.swagger.annotations.ApiModelProperty; + +import java.util.ArrayList; import java.util.List; /** @@ -30,15 +32,15 @@ public class AdapterMappingConfiguration { @ApiModelProperty(value = "Input mapping for json,text and xml mappings") private String inputMappingString; @ApiModelProperty(value = "Input mapping for json,map and xml mappings") - private List inputMappingProperties; + private List inputMappingProperties = new ArrayList<>(); @ApiModelProperty(value = "Name-scape mapping for xml mapping") - private List namespaceMappingProperties; + private List namespaceMappingProperties = new ArrayList<>(); @ApiModelProperty(value = "Correlation mapping for wso2 mapping") - private List correlationMappingProperties; + private List correlationMappingProperties = new ArrayList<>(); @ApiModelProperty(value = "Payload mapping for wso2 mapping") - private List payloadMappingProperties; + private List payloadMappingProperties = new ArrayList<>(); @ApiModelProperty(value = "Meta mapping for wso2 mapping") - private List metaMappingProperties; + private List metaMappingProperties = new ArrayList<>(); public MessageFormat getMessageFormat() { return messageFormat; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterType.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterType.java deleted file mode 100644 index 413be9fd90..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterType.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2019, Entgra (pvt) Ltd. (http://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 org.wso2.carbon.device.mgt.jaxrs.beans.analytics; - -/** - * Available adapter types - */ -public enum AdapterType { - EMAIL, FILE_TAIL, HTTP, IOT_EVENT, JMS, KAFKA, MQTT, OAUTH_MQTT, SOAP, WEBSOCKET, WEBSOCKET_LOCAL, - WSO2_EVENT, XMPP, UI, RDBMS, SECURED_WEBSOCKET, CASSANDRA, LOGGER; - - public String toStringFormatted() { - return super.toString().toLowerCase().replace("_", "-"); - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventPublisher.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventPublisher.java deleted file mode 100644 index a7e7a1f84b..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventPublisher.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2019, Entgra (pvt) Ltd. (http://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 org.wso2.carbon.device.mgt.jaxrs.beans.analytics; - -import io.swagger.annotations.ApiModelProperty; - -public class EventPublisher { - @ApiModelProperty(value = "Publisher definition") - private String definition; - - public String getDefinition() { - return definition; - } - - public void setDefinition(String definition) { - this.definition = definition; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventReceiver.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventReceiver.java deleted file mode 100755 index dd0c00f8db..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventReceiver.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2019, Entgra (pvt) Ltd. (http://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 org.wso2.carbon.device.mgt.jaxrs.beans.analytics; - -import io.swagger.annotations.ApiModelProperty; - -public class EventReceiver { - @ApiModelProperty(value = "Stream definition") - private String definition; - - public String getDefinition() { - return definition; - } - - public void setDefinition(String definition) { - this.definition = definition; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java index 1fc05ba31b..ed166446f3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java @@ -42,7 +42,7 @@ public class EventStream { @ApiModelProperty(value = "Payload attribute list") private List payloadData = new ArrayList<>(); - @ApiModelProperty(value = "Stream definition") + @ApiModelProperty(value = "Stream definition" , notes = "use only when creating stream as a String") private String definition; public String getName() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/TransportType.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/TransportType.java index abf6122459..bf53512b41 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/TransportType.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/TransportType.java @@ -21,6 +21,11 @@ package org.wso2.carbon.device.mgt.jaxrs.beans.analytics; * This hold the default transport types support by the server. */ public enum TransportType { - HTTP, MQTT; + EMAIL, FILE_TAIL, HTTP, IOT_EVENT, JMS, KAFKA, MQTT, OAUTH_MQTT, SOAP, WEBSOCKET, WEBSOCKET_LOCAL, + WSO2_EVENT, XMPP, UI, RDBMS, SECURED_WEBSOCKET, CASSANDRA, LOGGER; + + public String toStringFormatted() { + return super.toString().toLowerCase().replace("_", "-"); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java index c366b37ef4..cc094d3b77 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java @@ -28,11 +28,11 @@ public class InvalidExecutionPlanException extends WebApplicationException { private static final long serialVersionUID = 7583096344745990515L; public InvalidExecutionPlanException(ErrorResponse error) { - super(Response.status(Response.Status.NOT_FOUND).entity(error).build()); + super(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build()); } public InvalidExecutionPlanException(ErrorDTO errorDTO) { - super(Response.status(Response.Status.NOT_FOUND) + super(Response.status(Response.Status.INTERNAL_SERVER_ERROR) .entity(errorDTO) .header(Constants.DeviceConstants.HEADER_CONTENT_TYPE, Constants.DeviceConstants.APPLICATION_JSON) .build()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/AnalyticsArtifactsManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/AnalyticsArtifactsManagementService.java index 871042bf53..7e5456dadd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/AnalyticsArtifactsManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/AnalyticsArtifactsManagementService.java @@ -24,8 +24,6 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.Adapter; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventStream; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.SiddhiExecutionPlan; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventPublisher; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventReceiver; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import io.swagger.annotations.Api; @@ -258,7 +256,7 @@ public interface AnalyticsArtifactsManagementService { @QueryParam("isEdited") boolean isEdited, @ApiParam(name = "receiver", value = "Add the data to complete the EventReceiver object.", required = true) - @Valid EventReceiver receiver); + @Valid Adapter receiver); @POST @Path("/receiver") @@ -366,7 +364,7 @@ public interface AnalyticsArtifactsManagementService { @QueryParam("isEdited") boolean isEdited, @ApiParam(name = "publisher", value = "Add the data to complete the EventPublisher object.", required = true) - @Valid EventPublisher publisher); + @Valid Adapter publisher); @POST @Path("/publisher") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java index ecad0b7bd5..5c4f686254 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java @@ -26,8 +26,6 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AdapterConfiguration; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AdapterProperty; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.MessageFormat; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.SiddhiExecutionPlan; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventPublisher; -import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventReceiver; import org.wso2.carbon.device.mgt.jaxrs.exception.BadRequestException; import org.wso2.carbon.device.mgt.jaxrs.exception.ErrorDTO; import org.wso2.carbon.device.mgt.jaxrs.exception.InvalidExecutionPlanException; @@ -118,22 +116,22 @@ public class AnalyticsArtifactsManagementServiceImpl public Response deployEventDefinitionAsDto(@Valid EventStream stream) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); try { - publishStream(stream); + deployStream(stream); return Response.ok().build(); } catch (AxisFault e) { - String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; + String errMsg = "Failed to create event definitions for tenant " + tenantDomain; log.error(errMsg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (RemoteException e) { - String errMsg = "Failed to connect with the remote services for tenantDomain: " + tenantDomain; + String errMsg = "Failed to connect with the remote services for tenant " + tenantDomain; log.error(errMsg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (JWTClientException e) { - String errMsg = "Failed to generate jwt token for tenantDomain: " + tenantDomain; + String errMsg = "Failed to generate jwt token for tenant " + tenantDomain; log.error(errMsg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (UserStoreException e) { - String errMsg = "Failed to connect with the user store for tenantDomain: " + tenantDomain; + String errMsg = "Failed to connect with the user store for tenant " + tenantDomain; log.error(errMsg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } @@ -144,7 +142,7 @@ public class AnalyticsArtifactsManagementServiceImpl @Path("/receiver/{name}") public Response deployEventReceiverAsString(@PathParam("name") String name, @QueryParam("isEdited") boolean isEdited, - @Valid EventReceiver receiver) { + @Valid Adapter receiver) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); EventReceiverAdminServiceStub eventReceiverAdminServiceStub; try { @@ -155,20 +153,24 @@ public class AnalyticsArtifactsManagementServiceImpl } else { eventReceiverAdminServiceStub.editActiveEventReceiverConfiguration(receiverDefinition, name); } + return Response.ok().build(); } catch (AxisFault e) { - log.error("Failed to create event definitions for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (RemoteException e) { - log.error("Failed to connect with the remote services for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the remote services for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (JWTClientException e) { - log.error("Failed to generate jwt token for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to generate jwt token for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (UserStoreException e) { - log.error("Failed to connect with the user store for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the user store for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } - return Response.ok().entity(name).build(); } @Override @@ -176,59 +178,31 @@ public class AnalyticsArtifactsManagementServiceImpl @Path("/receiver") public Response deployEventReceiverAsDto(@Valid Adapter receiver) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); - String receiverName = receiver.getAdapterName(); - String adapterType = receiver.getAdapterType().toStringFormatted(); - AdapterConfiguration adapterConfiguration = receiver.getAdapterConfiguration(); - AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration.getAdapterMappingConfiguration(); - try { - List adapterProperties = adapterConfiguration.getAdapterProperties(); - if (adapterProperties == null) { - log.error("Invalid attribute payload"); - return Response.status(Response.Status.BAD_REQUEST).build(); - } - + AdapterConfiguration adapterConfiguration = receiver.getAdapterConfiguration(); boolean customMapping = adapterConfiguration.isCustomMappingEnabled(); - List inputMappingProperties = adapterMappingConfiguration.getInputMappingProperties(); - List namespaceMappingProperties = adapterMappingConfiguration.getNamespaceMappingProperties(); - List correlationMappingProperties = adapterMappingConfiguration.getCorrelationMappingProperties(); - List payloadMappingProperties = adapterMappingConfiguration.getPayloadMappingProperties(); - List metaMappingProperties = adapterMappingConfiguration.getMetaMappingProperties(); - MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); - /* - * Conditions - * - if CustomMappingEnabled check validity of property lists - * - if both inputMappingProperties and namespaceMappingProperties null check remaining property lists - * - if all correlationMappingProperties, payloadMappingProperties, metaMappingProperties null log error - * - if message format is null change the final result to TRUE - * - else continue - * */ - if ((customMapping && - (inputMappingProperties == null && namespaceMappingProperties == null) && - (correlationMappingProperties == null && payloadMappingProperties == null && - metaMappingProperties == null)) || messageFormat == null) { - String errMsg = "Invalid mapping payload"; - log.error(errMsg); - return Response.status(Response.Status.BAD_REQUEST).entity(errMsg).build(); + validateAdapterProperties(adapterConfiguration.getAdapterProperties()); + if (customMapping) { + validateAdapterMapping(adapterConfiguration.getAdapterMappingConfiguration()); } - String eventStreamWithVersion = receiver.getEventStreamWithVersion(); - - publishReceiver(receiverName, adapterType, adapterProperties, customMapping, inputMappingProperties, - namespaceMappingProperties, correlationMappingProperties, payloadMappingProperties, - metaMappingProperties, messageFormat, eventStreamWithVersion); + deployReceiver(receiver, customMapping, adapterConfiguration); return Response.ok().build(); } catch (AxisFault e) { - log.error("Failed to create event definitions for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (RemoteException e) { - log.error("Failed to connect with the remote services for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the remote services for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (JWTClientException e) { - log.error("Failed to generate jwt token for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to generate jwt token for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (UserStoreException e) { - log.error("Failed to connect with the user store for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the user store for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } } @@ -237,33 +211,35 @@ public class AnalyticsArtifactsManagementServiceImpl @Path("/publisher/{name}") public Response deployEventPublisherAsString(@PathParam("name") String name, @QueryParam("isEdited") boolean isEdited, - @Valid EventPublisher publisher) { + @Valid Adapter publisher) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); EventPublisherAdminServiceStub eventPublisherAdminServiceStub; - try { String publisherDefinition = publisher.getDefinition(); - eventPublisherAdminServiceStub = DeviceMgtAPIUtils.getEventPublisherAdminServiceStub(); if (!isEdited) { eventPublisherAdminServiceStub.deployEventPublisherConfiguration(publisherDefinition); } else { eventPublisherAdminServiceStub.editActiveEventPublisherConfiguration(publisherDefinition, name); } + return Response.ok().build(); } catch (AxisFault e) { - log.error("Failed to create event definitions for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (RemoteException e) { - log.error("Failed to connect with the remote services for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the remote services for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (JWTClientException e) { - log.error("Failed to generate jwt token for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to generate jwt token for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (UserStoreException e) { - log.error("Failed to connect with the user store for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the user store for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } - return Response.ok().entity(publisher).build(); } @Override @@ -271,59 +247,31 @@ public class AnalyticsArtifactsManagementServiceImpl @Path("/publisher") public Response deployEventPublisherAsDto(@Valid Adapter publisher) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); - String publisherName = publisher.getAdapterName(); - String adapterType = publisher.getAdapterType().toStringFormatted(); AdapterConfiguration adapterConfiguration = publisher.getAdapterConfiguration(); - AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration.getAdapterMappingConfiguration(); - try { - List adapterProperties = adapterConfiguration.getAdapterProperties(); - if (adapterProperties == null) { - log.error("Invalid attribute payload"); - return Response.status(Response.Status.BAD_REQUEST).build(); - } - + validateAdapterProperties(adapterConfiguration.getAdapterProperties()); boolean customMapping = adapterConfiguration.isCustomMappingEnabled(); - String inputMappingString = adapterMappingConfiguration.getInputMappingString(); - List inputMappingProperties = adapterMappingConfiguration.getInputMappingProperties(); - List correlationMappingProperties = adapterMappingConfiguration.getCorrelationMappingProperties(); - List payloadMappingProperties = adapterMappingConfiguration.getPayloadMappingProperties(); - List metaMappingProperties = adapterMappingConfiguration.getMetaMappingProperties(); - MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); - /* - * Conditions - * - if CustomMappingEnabled check validity of property lists - * - if all correlationMappingProperties, payloadMappingProperties, metaMappingProperties null log error - * - if message format is null change the final result to TRUE - * - else continue - */ - if ((customMapping && - (inputMappingProperties == null && inputMappingString == null) && - (correlationMappingProperties == null && payloadMappingProperties == null && - metaMappingProperties == null)) || messageFormat == null) { - String errMsg = "Invalid mapping payload"; - log.error(errMsg); - return Response.status(Response.Status.BAD_REQUEST).entity(errMsg).build(); + if (customMapping) { + validateAdapterMapping(adapterConfiguration.getAdapterMappingConfiguration()); } - String eventStreamWithVersion = publisher.getEventStreamWithVersion(); - - publishPublisher(publisherName, adapterType, adapterProperties, customMapping - , inputMappingString, inputMappingProperties, correlationMappingProperties - , payloadMappingProperties, metaMappingProperties, messageFormat - , eventStreamWithVersion); + deployPublisher(publisher, customMapping, adapterConfiguration); return Response.ok().build(); } catch (AxisFault e) { - log.error("Failed to create event definitions for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (RemoteException e) { - log.error("Failed to connect with the remote services for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the remote services for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (JWTClientException e) { - log.error("Failed to generate jwt token for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to generate jwt token for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (UserStoreException e) { - log.error("Failed to connect with the user store for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the user store for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } } @@ -338,46 +286,46 @@ public class AnalyticsArtifactsManagementServiceImpl publishSiddhiExecutionPlan(name, isEdited, plan.getDefinition()); return Response.ok().build(); } catch (AxisFault e) { - log.error("Failed to create event definitions for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (RemoteException e) { - log.error("Failed to connect with the remote services for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (InvalidExecutionPlanException e) { - log.error("Invalid Execution plan: " + tenantDomain, e); - return e.getResponse(); + String errMsg = "Failed to connect with the remote services for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (JWTClientException e) { - log.error("Failed to generate jwt token for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to generate jwt token for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (UserStoreException e) { - log.error("Failed to connect with the user store for tenantDomain: " + tenantDomain, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + String errMsg = "Failed to connect with the user store for tenantDomain: " + tenantDomain; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } } /** - * Set data to a stream dto and publish dto using a stub + * Set data to a Stream dto and deploy dto through a stub * * @param stream Stream definition - * @throws RemoteException Exception that may occur during a remote method call - * @throws UserStoreException Exception that may occur during JWT token generation - * @throws JWTClientException Exception that may occur during connecting to client store */ - private void publishStream(EventStream stream) + private void deployStream(EventStream stream) throws RemoteException, UserStoreException, JWTClientException { + EventStreamAdminServiceStub eventStreamAdminServiceStub = null; List metaData = stream.getMetaData(); List payloadData = stream.getPayloadData(); List correlationData = stream.getCorrelationData(); if (metaData.isEmpty() && correlationData.isEmpty() && payloadData.isEmpty()) { - String errMsg = "Invalid payload: event mapping attributes invalid!!!"; + String errMsg = String.format("Failed to validate Stream property attributes of %s:%s", + stream.getName(), stream.getVersion()); ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setMessage(errMsg); log.error(errMsg); throw new BadRequestException(errorResponse); } - EventStreamAdminServiceStub eventStreamAdminServiceStub = - DeviceMgtAPIUtils.getEventStreamAdminServiceStub(); try { + eventStreamAdminServiceStub = DeviceMgtAPIUtils.getEventStreamAdminServiceStub(); + EventStreamDefinitionDto eventStreamDefinitionDto = new EventStreamDefinitionDto(); eventStreamDefinitionDto.setName(stream.getName()); eventStreamDefinitionDto.setVersion(stream.getVersion()); @@ -386,6 +334,7 @@ public class AnalyticsArtifactsManagementServiceImpl eventStreamDefinitionDto.setMetaData(addEventAttributesToDto(metaData)); eventStreamDefinitionDto.setPayloadData(addEventAttributesToDto(payloadData)); eventStreamDefinitionDto.setCorrelationData(addEventAttributesToDto(correlationData)); + String streamId = stream.getName() + ":" + stream.getVersion(); if (eventStreamAdminServiceStub.getStreamDefinitionDto(streamId) != null) { eventStreamAdminServiceStub.editEventStreamDefinitionAsDto(eventStreamDefinitionDto, streamId); @@ -399,88 +348,75 @@ public class AnalyticsArtifactsManagementServiceImpl } /** - * Set data to a receiver dto and publish dto using a stub + * Set data to a receiver dto and deploy dto through a stub * - * @param receiverName Receiver name - * @param adapterType Receiver type - * @param adapterProperties Receiver properties - * @param customMapping Is receiver mapped - * @param inputMappingProperties Receiver input attribute mapping - * @param namespaceMappingProperties Receiver name-scape attribute mapping - * @param correlationMappingProperties Receiver correlation attribute mapping - * @param payloadMappingProperties Receiver payload attribute mapping - * @param metaMappingProperties Receiver meta attribute mapping - * @param messageFormat Receiver mapping format - * @param eventStreamWithVersion Attached stream + * @param receiver Event Receiver adapter + * @param customMapping Is Receiver mapped + * @param adapterConfiguration Adapter property and mapping configuration * @throws RemoteException Exception that may occur during a remote method call * @throws UserStoreException Exception that may occur during JWT token generation * @throws JWTClientException Exception that may occur during connecting to client store */ - private void publishReceiver(String receiverName, String adapterType, - List adapterProperties, boolean customMapping, - List inputMappingProperties, - List namespaceMappingProperties, - List correlationMappingProperties, - List payloadMappingProperties, - List metaMappingProperties, - MessageFormat messageFormat, - String eventStreamWithVersion) + private void deployReceiver(Adapter receiver, boolean customMapping, + AdapterConfiguration adapterConfiguration) throws RemoteException, UserStoreException, JWTClientException { EventReceiverAdminServiceStub eventReceiverAdminServiceStub = DeviceMgtAPIUtils.getEventReceiverAdminServiceStub(); try { + String receiverName = receiver.getAdapterName(); + String adapterType = receiver.getAdapterType().toStringFormatted(); + String eventStreamWithVersion = receiver.getEventStreamWithVersion(); + List adapterProperties = adapterConfiguration.getAdapterProperties(); EventReceiverConfigurationDto eventReceiverConfigurationDto = eventReceiverAdminServiceStub .getActiveEventReceiverConfiguration(receiverName); - - // Check if adapter already exists, if so un-deploy it if (eventReceiverConfigurationDto != null) { eventReceiverAdminServiceStub.undeployActiveEventReceiverConfiguration(receiverName); } + BasicInputAdapterPropertyDto[] basicInputAdapterPropertyDtos = addReceiverConfigToDto(adapterProperties); - // Adding attribute properties to DTOs - BasicInputAdapterPropertyDto[] basicInputAdapterPropertyDtos = - addReceiverConfigToDto(adapterProperties); - - if (eventReceiverAdminServiceStub.getActiveEventReceiverConfiguration(receiverName) == null) { - // Call stub deploy methods according to the message format - if (!messageFormat.toString().equals("wso2event")) { - EventMappingPropertyDto[] inputMappingPropertyDtos = - addReceiverMappingToDto(inputMappingProperties); - if (messageFormat.toString().equals("xml")) { - EventMappingPropertyDto[] namespaceMappingPropertyDtos = - addReceiverMappingToDto(namespaceMappingProperties); - - eventReceiverAdminServiceStub.deployXmlEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, null - , namespaceMappingPropertyDtos, inputMappingPropertyDtos + AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration.getAdapterMappingConfiguration(); + MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); + if (!messageFormat.toString().equals("wso2event")) { + EventMappingPropertyDto[] inputMappingPropertyDtos = + addReceiverMappingToDto(adapterMappingConfiguration.getInputMappingProperties()); + if (messageFormat.toString().equals("xml")) { + EventMappingPropertyDto[] namespaceMappingPropertyDtos = + addReceiverMappingToDto(adapterMappingConfiguration.getNamespaceMappingProperties()); + eventReceiverAdminServiceStub.deployXmlEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, null + , namespaceMappingPropertyDtos, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, customMapping); + } else { + if (messageFormat.toString().equals("map")) { + eventReceiverAdminServiceStub.deployMapEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, customMapping); + } else if (messageFormat.toString().equals("text")) { + eventReceiverAdminServiceStub.deployTextEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos , basicInputAdapterPropertyDtos, customMapping); } else { - if (messageFormat.toString().equals("map")) { - eventReceiverAdminServiceStub.deployMapEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); - } else if (messageFormat.toString().equals("text")) { - eventReceiverAdminServiceStub.deployTextEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); - } else { - eventReceiverAdminServiceStub.deployJsonEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); - } + eventReceiverAdminServiceStub.deployJsonEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, customMapping); } - } else { - EventMappingPropertyDto[] correlationMappingPropertyDtos = addReceiverMappingToDto(correlationMappingProperties); - EventMappingPropertyDto[] metaMappingPropertyDtos = addReceiverMappingToDto(metaMappingProperties); - EventMappingPropertyDto[] payloadMappingPropertyDtos = addReceiverMappingToDto(payloadMappingProperties); - - eventReceiverAdminServiceStub.deployWso2EventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, metaMappingPropertyDtos - , correlationMappingPropertyDtos, payloadMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping - , eventStreamWithVersion); } - + } else { + EventMappingPropertyDto[] correlationMappingPropertyDtos = addReceiverMappingToDto( + adapterMappingConfiguration.getCorrelationMappingProperties() + ); + EventMappingPropertyDto[] metaMappingPropertyDtos = addReceiverMappingToDto( + adapterMappingConfiguration.getInputMappingProperties() + ); + EventMappingPropertyDto[] payloadMappingPropertyDtos = addReceiverMappingToDto( + adapterMappingConfiguration.getPayloadMappingProperties() + ); + + eventReceiverAdminServiceStub.deployWso2EventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, metaMappingPropertyDtos + , correlationMappingPropertyDtos, payloadMappingPropertyDtos + , basicInputAdapterPropertyDtos, customMapping + , eventStreamWithVersion); } } finally { cleanup(eventReceiverAdminServiceStub); @@ -488,88 +424,82 @@ public class AnalyticsArtifactsManagementServiceImpl } /** - * Set data to a publisher dto and publish dto using a stub + * Set data to a publisher dto and deploy dto through a stub * - * @param publisherName Publisher name - * @param adapterType Publisher type - * @param adapterProperties Publisher properties - * @param customMapping Is publisher mapped - * @param correlationMappingProperties Publisher correlation attribute mapping - * @param payloadMappingProperties Publisher payload attribute mapping - * @param metaMappingProperties Publisher meta attribute mapping - * @param messageFormat Publisher mapping format - * @param eventStreamWithVersion Attached stream + * @param publisher Event Publisher adapter + * @param customMapping Is Publisher mapped + * @param adapterConfiguration Publisher property and mapping configuration * @throws RemoteException Exception that may occur during a remote method call * @throws UserStoreException Exception that may occur during JWT token generation * @throws JWTClientException Exception that may occur during connecting to client store */ - private void publishPublisher(String publisherName, String adapterType, - List adapterProperties, - boolean customMapping, - String inputMappingString, - List inputMappingProperties, - List correlationMappingProperties, - List payloadMappingProperties, - List metaMappingProperties, - MessageFormat messageFormat, - String eventStreamWithVersion) + private void deployPublisher(Adapter publisher, boolean customMapping, + AdapterConfiguration adapterConfiguration) throws RemoteException, UserStoreException, JWTClientException { EventPublisherAdminServiceStub eventPublisherAdminServiceStub = DeviceMgtAPIUtils.getEventPublisherAdminServiceStub(); - // Check if adapter already exists, if so un-deploy it try { + String publisherName = publisher.getAdapterName(); + String adapterType = publisher.getAdapterType().toStringFormatted(); + String eventStreamWithVersion = publisher.getEventStreamWithVersion(); + List adapterProperties = adapterConfiguration.getAdapterProperties(); EventPublisherConfigurationDto eventPublisherConfigurationDto = eventPublisherAdminServiceStub .getActiveEventPublisherConfiguration(publisherName); if (eventPublisherConfigurationDto != null) { eventPublisherAdminServiceStub.undeployActiveEventPublisherConfiguration(publisherName); } - // Adding attribute properties to DTOs BasicOutputAdapterPropertyDto[] basicOutputAdapterPropertyDtos = addPublisherConfigToDto(adapterProperties); - if (eventPublisherAdminServiceStub.getActiveEventPublisherConfiguration(publisherName) == null) { - // Call stub deploy methods according to the message format - if (!messageFormat.toString().equals("wso2event")) { - if (!messageFormat.toString().equals("map")) { - if (messageFormat.toString().equals("xml")) { - eventPublisherAdminServiceStub.deployXmlEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, inputMappingString - , basicOutputAdapterPropertyDtos, eventStreamWithVersion - , customMapping); - } else if (messageFormat.toString().equals("text")) { - eventPublisherAdminServiceStub.deployTextEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, inputMappingString - , basicOutputAdapterPropertyDtos, eventStreamWithVersion - , customMapping); - } else { - eventPublisherAdminServiceStub.deployJsonEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, inputMappingString - , basicOutputAdapterPropertyDtos, eventStreamWithVersion - , customMapping); - } + AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration.getAdapterMappingConfiguration(); + MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); + if (!messageFormat.toString().equals("wso2event")) { + if (!messageFormat.toString().equals("map")) { + if (messageFormat.toString().equals("xml")) { + eventPublisherAdminServiceStub.deployXmlEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, adapterMappingConfiguration.getInputMappingString() + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , customMapping); + } else if (messageFormat.toString().equals("text")) { + eventPublisherAdminServiceStub.deployTextEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, adapterMappingConfiguration.getInputMappingString() + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , customMapping); } else { - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] inputMappingPropertyDtos = - addPublisherMappingToDto(inputMappingProperties); - eventPublisherAdminServiceStub.deployMapEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicOutputAdapterPropertyDtos, customMapping); + eventPublisherAdminServiceStub.deployJsonEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, adapterMappingConfiguration.getInputMappingString() + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , customMapping); } } else { - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] correlationMappingPropertyDtos = - addPublisherMappingToDto(correlationMappingProperties); - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] metaMappingPropertyDtos = - addPublisherMappingToDto(metaMappingProperties); - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] payloadMappingPropertyDtos = - addPublisherMappingToDto(payloadMappingProperties); - - eventPublisherAdminServiceStub.deployWSO2EventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, metaMappingPropertyDtos - , correlationMappingPropertyDtos, payloadMappingPropertyDtos - , basicOutputAdapterPropertyDtos, customMapping - , eventStreamWithVersion); + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] inputMappingPropertyDtos = + addPublisherMappingToDto( + adapterMappingConfiguration.getInputMappingProperties() + ); + eventPublisherAdminServiceStub.deployMapEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicOutputAdapterPropertyDtos, customMapping); } - + } else { + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] correlationMappingPropertyDtos = + addPublisherMappingToDto( + adapterMappingConfiguration.getCorrelationMappingProperties() + ); + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] metaMappingPropertyDtos = + addPublisherMappingToDto( + adapterMappingConfiguration.getMetaMappingProperties() + ); + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] payloadMappingPropertyDtos = + addPublisherMappingToDto( + adapterMappingConfiguration.getPayloadMappingProperties() + ); + eventPublisherAdminServiceStub.deployWSO2EventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, metaMappingPropertyDtos + , correlationMappingPropertyDtos, payloadMappingPropertyDtos + , basicOutputAdapterPropertyDtos, customMapping + , eventStreamWithVersion); } + } finally { cleanup(eventPublisherAdminServiceStub); } @@ -588,24 +518,21 @@ public class AnalyticsArtifactsManagementServiceImpl */ private void publishSiddhiExecutionPlan(String name, boolean isEdited, String plan) - throws RemoteException, UserStoreException, JWTClientException, - InvalidExecutionPlanException { + throws RemoteException, UserStoreException, JWTClientException { EventProcessorAdminServiceStub eventProcessorAdminServiceStub = null; try { eventProcessorAdminServiceStub = DeviceMgtAPIUtils.getEventProcessorAdminServiceStub(); - // Validate the plan code String validationResponse = eventProcessorAdminServiceStub.validateExecutionPlan(plan); if (validationResponse.equals("success")) { if (!isEdited) { - // Create a new plan eventProcessorAdminServiceStub.deployExecutionPlan(plan); } else { - // Edit plan eventProcessorAdminServiceStub.editActiveExecutionPlan(plan, name); } } else { ErrorDTO errorDTO = new ErrorDTO(); errorDTO.setMessage(validationResponse); + log.error(validationResponse); throw new InvalidExecutionPlanException(errorDTO); } } finally { @@ -630,6 +557,51 @@ public class AnalyticsArtifactsManagementServiceImpl return eventStreamAttributeDtos; } + /** + * Validate adapter payload attributes + * + * @param adapterProperties Adapter payload attributes + */ + private void validateAdapterProperties(List adapterProperties) { + if (adapterProperties.isEmpty()) { + String errMsg = "Invalid payload: event property attributes invalid!!!"; + ErrorResponse errorResponse = new ErrorResponse(); + errorResponse.setMessage(errMsg); + log.error(errMsg); + throw new BadRequestException(errorResponse); + } + } + + /** + * Validate adapter mapping attributes + *

+ * Conditions + * - if both inputMappingProperties and namespaceMappingProperties null check remaining property lists + * - if all correlationMappingProperties, payloadMappingProperties, metaMappingProperties null log error + * - if message format is null change the final result to TRUE + * - else continue + * + * @param adapterMappingConfiguration Adapter mapping attributes + */ + private void validateAdapterMapping(AdapterMappingConfiguration adapterMappingConfiguration) { + + if (adapterMappingConfiguration.getInputMappingString() == null && + (adapterMappingConfiguration.getInputMappingProperties().isEmpty() && + adapterMappingConfiguration.getNamespaceMappingProperties().isEmpty()) && + ( + adapterMappingConfiguration.getCorrelationMappingProperties().isEmpty() && + adapterMappingConfiguration.getPayloadMappingProperties().isEmpty() && + adapterMappingConfiguration.getMetaMappingProperties().isEmpty() + ) + || adapterMappingConfiguration.getMessageFormat() == null) { + String errMsg = "Invalid payload: event mapping attributes invalid!!!"; + ErrorResponse errorResponse = new ErrorResponse(); + errorResponse.setMessage(errMsg); + log.error(errMsg); + throw new BadRequestException(errorResponse); + } + } + /** * This will set payload of receiver attributes to the DTO * @@ -655,7 +627,8 @@ public class AnalyticsArtifactsManagementServiceImpl * @param mapProperties List of receiver mapping attributes * @return DTO with all the receiver mapping attributes */ - private EventMappingPropertyDto[] addReceiverMappingToDto(List mapProperties) { + private EventMappingPropertyDto[] addReceiverMappingToDto + (List mapProperties) { EventMappingPropertyDto[] eventMappingPropertyDtos = new EventMappingPropertyDto[mapProperties.size()]; for (int i = 0; i < mapProperties.size(); i++) { EventMappingPropertyDto eventMappingPropertyDto = new EventMappingPropertyDto(); From 51b7f8b206443c1503ea587690396e859afec9de Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Wed, 16 Oct 2019 07:02:38 +0530 Subject: [PATCH 7/9] Modify code to limit garbage collectible objects and create method to deploy adapters without custom mapping Removed creation of ArrayLists when a service is been called and added null validation to those lists in service layer. Developed code to deploy adapters when custom mapping is false and updated exception handling. --- .../beans/analytics/AdapterConfiguration.java | 2 +- .../AdapterMappingConfiguration.java | 10 +- .../jaxrs/beans/analytics/EventStream.java | 6 +- .../jaxrs/exception/BadRequestException.java | 21 +- .../InvalidExecutionPlanException.java | 2 +- ...alyticsArtifactsManagementServiceImpl.java | 404 ++++++++++++------ 6 files changed, 298 insertions(+), 147 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterConfiguration.java index 5c0b820f41..1c07761073 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterConfiguration.java @@ -27,7 +27,7 @@ import java.util.List; */ public class AdapterConfiguration { @ApiModelProperty(value = "Adapter properties list") - private List adapterProperties = new ArrayList<>(); + private List adapterProperties; @ApiModelProperty(value = "Custom-mapping flag") private boolean isCustomMappingEnabled; @ApiModelProperty(value = "Mapping configurations") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java index a9894f459e..6885c0a3c4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/AdapterMappingConfiguration.java @@ -32,15 +32,15 @@ public class AdapterMappingConfiguration { @ApiModelProperty(value = "Input mapping for json,text and xml mappings") private String inputMappingString; @ApiModelProperty(value = "Input mapping for json,map and xml mappings") - private List inputMappingProperties = new ArrayList<>(); + private List inputMappingProperties; @ApiModelProperty(value = "Name-scape mapping for xml mapping") - private List namespaceMappingProperties = new ArrayList<>(); + private List namespaceMappingProperties; @ApiModelProperty(value = "Correlation mapping for wso2 mapping") - private List correlationMappingProperties = new ArrayList<>(); + private List correlationMappingProperties; @ApiModelProperty(value = "Payload mapping for wso2 mapping") - private List payloadMappingProperties = new ArrayList<>(); + private List payloadMappingProperties; @ApiModelProperty(value = "Meta mapping for wso2 mapping") - private List metaMappingProperties = new ArrayList<>(); + private List metaMappingProperties; public MessageFormat getMessageFormat() { return messageFormat; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java index ed166446f3..91a2db78e1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/EventStream.java @@ -36,11 +36,11 @@ public class EventStream { @ApiModelProperty(value = "Stream description") private String description; @ApiModelProperty(value = "Meta attribute list") - private List metaData = new ArrayList<>(); + private List metaData; @ApiModelProperty(value = "Correlation attribute list") - private List correlationData = new ArrayList<>(); + private List correlationData; @ApiModelProperty(value = "Payload attribute list") - private List payloadData = new ArrayList<>(); + private List payloadData; @ApiModelProperty(value = "Stream definition" , notes = "use only when creating stream as a String") private String definition; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java index d97b78b6dd..6a09ee3af2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.jaxrs.exception; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; @@ -27,8 +28,26 @@ import javax.ws.rs.core.Response; * Custom exception class for wrapping BadRequest related exceptions. */ public class BadRequestException extends WebApplicationException { + private String message; + private static final long serialVersionUID = -24991723711391192L; public BadRequestException(ErrorResponse error) { super(Response.status(Response.Status.BAD_REQUEST).entity(error).build()); } -} \ No newline at end of file + + public BadRequestException(ErrorDTO errorDTO) { + super(Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(errorDTO) + .header(Constants.DeviceConstants.HEADER_CONTENT_TYPE, Constants.DeviceConstants.APPLICATION_JSON) + .build()); + message = errorDTO.getMessage(); + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java index cc094d3b77..8c453ba9cc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java @@ -36,7 +36,7 @@ public class InvalidExecutionPlanException extends WebApplicationException { .entity(errorDTO) .header(Constants.DeviceConstants.HEADER_CONTENT_TYPE, Constants.DeviceConstants.APPLICATION_JSON) .build()); - message = errorDTO.getDescription(); + message = errorDTO.getMessage(); } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java index 5c4f686254..623d903400 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java @@ -18,7 +18,6 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.Attribute; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.AdapterMappingConfiguration; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.MappingProperty; @@ -116,8 +115,12 @@ public class AnalyticsArtifactsManagementServiceImpl public Response deployEventDefinitionAsDto(@Valid EventStream stream) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); try { + validateStreamProperties(stream); deployStream(stream); return Response.ok().build(); + } catch (BadRequestException e) { + log.error(e.getMessage(), e); + return e.getResponse(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenant " + tenantDomain; log.error(errMsg, e); @@ -187,6 +190,9 @@ public class AnalyticsArtifactsManagementServiceImpl } deployReceiver(receiver, customMapping, adapterConfiguration); return Response.ok().build(); + } catch (BadRequestException e) { + log.error(e.getMessage(), e); + return e.getResponse(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -256,6 +262,9 @@ public class AnalyticsArtifactsManagementServiceImpl } deployPublisher(publisher, customMapping, adapterConfiguration); return Response.ok().build(); + } catch (BadRequestException e) { + log.error(e.getMessage(), e); + return e.getResponse(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -283,8 +292,11 @@ public class AnalyticsArtifactsManagementServiceImpl @Valid SiddhiExecutionPlan plan) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); try { - publishSiddhiExecutionPlan(name, isEdited, plan.getDefinition()); + deploySiddhiExecutionPlan(name, isEdited, plan.getDefinition()); return Response.ok().build(); + } catch (InvalidExecutionPlanException e) { + log.error(e.getMessage(), e); + return e.getResponse(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -315,14 +327,6 @@ public class AnalyticsArtifactsManagementServiceImpl List metaData = stream.getMetaData(); List payloadData = stream.getPayloadData(); List correlationData = stream.getCorrelationData(); - if (metaData.isEmpty() && correlationData.isEmpty() && payloadData.isEmpty()) { - String errMsg = String.format("Failed to validate Stream property attributes of %s:%s", - stream.getName(), stream.getVersion()); - ErrorResponse errorResponse = new ErrorResponse(); - errorResponse.setMessage(errMsg); - log.error(errMsg); - throw new BadRequestException(errorResponse); - } try { eventStreamAdminServiceStub = DeviceMgtAPIUtils.getEventStreamAdminServiceStub(); @@ -331,10 +335,15 @@ public class AnalyticsArtifactsManagementServiceImpl eventStreamDefinitionDto.setVersion(stream.getVersion()); eventStreamDefinitionDto.setNickName(stream.getNickName()); eventStreamDefinitionDto.setDescription(stream.getDescription()); - eventStreamDefinitionDto.setMetaData(addEventAttributesToDto(metaData)); - eventStreamDefinitionDto.setPayloadData(addEventAttributesToDto(payloadData)); - eventStreamDefinitionDto.setCorrelationData(addEventAttributesToDto(correlationData)); - + if (metaData != null) { + eventStreamDefinitionDto.setMetaData(addEventAttributesToDto(metaData)); + } + if (payloadData != null) { + eventStreamDefinitionDto.setPayloadData(addEventAttributesToDto(payloadData)); + } + if (correlationData != null) { + eventStreamDefinitionDto.setCorrelationData(addEventAttributesToDto(correlationData)); + } String streamId = stream.getName() + ":" + stream.getVersion(); if (eventStreamAdminServiceStub.getStreamDefinitionDto(streamId) != null) { eventStreamAdminServiceStub.editEventStreamDefinitionAsDto(eventStreamDefinitionDto, streamId); @@ -344,7 +353,6 @@ public class AnalyticsArtifactsManagementServiceImpl } finally { cleanup(eventStreamAdminServiceStub); } - } /** @@ -360,8 +368,8 @@ public class AnalyticsArtifactsManagementServiceImpl private void deployReceiver(Adapter receiver, boolean customMapping, AdapterConfiguration adapterConfiguration) throws RemoteException, UserStoreException, JWTClientException { - EventReceiverAdminServiceStub eventReceiverAdminServiceStub = DeviceMgtAPIUtils.getEventReceiverAdminServiceStub(); - + EventReceiverAdminServiceStub eventReceiverAdminServiceStub = DeviceMgtAPIUtils + .getEventReceiverAdminServiceStub(); try { String receiverName = receiver.getAdapterName(); String adapterType = receiver.getAdapterType().toStringFormatted(); @@ -374,55 +382,95 @@ public class AnalyticsArtifactsManagementServiceImpl } BasicInputAdapterPropertyDto[] basicInputAdapterPropertyDtos = addReceiverConfigToDto(adapterProperties); - AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration.getAdapterMappingConfiguration(); - MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); - if (!messageFormat.toString().equals("wso2event")) { - EventMappingPropertyDto[] inputMappingPropertyDtos = - addReceiverMappingToDto(adapterMappingConfiguration.getInputMappingProperties()); - if (messageFormat.toString().equals("xml")) { - EventMappingPropertyDto[] namespaceMappingPropertyDtos = - addReceiverMappingToDto(adapterMappingConfiguration.getNamespaceMappingProperties()); - eventReceiverAdminServiceStub.deployXmlEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, null - , namespaceMappingPropertyDtos, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); - } else { - if (messageFormat.toString().equals("map")) { - eventReceiverAdminServiceStub.deployMapEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); - } else if (messageFormat.toString().equals("text")) { - eventReceiverAdminServiceStub.deployTextEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); + if (customMapping) { + AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration + .getAdapterMappingConfiguration(); + MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); + if (!messageFormat.toString().equals("wso2event")) { + EventMappingPropertyDto[] inputMappingPropertyDtos = + addReceiverMappingToDto(adapterMappingConfiguration.getInputMappingProperties()); + if (messageFormat.toString().equals("xml")) { + EventMappingPropertyDto[] namespaceMappingPropertyDtos = + addReceiverMappingToDto(adapterMappingConfiguration.getNamespaceMappingProperties()); + eventReceiverAdminServiceStub.deployXmlEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, null + , namespaceMappingPropertyDtos, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, true); } else { - eventReceiverAdminServiceStub.deployJsonEventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping); + if (messageFormat.toString().equals("map")) { + eventReceiverAdminServiceStub.deployMapEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, true); + } else if (messageFormat.toString().equals("text")) { + eventReceiverAdminServiceStub.deployTextEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, true); + } else { + eventReceiverAdminServiceStub.deployJsonEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicInputAdapterPropertyDtos, true); + } } + } else { + EventMappingPropertyDto[] correlationMappingPropertyDtos = addReceiverMappingToDto( + adapterMappingConfiguration.getCorrelationMappingProperties() + ); + EventMappingPropertyDto[] metaMappingPropertyDtos = addReceiverMappingToDto( + adapterMappingConfiguration.getInputMappingProperties() + ); + EventMappingPropertyDto[] payloadMappingPropertyDtos = addReceiverMappingToDto( + adapterMappingConfiguration.getPayloadMappingProperties() + ); + + eventReceiverAdminServiceStub.deployWso2EventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, metaMappingPropertyDtos + , correlationMappingPropertyDtos, payloadMappingPropertyDtos + , basicInputAdapterPropertyDtos, true + , eventStreamWithVersion); } } else { - EventMappingPropertyDto[] correlationMappingPropertyDtos = addReceiverMappingToDto( - adapterMappingConfiguration.getCorrelationMappingProperties() - ); - EventMappingPropertyDto[] metaMappingPropertyDtos = addReceiverMappingToDto( - adapterMappingConfiguration.getInputMappingProperties() - ); - EventMappingPropertyDto[] payloadMappingPropertyDtos = addReceiverMappingToDto( - adapterMappingConfiguration.getPayloadMappingProperties() - ); - - eventReceiverAdminServiceStub.deployWso2EventReceiverConfiguration(receiverName - , eventStreamWithVersion, adapterType, metaMappingPropertyDtos - , correlationMappingPropertyDtos, payloadMappingPropertyDtos - , basicInputAdapterPropertyDtos, customMapping - , eventStreamWithVersion); + deployReceiverWithoutMapping(receiverName, eventStreamWithVersion, adapterType, + eventReceiverAdminServiceStub, basicInputAdapterPropertyDtos); } } finally { cleanup(eventReceiverAdminServiceStub); } } + /** + * To deploy receiver if custom mapping is false + * + * @param receiverName Name of the receiver + * @param eventStreamWithVersion Attached event stream of the receiver + * @param adapterType Adapter type name + * @param eventReceiverAdminServiceStub Stub to deploy receiver + * @param basicInputAdapterPropertyDtos DTO to attach receiver data + * @throws RemoteException Exception that may occur during a remote method call + */ + private void deployReceiverWithoutMapping(String receiverName, String eventStreamWithVersion + , String adapterType, EventReceiverAdminServiceStub eventReceiverAdminServiceStub + , BasicInputAdapterPropertyDto[] basicInputAdapterPropertyDtos) + throws RemoteException { + switch (adapterType) { + case "iot-event": + case "wso2event": + eventReceiverAdminServiceStub.deployWso2EventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, null, null + , null, basicInputAdapterPropertyDtos, false + , eventStreamWithVersion); + break; + case "soap": + eventReceiverAdminServiceStub.deployXmlEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, null, null + , null, basicInputAdapterPropertyDtos, false); + break; + default: + eventReceiverAdminServiceStub.deployTextEventReceiverConfiguration(receiverName + , eventStreamWithVersion, adapterType, null + , basicInputAdapterPropertyDtos, false); + } + } + /** * Set data to a publisher dto and deploy dto through a stub * @@ -436,7 +484,8 @@ public class AnalyticsArtifactsManagementServiceImpl private void deployPublisher(Adapter publisher, boolean customMapping, AdapterConfiguration adapterConfiguration) throws RemoteException, UserStoreException, JWTClientException { - EventPublisherAdminServiceStub eventPublisherAdminServiceStub = DeviceMgtAPIUtils.getEventPublisherAdminServiceStub(); + EventPublisherAdminServiceStub eventPublisherAdminServiceStub = DeviceMgtAPIUtils + .getEventPublisherAdminServiceStub(); try { String publisherName = publisher.getAdapterName(); String adapterType = publisher.getAdapterType().toStringFormatted(); @@ -451,53 +500,65 @@ public class AnalyticsArtifactsManagementServiceImpl BasicOutputAdapterPropertyDto[] basicOutputAdapterPropertyDtos = addPublisherConfigToDto(adapterProperties); - AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration.getAdapterMappingConfiguration(); - MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); - if (!messageFormat.toString().equals("wso2event")) { - if (!messageFormat.toString().equals("map")) { - if (messageFormat.toString().equals("xml")) { - eventPublisherAdminServiceStub.deployXmlEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, adapterMappingConfiguration.getInputMappingString() - , basicOutputAdapterPropertyDtos, eventStreamWithVersion - , customMapping); - } else if (messageFormat.toString().equals("text")) { - eventPublisherAdminServiceStub.deployTextEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, adapterMappingConfiguration.getInputMappingString() - , basicOutputAdapterPropertyDtos, eventStreamWithVersion - , customMapping); + if (customMapping) { + AdapterMappingConfiguration adapterMappingConfiguration = adapterConfiguration + .getAdapterMappingConfiguration(); + MessageFormat messageFormat = adapterMappingConfiguration.getMessageFormat(); + if (!messageFormat.toString().equals("wso2event")) { + if (!messageFormat.toString().equals("map")) { + if (messageFormat.toString().equals("xml")) { + eventPublisherAdminServiceStub.deployXmlEventPublisherConfiguration( + publisherName, eventStreamWithVersion, adapterType + , adapterMappingConfiguration.getInputMappingString() + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , true); + } else if (messageFormat.toString().equals("text")) { + eventPublisherAdminServiceStub.deployTextEventPublisherConfiguration( + publisherName, eventStreamWithVersion, adapterType + , adapterMappingConfiguration.getInputMappingString() + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , true); + } else { + eventPublisherAdminServiceStub.deployJsonEventPublisherConfiguration( + publisherName, eventStreamWithVersion, adapterType + , adapterMappingConfiguration.getInputMappingString() + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , true); + } } else { - eventPublisherAdminServiceStub.deployJsonEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, adapterMappingConfiguration.getInputMappingString() - , basicOutputAdapterPropertyDtos, eventStreamWithVersion - , customMapping); + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] inputMappingPropertyDtos = + addPublisherMappingToDto( + adapterMappingConfiguration.getInputMappingProperties() + ); + eventPublisherAdminServiceStub.deployMapEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, inputMappingPropertyDtos + , basicOutputAdapterPropertyDtos, true); } } else { - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] inputMappingPropertyDtos = - addPublisherMappingToDto( - adapterMappingConfiguration.getInputMappingProperties() + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] + correlationMappingPropertyDtos = addPublisherMappingToDto + ( + adapterMappingConfiguration.getCorrelationMappingProperties() + ); + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] + metaMappingPropertyDtos = addPublisherMappingToDto + ( + adapterMappingConfiguration.getMetaMappingProperties() ); - eventPublisherAdminServiceStub.deployMapEventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, inputMappingPropertyDtos - , basicOutputAdapterPropertyDtos, customMapping); + org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] + payloadMappingPropertyDtos = addPublisherMappingToDto + ( + adapterMappingConfiguration.getPayloadMappingProperties() + ); + eventPublisherAdminServiceStub.deployWSO2EventPublisherConfiguration( + publisherName, eventStreamWithVersion, adapterType, metaMappingPropertyDtos + , correlationMappingPropertyDtos, payloadMappingPropertyDtos + , basicOutputAdapterPropertyDtos, true + , eventStreamWithVersion); } } else { - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] correlationMappingPropertyDtos = - addPublisherMappingToDto( - adapterMappingConfiguration.getCorrelationMappingProperties() - ); - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] metaMappingPropertyDtos = - addPublisherMappingToDto( - adapterMappingConfiguration.getMetaMappingProperties() - ); - org.wso2.carbon.event.publisher.stub.types.EventMappingPropertyDto[] payloadMappingPropertyDtos = - addPublisherMappingToDto( - adapterMappingConfiguration.getPayloadMappingProperties() - ); - eventPublisherAdminServiceStub.deployWSO2EventPublisherConfiguration(publisherName - , eventStreamWithVersion, adapterType, metaMappingPropertyDtos - , correlationMappingPropertyDtos, payloadMappingPropertyDtos - , basicOutputAdapterPropertyDtos, customMapping - , eventStreamWithVersion); + deployPublisherWithoutMapping(publisherName, eventStreamWithVersion, adapterType + , eventPublisherAdminServiceStub, basicOutputAdapterPropertyDtos); } } finally { @@ -505,6 +566,49 @@ public class AnalyticsArtifactsManagementServiceImpl } } + /** + * To deploy publisher if custom mapping is false + * + * @param publisherName Name of the publisher + * @param eventStreamWithVersion Attached event stream of the publisher + * @param adapterType Adapter type name + * @param eventPublisherAdminServiceStub Stub to deploy publisher + * @param basicOutputAdapterPropertyDtos DTO to attach publisher data + * @throws RemoteException Exception that may occur during a remote method call + */ + private void deployPublisherWithoutMapping(String publisherName, String eventStreamWithVersion + , String adapterType, EventPublisherAdminServiceStub eventPublisherAdminServiceStub + , BasicOutputAdapterPropertyDto[] basicOutputAdapterPropertyDtos) + throws RemoteException { + switch (adapterType) { + case "wso2event": + case "ui": + case "secured-websocket": + eventPublisherAdminServiceStub.deployWSO2EventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, null + , null, null, basicOutputAdapterPropertyDtos + , false, eventStreamWithVersion); + break; + case "soap": + eventPublisherAdminServiceStub.deployXmlEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, null + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , false); + break; + case "cassandra": + case "rdbms": + eventPublisherAdminServiceStub.deployMapEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, null + , basicOutputAdapterPropertyDtos, false); + break; + default: + eventPublisherAdminServiceStub.deployTextEventPublisherConfiguration(publisherName + , eventStreamWithVersion, adapterType, null + , basicOutputAdapterPropertyDtos, eventStreamWithVersion + , false); + } + } + /** * Publish a siddhi execution plan using a stub * @@ -516,9 +620,9 @@ public class AnalyticsArtifactsManagementServiceImpl * @throws JWTClientException Exception that may occur during connecting to client store * @throws InvalidExecutionPlanException Exception that may occur if execution plan validation fails */ - private void publishSiddhiExecutionPlan(String name, boolean isEdited, - String plan) - throws RemoteException, UserStoreException, JWTClientException { + private void deploySiddhiExecutionPlan(String name, boolean isEdited, String plan) + throws RemoteException, UserStoreException, JWTClientException, + InvalidExecutionPlanException { EventProcessorAdminServiceStub eventProcessorAdminServiceStub = null; try { eventProcessorAdminServiceStub = DeviceMgtAPIUtils.getEventProcessorAdminServiceStub(); @@ -532,7 +636,6 @@ public class AnalyticsArtifactsManagementServiceImpl } else { ErrorDTO errorDTO = new ErrorDTO(); errorDTO.setMessage(validationResponse); - log.error(validationResponse); throw new InvalidExecutionPlanException(errorDTO); } } finally { @@ -541,20 +644,21 @@ public class AnalyticsArtifactsManagementServiceImpl } /** - * This will set payload of event attribute's mapping to the DTO + * Validate stream properties * - * @param attributes list of event attributes - * @return DTO with all the event attributes + * @param stream EventStream object */ - private EventStreamAttributeDto[] addEventAttributesToDto(List attributes) { - EventStreamAttributeDto[] eventStreamAttributeDtos = new EventStreamAttributeDto[attributes.size()]; - for (Attribute attribute : attributes) { - EventStreamAttributeDto eventStreamAttributeDto = new EventStreamAttributeDto(); - eventStreamAttributeDto.setAttributeName(attribute.getName()); - eventStreamAttributeDto.setAttributeType(attribute.getType().toString()); - + private void validateStreamProperties(EventStream stream) throws BadRequestException { + if ((stream.getMetaData() == null || stream.getMetaData().isEmpty()) && + (stream.getCorrelationData() == null || stream.getCorrelationData().isEmpty()) && + (stream.getPayloadData() == null || stream.getPayloadData().isEmpty())) { + String errMsg = String.format("Failed to validate Stream property attributes of %s:%s. " + + "Stream mapping can't be null or empty", + stream.getName(), stream.getVersion()); + ErrorDTO errorDTO = new ErrorDTO(); + errorDTO.setMessage(errMsg); + throw new BadRequestException(errorDTO); } - return eventStreamAttributeDtos; } /** @@ -562,13 +666,13 @@ public class AnalyticsArtifactsManagementServiceImpl * * @param adapterProperties Adapter payload attributes */ - private void validateAdapterProperties(List adapterProperties) { - if (adapterProperties.isEmpty()) { - String errMsg = "Invalid payload: event property attributes invalid!!!"; - ErrorResponse errorResponse = new ErrorResponse(); - errorResponse.setMessage(errMsg); - log.error(errMsg); - throw new BadRequestException(errorResponse); + private void validateAdapterProperties(List adapterProperties) + throws BadRequestException { + if (adapterProperties == null) { + String errMsg = "Failed to validate adapter attributes. Adapter attributes can't be null"; + ErrorDTO errorDTO = new ErrorDTO(); + errorDTO.setMessage(errMsg); + throw new BadRequestException(errorDTO); } } @@ -583,25 +687,53 @@ public class AnalyticsArtifactsManagementServiceImpl * * @param adapterMappingConfiguration Adapter mapping attributes */ - private void validateAdapterMapping(AdapterMappingConfiguration adapterMappingConfiguration) { - - if (adapterMappingConfiguration.getInputMappingString() == null && - (adapterMappingConfiguration.getInputMappingProperties().isEmpty() && - adapterMappingConfiguration.getNamespaceMappingProperties().isEmpty()) && - ( - adapterMappingConfiguration.getCorrelationMappingProperties().isEmpty() && - adapterMappingConfiguration.getPayloadMappingProperties().isEmpty() && - adapterMappingConfiguration.getMetaMappingProperties().isEmpty() - ) - || adapterMappingConfiguration.getMessageFormat() == null) { - String errMsg = "Invalid payload: event mapping attributes invalid!!!"; - ErrorResponse errorResponse = new ErrorResponse(); - errorResponse.setMessage(errMsg); - log.error(errMsg); - throw new BadRequestException(errorResponse); + private void validateAdapterMapping(AdapterMappingConfiguration adapterMappingConfiguration) + throws BadRequestException { + if (adapterMappingConfiguration == null) { + String errMsg = "Failed to validate adapter mapping attributes. " + + "Adapter mapping configuration can't be null"; + ErrorDTO errorDTO = new ErrorDTO(); + errorDTO.setMessage(errMsg); + throw new BadRequestException(errorDTO); + } else if (adapterMappingConfiguration.getMessageFormat() == null || + ((adapterMappingConfiguration.getInputMappingString() == null) + && (adapterMappingConfiguration.getInputMappingProperties() == null || + adapterMappingConfiguration.getInputMappingProperties().isEmpty()) + && (adapterMappingConfiguration.getNamespaceMappingProperties() == null || + adapterMappingConfiguration.getNamespaceMappingProperties().isEmpty())) + && + ((adapterMappingConfiguration.getCorrelationMappingProperties() == null || + adapterMappingConfiguration.getCorrelationMappingProperties().isEmpty()) + && (adapterMappingConfiguration.getPayloadMappingProperties() == null || + adapterMappingConfiguration.getPayloadMappingProperties().isEmpty()) + && (adapterMappingConfiguration.getMetaMappingProperties() == null || + adapterMappingConfiguration.getMetaMappingProperties().isEmpty())) + ) { + String errMsg = "Failed to validate adapter mapping attributes. " + + "Adapter mapping configuration invalid"; + ErrorDTO errorDTO = new ErrorDTO(); + errorDTO.setMessage(errMsg); + throw new BadRequestException(errorDTO); } } + /** + * This will set payload of event attribute's mapping to the DTO + * + * @param attributes list of event attributes + * @return DTO with all the event attributes + */ + private EventStreamAttributeDto[] addEventAttributesToDto(List attributes) { + EventStreamAttributeDto[] eventStreamAttributeDtos = new EventStreamAttributeDto[attributes.size()]; + for (int i = 0; i < attributes.size(); i++) { + EventStreamAttributeDto eventStreamAttributeDto = new EventStreamAttributeDto(); + eventStreamAttributeDto.setAttributeName(attributes.get(i).getName()); + eventStreamAttributeDto.setAttributeType(attributes.get(i).getType().toString()); + eventStreamAttributeDtos[i] = eventStreamAttributeDto; + } + return eventStreamAttributeDtos; + } + /** * This will set payload of receiver attributes to the DTO * From 8bebf2c126dad3b298434a6f6f42dfbe9dbcf165 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Wed, 23 Oct 2019 06:50:59 +0530 Subject: [PATCH 8/9] Wrap service level error messages from api level error messages --- .../jaxrs/exception/BadRequestException.java | 4 +++ .../InvalidExecutionPlanException.java | 4 +++ ...alyticsArtifactsManagementServiceImpl.java | 36 +++++++++---------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java index 6a09ee3af2..0be9790e3a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java @@ -43,6 +43,10 @@ public class BadRequestException extends WebApplicationException { message = errorDTO.getMessage(); } + public BadRequestException(String message) { + this.message = message; + } + public String getMessage() { return message; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java index 8c453ba9cc..61f1eacc0c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java @@ -39,6 +39,10 @@ public class InvalidExecutionPlanException extends WebApplicationException { message = errorDTO.getMessage(); } + public InvalidExecutionPlanException(String message) { + this.message = message; + } + @Override public String getMessage() { return message; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java index 623d903400..1ca322f442 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java @@ -119,8 +119,9 @@ public class AnalyticsArtifactsManagementServiceImpl deployStream(stream); return Response.ok().build(); } catch (BadRequestException e) { - log.error(e.getMessage(), e); - return e.getResponse(); + String errMsg = "Failed to deploy stream due to invalid payload"; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenant " + tenantDomain; log.error(errMsg, e); @@ -191,8 +192,9 @@ public class AnalyticsArtifactsManagementServiceImpl deployReceiver(receiver, customMapping, adapterConfiguration); return Response.ok().build(); } catch (BadRequestException e) { - log.error(e.getMessage(), e); - return e.getResponse(); + String errMsg = "Failed to deploy receiver due to invalid payload"; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -263,8 +265,9 @@ public class AnalyticsArtifactsManagementServiceImpl deployPublisher(publisher, customMapping, adapterConfiguration); return Response.ok().build(); } catch (BadRequestException e) { - log.error(e.getMessage(), e); - return e.getResponse(); + String errMsg = "Failed to deploy publisher due to invalid payload"; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -295,8 +298,9 @@ public class AnalyticsArtifactsManagementServiceImpl deploySiddhiExecutionPlan(name, isEdited, plan.getDefinition()); return Response.ok().build(); } catch (InvalidExecutionPlanException e) { - log.error(e.getMessage(), e); - return e.getResponse(); + String errMsg = "Failed to deploy siddhi script due to invalid payload"; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -634,9 +638,7 @@ public class AnalyticsArtifactsManagementServiceImpl eventProcessorAdminServiceStub.editActiveExecutionPlan(plan, name); } } else { - ErrorDTO errorDTO = new ErrorDTO(); - errorDTO.setMessage(validationResponse); - throw new InvalidExecutionPlanException(errorDTO); + throw new InvalidExecutionPlanException(validationResponse); } } finally { cleanup(eventProcessorAdminServiceStub); @@ -655,9 +657,7 @@ public class AnalyticsArtifactsManagementServiceImpl String errMsg = String.format("Failed to validate Stream property attributes of %s:%s. " + "Stream mapping can't be null or empty", stream.getName(), stream.getVersion()); - ErrorDTO errorDTO = new ErrorDTO(); - errorDTO.setMessage(errMsg); - throw new BadRequestException(errorDTO); + throw new BadRequestException(errMsg); } } @@ -670,9 +670,7 @@ public class AnalyticsArtifactsManagementServiceImpl throws BadRequestException { if (adapterProperties == null) { String errMsg = "Failed to validate adapter attributes. Adapter attributes can't be null"; - ErrorDTO errorDTO = new ErrorDTO(); - errorDTO.setMessage(errMsg); - throw new BadRequestException(errorDTO); + throw new BadRequestException(errMsg); } } @@ -692,9 +690,7 @@ public class AnalyticsArtifactsManagementServiceImpl if (adapterMappingConfiguration == null) { String errMsg = "Failed to validate adapter mapping attributes. " + "Adapter mapping configuration can't be null"; - ErrorDTO errorDTO = new ErrorDTO(); - errorDTO.setMessage(errMsg); - throw new BadRequestException(errorDTO); + throw new BadRequestException(errMsg); } else if (adapterMappingConfiguration.getMessageFormat() == null || ((adapterMappingConfiguration.getInputMappingString() == null) && (adapterMappingConfiguration.getInputMappingProperties() == null || From 4a3a9b587ee351061cc84f064659b1d6a2ae0642 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Wed, 23 Oct 2019 11:51:34 +0530 Subject: [PATCH 9/9] Change invalid payload exception responses to bad requests --- .../device/mgt/jaxrs/exception/BadRequestException.java | 2 +- .../impl/AnalyticsArtifactsManagementServiceImpl.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java index 0be9790e3a..0c6b68217d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java @@ -36,7 +36,7 @@ public class BadRequestException extends WebApplicationException { } public BadRequestException(ErrorDTO errorDTO) { - super(Response.status(Response.Status.INTERNAL_SERVER_ERROR) + super(Response.status(Response.Status.BAD_REQUEST) .entity(errorDTO) .header(Constants.DeviceConstants.HEADER_CONTENT_TYPE, Constants.DeviceConstants.APPLICATION_JSON) .build()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java index 1ca322f442..bdd7fdb420 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java @@ -121,7 +121,7 @@ public class AnalyticsArtifactsManagementServiceImpl } catch (BadRequestException e) { String errMsg = "Failed to deploy stream due to invalid payload"; log.error(errMsg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); + return Response.status(Response.Status.BAD_REQUEST).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenant " + tenantDomain; log.error(errMsg, e); @@ -194,7 +194,7 @@ public class AnalyticsArtifactsManagementServiceImpl } catch (BadRequestException e) { String errMsg = "Failed to deploy receiver due to invalid payload"; log.error(errMsg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); + return Response.status(Response.Status.BAD_REQUEST).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -267,7 +267,7 @@ public class AnalyticsArtifactsManagementServiceImpl } catch (BadRequestException e) { String errMsg = "Failed to deploy publisher due to invalid payload"; log.error(errMsg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); + return Response.status(Response.Status.BAD_REQUEST).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -300,7 +300,7 @@ public class AnalyticsArtifactsManagementServiceImpl } catch (InvalidExecutionPlanException e) { String errMsg = "Failed to deploy siddhi script due to invalid payload"; log.error(errMsg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); + return Response.status(Response.Status.BAD_REQUEST).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e);