-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Transformer and related classes. no-ticket
- Loading branch information
1 parent
222f1be
commit 1b2f45d
Showing
13 changed files
with
1,237 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,5 @@ out/ | |
*.code-workspace | ||
# Local History for Visual Studio Code | ||
.history/ | ||
.vscode/settings.json | ||
|
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
132 changes: 132 additions & 0 deletions
132
src/main/java/to/wetransform/hale/transformer/ResultCallbackHelper.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,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
28
src/main/java/to/wetransform/hale/transformer/RunContext.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,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(); | ||
} | ||
} |
Oops, something went wrong.