|
15 | 15 | */ |
16 | 16 | package io.serverlessworkflow.api.serializers; |
17 | 17 |
|
| 18 | +import java.io.IOException; |
| 19 | +import java.security.MessageDigest; |
| 20 | +import java.util.List; |
| 21 | +import java.util.UUID; |
| 22 | + |
18 | 23 | import com.fasterxml.jackson.core.JsonGenerator; |
19 | 24 | import com.fasterxml.jackson.databind.SerializerProvider; |
20 | 25 | import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
21 | 26 | import io.serverlessworkflow.api.Workflow; |
22 | | -import io.serverlessworkflow.api.error.ErrorDefinition; |
23 | | -import io.serverlessworkflow.api.events.EventDefinition; |
24 | | -import io.serverlessworkflow.api.functions.FunctionDefinition; |
25 | 27 | import io.serverlessworkflow.api.interfaces.Extension; |
26 | 28 | import io.serverlessworkflow.api.interfaces.State; |
27 | | -import io.serverlessworkflow.api.retry.RetryDefinition; |
28 | | -import java.io.IOException; |
29 | | -import java.security.MessageDigest; |
30 | | -import java.util.UUID; |
31 | 29 |
|
32 | 30 | public class WorkflowSerializer extends StdSerializer<Workflow> { |
33 | 31 |
|
34 | | - public WorkflowSerializer() { |
35 | | - this(Workflow.class); |
36 | | - } |
37 | | - |
38 | | - protected WorkflowSerializer(Class<Workflow> t) { |
39 | | - super(t); |
40 | | - } |
41 | | - |
42 | | - private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); |
43 | | - |
44 | | - @Override |
45 | | - public void serialize(Workflow workflow, JsonGenerator gen, SerializerProvider provider) |
46 | | - throws IOException { |
47 | | - |
48 | | - gen.writeStartObject(); |
49 | | - |
50 | | - if (workflow.getId() != null && !workflow.getId().isEmpty()) { |
51 | | - gen.writeStringField("id", workflow.getId()); |
52 | | - } else { |
53 | | - gen.writeStringField("id", generateUniqueId()); |
54 | | - } |
55 | | - |
56 | | - if (workflow.getKey() != null) { |
57 | | - gen.writeStringField("key", workflow.getKey()); |
58 | | - } |
59 | | - gen.writeStringField("name", workflow.getName()); |
60 | | - |
61 | | - if (workflow.getDescription() != null && !workflow.getDescription().isEmpty()) { |
62 | | - gen.writeStringField("description", workflow.getDescription()); |
63 | | - } |
64 | | - |
65 | | - if (workflow.getVersion() != null && !workflow.getVersion().isEmpty()) { |
66 | | - gen.writeStringField("version", workflow.getVersion()); |
67 | | - } |
68 | | - |
69 | | - if (workflow.getAnnotations() != null && !workflow.getAnnotations().isEmpty()) { |
70 | | - gen.writeObjectField("annotations", workflow.getAnnotations()); |
71 | | - } |
72 | | - |
73 | | - if (workflow.getDataInputSchema() != null) { |
74 | | - if (workflow.getDataInputSchema().getSchema() != null |
75 | | - && workflow.getDataInputSchema().getSchema().length() > 0 |
76 | | - && workflow.getDataInputSchema().isFailOnValidationErrors()) { |
77 | | - gen.writeStringField("dataInputSchema", workflow.getDataInputSchema().getSchema()); |
78 | | - |
79 | | - } else if (workflow.getDataInputSchema().getSchema() != null |
80 | | - && workflow.getDataInputSchema().getSchema().length() > 0 |
81 | | - && !workflow.getDataInputSchema().isFailOnValidationErrors()) { |
82 | | - gen.writeObjectField("dataInputSchema", workflow.getDataInputSchema()); |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - if (workflow.getStart() != null) { |
87 | | - gen.writeObjectField("start", workflow.getStart()); |
88 | | - } |
89 | | - |
90 | | - if (workflow.getSpecVersion() != null && !workflow.getSpecVersion().isEmpty()) { |
91 | | - gen.writeStringField("specVersion", workflow.getSpecVersion()); |
92 | | - } |
93 | | - |
94 | | - if (workflow.getExtensions() != null && !workflow.getExpressionLang().isEmpty()) { |
95 | | - gen.writeStringField("expressionLang", workflow.getExpressionLang()); |
96 | | - } |
97 | | - |
98 | | - if (workflow.isKeepActive()) { |
99 | | - gen.writeBooleanField("keepActive", workflow.isKeepActive()); |
100 | | - } |
101 | | - |
102 | | - if (workflow.isAutoRetries()) { |
103 | | - gen.writeBooleanField("autoRetries", workflow.isAutoRetries()); |
104 | | - } |
105 | | - |
106 | | - if (workflow.getMetadata() != null && !workflow.getMetadata().isEmpty()) { |
107 | | - gen.writeObjectField("metadata", workflow.getMetadata()); |
108 | | - } |
109 | | - |
110 | | - if (workflow.getEvents() != null && !workflow.getEvents().getEventDefs().isEmpty()) { |
111 | | - gen.writeArrayFieldStart("events"); |
112 | | - for (EventDefinition eventDefinition : workflow.getEvents().getEventDefs()) { |
113 | | - gen.writeObject(eventDefinition); |
114 | | - } |
115 | | - gen.writeEndArray(); |
116 | | - } |
117 | | - |
118 | | - if (workflow.getFunctions() != null && !workflow.getFunctions().getFunctionDefs().isEmpty()) { |
119 | | - gen.writeArrayFieldStart("functions"); |
120 | | - for (FunctionDefinition function : workflow.getFunctions().getFunctionDefs()) { |
121 | | - gen.writeObject(function); |
122 | | - } |
123 | | - gen.writeEndArray(); |
124 | | - } |
125 | | - |
126 | | - if (workflow.getRetries() != null && !workflow.getRetries().getRetryDefs().isEmpty()) { |
127 | | - gen.writeArrayFieldStart("retries"); |
128 | | - for (RetryDefinition retry : workflow.getRetries().getRetryDefs()) { |
129 | | - gen.writeObject(retry); |
130 | | - } |
131 | | - gen.writeEndArray(); |
132 | | - } |
133 | | - |
134 | | - if (workflow.getErrors() != null && !workflow.getErrors().getErrorDefs().isEmpty()) { |
135 | | - gen.writeArrayFieldStart("errors"); |
136 | | - for (ErrorDefinition error : workflow.getErrors().getErrorDefs()) { |
137 | | - gen.writeObject(error); |
138 | | - } |
139 | | - gen.writeEndArray(); |
140 | | - } |
141 | | - |
142 | | - if (workflow.getSecrets() != null && !workflow.getSecrets().getSecretDefs().isEmpty()) { |
143 | | - gen.writeArrayFieldStart("secrets"); |
144 | | - for (String secretDef : workflow.getSecrets().getSecretDefs()) { |
145 | | - gen.writeString(secretDef); |
146 | | - } |
147 | | - gen.writeEndArray(); |
148 | | - } |
149 | | - |
150 | | - if (workflow.getConstants() != null && !workflow.getConstants().getConstantsDef().isEmpty()) { |
151 | | - gen.writeObjectField("constants", workflow.getConstants().getConstantsDef()); |
152 | | - } |
153 | | - |
154 | | - if (workflow.getTimeouts() != null) { |
155 | | - gen.writeObjectField("timeouts", workflow.getTimeouts()); |
156 | | - } |
157 | | - |
158 | | - if (workflow.getAuth() != null && !workflow.getAuth().getAuthDefs().isEmpty()) { |
159 | | - gen.writeObjectField("auth", workflow.getAuth().getAuthDefs()); |
160 | | - } |
161 | | - |
162 | | - if (workflow.getStates() != null && !workflow.getStates().isEmpty()) { |
163 | | - gen.writeArrayFieldStart("states"); |
164 | | - for (State state : workflow.getStates()) { |
165 | | - gen.writeObject(state); |
166 | | - } |
167 | | - gen.writeEndArray(); |
168 | | - } |
169 | | - |
170 | | - if (workflow.getExtensions() != null && !workflow.getExtensions().isEmpty()) { |
171 | | - gen.writeArrayFieldStart("extensions"); |
172 | | - for (Extension extension : workflow.getExtensions()) { |
173 | | - gen.writeObject(extension); |
174 | | - } |
175 | | - gen.writeEndArray(); |
176 | | - } |
177 | | - |
178 | | - gen.writeEndObject(); |
179 | | - } |
180 | | - |
181 | | - protected static String generateUniqueId() { |
182 | | - try { |
183 | | - MessageDigest salt = MessageDigest.getInstance("SHA-256"); |
184 | | - |
185 | | - salt.update(UUID.randomUUID().toString().getBytes("UTF-8")); |
186 | | - return bytesToHex(salt.digest()); |
187 | | - } catch (Exception e) { |
188 | | - return UUID.randomUUID().toString(); |
189 | | - } |
190 | | - } |
191 | | - |
192 | | - protected static String bytesToHex(byte[] bytes) { |
193 | | - char[] hexChars = new char[bytes.length * 2]; |
194 | | - for (int j = 0; j < bytes.length; j++) { |
195 | | - int v = bytes[j] & 0xFF; |
196 | | - hexChars[j * 2] = hexArray[v >>> 4]; |
197 | | - hexChars[j * 2 + 1] = hexArray[v & 0x0F]; |
| 32 | + private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); |
| 33 | + |
| 34 | + public WorkflowSerializer() { |
| 35 | + this(Workflow.class); |
| 36 | + } |
| 37 | + |
| 38 | + protected WorkflowSerializer(Class<Workflow> t) { |
| 39 | + super(t); |
| 40 | + } |
| 41 | + |
| 42 | + protected static String generateUniqueId() { |
| 43 | + try { |
| 44 | + MessageDigest salt = MessageDigest.getInstance("SHA-256"); |
| 45 | + salt.update(UUID.randomUUID().toString().getBytes("UTF-8")); |
| 46 | + return bytesToHex(salt.digest()); |
| 47 | + } catch (Exception e) { |
| 48 | + return UUID.randomUUID().toString(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + protected static String bytesToHex(byte[] bytes) { |
| 53 | + char[] hexChars = new char[bytes.length * 2]; |
| 54 | + for (int j = 0; j < bytes.length; j++) { |
| 55 | + int v = bytes[j] & 0xFF; |
| 56 | + hexChars[j * 2] = hexArray[v >>> 4]; |
| 57 | + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; |
| 58 | + } |
| 59 | + return new String(hexChars); |
| 60 | + } |
| 61 | + |
| 62 | + // Helper to write either an array of items or a single string reference |
| 63 | + private <T> void writeListOrRef( |
| 64 | + JsonGenerator gen, |
| 65 | + SerializerProvider prov, |
| 66 | + String fieldName, |
| 67 | + List<T> items, |
| 68 | + String refValue) throws IOException { |
| 69 | + if (items != null && !items.isEmpty()) { |
| 70 | + gen.writeArrayFieldStart(fieldName); |
| 71 | + for (T item : items) { |
| 72 | + prov.defaultSerializeValue(item, gen); |
| 73 | + } |
| 74 | + gen.writeEndArray(); |
| 75 | + } else if (refValue != null) { |
| 76 | + gen.writeStringField(fieldName, refValue); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void serialize(Workflow workflow, JsonGenerator gen, SerializerProvider provider) |
| 82 | + throws IOException { |
| 83 | + |
| 84 | + gen.writeStartObject(); |
| 85 | + |
| 86 | + // --- ID / key / basic fields --- |
| 87 | + if (workflow.getId() != null && !workflow.getId().isEmpty()) { |
| 88 | + gen.writeStringField("id", workflow.getId()); |
| 89 | + } else { |
| 90 | + gen.writeStringField("id", generateUniqueId()); |
| 91 | + } |
| 92 | + if (workflow.getKey() != null) { |
| 93 | + gen.writeStringField("key", workflow.getKey()); |
| 94 | + } |
| 95 | + gen.writeStringField("name", workflow.getName()); |
| 96 | + if (workflow.getDescription() != null && !workflow.getDescription().isEmpty()) { |
| 97 | + gen.writeStringField("description", workflow.getDescription()); |
| 98 | + } |
| 99 | + if (workflow.getVersion() != null && !workflow.getVersion().isEmpty()) { |
| 100 | + gen.writeStringField("version", workflow.getVersion()); |
| 101 | + } |
| 102 | + if (workflow.getAnnotations() != null && !workflow.getAnnotations().isEmpty()) { |
| 103 | + gen.writeObjectField("annotations", workflow.getAnnotations()); |
| 104 | + } |
| 105 | + if (workflow.getDataInputSchema() != null) { |
| 106 | + if (workflow.getDataInputSchema().getSchema() != null |
| 107 | + && !workflow.getDataInputSchema().getSchema().isEmpty() |
| 108 | + && workflow.getDataInputSchema().isFailOnValidationErrors()) { |
| 109 | + gen.writeStringField("dataInputSchema", workflow.getDataInputSchema().getSchema()); |
| 110 | + } else if (workflow.getDataInputSchema().getSchema() != null |
| 111 | + && !workflow.getDataInputSchema().getSchema().isEmpty() |
| 112 | + && !workflow.getDataInputSchema().isFailOnValidationErrors()) { |
| 113 | + gen.writeObjectField("dataInputSchema", workflow.getDataInputSchema()); |
| 114 | + } |
| 115 | + } |
| 116 | + if (workflow.getStart() != null) { |
| 117 | + gen.writeObjectField("start", workflow.getStart()); |
| 118 | + } |
| 119 | + if (workflow.getSpecVersion() != null && !workflow.getSpecVersion().isEmpty()) { |
| 120 | + gen.writeStringField("specVersion", workflow.getSpecVersion()); |
| 121 | + } |
| 122 | + if (workflow.getExpressionLang() != null && !workflow.getExpressionLang().isEmpty()) { |
| 123 | + gen.writeStringField("expressionLang", workflow.getExpressionLang()); |
| 124 | + } |
| 125 | + if (workflow.isKeepActive()) { |
| 126 | + gen.writeBooleanField("keepActive", workflow.isKeepActive()); |
| 127 | + } |
| 128 | + if (workflow.isAutoRetries()) { |
| 129 | + gen.writeBooleanField("autoRetries", workflow.isAutoRetries()); |
| 130 | + } |
| 131 | + if (workflow.getMetadata() != null && !workflow.getMetadata().isEmpty()) { |
| 132 | + gen.writeObjectField("metadata", workflow.getMetadata()); |
| 133 | + } |
| 134 | + |
| 135 | + // --- Collections or references --- |
| 136 | + if (workflow.getEvents() != null) { |
| 137 | + writeListOrRef(gen, provider, |
| 138 | + "events", |
| 139 | + workflow.getEvents().getEventDefs(), |
| 140 | + workflow.getEvents().getRefValue()); |
| 141 | + } |
| 142 | + if (workflow.getFunctions() != null) { |
| 143 | + writeListOrRef(gen, provider, |
| 144 | + "functions", |
| 145 | + workflow.getFunctions().getFunctionDefs(), |
| 146 | + workflow.getFunctions().getRefValue()); |
| 147 | + } |
| 148 | + if (workflow.getRetries() != null) { |
| 149 | + writeListOrRef(gen, provider, |
| 150 | + "retries", |
| 151 | + workflow.getRetries().getRetryDefs(), |
| 152 | + workflow.getRetries().getRefValue()); |
| 153 | + } |
| 154 | + if (workflow.getErrors() != null) { |
| 155 | + writeListOrRef(gen, provider, |
| 156 | + "errors", |
| 157 | + workflow.getErrors().getErrorDefs(), |
| 158 | + workflow.getErrors().getRefValue()); |
| 159 | + } |
| 160 | + if (workflow.getSecrets() != null) { |
| 161 | + writeListOrRef(gen, provider, |
| 162 | + "secrets", |
| 163 | + workflow.getSecrets().getSecretDefs(), |
| 164 | + workflow.getSecrets().getRefValue()); |
| 165 | + } |
| 166 | + |
| 167 | + // --- Always-array fields --- |
| 168 | + if (workflow.getStates() != null && !workflow.getStates().isEmpty()) { |
| 169 | + gen.writeArrayFieldStart("states"); |
| 170 | + for (State state : workflow.getStates()) { |
| 171 | + gen.writeObject(state); |
| 172 | + } |
| 173 | + gen.writeEndArray(); |
| 174 | + } |
| 175 | + if (workflow.getExtensions() != null && !workflow.getExtensions().isEmpty()) { |
| 176 | + gen.writeArrayFieldStart("extensions"); |
| 177 | + for (Extension ext : workflow.getExtensions()) { |
| 178 | + gen.writeObject(ext); |
| 179 | + } |
| 180 | + gen.writeEndArray(); |
| 181 | + } |
| 182 | + |
| 183 | + gen.writeEndObject(); |
198 | 184 | } |
199 | | - return new String(hexChars); |
200 | | - } |
201 | 185 | } |
0 commit comments