Skip to content

Commit

Permalink
Merge pull request #27 from camunda/20-remove-unescape-workaround
Browse files Browse the repository at this point in the history
feat: remove unescape workaround
  • Loading branch information
Oleksiivanov authored Aug 10, 2022
2 parents 493d8bf + 55257ba commit b66a78e
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 127 deletions.
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mvn clean package
## API

### Create folder

### Input

```json
Expand All @@ -20,12 +21,15 @@ mvn clean package
"type": "folder",
"name": "MyNewFolder",
"parent": "secrets.PARENT_ID",
"additionalGoogleDriveProperties": "{\\\"description\\\":\\\" description\\\"}"
"additionalGoogleDriveProperties": {
"description": " description"
}
}
}
```

### Create file

### Input

```json
Expand All @@ -35,10 +39,24 @@ mvn clean package
"type": "folder",
"name": "MyNewFolder",
"parent": "secrets.PARENT_ID",
"additionalGoogleDriveProperties": "{\\\"description\\\":\\\" description\\\"}",
"additionalGoogleDriveProperties": {
"description": " description"
},
"template": {
"id": "myTemplateId",
"variables": "[{\\\"replaceAllText\\\":{\"containsText\\\":{\\\"matchCase\\\":true,\\\"text\\\":\\\"replaceFrom\\\"},\\\"replaceText\\\":\\\"replaceTo\\\"}}]"
"variables": {
"requests": [
{
"replaceAllText": {
"containsText": {
"text": "replaceFrom",
"matchCase": "true"
},
"replaceText": "replaceTo"
}
}
]
}
}
}
}
Expand Down Expand Up @@ -73,15 +91,18 @@ mvn function:run -Pcloud-function

The function will be available at http://localhost:9082.

Have a look at the [Camunda Cloud Connector Run-Time](https://github.com/camunda/connector-runtime-cloud) to see how your Connector function is wrapped as a Google Cloud Function.
Have a look at the [Camunda Cloud Connector Run-Time](https://github.com/camunda/connector-runtime-cloud) to see how
your Connector function is wrapped as a Google Cloud Function.

### Test as local Job Worker

Use the [Camunda Job Worker Connector Run-Time](https://github.com/camunda/connector-framework/tree/main/runtime-job-worker) to run your function as a local Job Worker.
Use the [Camunda Job Worker Connector Run-Time](https://github.com/camunda/connector-framework/tree/main/runtime-job-worker)
to run your function as a local Job Worker.

## Element Template

The element templates can be found in the [element-templates/template-connector.json](element-templates/google-drive-connector.json) file.
The element templates can be found in
the [element-templates/template-connector.json](element-templates/google-drive-connector.json) file.

## Build a release

Expand Down
8 changes: 0 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
<version.google-oauth-client-jetty>1.34.1</version.google-oauth-client-jetty>
<version.google-auth-library-oauth2-http>1.8.1</version.google-auth-library-oauth2-http>
<version.google-api-services-docs>v1-rev61-1.25.0</version.google-api-services-docs>
<version.org.apache.commons>1.9</version.org.apache.commons>

</properties>

<dependencies>
Expand All @@ -37,12 +35,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- This dependency will be removed after camunda/zeebe#9859 is resolved. -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${version.org.apache.commons}</version>
</dependency>
<!-- GOOGLE DRIVE API-->
<dependency>
<groupId>com.google.api-client</groupId>
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/io/camunda/connector/gdrive/GoogleDriveFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,50 @@
package io.camunda.connector.gdrive;

import com.google.api.client.json.JsonFactory;
import com.google.gson.Gson;
import com.google.api.client.json.JsonParser;
import io.camunda.connector.api.ConnectorContext;
import io.camunda.connector.api.ConnectorFunction;
import io.camunda.connector.gdrive.model.GoogleDriveResult;
import io.camunda.connector.gdrive.model.request.GoogleDriveRequest;
import io.camunda.connector.gdrive.supliers.GoogleServicesSupplier;
import io.camunda.connector.gdrive.supliers.GsonComponentSupplier;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GoogleDriveFunction implements ConnectorFunction {
private static final Logger LOGGER = LoggerFactory.getLogger(GoogleDriveFunction.class);

private final GoogleDriveService service;
private final Gson gson;
private final JsonFactory jsonFactory;

public GoogleDriveFunction() {
this(
new GoogleDriveService(),
GsonComponentSupplier.getGson(),
GsonComponentSupplier.getJsonFactory());
this(new GoogleDriveService(), GsonComponentSupplier.getJsonFactory());
}

public GoogleDriveFunction(
final GoogleDriveService service, final Gson gson, final JsonFactory jsonFactory) {
public GoogleDriveFunction(final GoogleDriveService service, final JsonFactory jsonFactory) {
this.service = service;
this.gson = gson;
this.jsonFactory = jsonFactory;
}

@Override
public Object execute(final ConnectorContext context) {
var requestAsJson = context.getVariables();
final var request = gson.fromJson(requestAsJson, GoogleDriveRequest.class);
final GoogleDriveRequest request = parseVariablesToRequest(context.getVariables());
context.validate(request);
context.replaceSecrets(request);
LOGGER.debug("Request verified successfully and all required secrets replaced");
return executeConnector(request);
}

private GoogleDriveRequest parseVariablesToRequest(final String requestAsJson) {
try {
JsonParser jsonParser = jsonFactory.createJsonParser(requestAsJson);
return jsonParser.parseAndClose(GoogleDriveRequest.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private GoogleDriveResult executeConnector(final GoogleDriveRequest request) {
LOGGER.debug("Executing my connector with request {}", request);
GoogleDriveClient drive =
Expand Down
41 changes: 7 additions & 34 deletions src/main/java/io/camunda/connector/gdrive/GoogleDriveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,24 @@

package io.camunda.connector.gdrive;

import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonParser;
import com.google.api.services.docs.v1.model.BatchUpdateDocumentResponse;
import com.google.api.services.docs.v1.model.Request;
import com.google.api.services.drive.model.File;
import com.google.gson.Gson;
import io.camunda.connector.gdrive.model.GoogleDriveResult;
import io.camunda.connector.gdrive.model.MimeTypeUrl;
import io.camunda.connector.gdrive.model.request.Resource;
import io.camunda.connector.gdrive.model.request.Template;
import io.camunda.connector.gdrive.model.request.Type;
import io.camunda.connector.gdrive.supliers.GsonComponentSupplier;
import java.io.IOException;
import io.camunda.connector.gdrive.model.request.Variables;
import java.util.List;
import java.util.Optional;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GoogleDriveService {
private static final Logger LOGGER = LoggerFactory.getLogger(GoogleDriveService.class);

private final Gson gson;
private final JsonFactory jsonFactory;

public GoogleDriveService() {
this(GsonComponentSupplier.getGson(), GsonComponentSupplier.getJsonFactory());
}

public GoogleDriveService(final Gson gson, final JsonFactory jsonFactory) {
this.gson = gson;
this.jsonFactory = jsonFactory;
}
public GoogleDriveService() {}

public GoogleDriveResult execute(final GoogleDriveClient client, final Resource resource) {
switch (resource.getType()) {
Expand All @@ -73,7 +58,6 @@ private GoogleDriveResult createFolder(final GoogleDriveClient client, final Res
}

private GoogleDriveResult createFile(final GoogleDriveClient client, final Resource resource) {
final Optional<List<Request>> requestsForFile = getRequestsForFile(resource);
final File metaData = createMetaDataFile(resource);
File result;
if (resource.getTemplate() != null) {
Expand All @@ -82,7 +66,11 @@ private GoogleDriveResult createFile(final GoogleDriveClient client, final Resou
"File successfully created by template, file name [{}], templateId [{}]",
result.getId(),
resource.getName());
requestsForFile.ifPresent(requests -> updateWithRequests(client, requests, result));
Optional.ofNullable(resource)
.map(Resource::getTemplate)
.map(Template::getVariables)
.map(Variables::getRequests)
.ifPresent(requests -> updateWithRequests(client, requests, result));
} else {
result = client.createWithMetadata(metaData);
}
Expand All @@ -92,27 +80,12 @@ private GoogleDriveResult createFile(final GoogleDriveClient client, final Resou

private File createMetaDataFile(final Resource resource) {
return Optional.ofNullable(resource.getAdditionalGoogleDriveProperties())
.map(prop -> gson.fromJson(StringEscapeUtils.unescapeJson(prop), File.class))
.orElseGet(File::new)
.setName(resource.getName())
.setMimeType(resource.getType() == Type.FOLDER ? MimeTypeUrl.FOLDER.getMimeType() : null)
.setParents(resource.getParent() != null ? List.of(resource.getParent()) : null);
}

private Optional<List<Request>> getRequestsForFile(Resource resource) {
return Optional.ofNullable(resource.getTemplate())
.map(Template::getVariables)
.map(StringEscapeUtils::unescapeJson)
.map(
variables -> {
try (JsonParser jsonParser = jsonFactory.createJsonParser(variables)) {
return List.copyOf(jsonParser.parseArrayAndClose(List.class, Request.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

private void updateWithRequests(
final GoogleDriveClient client, final List<Request> requests, final File metaData) {
if (MimeTypeUrl.DOCUMENT.getMimeType().equals(metaData.getMimeType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

package io.camunda.connector.gdrive.model.request;

import com.google.api.client.util.Key;
import io.camunda.connector.api.ConnectorInput;
import io.camunda.connector.api.SecretStore;
import io.camunda.connector.api.Validator;
import java.util.Objects;

public class GoogleDriveRequest implements ConnectorInput {

private String token;
private Resource resource;
@Key private String token;
@Key private Resource resource;

@Override
public void validateWith(final Validator validator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@

package io.camunda.connector.gdrive.model.request;

import com.google.api.client.util.Key;
import com.google.api.services.drive.model.File;
import io.camunda.connector.api.ConnectorInput;
import io.camunda.connector.api.SecretStore;
import io.camunda.connector.api.Validator;
import java.util.Objects;

public class Resource implements ConnectorInput {

private Type type;
private String name;
private String parent;
private String additionalGoogleDriveProperties;
private Template template;
@Key private Type type;
@Key private String name;
@Key private String parent;
@Key private File additionalGoogleDriveProperties;
@Key private Template template;

@Override
public void validateWith(final Validator validator) {
Expand Down Expand Up @@ -67,11 +68,11 @@ public void setParent(final String parent) {
this.parent = parent;
}

public String getAdditionalGoogleDriveProperties() {
public File getAdditionalGoogleDriveProperties() {
return additionalGoogleDriveProperties;
}

public void setAdditionalGoogleDriveProperties(final String additionalGoogleDriveProperties) {
public void setAdditionalGoogleDriveProperties(final File additionalGoogleDriveProperties) {
this.additionalGoogleDriveProperties = additionalGoogleDriveProperties;
}

Expand Down Expand Up @@ -115,9 +116,8 @@ public String toString() {
+ ", parent='"
+ parent
+ "'"
+ ", additionalGoogleDriveProperties='"
+ ", additionalGoogleDriveProperties="
+ additionalGoogleDriveProperties
+ "'"
+ ", template="
+ template
+ "}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

package io.camunda.connector.gdrive.model.request;

import com.google.api.client.util.Key;
import io.camunda.connector.api.ConnectorInput;
import io.camunda.connector.api.SecretStore;
import io.camunda.connector.api.Validator;
import java.util.Objects;

public class Template implements ConnectorInput {
private String id;
private String variables;
@Key private String id;
@Key private Variables variables;

@Override
public void validateWith(final Validator validator) {
Expand All @@ -44,11 +45,11 @@ public void setId(final String id) {
this.id = id;
}

public String getVariables() {
public Variables getVariables() {
return variables;
}

public void setVariables(final String variables) {
public void setVariables(final Variables variables) {
this.variables = variables;
}

Expand All @@ -71,6 +72,6 @@ public int hashCode() {

@Override
public String toString() {
return "Template{" + "id='" + id + "'" + ", variables='" + variables + "'" + "}";
return "Template{" + "id='" + id + "'" + ", variables=" + variables + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

package io.camunda.connector.gdrive.model.request;

import com.google.gson.annotations.SerializedName;
import com.google.api.client.util.Value;

public enum Type {
@SerializedName("folder")
@Value("folder")
FOLDER,
@SerializedName("file")
@Value("file")
FILE
}
Loading

0 comments on commit b66a78e

Please sign in to comment.