Skip to content

Commit

Permalink
Implement SSE (#90)
Browse files Browse the repository at this point in the history
* Update Examples to an ENV based credential management and update Kotlin Example

* First attempt on implementing SSE, doesn't work without the socket yet.

* Second Attempt on SSE -> Still not working. Will consider CISCO SSE Client now:
https://github.com/CiscoSE/commons-networking/tree/main#sse-client

* Second Attempt on SSE -> Still not working. Will consider CISCO SSE Client now:
https://github.com/CiscoSE/commons-networking/tree/main#sse-client

* Revert Changes. Still not working.

* Added basic body of the Eventsource Runnable

* Prepare Handling Methods

* Add the first really working implementation of SSE

* Add the first really working implementation of SSE

* Satisfy linter

* Start with fixing unit test

* Return Point Mock Socket before transition to SSE

* Remove socket from MockTusAssembly

* Patch empty JSON Object errors.

* Add a first attempt on SSE testing

* Add tests for Server Sent Events

* Remove Debug Output from EventsourceRunnable

* Linting

* Linting

* Disable auto build for examples

* Remove PI

* Activate Debugging

* Remove unnecessary URI parameter

* Dont exit on error

* Add missing imports

* Adjust Timeouts in Assembly.java

Remove Debugging Output in EventsourceRunnable.java

Stop throwing on unknown events in EventsourceRunnable.java

Adjust AssemblyListener.java to reflect the new SSE functionalities

* Patch Assembly Examples in order to work with the new SSE features

* Fix Linting Errors

* Implement changes on AssemblyListener
Change the SSE test resource-file's extension

* Implement changes on AssemblyListener

* Flip assertion order to the correct orientation

* Fix some doc-strings

* Cleanup debug line

* Fix line

* Attempt on checking JSON contents

* Apply some of the suggestions from code review

Co-authored-by: Marius Kleidl <[email protected]>

* - Patch styling in Assembly.java
 - Change TusUploadRunnable.java and EventsourceRunnable.java visibility to package private

* Update test to live without the complicated map construction.
Also attempt to make the test thread safe.

* Fix examples

---------

Co-authored-by: Marius Kleidl <[email protected]>
  • Loading branch information
cdr-chakotay and Acconut authored Dec 6, 2024
1 parent 913c812 commit 6f6d41c
Show file tree
Hide file tree
Showing 23 changed files with 759 additions and 455 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ dependencies {
implementation 'org.json:json:20231013'
implementation 'commons-codec:commons-codec:1.15'
implementation 'io.socket:socket.io-client:2.1.0'
implementation 'com.launchdarkly:okhttp-eventsource:4.1.1'


testImplementation 'org.mock-server:mockserver-junit-jupiter-no-dependencies:5.15.0'
testImplementation 'org.mockito:mockito-core:4.8.0'
Expand Down
38 changes: 23 additions & 15 deletions examples/src/main/java/com/transloadit/examples/ImageResizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class ImageResizer {
* @param args
*/
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("TRANSLOADIT_KEY", "TRANSLOADIT_SECRET");
Transloadit transloadit = new Transloadit(System.getenv("TRANSLOADIT_KEY"), System.getenv("TRANSLOADIT_SECRET"));

Map<String, Object> stepOptions = new HashMap<>();
stepOptions.put("width", 75);
Expand All @@ -37,56 +37,64 @@ public static void main(String[] args) {
assembly.setAssemblyListener(new AssemblyListener() {
@Override
public void onAssemblyFinished(AssemblyResponse response) {
JSONArray result = response.getStepResult("resize");
System.out.println("Resize result:");
for (int i = 0; i < result.length(); i++) {
JSONObject item = result.getJSONObject(i);
System.out.println(String.format("%s.%s: %s",
item.getString("basename"), item.getString("ext"), item.getString("ssl_url")));
}
System.out.println("Assembly finished");
System.out.println("Result: " + response.getSslUrl());
}

@Override
public void onError(Exception error) {
System.out.println("Error");
error.printStackTrace();
}

@Override
public void onMetadataExtracted() {
System.out.println("Metadata Extracted");
System.out.println("Metadata extracted");
}

@Override
public void onAssemblyUploadFinished() {
System.out.println("Assembly Upload complete, Executing ...");

System.out.println("Assembly upload complete. Executing...");
}

@Override
public void onFileUploadFinished(String fileName, JSONObject uploadInformation) {
public void onFileUploadFinished(JSONObject uploadInformation) {
String fileName = uploadInformation.getString("name");
System.out.println("File uploaded: " + fileName);
}

@Override
public void onFileUploadPaused(String name) {

System.out.println("File upload paused: " + name);
}

@Override
public void onFileUploadResumed(String name) {
System.out.println("File upload resumed: " + name);

}

@Override
public void onFileUploadProgress(long uploadedBytes, long totalBytes) {
System.out.println("Uploaded " + uploadedBytes + "/" + totalBytes + " bytes");

}

@Override
public void onAssemblyResultFinished(String stepName, JSONObject result) {

public void onAssemblyProgress(JSONObject progressPerOriginalFile) {
double combinedProgress = progressPerOriginalFile.getDouble("bytes_processed")
/ progressPerOriginalFile.getDouble("bytes_expected");
System.out.println("Assembly Execution Progress: " + combinedProgress + "%");
}

@Override
public void onAssemblyResultFinished(JSONArray result) {
String stepName = result.getString(0);
JSONObject resultData = result.getJSONObject(1);
System.out.println("\n ---- Step Result for Step: ---- ");
System.out.println("Step name: " + stepName + "\nFile: " + resultData.get("name"));
System.out.println("Download link: " + resultData.getString("ssl_url") + "\n");
}
});

try {
Expand Down
29 changes: 17 additions & 12 deletions examples/src/main/java/com/transloadit/examples/KotlinExample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class KotlinExample {
@JvmStatic
fun main(args: Array<String>) {
// Setup the TransloaditClient and a new Assembly
val transloadit = Transloadit("TRANSLOADIT_KEY", "TRANSLOADIT_SECRET")
val transloadit = Transloadit(System.getenv("TRANSLOADIT_KEY"), System.getenv("TRANSLOADIT_SECRET"))

val assembly: Assembly = transloadit.newAssembly()
val kotlinExample = KotlinExample()
Expand All @@ -36,7 +36,7 @@ class KotlinExample {
JSONObject()
.put("text","Kotlin Example")
.put("color","#e61010")
.put("font","Helvetica")
.put("font","Ubuntu-Mono-Bold")
.put("size", 15)
)

Expand All @@ -47,7 +47,8 @@ class KotlinExample {
// Add Assembly Listener to receive notifications.
assembly.assemblyListener = object : AssemblyListener {
override fun onAssemblyFinished(response: AssemblyResponse?) {
println("Download Results")
println("--- Assembly finished ---")
println("You can download the following result files:")
if (response != null) {
val resultJson: JSONArray = response.getStepResult("convert_and_text");
for ( i in 0 until resultJson.length()){
Expand All @@ -73,27 +74,31 @@ class KotlinExample {
println("Assembly uploaded, executing ...")
}

override fun onFileUploadFinished(fileName: String?, uploadInformation: JSONObject?) {
println("File " + fileName + "has been uploaded successfully")
override fun onFileUploadFinished(uploadInformation: JSONObject?) {
val fileName = uploadInformation!!.getString("name")
println("File $fileName has been uploaded successfully")
}

override fun onFileUploadPaused(name: String?) {
println("Upload" + name + "has been paused")
println("Upload $name has been paused")
}

override fun onFileUploadResumed(name: String?) {
println("Upload" + name + "has been resumed")
println("Upload $name has been resumed")
}

override fun onFileUploadProgress(uploadedBytes: Long, totalBytes: Long) {
println("Uploaded $uploadedBytes from $totalBytes bytes")
}

override fun onAssemblyResultFinished(stepName: String?, result: JSONObject?) {
println("Step Result available: ")
println(
"""StepName: $stepName File: ${result!!.getString("basename")}.${result.getString("ext")}
""".trimIndent())
override fun onAssemblyProgress(progress: JSONObject?) {
println("Assembly Progress: $progress")
}


override fun onAssemblyResultFinished(result: JSONArray) {
val stepName = result.getString(0)
println("Step - $stepName - has a result")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class MultiStepProcessing {
*/
public static void main(String[] args) throws FileNotFoundException {
// New Transloadit Instance
Transloadit transloadit = new Transloadit("TRANSLOADIT_KEY", "TRANSLOADIT_SECRET");
Transloadit transloadit = new Transloadit(System.getenv("TRANSLOADIT_KEY"), System.getenv("TRANSLOADIT_SECRET"));
Assembly assembly = transloadit.newAssembly();

// Add Files and define Field name
Expand Down Expand Up @@ -87,14 +87,6 @@ public static void main(String[] args) throws FileNotFoundException {
public void onAssemblyFinished(AssemblyResponse response) {
System.out.println("Assembly finished");
}
public void printStatus(JSONArray status) {
for (int i = 0; i < status.length(); i++) {
JSONObject obj = status.getJSONObject(i);
System.out.printf(
"Resulting file: %s %s, Download at:\n %s%n",
obj.getString("basename"), obj.getString("ext"), obj.getString("ssl_url"));
}
}

@Override
public void onError(Exception error) {
Expand All @@ -113,7 +105,8 @@ public void onAssemblyUploadFinished() {
}

@Override
public void onFileUploadFinished(String fileName, JSONObject uploadInformation) {
public void onFileUploadFinished(JSONObject uploadInformation) {
String fileName = uploadInformation.getString("name");
System.out.println("File uploaded: " + fileName);
}

Expand All @@ -135,11 +128,19 @@ public void onFileUploadProgress(long uploadedBytes, long totalBytes) {
}

@Override
public void onAssemblyResultFinished(String stepName, JSONObject result) {
public void onAssemblyProgress(JSONObject progressPerOriginalFile) {
double combinedProgress = progressPerOriginalFile.getDouble("bytes_processed")
/ progressPerOriginalFile.getDouble("bytes_expected");
System.out.println("Assembly Execution Progress: " + combinedProgress + "%");
}

@Override
public void onAssemblyResultFinished(JSONArray result) {
String stepName = result.getString(0);
JSONObject resultData = result.getJSONObject(1);
System.out.println("\n ---- Step Result for Step: ---- ");
System.out.println("StepName: " + stepName + "\nFile: " + result.get("basename") + "."
+ result.get("ext"));
System.out.println("Download link: " + result.getString("ssl_url") + "\n");
System.out.println("Step name: " + stepName + "\nFile: " + resultData.get("name"));
System.out.println("Download link: " + resultData.getString("ssl_url") + "\n");
}
});
try {
Expand All @@ -154,7 +155,6 @@ public void onAssemblyResultFinished(String stepName, JSONObject result) {
Thread.sleep(3000);
assembly.resumeUploads();

System.out.println("Assembly ID: " + assembly.getClientSideGeneratedAssemblyID());
} catch (LocalOperationException | RequestException | InterruptedException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class OverrideTemplate {
* @param args
*/
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("TRANSLOADIT_KEY", "TRANSLOADIT_SECRET");
Transloadit transloadit = new Transloadit(System.getenv("TRANSLOADIT_KEY"), System.getenv("TRANSLOADIT_SECRET"));

Map<String, Object> stepOptions = new HashMap<>();
stepOptions.put("url", "https://transloadit.com/");
Expand All @@ -49,56 +49,65 @@ public static void main(String[] args) {
assembly.setAssemblyListener(new AssemblyListener() {
@Override
public void onAssemblyFinished(AssemblyResponse response) {
JSONArray result = response.getStepResult("screenshot");
System.out.println("Resize result:");
for (int i = 0; i < result.length(); i++) {
JSONObject item = result.getJSONObject(i);
System.out.println(String.format("%s.%s: %s",
item.getString("basename"), item.getString("ext"), item.getString("ssl_url")));
}
System.out.println("Assembly finished");
System.out.println("Result: " + response.json().getJSONObject("results")
.getJSONArray("screenshot").getJSONObject(0).getString("ssl_url"));
}

@Override
public void onError(Exception error) {
System.out.println("Error");
error.printStackTrace();
}

@Override
public void onMetadataExtracted() {
System.out.println("Metadata Extracted");
System.out.println("Metadata extracted");
}

@Override
public void onAssemblyUploadFinished() {
System.out.println("Assembly Upload complete, Executing ...");

System.out.println("Assembly upload complete. Executing...");
}

@Override
public void onFileUploadFinished(String fileName, JSONObject uploadInformation) {
public void onFileUploadFinished(JSONObject uploadInformation) {
String fileName = uploadInformation.getString("name");
System.out.println("File uploaded: " + fileName);
}

@Override
public void onFileUploadPaused(String name) {

System.out.println("File upload paused: " + name);
}

@Override
public void onFileUploadResumed(String name) {
System.out.println("File upload resumed: " + name);

}

@Override
public void onFileUploadProgress(long uploadedBytes, long totalBytes) {
System.out.println("Uploaded " + uploadedBytes + "/" + totalBytes + " bytes");

}

@Override
public void onAssemblyResultFinished(String stepName, JSONObject result) {

public void onAssemblyProgress(JSONObject progressPerOriginalFile) {
double combinedProgress = progressPerOriginalFile.getDouble("bytes_processed")
/ progressPerOriginalFile.getDouble("bytes_expected");
System.out.println("Assembly Execution Progress: " + combinedProgress + "%");
}

@Override
public void onAssemblyResultFinished(JSONArray result) {
String stepName = result.getString(0);
JSONObject resultData = result.getJSONObject(1);
System.out.println("\n ---- Step Result for Step: ---- ");
System.out.println("Step name: " + stepName + "\nFile: " + resultData.get("name") + ".");
System.out.println("Download link: " + resultData.getString("ssl_url") + "\n");
}
});

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class TemplateExample {
* @param args
*/
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("TRANSLOADIT_KEY", "TRANSLOADIT_SECRET");
Transloadit transloadit = new Transloadit(System.getenv("TRANSLOADIT_KEY"), System.getenv("TRANSLOADIT_SECRET"));

Assembly assembly = transloadit.newAssembly();
// set template id
Expand All @@ -34,9 +34,16 @@ public static void main(String[] args) {
System.out.println("waiting for assembly to finish ...");
while (!response.isFinished()) {
response = transloadit.getAssemblyByUrl(response.getSslUrl());

// wait for 500ms before checking again in order to avoid hitting the rate limit
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

String resultUrl = response.getStepResult("resize").getJSONObject(0).getString("ssl_url");
String resultUrl = response.getSslUrl();
System.out.println("Here's your assembly result: " + resultUrl);

} catch (RequestException | LocalOperationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class Watermarker {
* @param args
*/
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("TRANSLOADIT_KEY", "TRANSLOADIT_SECRET");
Transloadit transloadit = new Transloadit(System.getenv("TRANSLOADIT_KEY"), System.getenv("TRANSLOADIT_SECRET"));

Map<String, Object> stepOptions = new HashMap<>();
stepOptions.put("use", ":original");
Expand All @@ -43,6 +43,13 @@ public static void main(String[] args) {
System.out.println("waiting for assembly to finish ...");
while (!response.isFinished()) {
response = transloadit.getAssemblyByUrl(response.getSslUrl());

// wait for 500ms before checking again in order to avoid hitting the rate limit
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

String resultUrl = response.getStepResult("encode").getJSONObject(0).getString("ssl_url");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class AsyncExample {
* @param args
*/
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("TRANSLOADIT_KEY", "TRANSLOADIT_SECRET");
Transloadit transloadit = new Transloadit(System.getenv("TRANSLOADIT_KEY"), System.getenv("TRANSLOADIT_SECRET"));

Map<String, Object> stepOptions = new HashMap<String, Object>();
stepOptions.put("width", 75);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class AsyncPausePlayExample {
* @param args
*/
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("TRANSLOADIT_KEY", "TRANSLOADIT_SECRET");
Transloadit transloadit = new Transloadit(System.getenv("TRANSLOADIT_KEY"), System.getenv("TRANSLOADIT_SECRET"));

Map<String, Object> stepOptions = new HashMap<String, Object>();
stepOptions.put("width", 75);
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Uncomment following line if you want to use the local java-sdk
// for the example instead of pulling the JARs from JCenter.
// This is useful for debugging and testing new features.
//include ':examples'
// include ':examples'
rootProject.name = 'transloadit'
Loading

0 comments on commit 6f6d41c

Please sign in to comment.