/** * 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()); }
/** * The content type of the response. * <p> * * @return The name of the content type. */ default Optional<String> getContentType(){ return getHeaders().get(CONTENT_TYPE_HEADER); }
/** * Report the boolean success of this event. * For default-format functions, this is used to map the HTTP status code into a straight success/failure. * * @return true if the output event results from a successful invocation. */ default boolean isSuccess() { return getStatus() == Status.Success; }
@Override public void initialize(RuntimeContext ctx) { ctx.addInvoker(new SpringCloudFunctionInvoker(ctx.getMethod().getTargetClass()),FunctionInvoker.Phase.Call); } }
@Override public Headers getHeaders() { return Headers.emptyHeaders(); }
@Override public void setResponseHeader(String key, String value, String... vs) { if (Headers.canonicalKey(key).equals(OutputEvent.CONTENT_TYPE_HEADER)) { invocationContext.setResponseContentType(value); invocationContext.setResponseHeader("Fn-Http-H-" + key, value); } else { invocationContext.setResponseHeader("Fn-Http-H-" + key, value, vs); } }
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 Optional<OutputEvent> wrapFunctionResult(InvocationContext ctx, MethodWrapper method, Object value) { if (method.getReturnType().getParameterClass().equals(Void.class)) { return Optional.of(OutputEvent.emptyResult(OutputEvent.Status.Success)); } else { return Optional.empty(); } } }
/** * Set an {@link FunctionInvoker} for this function. The invoker will override * the built in function invoker, although the cloud threads invoker will still * have precedence so that cloud threads can be used from functions using custom invokers. * * @param invoker The {@link FunctionInvoker} to add. * @deprecated this is equivalent to {@link #addInvoker(FunctionInvoker, FunctionInvoker.Phase)} with a phase of {@link FunctionInvoker.Phase#Call} */ default void setInvoker(FunctionInvoker invoker) { addInvoker(invoker, FunctionInvoker.Phase.Call); }
@Override public void writeToOutput(OutputStream out) throws IOException { a.writeToOutput(out); } };
/** * 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<String> getContentType() { return from.getContentType(); }
@Override public Headers getHeaders() { return from.getHeaders(); }
/** * Sets the response content type, this will override the default content type of the output * * @param contentType a mime type for the response */ default void setResponseContentType(String contentType) { this.setResponseHeader(OutputEvent.CONTENT_TYPE_HEADER, contentType); }
@Override public void fireOnFailedInvocation() { for (InvocationListener listener : invocationListeners) { try { listener.onFailure(); } catch (Exception ignored) { } } } }
@Override public void fireOnSuccessfulInvocation() { for (InvocationListener listener : invocationListeners) { try { listener.onSuccess(); } catch (Exception ignored) { } } }
/** * This simply allows the FnRequestHandler to access the runtime context so it can later * be injected into JAX-RS functions. * * @param rctx The runtime context passed in from the Fn java FDK */ @FnConfiguration public void setRuntimeContext(RuntimeContext rctx) { this.rctx = rctx; }
@Override public void addResponseHeader(String key, String value) { invocationContext.addResponseHeader("Fn-Http-H-" + key, value); }
@Override public Status getStatus() { return from.getStatus(); }
@Override public Status getStatus() { return a.getStatus(); }