From 8b7b0948eca011da01eb97de093f7a09aa18f3f0 Mon Sep 17 00:00:00 2001 From: FunctionsAPI Date: Sun, 19 Jan 2025 13:51:30 +0000 Subject: [PATCH] Automatic push from FunctionsAPI --- README.md | 3 +- pom.xml | 38 ++++++++++ .../samples/CloudEventFunctionImpl.java | 34 +++++++++ .../java/dev/openfunction/samples/Main.java | 27 +++++++ .../samples/OpenFunctionImpl.java | 41 ++++++++++ .../samples/plugins/ExamplePlugin.java | 74 +++++++++++++++++++ 6 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 pom.xml create mode 100644 src/main/java/dev/openfunction/samples/CloudEventFunctionImpl.java create mode 100644 src/main/java/dev/openfunction/samples/Main.java create mode 100644 src/main/java/dev/openfunction/samples/OpenFunctionImpl.java create mode 100644 src/main/java/dev/openfunction/samples/plugins/ExamplePlugin.java diff --git a/README.md b/README.md index a26729d..0b64d2d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ -# samples - +# java hello-world diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8e316e9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + dev.openfunction.samples + samples + 1.0-SNAPSHOT + + + 11 + 11 + + + + + snapshots + Maven snapshots + https://s01.oss.sonatype.org/content/repositories/snapshots/ + + false + + + true + + + + + + + dev.openfunction.functions + functions-framework-api + 1.0.0 + + + + diff --git a/src/main/java/dev/openfunction/samples/CloudEventFunctionImpl.java b/src/main/java/dev/openfunction/samples/CloudEventFunctionImpl.java new file mode 100644 index 0000000..ce876d8 --- /dev/null +++ b/src/main/java/dev/openfunction/samples/CloudEventFunctionImpl.java @@ -0,0 +1,34 @@ +/* +Copyright 2022 The OpenFunction Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package dev.openfunction.samples; + +import dev.openfunction.functions.CloudEventFunction; +import dev.openfunction.functions.Context; +import io.cloudevents.CloudEvent; + +public class CloudEventFunctionImpl implements CloudEventFunction { + @Override + public Error accept(Context ctx, CloudEvent event) throws Exception { + if (event.getData() == null) { + System.out.println("receive event without data"); + } else { + System.out.println("receive event: " + new String(event.getData().toBytes())); + } + + return null; + } +} diff --git a/src/main/java/dev/openfunction/samples/Main.java b/src/main/java/dev/openfunction/samples/Main.java new file mode 100644 index 0000000..235b95a --- /dev/null +++ b/src/main/java/dev/openfunction/samples/Main.java @@ -0,0 +1,27 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package dev.openfunction.samples; + +import dev.openfunction.functions.HttpFunction; +import dev.openfunction.functions.HttpRequest; +import dev.openfunction.functions.HttpResponse; + +public class Main implements HttpFunction { + // Simple function to return "Hello World" + @Override + public void service(HttpRequest request, HttpResponse response) throws Exception { + + response.getWriter().write("Hello World"); + } +} diff --git a/src/main/java/dev/openfunction/samples/OpenFunctionImpl.java b/src/main/java/dev/openfunction/samples/OpenFunctionImpl.java new file mode 100644 index 0000000..d26e553 --- /dev/null +++ b/src/main/java/dev/openfunction/samples/OpenFunctionImpl.java @@ -0,0 +1,41 @@ +/* +Copyright 2022 The OpenFunction Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package dev.openfunction.samples; + +import dev.openfunction.functions.OpenFunction; +import dev.openfunction.functions.Context; +import dev.openfunction.functions.Out; + +public class OpenFunctionImpl implements OpenFunction { + + @Override + public Out accept(Context context, String payload) throws Exception { + System.out.printf("receive event: %s", payload).println(); + + if (context.getOutputs() != null) { + for (String key : context.getOutputs().keySet()) { + Error error = context.send(key, payload); + if (error != null) { + System.out.printf("send to output %s error, %s", key, error.getMessage()).println(); + } else { + System.out.printf("send to output %s", key).println(); + } + } + } + return new Out(); + } +} diff --git a/src/main/java/dev/openfunction/samples/plugins/ExamplePlugin.java b/src/main/java/dev/openfunction/samples/plugins/ExamplePlugin.java new file mode 100644 index 0000000..828d388 --- /dev/null +++ b/src/main/java/dev/openfunction/samples/plugins/ExamplePlugin.java @@ -0,0 +1,74 @@ +/* +Copyright 2022 The OpenFunction Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package dev.openfunction.samples.plugins; + +import dev.openfunction.functions.Context; +import dev.openfunction.functions.Plugin; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class ExamplePlugin implements Plugin { + @Override + public String name() { + return "plugin-example"; + } + + @Override + public String version() { + return "v1.0.0"; + } + + @Override + public Plugin init() { + return this; + } + + @Override + public Error execPreHook(Context ctx) { + execHook(ctx, "pre"); + return null; + } + + @Override + public Error execPostHook(Context ctx) { + execHook(ctx, "post"); + return null; + } + + private void execHook(Context ctx, String type) { + String ts = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.XXX").format(new Date()); + if (ctx.getBindingEvent() != null) { + System.out.printf("plugin %s:%s exec %s hook for binding %s at %s", name(), version(), type, ctx.getBindingEvent().getName(), ts).println(); + } else if (ctx.getTopicEvent() != null) { + System.out.printf("plugin %s:%s exec %s hook for pubsub %s at %s", name(), version(), type, ctx.getTopicEvent().getName(), ts).println(); + } else if (ctx.getHttpRequest() != null) { + if (ctx.getCloudEvent() != null) { + System.out.printf("plugin %s:%s exec %s hook for cloudevent function at %s", name(), version(), type, ts).println(); + } else { + System.out.printf("plugin %s:%s exec %s hook for http function at %s", name(), version(), type, ts).println(); + } + } else { + System.out.println("unknown function type"); + } + } + + @Override + public Object getField(String fieldName) { + return null; + } +}