diff --git a/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/plugins/PluginExecutor.java b/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/plugins/PluginExecutor.java index b44e087560..df7314e645 100644 --- a/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/plugins/PluginExecutor.java +++ b/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/plugins/PluginExecutor.java @@ -1,7 +1,6 @@ package com.appsmith.external.plugins; import com.appsmith.external.models.ActionConfiguration; -import com.appsmith.external.models.CommandParams; import com.appsmith.external.models.Param; import com.appsmith.external.models.ResourceConfiguration; import org.pf4j.ExtensionPoint; diff --git a/app/server/appsmith-plugins/postgresPlugin/pom.xml b/app/server/appsmith-plugins/postgresPlugin/pom.xml index 1e3e069f8d..b634879c2f 100644 --- a/app/server/appsmith-plugins/postgresPlugin/pom.xml +++ b/app/server/appsmith-plugins/postgresPlugin/pom.xml @@ -1,79 +1,79 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.external.plugins - postgresPlugin - 1.0-SNAPSHOT + com.external.plugins + postgresPlugin + 1.0-SNAPSHOT - postgresPlugin + postgresPlugin - - UTF-8 - 11 - ${java.version} - ${java.version} - postgres-plugin - com.external.plugins.PostgresPlugin - 1.0-SNAPSHOT - tech@appsmith.com - - + + UTF-8 + 11 + ${java.version} + ${java.version} + postgres-plugin + com.external.plugins.PostgresPlugin + 1.0-SNAPSHOT + tech@appsmith.com + + - - - junit - junit - 4.11 - test - + + + junit + junit + 4.11 + test + - - org.pf4j - pf4j-spring - 0.5.0 - + + org.pf4j + pf4j-spring + 0.5.0 + - - com.appsmith - interfaces - 1.0-SNAPSHOT - + + com.appsmith + interfaces + 1.0-SNAPSHOT + - - org.projectlombok - lombok - 1.18.8 - provided - + + org.projectlombok + lombok + 1.18.8 + provided + - + - - - - maven-compiler-plugin - 3.8.1 - - - org.apache.maven.plugins - maven-jar-plugin - 3.1.2 - - - - ${plugin.id} - ${plugin.class} - ${plugin.version} - ${plugin.provider} - ${plugin.dependencies} - - - - - - + + + + maven-compiler-plugin + 3.8.1 + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.2 + + + + ${plugin.id} + ${plugin.class} + ${plugin.version} + ${plugin.provider} + ${plugin.dependencies} + + + + + + diff --git a/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java b/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java index 4ad7f852ca..b10602dee2 100644 --- a/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java +++ b/app/server/appsmith-plugins/postgresPlugin/src/main/java/com/external/plugins/PostgresPlugin.java @@ -1,7 +1,6 @@ package com.external.plugins; import com.appsmith.external.models.ActionConfiguration; -import com.appsmith.external.models.CommandParams; import com.appsmith.external.models.Param; import com.appsmith.external.models.ResourceConfiguration; import com.appsmith.external.plugins.BasePlugin; @@ -13,7 +12,6 @@ import org.pf4j.PluginWrapper; import org.springframework.util.Assert; import reactor.core.publisher.Flux; -import javax.annotation.Resource; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; @@ -26,14 +24,14 @@ import java.util.List; @Slf4j public class PostgresPlugin extends BasePlugin { - static String JDBC_DRIVER="org.postgresql.Driver"; + static String JDBC_DRIVER = "org.postgresql.Driver"; - static String DB_URL="jdbc:postgresql://localhost/mobtools"; + static String DB_URL = "jdbc:postgresql://localhost/mobtools"; // Database credentials - static String DB_USER="root"; + static String DB_USER = "root"; - static String DB_PASS="root"; + static String DB_PASS = "root"; static Connection conn = null; @@ -61,7 +59,7 @@ public class PostgresPlugin extends BasePlugin { public void stop() throws PluginException { log.debug("PostgresPlugin.stop()"); try { - if(conn != null) { + if (conn != null) { conn.close(); } } catch (SQLException e) { diff --git a/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java b/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java index 737371925d..b61f54c95f 100644 --- a/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java +++ b/app/server/appsmith-plugins/postgresPlugin/src/test/java/com/external/plugins/PostgresPluginTest.java @@ -1,20 +1,18 @@ package com.external.plugins; -import static org.junit.Assert.assertTrue; - import org.junit.Test; +import static org.junit.Assert.assertTrue; + /** * Unit test for simple App. */ -public class PostgresPluginTest -{ +public class PostgresPluginTest { /** * Rigorous Test :-) */ @Test - public void shouldAnswerWithTrue() - { - assertTrue( true ); + public void shouldAnswerWithTrue() { + assertTrue(true); } } diff --git a/app/server/appsmith-plugins/restApiPlugin/pom.xml b/app/server/appsmith-plugins/restApiPlugin/pom.xml index 36d180dd87..d01b3ddfa1 100644 --- a/app/server/appsmith-plugins/restApiPlugin/pom.xml +++ b/app/server/appsmith-plugins/restApiPlugin/pom.xml @@ -1,85 +1,85 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.external.plugins - restApiPlugin - 1.0-SNAPSHOT + com.external.plugins + restApiPlugin + 1.0-SNAPSHOT - restApiPlugin + restApiPlugin - - UTF-8 - 11 - ${java.version} - ${java.version} - restapi-plugin - com.external.plugins.RestApiPlugin - 1.0-SNAPSHOT - tech@appsmith.com - - + + UTF-8 + 11 + ${java.version} + ${java.version} + restapi-plugin + com.external.plugins.RestApiPlugin + 1.0-SNAPSHOT + tech@appsmith.com + + - - - junit - junit - 4.11 - test - + + + junit + junit + 4.11 + test + - - org.pf4j - pf4j-spring - 0.5.0 - + + org.pf4j + pf4j-spring + 0.5.0 + - - com.appsmith - interfaces - 1.0-SNAPSHOT - + + com.appsmith + interfaces + 1.0-SNAPSHOT + - - org.springframework - spring-webflux - 5.1.5.RELEASE - + + org.springframework + spring-webflux + 5.1.5.RELEASE + - - org.projectlombok - lombok - 1.18.8 - provided - + + org.projectlombok + lombok + 1.18.8 + provided + - + - - - - maven-compiler-plugin - 3.8.1 - - - org.apache.maven.plugins - maven-jar-plugin - 3.1.2 - - - - ${plugin.id} - ${plugin.class} - ${plugin.version} - ${plugin.provider} - ${plugin.dependencies} - - - - - - + + + + maven-compiler-plugin + 3.8.1 + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.2 + + + + ${plugin.id} + ${plugin.class} + ${plugin.version} + ${plugin.provider} + ${plugin.dependencies} + + + + + + diff --git a/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java b/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java index a0a4327053..c0c5c060b2 100644 --- a/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java +++ b/app/server/appsmith-plugins/restApiPlugin/src/main/java/com/external/plugins/RestApiPlugin.java @@ -37,16 +37,17 @@ public class RestApiPlugin extends BasePlugin { ActionConfiguration actionConfiguration, List params) { JSONObject requestBody = actionConfiguration.getBody(); - if(requestBody == null) { + if (requestBody == null) { requestBody = new JSONObject(); } + //TODO: Substitue variables from params in all parts (actionConfig, resourceConfig etc) via JsonPath: https://github.com/json-path/JsonPath Map propertyMap = params.stream() .collect(Collectors.toMap(Param::getKey, param -> param)); String path = (actionConfiguration.getPath() == null) ? "" : actionConfiguration.getPath(); String url = resourceConfiguration.getUrl() + path; HttpMethod httpMethod = actionConfiguration.getHttpMethod(); - if(httpMethod == null) { + if (httpMethod == null) { return Flux.error(new Exception("HttpMethod must not be null")); } @@ -54,6 +55,7 @@ public class RestApiPlugin extends BasePlugin { WebClient webClient = WebClient.builder() .baseUrl(url) + // TODO: Ideally this should come from action / resource config .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .build(); @@ -63,6 +65,7 @@ public class RestApiPlugin extends BasePlugin { Mono responseMono = request.exchange(); return responseMono.flatMapMany(response -> { log.debug("Got response: {}", response); + // TODO: Refactor for better switch case List contentTypes = response.headers().header(HttpHeaders.CONTENT_TYPE); Class clazz = String.class; if (contentTypes != null && contentTypes.size() > 0) { diff --git a/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java b/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java index 065ab1dcce..899fdd3e98 100644 --- a/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java +++ b/app/server/appsmith-plugins/restApiPlugin/src/test/java/com/external/plugins/RestApiPluginTest.java @@ -1,20 +1,18 @@ package com.external.plugins; -import static org.junit.Assert.assertTrue; - import org.junit.Test; +import static org.junit.Assert.assertTrue; + /** * Unit test for simple App. */ -public class RestApiPluginTest -{ +public class RestApiPluginTest { /** * Rigorous Test :-) */ @Test - public void shouldAnswerWithTrue() - { - assertTrue( true ); + public void shouldAnswerWithTrue() { + assertTrue(true); } } diff --git a/app/server/appsmith-server/pom.xml b/app/server/appsmith-server/pom.xml index bb5c35fbaa..fce897e7d3 100644 --- a/app/server/appsmith-server/pom.xml +++ b/app/server/appsmith-server/pom.xml @@ -1,147 +1,167 @@ - 4.0.0 - - com.appsmith - integrated - 1.0-SNAPSHOT - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + com.appsmith + integrated + 1.0-SNAPSHOT + com.appsmith server 1.0-SNAPSHOT jar - server - This is the API server for the Appsmith project + server + This is the API server for the Appsmith project - - 11 - + + 11 + - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - jboss-maven2-release-repository - JBoss Spring Repository - https://repository.jboss.org/nexus/content/repositories/public/ - - false - - - + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + jboss-maven2-release-repository + JBoss Spring Repository + https://repository.jboss.org/nexus/content/repositories/public/ + + false + + + - - - org.springframework.boot - spring-boot-starter-cache - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.security - spring-security-oauth2-client - 5.1.4.RELEASE - - - org.springframework.security - spring-security-oauth2-jose - 5.1.4.RELEASE - - - org.springframework.security - spring-security-config - 5.1.4.RELEASE - - - org.springframework.boot - spring-boot-starter-webflux - - - org.postgresql - postgresql - runtime - - - org.springframework.boot - spring-boot-starter-data-mongodb-reactive - - - org.hibernate.validator - hibernate-validator - 6.0.17.Final - - - org.glassfish - javax.el - 3.0.0 - - - org.projectlombok - lombok - true - - - com.github.spullara.mustache.java - compiler - 0.9.6 - - + + + org.springframework.boot + spring-boot-starter-cache + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.security + spring-security-oauth2-client + 5.1.4.RELEASE + + + org.springframework.security + spring-security-oauth2-jose + 5.1.4.RELEASE + + + org.springframework.security + spring-security-config + 5.1.4.RELEASE + + + org.springframework.boot + spring-boot-starter-webflux + + + org.postgresql + postgresql + runtime + + + org.springframework.boot + spring-boot-starter-data-mongodb-reactive + + + org.hibernate.validator + hibernate-validator + 6.0.17.Final + + + org.glassfish + javax.el + 3.0.0 + + + org.projectlombok + lombok + true + + + com.github.spullara.mustache.java + compiler + 0.9.6 + + de.flapdoodle.embed de.flapdoodle.embed.mongo test + + com.segment.analytics.java + analytics + 2.1.1 + + + com.google.guava + guava + 28.1-jre + + + + org.pf4j + pf4j-spring + 0.5.0 + - - - org.pf4j - pf4j-spring - 0.5.0 - + + + com.appsmith + interfaces + 1.0-SNAPSHOT + - - - com.appsmith - interfaces - 1.0-SNAPSHOT - + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + + + io.projectreactor + reactor-test + 3.2.11.RELEASE + test + + + com.segment.analytics.java + analytics + 2.1.1 + + + com.rollbar + rollbar-java + 1.5.2 + - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.security - spring-security-test - test - - - io.projectreactor - reactor-test - 3.2.11.RELEASE - test - - + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/RollbarConfig.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/RollbarConfig.java new file mode 100644 index 0000000000..39322b7598 --- /dev/null +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/RollbarConfig.java @@ -0,0 +1,20 @@ +package com.appsmith.server.configurations; + +import com.rollbar.notifier.Rollbar; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static com.rollbar.notifier.config.ConfigBuilder.withAccessToken; + +@Configuration +public class RollbarConfig { + + @Value("${com.rollbar.access-token}") + String rollbarAccessToken; + + @Bean + Rollbar rollbarConfiguration() { + return Rollbar.init(withAccessToken(rollbarAccessToken).build()); + } +} diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/SegmentConfig.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/SegmentConfig.java new file mode 100644 index 0000000000..2388aa89f3 --- /dev/null +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/SegmentConfig.java @@ -0,0 +1,17 @@ +package com.appsmith.server.configurations; + +import com.segment.analytics.Analytics; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SegmentConfig { + @Value("${segment.writeKey}") + private String writeKey; + + @Bean + public Analytics analyticsRunner() { + return Analytics.builder(writeKey).build(); + } +} diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/IndexController.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/IndexController.java index f7408a1c8a..184f7baf61 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/IndexController.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/IndexController.java @@ -1,7 +1,7 @@ package com.appsmith.server.controllers; import com.appsmith.server.domains.User; -import com.appsmith.server.services.UserService; +import com.appsmith.server.services.SessionUserService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; @@ -16,10 +16,10 @@ import java.security.Principal; @RequestMapping("") public class IndexController { - private final UserService service; + private final SessionUserService service; @Autowired - public IndexController(UserService service) { + public IndexController(SessionUserService service) { this.service = service; } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/PropertyPane.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/PropertyPane.java index ff5578d118..afcfc415e2 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/PropertyPane.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/PropertyPane.java @@ -18,5 +18,4 @@ public class PropertyPane extends BaseDomain { Map> config; - String configVersion; } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java index ebf6f48eb1..9e5e271468 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/exceptions/GlobalExceptionHandler.java @@ -1,7 +1,10 @@ package com.appsmith.server.exceptions; import com.appsmith.server.dtos.ResponseDTO; +import com.rollbar.notifier.Rollbar; +import com.segment.analytics.Analytics; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -16,6 +19,14 @@ import reactor.core.publisher.Mono; @ControllerAdvice @Slf4j public class GlobalExceptionHandler { + private final Analytics analytics; + private final Rollbar rollbar; + + @Autowired + public GlobalExceptionHandler(Analytics analytics, Rollbar rollbar) { + this.analytics = analytics; + this.rollbar = rollbar; + } /** * This function only catches the AppsmithException type and formats it into ResponseEntity object @@ -31,6 +42,7 @@ public class GlobalExceptionHandler { public Mono> catchAppsmithException(AppsmithException e, ServerWebExchange exchange) { exchange.getResponse().setStatusCode(HttpStatus.resolve(e.getHttpStatus())); log.error("", e); + rollbar.log(e); return Mono.just(new ResponseDTO<>(e.getHttpStatus(), new ErrorDTO(e.getAppErrorCode(), e.getMessage()))); } @@ -47,6 +59,7 @@ public class GlobalExceptionHandler { public Mono> catchException(Exception e, ServerWebExchange exchange) { exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR); log.error("", e); + rollbar.log(e); return Mono.just(new ResponseDTO<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), new ErrorDTO(AppsmithError.INTERNAL_SERVER_ERROR.getHttpErrorCode(), AppsmithError.INTERNAL_SERVER_ERROR.getMessage()))); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ActionServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ActionServiceImpl.java index 503ec4ad6c..5078eb7a7b 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ActionServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ActionServiceImpl.java @@ -6,10 +6,12 @@ import com.appsmith.server.domains.Page; import com.appsmith.server.domains.PageAction; import com.appsmith.server.domains.Plugin; import com.appsmith.server.domains.Resource; +import com.appsmith.server.domains.User; import com.appsmith.server.dtos.ExecuteActionDTO; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.repositories.ActionRepository; +import com.segment.analytics.Analytics; import lombok.extern.slf4j.Slf4j; import org.pf4j.PluginManager; import org.springframework.beans.factory.annotation.Autowired; @@ -44,8 +46,10 @@ public class ActionServiceImpl extends BaseService userMono = super.sessionUserService.getCurrentUser(); Mono resourceMono = resourceService.findById(action.getResourceId()); Mono pluginMono = resourceMono.flatMap(resource -> pluginService.findById(resource.getPluginId())); Mono pageMono = pageService.findById(action.getPageId()); @@ -96,7 +101,9 @@ public class ActionServiceImpl extends BaseService resourceMono.zipWith(pluginExecutorMono, (resource, pluginExecutor) -> { log.debug("*** About to invoke the plugin**"); - // TODO: The CommandParams is being passed as null here. Move it to interfaces.CommandParams + // TODO: The CommandParams is being passed as null here. Move it to interfaces.CommandParams - N/A return pluginExecutor.execute(resource.getResourceConfiguration(), action.getActionConfiguration(), executeActionDTO.getParams()); })) .flatMapIterable(Flux::toIterable); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java index 1714ac3165..513e8199f4 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ApplicationServiceImpl.java @@ -6,6 +6,7 @@ import com.appsmith.server.domains.User; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.repositories.ApplicationRepository; +import com.segment.analytics.Analytics; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; @@ -22,12 +23,12 @@ import javax.validation.Validator; @Service public class ApplicationServiceImpl extends BaseService implements ApplicationService { - private final UserService userService; + private final Analytics analytics; @Autowired - public ApplicationServiceImpl(Scheduler scheduler, Validator validator, MongoConverter mongoConverter, ReactiveMongoTemplate reactiveMongoTemplate, ApplicationRepository repository, UserService userService) { - super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository); - this.userService = userService; + public ApplicationServiceImpl(Scheduler scheduler, Validator validator, MongoConverter mongoConverter, ReactiveMongoTemplate reactiveMongoTemplate, ApplicationRepository repository, SessionUserService sessionUserService, Analytics analytics) { + super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analytics, sessionUserService); + this.analytics = analytics; } @Override @@ -36,7 +37,7 @@ public class ApplicationServiceImpl extends BaseService userMono = userService.getCurrentUser(); + Mono userMono = super.sessionUserService.getCurrentUser(); return userMono .map(user -> user.getOrganizationId()) @@ -44,12 +45,14 @@ public class ApplicationServiceImpl extends BaseService get() { - Mono userMono = userService.getCurrentUser(); + Mono userMono = super.sessionUserService.getCurrentUser(); return userMono .map(user -> user.getOrganizationId()) @@ -62,7 +65,7 @@ public class ApplicationServiceImpl extends BaseService userMono = userService.getCurrentUser(); + Mono userMono = super.sessionUserService.getCurrentUser(); return userMono .map(user -> user.getOrganizationId()) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/BaseService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/BaseService.java index ed4ab62a32..a03b74f84a 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/BaseService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/BaseService.java @@ -2,11 +2,14 @@ package com.appsmith.server.services; import com.appsmith.server.constants.FieldName; import com.appsmith.server.domains.BaseDomain; +import com.appsmith.server.domains.User; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.repositories.BaseRepository; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; +import com.segment.analytics.Analytics; +import com.segment.analytics.messages.TrackMessage; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.data.mongodb.core.convert.MongoConverter; import org.springframework.data.mongodb.core.query.Criteria; @@ -17,6 +20,7 @@ import reactor.core.publisher.Mono; import reactor.core.scheduler.Scheduler; import javax.validation.Validator; +import java.util.HashMap; import java.util.Map; public abstract class BaseService implements CrudService { @@ -31,16 +35,22 @@ public abstract class BaseService create(T object) { + Mono userMono = sessionUserService.getCurrentUser(); return Mono.just(object) .flatMap(this::validateObject) - .flatMap(repository::save); + .flatMap(repository::save) + .flatMap(this::segmentTrackCreate); } private DBObject getDbObject(Object o) { @@ -106,4 +118,21 @@ public abstract class BaseService segmentTrackCreate(Object savedObject) { + Mono userMono = sessionUserService.getCurrentUser(); + return userMono + .map(user -> { + HashMap analyticsProperties = new HashMap<>(); + analyticsProperties.put("id", ((BaseDomain) savedObject).getId()); + analyticsProperties.put("organizationId", user.getOrganizationId()); + analytics.enqueue( + TrackMessage.builder("MONGO_DB_CREATE_" + savedObject.getClass()) + .userId(user.getId()) + .properties(analyticsProperties) + ); + return (T) savedObject; + }) + .switchIfEmpty(Mono.just((T) savedObject)); + } } \ No newline at end of file diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/OrganizationServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/OrganizationServiceImpl.java index 2cf183cb2b..76107e776f 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/OrganizationServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/OrganizationServiceImpl.java @@ -4,9 +4,11 @@ import com.appsmith.server.constants.FieldName; import com.appsmith.server.domains.Organization; import com.appsmith.server.domains.OrganizationSetting; import com.appsmith.server.domains.Setting; +import com.appsmith.server.domains.User; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.repositories.OrganizationRepository; +import com.segment.analytics.Analytics; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; @@ -33,8 +35,10 @@ public class OrganizationServiceImpl extends BaseService enhanceOrganizationSettingList(Organization organization) { diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java index ed702029df..4171fe84c6 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PageServiceImpl.java @@ -7,6 +7,7 @@ import com.appsmith.server.domains.User; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.repositories.PageRepository; +import com.segment.analytics.Analytics; import lombok.extern.slf4j.Slf4j; import org.bson.types.ObjectId; import org.springframework.beans.factory.annotation.Autowired; @@ -24,13 +25,12 @@ import java.util.List; @Slf4j public class PageServiceImpl extends BaseService implements PageService { - private final UserService userService; private final ApplicationService applicationService; @Autowired - public PageServiceImpl(Scheduler scheduler, Validator validator, MongoConverter mongoConverter, ReactiveMongoTemplate reactiveMongoTemplate, PageRepository repository, UserService userService, ApplicationService applicationService) { - super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository); - this.userService = userService; + public PageServiceImpl(Scheduler scheduler, Validator validator, MongoConverter mongoConverter, ReactiveMongoTemplate reactiveMongoTemplate, PageRepository repository, ApplicationService applicationService, + Analytics analytics, SessionUserService sessionUserService) { + super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analytics, sessionUserService); this.applicationService = applicationService; } @@ -43,6 +43,9 @@ public class PageServiceImpl extends BaseService i } else if (page.getApplicationId() == null) { return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.APPLICATIONID)); } + + Mono userMono = super.sessionUserService.getCurrentUser(); + List layoutList = page.getLayouts(); if (layoutList == null) { layoutList = new ArrayList<>(); @@ -51,7 +54,9 @@ public class PageServiceImpl extends BaseService i layoutList.add(createDefaultLayout()); page.setLayouts(layoutList); } - return repository.save(page); + return repository + .save(page) + .flatMap(this::segmentTrackCreate); } @@ -72,7 +77,7 @@ public class PageServiceImpl extends BaseService i @Override public Mono doesPageIdBelongToCurrentUserOrganization(Page page) { - Mono userMono = userService.getCurrentUser(); + Mono userMono = super.sessionUserService.getCurrentUser(); final String[] username = {null}; return userMono diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PluginServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PluginServiceImpl.java index 7d96e5f88f..5f6bfa5aeb 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PluginServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PluginServiceImpl.java @@ -10,6 +10,7 @@ import com.appsmith.server.dtos.PluginOrgDTO; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.repositories.PluginRepository; +import com.segment.analytics.Analytics; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; @@ -30,7 +31,6 @@ public class PluginServiceImpl extends BaseService userMono = super.sessionUserService.getCurrentUser(); + plugin.setDeleted(false); - return pluginRepository.save(plugin); + return pluginRepository + .save(plugin) + .flatMap(this::segmentTrackCreate); } @Override @@ -86,7 +91,7 @@ public class PluginServiceImpl extends BaseService This is to find if the organization has the plugin installed - Mono userMono = userService.getCurrentUser(); + Mono userMono = super.sessionUserService.getCurrentUser(); Mono organizationMono = userMono.flatMap(user -> organizationService.findByIdAndPluginsPluginId(user.getOrganizationId(), pluginDTO.getPluginId())); @@ -107,7 +112,7 @@ public class PluginServiceImpl extends BaseService storeOrganizationPlugin(PluginOrgDTO pluginDTO, OrganizationPluginStatus status) { //Find the organization using id and plugin id -> This is to find if the organization already has the plugin installed - Mono userMono = userService.getCurrentUser(); + Mono userMono = super.sessionUserService.getCurrentUser(); Mono organizationMono = userMono.flatMap(user -> organizationService.findByIdAndPluginsPluginId(user.getOrganizationId(), pluginDTO.getPluginId())); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PropertyPaneServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PropertyPaneServiceImpl.java index 92816f5e2c..abc2f3901a 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PropertyPaneServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/PropertyPaneServiceImpl.java @@ -6,6 +6,7 @@ import com.appsmith.server.domains.WidgetSectionProperty; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.repositories.PropertyPaneRepository; +import com.segment.analytics.Analytics; import lombok.extern.slf4j.Slf4j; import org.bson.types.ObjectId; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; @@ -15,19 +16,18 @@ import reactor.core.publisher.Mono; import reactor.core.scheduler.Scheduler; import javax.validation.Validator; -import java.util.Iterator; import java.util.List; import java.util.Map; @Slf4j @Service public class PropertyPaneServiceImpl extends BaseService implements PropertyPaneService { - public PropertyPaneServiceImpl(Scheduler scheduler, Validator validator, MongoConverter mongoConverter, ReactiveMongoTemplate reactiveMongoTemplate, PropertyPaneRepository repository) { - super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository); + public PropertyPaneServiceImpl(Scheduler scheduler, Validator validator, MongoConverter mongoConverter, ReactiveMongoTemplate reactiveMongoTemplate, PropertyPaneRepository repository, Analytics analytics, SessionUserService sessionUserService) { + super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analytics, sessionUserService); } @Override - public Mono create (PropertyPane propertyPane) { + public Mono create(PropertyPane propertyPane) { if (propertyPane.getId() != null) { return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, "id")); } @@ -49,9 +49,6 @@ public class PropertyPaneServiceImpl extends BaseService { - savedPropertyPane.setConfigVersion(savedPropertyPane.getId()); - return repository.save(savedPropertyPane); - }); + .flatMap(this::segmentTrackCreate); } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ResourceServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ResourceServiceImpl.java index 0bc9733f7d..c6df7a3e58 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ResourceServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ResourceServiceImpl.java @@ -6,6 +6,7 @@ import com.appsmith.server.domains.User; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.repositories.ResourceRepository; +import com.segment.analytics.Analytics; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; @@ -23,14 +24,13 @@ public class ResourceServiceImpl extends BaseService userMono = userService.getCurrentUser(); + Mono userMono = super.sessionUserService.getCurrentUser(); Mono organizationMono = userMono.flatMap(user -> organizationService.findByIdAndPluginsPluginId(user.getOrganizationId(), resource.getPluginId())); @@ -55,7 +55,8 @@ public class ResourceServiceImpl extends BaseService getCurrentUser(); +} diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/SessionUserServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/SessionUserServiceImpl.java new file mode 100644 index 0000000000..03d6386f25 --- /dev/null +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/SessionUserServiceImpl.java @@ -0,0 +1,41 @@ +package com.appsmith.server.services; + +import com.appsmith.server.domains.User; +import com.appsmith.server.repositories.UserRepository; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.ReactiveSecurityContextHolder; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +@Slf4j +@Service +public class SessionUserServiceImpl implements SessionUserService { + + private final UserRepository repository; + + public SessionUserServiceImpl(UserRepository userRepository) { + this.repository = userRepository; + } + + @Override + public Mono getCurrentUser() { + return ReactiveSecurityContextHolder.getContext() + .map(SecurityContext::getAuthentication) + .map(Authentication::getPrincipal) + .flatMap(principal -> { + String email; + if (principal instanceof org.springframework.security.core.userdetails.User) { + org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) principal; + //Assumption that the user has inputted an email as username during user creation and not english passport name + email = user.getUsername(); + } else { + DefaultOidcUser defaultOidcUser = (DefaultOidcUser) principal; + email = defaultOidcUser.getEmail(); + } + return repository.findByEmail(email); + }); + } +} diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/SettingServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/SettingServiceImpl.java index 08dd05c8b7..0d0e8d3535 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/SettingServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/SettingServiceImpl.java @@ -2,6 +2,7 @@ package com.appsmith.server.services; import com.appsmith.server.domains.Setting; import com.appsmith.server.repositories.SettingRepository; +import com.segment.analytics.Analytics; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.data.mongodb.core.convert.MongoConverter; @@ -21,8 +22,10 @@ public class SettingServiceImpl extends BaseService { Mono save(User newUser); - Mono getCurrentUser(); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java index 58f3d83f1d..0c1cb36124 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java @@ -5,22 +5,22 @@ import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.helpers.BeanCopyUtils; import com.appsmith.server.repositories.UserRepository; +import com.segment.analytics.Analytics; +import com.segment.analytics.messages.IdentifyMessage; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.data.mongodb.core.convert.MongoConverter; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.ReactiveSecurityContextHolder; -import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; -import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; import org.springframework.stereotype.Service; import reactor.core.publisher.Mono; import reactor.core.scheduler.Scheduler; import javax.validation.Validator; +import java.util.HashMap; +import java.util.Map; @Slf4j @Service @@ -28,16 +28,21 @@ public class UserServiceImpl extends BaseService i private UserRepository repository; private final OrganizationService organizationService; + private final Analytics analytics; @Autowired public UserServiceImpl(Scheduler scheduler, Validator validator, MongoConverter mongoConverter, ReactiveMongoTemplate reactiveMongoTemplate, - UserRepository repository, OrganizationService organizationService) { - super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository); + UserRepository repository, + OrganizationService organizationService, + Analytics analytics, + SessionUserService sessionUserService) { + super(scheduler, validator, mongoConverter, reactiveMongoTemplate, repository, analytics, sessionUserService); this.repository = repository; this.organizationService = organizationService; + this.analytics = analytics; } @Override @@ -52,7 +57,20 @@ public class UserServiceImpl extends BaseService i @Override public Mono save(User user) { - return repository.save(user); + + Mono savedUserMono = repository.save(user); + return savedUserMono + .map(savedUser -> { + Map traitsMap = new HashMap<>(); + traitsMap.put("name", savedUser.getName()); + traitsMap.put("email", savedUser.getEmail()); + analytics.enqueue(IdentifyMessage.builder() + .userId(savedUser.getId()) + .traits(traitsMap) + ); + analytics.flush(); + return savedUser; + }); } @Override @@ -60,25 +78,6 @@ public class UserServiceImpl extends BaseService i return repository.findByName(username).block(); } - @Override - public Mono getCurrentUser() { - return ReactiveSecurityContextHolder.getContext() - .map(SecurityContext::getAuthentication) - .map(Authentication::getPrincipal) - .flatMap(principal -> { - String email; - if (principal instanceof org.springframework.security.core.userdetails.User) { - org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) principal; - //Assumption that the user has inputted an email as username during user creation and not english passport name - email = user.getUsername(); - } else { - DefaultOidcUser defaultOidcUser = (DefaultOidcUser) principal; - email = defaultOidcUser.getEmail(); - } - return repository.findByEmail(email); - }); - } - @Override public Mono update(String id, User userUpdate) { Mono userFromRepository = repository.findById(id); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/WidgetServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/WidgetServiceImpl.java index a7886d99f5..bb4065677c 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/WidgetServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/WidgetServiceImpl.java @@ -2,6 +2,7 @@ package com.appsmith.server.services; import com.appsmith.server.domains.Widget; import com.appsmith.server.repositories.WidgetRepository; +import com.segment.analytics.Analytics; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.ReactiveMongoTemplate; @@ -23,8 +24,10 @@ public class WidgetServiceImpl extends BaseService organizationResponse = organizationService.create(null); StepVerifier.create(organizationResponse) @@ -46,6 +48,7 @@ public class OrganizationServiceTest { } @Test + @WithMockUser(username = "api_user") public void nullName() { organization.setName(null); Mono organizationResponse = organizationService.create(organization); @@ -56,8 +59,10 @@ public class OrganizationServiceTest { } @Test + @WithMockUser(username = "api_user") public void validCreateOrganizationTest() { - Mono organizationResponse = organizationService.create(organization); + Mono organizationResponse = organizationService.create(organization) + .switchIfEmpty(Mono.error(new Exception("create is returning empty!!"))); StepVerifier.create(organizationResponse) .assertNext(organization1 -> { assertThat(organization1.getName()).isEqualTo("Test Name"); @@ -68,6 +73,7 @@ public class OrganizationServiceTest { /* Tests for Get Organization Flow */ @Test + @WithMockUser(username = "api_user") public void getOrganizationInvalidId() { Mono organizationMono = organizationService.getById("random-id"); StepVerifier.create(organizationMono) @@ -77,6 +83,7 @@ public class OrganizationServiceTest { } @Test + @WithMockUser(username = "api_user") public void getOrganizationNullId() { Mono organizationMono = organizationService.getById(null); StepVerifier.create(organizationMono) @@ -86,6 +93,7 @@ public class OrganizationServiceTest { } @Test + @WithMockUser(username = "api_user") public void validGetOrganizationByName() { Mono createOrganization = organizationService.create(organization); Mono getOrganization = createOrganization.flatMap(t -> organizationService.getById(t.getId())); @@ -100,6 +108,7 @@ public class OrganizationServiceTest { /* Tests for Update Organization Flow */ @Test + @WithMockUser(username = "api_user") public void validUpdateOrganization() { Organization organization = new Organization(); organization.setName("Test Name");