Skip to content

Commit

Permalink
feat: add Transformer
Browse files Browse the repository at this point in the history
Add Transformer and related classes.

no-ticket
  • Loading branch information
emanuelaepure10 committed Dec 18, 2023
1 parent 222f1be commit 1b2f45d
Show file tree
Hide file tree
Showing 13 changed files with 1,237 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ out/
*.code-workspace
# Local History for Visual Studio Code
.history/
.vscode/settings.json

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
import java.util.Map;

import eu.esdihumboldt.hale.common.core.io.Value;
import org.json.JSONObject;

public record CustomTarget(String providerId, Map<String, Value> settings) {

public CustomTarget(String providerId) {
this(providerId, new HashMap<>());
}

public void setProviderId(String string) {}

public JSONObject getSettings() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package to.wetransform.hale.transformer;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.zip.GZIPOutputStream;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Helper to perform transformation result callbacks.
*/
public class ResultCallbackHelper {

/**
* The logger instance for this class.
*/
private static Logger logger = LoggerFactory.getLogger(ResultCallbackHelper.class);

/**
* Perform the transformation result callback.
*
* @param success Whether or not the transformation has been successful.
* @param logs The transformation log.
* @param callbackUrl The callback url.
* @param token The jwt token for the request.
* @param jobId The transformation job id.
*/
public static void performResultCallback(
boolean success,
String logs,
Optional<JSONObject> stats,
String callbackUrl,
String token,
Optional<String> jobId) {
try {
// Create transformation result object.
JSONObject resultObj = new JSONObject();
resultObj.put("success", success);

JSONObject propertiesObj = new JSONObject();
if (Optional.ofNullable(logs).isPresent()) {
propertiesObj.put("logs", logs);
}

if (jobId.isPresent()) {
propertiesObj.put("jobId", jobId.get());
} else {
logger.warn("Transformation result callback does not contain a jobId.");
}

if (stats.isPresent()) {
propertiesObj.put("stats", stats.get());
}

resultObj.put("properties", propertiesObj);

// Send result object to callback URL.
sendTransformationResult(resultObj, callbackUrl, token);

} catch (JSONException e) {
logger.warn("Failed to create result object to be sent ");
}
}

/**
* Send the transformation result object to the given url.
*
* @param result The transformation result object.
* @param url The callback url.
* @param token The jwt token.
*/
private static void sendTransformationResult(JSONObject result, String url, String token) {

try {
logger.info("Sending transformation result to " + url.toString());

// Create http request to send result object.
URL sendUrl = new URL(url);
HttpURLConnection http = (HttpURLConnection) sendUrl.openConnection();
if (token != null) {
http.setRequestProperty("Authorization", "Bearer " + token);
} else {
logger.warn("Transformation callback is being performed without JWT token!");
}
http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
http.setRequestProperty("Content-Encoding", "gzip");
http.setDoOutput(true);
http.setDoInput(true);
http.setRequestMethod("PUT");
http.setConnectTimeout(10000);

// write zipped content
OutputStream os = http.getOutputStream();
OutputStream gzip = new GZIPOutputStream(os);

try {
OutputStreamWriter osw = new OutputStreamWriter(gzip, StandardCharsets.UTF_8);
osw.write(result.toString());
osw.flush();
osw.close();
} catch (IOException e) {
logger.warn("Failed to write result callback request body.", e);
} finally {
os.close();
gzip.close();
}

// handle http response
int statusCode = http.getResponseCode();
String statusMessage = http.getResponseMessage();
String resLogMsg = "Transformation result callback endpoint responded with status code " + statusCode + ": "
+ statusMessage;
if (statusCode != 200) {
logger.warn(resLogMsg);
} else {
logger.info(resLogMsg);
}

http.disconnect();
} catch (Throwable t) {
logger.warn("Failed to perform transformation result callback.", t);
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/to/wetransform/hale/transformer/RunContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package to.wetransform.hale.transformer;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class RunContext {

private final List<Path> tempFiles = new ArrayList<>();

public File createTempDir() throws IOException {
Path path = Files.createTempDirectory("hale-progen");
tempFiles.add(path);
return path.toFile();
}

public void cleanUp() throws IOException {
for (Path path : tempFiles) {
FileUtils.deleteDirectory(path.toFile());
}
tempFiles.clear();
}
}
Loading

0 comments on commit 1b2f45d

Please sign in to comment.