-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
(cherry picked from commit c95a1a9) Co-authored-by: DenovVasil <vasil.denov-ext@camunda.com>
- Loading branch information
1 parent
420f579
commit 5e24136
Showing
9 changed files
with
720 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
connectors/slack/src/main/java/io/camunda/connector/slack/outbound/caller/FileUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. Licensed under a proprietary license. | ||
* See the License.txt file for more information. You may not use this file | ||
* except in compliance with the proprietary license. | ||
*/ | ||
package io.camunda.connector.slack.outbound.caller; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import com.slack.api.methods.MethodsClient; | ||
import com.slack.api.methods.SlackApiException; | ||
import com.slack.api.methods.request.files.FilesCompleteUploadExternalRequest; | ||
import com.slack.api.methods.request.files.FilesGetUploadURLExternalRequest; | ||
import com.slack.api.methods.response.files.FilesCompleteUploadExternalResponse; | ||
import com.slack.api.methods.response.files.FilesGetUploadURLExternalResponse; | ||
import com.slack.api.model.File; | ||
import com.slack.api.util.http.SlackHttpClient; | ||
import io.camunda.document.Document; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class FileUploader { | ||
|
||
public static final String GET_EXTERNAL_URL_EX = "Error during filesGetUploadURLExternal call"; | ||
public static final String EXTERNAL_URL_CALL_EX = "Error during external call: "; | ||
public static final String COMPLETE_UPLOAD_CALL_EX = | ||
"Error during filesCompleteUploadExternal call"; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(FileUploader.class); | ||
|
||
private MethodsClient methodsClient; | ||
|
||
public FileUploader(MethodsClient methodsClient) { | ||
this.methodsClient = methodsClient; | ||
} | ||
|
||
public List<File> uploadDocuments(List<Document> documents) { | ||
return documents.stream() | ||
.map( | ||
doc -> { | ||
try { | ||
return this.uploadDocument(methodsClient, doc); | ||
} catch (SlackApiException | IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}) | ||
.filter(Objects::nonNull) | ||
.collect(toList()); | ||
} | ||
|
||
private File uploadDocument(MethodsClient methodsClient, Document document) | ||
throws SlackApiException, IOException { | ||
FilesGetUploadURLExternalResponse externalURLUploadResponse = | ||
getFileUploadURL(methodsClient, document); | ||
uploadFileByURL(externalURLUploadResponse, document); | ||
return completeFileUpload(externalURLUploadResponse, document); | ||
} | ||
|
||
private FilesGetUploadURLExternalResponse getFileUploadURL( | ||
MethodsClient methodsClient, Document document) throws SlackApiException, IOException { | ||
var filesGetUploadURLExternalRequest = | ||
FilesGetUploadURLExternalRequest.builder() | ||
.filename(document.metadata().getFileName()) | ||
.length(document.asByteArray().length) | ||
.build(); | ||
return methodsClient.filesGetUploadURLExternal(filesGetUploadURLExternalRequest); | ||
} | ||
|
||
private void uploadFileByURL( | ||
FilesGetUploadURLExternalResponse uploadFileURLResp, Document document) throws IOException { | ||
if (!uploadFileURLResp.isOk()) { | ||
String msg = GET_EXTERNAL_URL_EX + "\n Errors: " + uploadFileURLResp.getError(); | ||
LOGGER.error(msg); | ||
throw new RuntimeException(msg); | ||
} | ||
var config = methodsClient.getSlackHttpClient().getConfig(); | ||
OkHttpClient okHttpClient = SlackHttpClient.buildOkHttpClient(config); | ||
|
||
var request = | ||
new Request.Builder() | ||
.url(uploadFileURLResp.getUploadUrl()) | ||
.post(RequestBody.create(document.asByteArray())) | ||
.build(); | ||
|
||
try (Response directCallResp = okHttpClient.newCall(request).execute()) { | ||
if (directCallResp.code() != 200) { | ||
String msg = EXTERNAL_URL_CALL_EX + directCallResp.message(); | ||
LOGGER.error(msg); | ||
throw new RuntimeException(msg); | ||
} | ||
} | ||
} | ||
|
||
private File completeFileUpload( | ||
FilesGetUploadURLExternalResponse uploadFileUrlResp, Document document) | ||
throws SlackApiException, IOException { | ||
FilesCompleteUploadExternalResponse completeUploadResp = | ||
methodsClient.filesCompleteUploadExternal( | ||
FilesCompleteUploadExternalRequest.builder() | ||
.files( | ||
List.of( | ||
FilesCompleteUploadExternalRequest.FileDetails.builder() | ||
.id(uploadFileUrlResp.getFileId()) | ||
.title(document.metadata().getFileName()) | ||
.build())) | ||
.build()); | ||
|
||
if (completeUploadResp.isOk()) { | ||
List<File> files = completeUploadResp.getFiles(); | ||
return getFirst(files); | ||
} else { | ||
String msg = COMPLETE_UPLOAD_CALL_EX + "\n Errors: " + completeUploadResp.getError(); | ||
LOGGER.error(msg); | ||
throw new RuntimeException(msg); | ||
} | ||
} | ||
|
||
// In fact, we always have only one file in List | ||
private File getFirst(List<File> files) { | ||
return files == null || files.isEmpty() ? null : files.getFirst(); | ||
} | ||
|
||
public void setMethodsClient(MethodsClient methodsClient) { | ||
this.methodsClient = methodsClient; | ||
} | ||
|
||
public MethodsClient getMethodsClient() { | ||
return methodsClient; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
connectors/slack/src/main/java/io/camunda/connector/slack/outbound/mapper/BlocksMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. Licensed under a proprietary license. | ||
* See the License.txt file for more information. You may not use this file | ||
* except in compliance with the proprietary license. | ||
*/ | ||
package io.camunda.connector.slack.outbound.mapper; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.slack.api.model.File; | ||
import com.slack.api.model.block.ContextBlock; | ||
import com.slack.api.model.block.FileBlock; | ||
import com.slack.api.model.block.LayoutBlock; | ||
import com.slack.api.model.block.composition.PlainTextObject; | ||
import com.slack.api.util.json.GsonFactory; | ||
import io.camunda.connector.api.error.ConnectorException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public final class BlocksMapper { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(BlocksMapper.class); | ||
public static final String REMOTE_FILE_SOURCE = "remote"; | ||
|
||
private BlocksMapper() {} | ||
|
||
public static List<LayoutBlock> mapBlocks(List<File> files, String text, JsonNode blockContent) { | ||
List<LayoutBlock> blocks = mapBlocks(text, blockContent); | ||
blocks.addAll(prepareFileBlocks(files)); | ||
return blocks; | ||
} | ||
|
||
public static List<LayoutBlock> mapBlocks(String text, JsonNode blockContent) { | ||
List<LayoutBlock> blocks = new ArrayList<>(); | ||
if (blockContent != null) { | ||
blocks = parseBlockContent(blockContent); | ||
} else { | ||
blocks.add(prepareBlockFromMessage(text)); | ||
} | ||
|
||
return blocks; | ||
} | ||
|
||
private static LayoutBlock prepareBlockFromMessage(String text) { | ||
return ContextBlock.builder() | ||
.elements(List.of(PlainTextObject.builder().text(text).build())) | ||
.build(); | ||
} | ||
|
||
private static List<LayoutBlock> prepareFileBlocks(List<File> files) { | ||
return files.stream() | ||
.map(file -> FileBlock.builder().fileId(file.getId()).source(REMOTE_FILE_SOURCE).build()) | ||
.collect(toList()); | ||
} | ||
|
||
private static List<LayoutBlock> parseBlockContent(JsonNode blockContent) { | ||
if (!blockContent.isArray()) { | ||
String msg = "Block section must be an array"; | ||
LOGGER.warn(msg); | ||
throw new ConnectorException(msg); | ||
} | ||
|
||
ArrayNode arrayNode = (ArrayNode) blockContent; | ||
List<LayoutBlock> blocks = new ArrayList<>(); | ||
for (JsonNode node : arrayNode) { | ||
blocks.add(GsonFactory.createSnakeCase().fromJson(node.toString(), LayoutBlock.class)); | ||
} | ||
return blocks; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.