/** * create a function invocation exception * * @param message private message for this exception - * @param target the underlying user exception that triggered this failure */ public InternalFunctionInvocationException(String message, Throwable target) { super(message); this.cause = target; this.event = OutputEvent.fromBytes(new byte[0], OutputEvent.Status.FunctionError, null); }
@Override public Optional<OutputEvent> wrapFunctionResult(InvocationContext ctx, MethodWrapper method, Object value) { try { return Optional.of(OutputEvent.fromBytes(objectMapper(ctx).writeValueAsBytes(value), OutputEvent.Status.Success, "application/json")); } catch (JsonProcessingException e) { throw new RuntimeException("Failed to render response to JSON", e); } }
/** * Create an output event from a byte array * * @param bytes the byte array to write to the output * @param status the status code to report * @param contentType the content type to present on HTTP responses * @return a new output event */ static OutputEvent fromBytes(byte[] bytes, Status status, String contentType) { return fromBytes(bytes, status, contentType, Headers.emptyHeaders()); }
@Override public Optional<OutputEvent> wrapFunctionResult(InvocationContext ctx, MethodWrapper method, Object value) { if (method.getReturnType().getParameterClass().equals(String.class)) { return Optional.of(OutputEvent.fromBytes(((String) value).getBytes(), OutputEvent.Status.Success, "text/plain")); } else { return Optional.empty(); } }
public Optional<OutputEvent> wrapFunctionResult(InvocationContext ctx, MethodWrapper method, Object value) { if (method.getReturnType().getParameterClass().equals(byte[].class)) { return Optional.of(OutputEvent.fromBytes(((byte[]) value), OutputEvent.Status.Success, "application/octet-stream")); } else { return Optional.empty(); } }
@Override public void writeResponse(Response.StatusType statusType, Map<String, List<String>> headers, OutputStream outputStream) throws IOException { // NOTE: This is a safe cast as it is set to a ByteArrayOutputStream by getEntityOutputStream // See JRestlessHandlerContainer class for more details String responseBody = ((ByteArrayOutputStream) outputStream).toString(StandardCharsets.UTF_8.name()); String contentType = headers .getOrDefault(HttpHeaders.CONTENT_TYPE, Collections.singletonList(DEFAULT_CONTENT_TYPE)) .get(0); Map<String, String> outHeaders = new HashMap<>(); headers.forEach((k, v) -> outHeaders.put(k, v.stream().collect(Collectors.joining(",")))); OutputEvent outputEvent = OutputEvent.fromBytes( responseBody.getBytes(StandardCharsets.UTF_8), statusType.getStatusCode(), contentType, Headers.fromMap(outHeaders)); response = new WrappedOutput(outputEvent, responseBody, statusType); } }