From 609045747bed67aa3bbeb27394421505a75bb11c Mon Sep 17 00:00:00 2001 From: Trisha Anand Date: Tue, 25 Feb 2020 08:54:16 +0000 Subject: [PATCH] Creating an embedded datasource for an action. Also setting defaults for an action when created via a curl command. --- .../com/appsmith/server/domains/Action.java | 20 ------ .../server/services/ActionServiceImpl.java | 66 ++++++------------- .../server/services/CurlImporterService.java | 14 +++- .../DatasourceContextServiceImpl.java | 2 +- .../services/DatasourceServiceImpl.java | 2 +- 5 files changed, 33 insertions(+), 71 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Action.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Action.java index e1bb2e6fac..115e8ef620 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Action.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/domains/Action.java @@ -2,7 +2,6 @@ package com.appsmith.server.domains; import com.appsmith.external.models.ActionConfiguration; import com.appsmith.external.models.BaseDomain; -import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Getter; import lombok.NoArgsConstructor; @@ -27,9 +26,6 @@ public class Action extends BaseDomain { @Transient Datasource datasource; - @JsonIgnore - String datasourceId; - String organizationId; String pageId; @@ -55,20 +51,4 @@ public class Action extends BaseDomain { String cacheResponse; String templateId; //If action is created via a template, store the id here. - - public Datasource getDatasource() { - if (this.datasource != null) { - //The action object has been created from JSON. - return this.datasource; - } - - //If the action object has been fetched from the db, it would not have datasource. Create and return one. - if (this.getDatasourceId() != null) { - Datasource datasource = new Datasource(); - datasource.setId(this.datasourceId); - return datasource; - } - - return null; - } } 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 53c15c1549..a473da8466 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 @@ -112,15 +112,12 @@ public class ActionServiceImpl extends BaseService replaceOrCreateNewDataSourceMono = replaceOrCreateNewDataSource(action); Mono dbActionMono = repository.findById(id) .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "action", id))); - return Mono.zip(replaceOrCreateNewDataSourceMono, dbActionMono) - .map(tuple -> { - Action updatedActionWithDatasource = tuple.getT1(); - Action dbAction = tuple.getT2(); - copyNewFieldValuesIntoOldObject(updatedActionWithDatasource, dbAction); + return dbActionMono + .map(dbAction -> { + copyNewFieldValuesIntoOldObject(action, dbAction); return dbAction; }) .flatMap(this::validateAndSaveActionToRepository) @@ -133,33 +130,6 @@ public class ActionServiceImpl extends BaseService replaceOrCreateNewDataSource(Action action) { - Datasource datasource = action.getDatasource(); - if (datasource != null) { - //Update action contains a change for datasource - if (datasource.getId() != null) { - //Datasource changed to another existing data source. Confirm and return. - return datasourceService.findById(datasource.getId()) - .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "datasource", datasource.getId()))) - .map(datasource1 -> { - action.setDatasource(datasource1); - action.setDatasourceId(datasource1.getId()); - return action; - }); - } - - //New datasource needs to be created here. - return datasourceService.create(datasource) - .map(datasource1 -> { - action.setDatasource(datasource1); - action.setDatasourceId(datasource1.getId()); - return action; - }); - } - //No changes in the datasource. - return Mono.just(action); - } - private Boolean validateActionName(String name) { boolean isValidName = SourceVersion.isName(name); String pattern = "^((?=[A-Za-z0-9_])(?![\\\\-]).)*$"; @@ -220,17 +190,22 @@ public class ActionServiceImpl extends BaseService { action.setIsValid(false); - invalids.add(AppsmithError.NO_RESOURCE_FOUND.getMessage(FieldName.DATASOURCE, action.getDatasourceId())); + invalids.add(AppsmithError.NO_RESOURCE_FOUND.getMessage(FieldName.DATASOURCE, action.getDatasource().getId())); return Mono.just(action.getDatasource()); })); } - Mono pluginMono = datasourceMono.flatMap(datasource -> pluginService.findById(datasource.getPluginId()) - .switchIfEmpty(Mono.defer(() -> { - action.setIsValid(false); - invalids.add(AppsmithError.NO_RESOURCE_FOUND.getMessage(FieldName.PLUGIN, datasource.getPluginId())); - return Mono.just(new Plugin()); - }))); + Mono pluginMono = datasourceMono.flatMap(datasource -> { + if (datasource.getPluginId() == null) { + return Mono.error(new AppsmithException(AppsmithError.PLUGIN_ID_NOT_GIVEN)); + } + return pluginService.findById(datasource.getPluginId()) + .switchIfEmpty(Mono.defer(() -> { + action.setIsValid(false); + invalids.add(AppsmithError.NO_RESOURCE_FOUND.getMessage(FieldName.PLUGIN, datasource.getPluginId())); + return Mono.just(new Plugin()); + })); + }); return pluginMono .zipWith(datasourceMono) @@ -239,7 +214,6 @@ public class ActionServiceImpl extends BaseService pluginMono = datasourceMono .flatMap(datasource -> { diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CurlImporterService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CurlImporterService.java index 33f2dcb5de..ac6ecdc991 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CurlImporterService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/CurlImporterService.java @@ -47,6 +47,10 @@ public class CurlImporterService extends BaseApiImporter { Datasource datasource = new Datasource(); DatasourceConfiguration datasourceConfiguration = new DatasourceConfiguration(); + //Set defaults + actionConfiguration.setHttpMethod(HttpMethod.GET); + + // Matches : "-H 'headerKey:headerValue' Pattern headerPattern = Pattern.compile(headerRegex); Matcher headerMatcher = headerPattern.matcher(command); @@ -103,6 +107,10 @@ public class CurlImporterService extends BaseApiImporter { try { // If the string doesnt throw an exception when being converted to a URI, its a valid URL. URI uri = new URL(cmdSplit[i]).toURI(); + URL url = new URL(cmdSplit[i]); + String path = url.getFile().substring(0, url.getFile().lastIndexOf('/')); + String base = url.getProtocol() + "://" + url.getHost(); + log.debug("url is : {}, \npath is : {} & \nbase is : {}", url.getProtocol() + "://" + url.getHost() + path, path, base); // If it reaches here, we have successfully found a valid URL. urlFound = true; //Extract query params @@ -118,8 +126,10 @@ public class CurlImporterService extends BaseApiImporter { queryParameters.add(queryParam); } actionConfiguration.setQueryParameters(queryParameters); - //Set the URL without the query params - datasourceConfiguration.setUrl(cmdSplit[i].split("\\?")[0]); + //Set the URL without the query params & the path + datasourceConfiguration.setUrl(base); + //Set the path in actionConfiguration + actionConfiguration.setPath(path); } catch (Exception e) { //Not a valid URL. Continue to the next word in the CURL command } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/DatasourceContextServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/DatasourceContextServiceImpl.java index 70f8948825..c0459dc584 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/DatasourceContextServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/DatasourceContextServiceImpl.java @@ -37,7 +37,7 @@ public class DatasourceContextServiceImpl implements DatasourceContextService { public Mono getDatasourceContext(Datasource datasource) { String datasourceId = datasource.getId(); if (datasourceId == null) { - log.debug("This is a dry run"); + log.debug("This is a dry run or an embedded datasource. The datasource context would not exist in this scenario"); } else if (datasourceContextMap.get(datasourceId) != null) { log.debug("resource context exists. Returning the same."); return Mono.just(datasourceContextMap.get(datasourceId)); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/DatasourceServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/DatasourceServiceImpl.java index cdb0a64b6f..61e4bf183f 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/DatasourceServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/DatasourceServiceImpl.java @@ -99,7 +99,7 @@ public class DatasourceServiceImpl extends BaseService organizationMono = userMono