Skip to content

googleapis/google-cloudevents-java

Google CloudEvents Types for Java

Maven Central

This library provides Java classes for Google CloudEvent data to support unmarshalling of event data into a Java object. This library is generated from the protobufs sourced from https://github.com/googleapis/google-cloudevents.

Example event classes:

Supported Java Versions

Java 11 or above is required.

Installation

If you are using Maven, add this to your pom.xml file:

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloudevent-types</artifactId>
  <version>0.8.1</version>
</dependency>

Usage

CloudEvents are currently delivered to targets via HTTP protocol binding with a JSON payload.

Using the JsonFormat the JSON payload can be converted into the specific event type object.

Example usage in an Event-driven Cloud Function:

package functions;

import com.google.cloud.functions.CloudEventsFunction;
import com.google.events.cloud.audit.v1.LogEntryData;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import io.cloudevents.CloudEvent;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;

public class HelloProto implements CloudEventsFunction {
  private static final Logger LOGGER = Logger.getLogger(HelloProto.class.getName());

  @Override
  public void accept(CloudEvent event) throws InvalidProtocolBufferException {
    if (event.getData() != null) {
      // Extract JSON from CloudEvent
      String json = new String(event.getData().toBytes(), StandardCharsets.UTF_8);
      // Convert data to LogEntryData object
      LogEntryData.Builder builder = LogEntryData.newBuilder();
      JsonFormat.parser().merge(json, builder);
      LogEntryData data = builder.build();
      
      // Extract specific data from LogEntryData object
      LOGGER.info("### CloudEvent Data ###");
      LOGGER.info(data.getLogName());
      LOGGER.info(data.getProtoPayload().getAuthenticationInfo().getPrincipalEmail());
    }
  }
}

Versioning

This library follows Semantic Versioning.

Contributing

Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING for more information how to get started.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.

License

Apache 2.0 - See LICENSE for more information.