mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
refactor(apigateway,appsync): extract shared VTL $util functions into a common utility (#3583)
VtlTemplateEngine.UtilVariable (API Gateway) and AppSyncUtil (AppSync) each independently implemented the same ~49-line block of AWS $util VTL functions (escapeJavaScript, urlEncode, urlDecode, base64Encode, base64Decode, parseJson), flagged as duplicated by CPD. AWS specifies $util identically across both services' VTL mapping-template docs, so the two floci implementations risked silently disagreeing on edge cases over time. Extracted the shared functions into VtlUtilFunctions under core.common.vtl; both $util implementations now delegate to it. base64Encode keeps two overloads matching each service's real AWS signature: API Gateway's $util.base64Encode takes a string, AppSync's takes a Blob (byte[] in the Java model), so both overloads are preserved rather than collapsed into one. No behavior change. All existing VtlTemplateEngine and AppSyncUtil tests, plus the full ApiGateway and AppSync suites, pass unchanged.
This commit is contained in:
1 parent
8d3fdbdd56
commit
7a228c5325
3 files changed
+119
-86
No files matched your search
@@ -0,0 +1,105 @@
|
||||
package io.github.hectorvent.floci.core.common.vtl;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Implements the AWS VTL {@code $util} functions that API Gateway and AppSync specify
|
||||
* identically: string escaping, URL and base64 encoding, and JSON parsing. AWS documents
|
||||
* {@code $util} as one shared contract across both services' mapping-template specs, so
|
||||
* these implementations are shared rather than kept as two independently maintained
|
||||
* copies. Each service's own {@code $util} wrapper (API Gateway's
|
||||
* {@code VtlTemplateEngine.UtilVariable}, AppSync's {@code AppSyncUtil}) delegates the
|
||||
* overlapping functions here and keeps everything specific to its own template dialect
|
||||
* (error signaling, GraphQL-specific helpers, response overrides, etc.) to itself.
|
||||
*/
|
||||
public final class VtlUtilFunctions {
|
||||
|
||||
private VtlUtilFunctions() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes a string using EcmaScript/JavaScript string rules, matching AWS's
|
||||
* {@code $util.escapeJavaScript} (Apache Commons Lang escapeEcmaScript). Escapes:
|
||||
* backslash, double/single quotes, forward slash, control chars, and non-ASCII
|
||||
* characters (outside 0x20-0x7E) as unicode escape sequences.
|
||||
*/
|
||||
public static String escapeJavaScript(String s) {
|
||||
if (s == null) return "";
|
||||
StringBuilder sb = new StringBuilder(s.length() + 16);
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
switch (c) {
|
||||
case '\\' -> sb.append("\\\\");
|
||||
case '"' -> sb.append("\\\"");
|
||||
case '\'' -> sb.append("\\'");
|
||||
case '/' -> sb.append("\\/");
|
||||
case '\b' -> sb.append("\\b");
|
||||
case '\t' -> sb.append("\\t");
|
||||
case '\n' -> sb.append("\\n");
|
||||
case '\f' -> sb.append("\\f");
|
||||
case '\r' -> sb.append("\\r");
|
||||
default -> {
|
||||
if (c < 0x20 || c > 0x7E) {
|
||||
sb.append("\\u").append(String.format("%04x", (int) c));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** URL-encodes a string. */
|
||||
public static String urlEncode(String s) {
|
||||
if (s == null) return "";
|
||||
return URLEncoder.encode(s, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
/** URL-decodes a string. */
|
||||
public static String urlDecode(String s) {
|
||||
if (s == null) return "";
|
||||
return URLDecoder.decode(s, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64-encodes a string, UTF-8 encoded first. This is API Gateway's
|
||||
* {@code $util.base64Encode}, which takes a string.
|
||||
*/
|
||||
public static String base64Encode(String s) {
|
||||
if (s == null) return "";
|
||||
return Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64-encodes raw bytes. This is AppSync's {@code $util.base64Encode}, which
|
||||
* operates on a Blob rather than a string.
|
||||
*/
|
||||
public static String base64Encode(byte[] data) {
|
||||
if (data == null) return "";
|
||||
return Base64.getEncoder().encodeToString(data);
|
||||
}
|
||||
|
||||
/** Base64-decodes a string. */
|
||||
public static String base64Decode(String s) {
|
||||
if (s == null) return "";
|
||||
return new String(Base64.getDecoder().decode(s), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
/** Parses a JSON string into a Map/List structure navigable in VTL. */
|
||||
public static Object parseJson(ObjectMapper objectMapper, String s) {
|
||||
if (s == null || s.isEmpty()) return Map.of();
|
||||
try {
|
||||
return objectMapper.readValue(s, Object.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
return Map.of();
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-45
@@ -1,8 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.vtl.VtlUtilFunctions;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
@@ -28,11 +28,7 @@ import org.apache.velocity.util.introspection.TypeConversionHandlerImpl;
|
||||
import org.apache.velocity.util.introspection.UberspectImpl;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -336,68 +332,34 @@ public class VtlTemplateEngine {
|
||||
/**
|
||||
* Escapes a string using EcmaScript/JavaScript string rules.
|
||||
* Matches AWS API Gateway behavior (Apache Commons Lang escapeEcmaScript).
|
||||
* Escapes: backslash, double/single quotes, forward slash, control chars,
|
||||
* and non-ASCII characters (outside 0x20-0x7E) as unicode escape sequences.
|
||||
*/
|
||||
public String escapeJavaScript(String s) {
|
||||
if (s == null) return "";
|
||||
StringBuilder sb = new StringBuilder(s.length() + 16);
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
switch (c) {
|
||||
case '\\' -> sb.append("\\\\");
|
||||
case '"' -> sb.append("\\\"");
|
||||
case '\'' -> sb.append("\\'");
|
||||
case '/' -> sb.append("\\/");
|
||||
case '\b' -> sb.append("\\b");
|
||||
case '\t' -> sb.append("\\t");
|
||||
case '\n' -> sb.append("\\n");
|
||||
case '\f' -> sb.append("\\f");
|
||||
case '\r' -> sb.append("\\r");
|
||||
default -> {
|
||||
if (c < 0x20 || c > 0x7E) {
|
||||
sb.append("\\u").append(String.format("%04x", (int) c));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
return VtlUtilFunctions.escapeJavaScript(s);
|
||||
}
|
||||
|
||||
/** URL-encodes a string. */
|
||||
public String urlEncode(String s) {
|
||||
if (s == null) return "";
|
||||
return URLEncoder.encode(s, StandardCharsets.UTF_8);
|
||||
return VtlUtilFunctions.urlEncode(s);
|
||||
}
|
||||
|
||||
/** URL-decodes a string. */
|
||||
public String urlDecode(String s) {
|
||||
if (s == null) return "";
|
||||
return URLDecoder.decode(s, StandardCharsets.UTF_8);
|
||||
return VtlUtilFunctions.urlDecode(s);
|
||||
}
|
||||
|
||||
/** Base64-encodes a string. */
|
||||
public String base64Encode(String s) {
|
||||
if (s == null) return "";
|
||||
return Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_8));
|
||||
return VtlUtilFunctions.base64Encode(s);
|
||||
}
|
||||
|
||||
/** Base64-decodes a string. */
|
||||
public String base64Decode(String s) {
|
||||
if (s == null) return "";
|
||||
return new String(Base64.getDecoder().decode(s), StandardCharsets.UTF_8);
|
||||
return VtlUtilFunctions.base64Decode(s);
|
||||
}
|
||||
|
||||
/** Parses a JSON string into a Map/List structure navigable in VTL. */
|
||||
public Object parseJson(String s) {
|
||||
if (s == null || s.isEmpty()) return Map.of();
|
||||
try {
|
||||
return objectMapper.readValue(s, Object.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
return Map.of();
|
||||
}
|
||||
return VtlUtilFunctions.parseJson(objectMapper, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-41
@@ -2,10 +2,8 @@ package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.vtl.VtlUtilFunctions;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -50,59 +48,27 @@ public class AppSyncUtil {
|
||||
public MapUtil getMap() { return mapUtil; }
|
||||
|
||||
public String escapeJavaScript(String s) {
|
||||
if (s == null) return "";
|
||||
StringBuilder sb = new StringBuilder(s.length() + 16);
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
switch (c) {
|
||||
case '\\' -> sb.append("\\\\");
|
||||
case '"' -> sb.append("\\\"");
|
||||
case '\'' -> sb.append("\\'");
|
||||
case '/' -> sb.append("\\/");
|
||||
case '\b' -> sb.append("\\b");
|
||||
case '\t' -> sb.append("\\t");
|
||||
case '\n' -> sb.append("\\n");
|
||||
case '\f' -> sb.append("\\f");
|
||||
case '\r' -> sb.append("\\r");
|
||||
default -> {
|
||||
if (c < 0x20 || c > 0x7E) {
|
||||
sb.append("\\u").append(String.format("%04x", (int) c));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
return VtlUtilFunctions.escapeJavaScript(s);
|
||||
}
|
||||
|
||||
public String urlEncode(String s) {
|
||||
if (s == null) return "";
|
||||
return URLEncoder.encode(s, StandardCharsets.UTF_8);
|
||||
return VtlUtilFunctions.urlEncode(s);
|
||||
}
|
||||
|
||||
public String urlDecode(String s) {
|
||||
if (s == null) return "";
|
||||
return URLDecoder.decode(s, StandardCharsets.UTF_8);
|
||||
return VtlUtilFunctions.urlDecode(s);
|
||||
}
|
||||
|
||||
public String base64Encode(byte[] data) {
|
||||
if (data == null) return "";
|
||||
return Base64.getEncoder().encodeToString(data);
|
||||
return VtlUtilFunctions.base64Encode(data);
|
||||
}
|
||||
|
||||
public String base64Decode(String s) {
|
||||
if (s == null) return "";
|
||||
return new String(Base64.getDecoder().decode(s), StandardCharsets.UTF_8);
|
||||
return VtlUtilFunctions.base64Decode(s);
|
||||
}
|
||||
|
||||
public Object parseJson(String s) {
|
||||
if (s == null || s.isEmpty()) return Map.of();
|
||||
try {
|
||||
return objectMapper.readValue(s, Object.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
return Map.of();
|
||||
}
|
||||
return VtlUtilFunctions.parseJson(objectMapper, s);
|
||||
}
|
||||
|
||||
public String toJson(Object o) {
|
||||
|
||||
Reference in new issue
Block a user