001/* 002 * Copyright 2019 Perihelios LLC 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.perihelios.aws.lambda.cloudwatch.dispatcher.event; 017 018import com.perihelios.aws.lambda.cloudwatch.dispatcher.CloudWatchEventDispatcher; 019 020import java.util.function.BiConsumer; 021 022/** 023 * Base class of all CloudWatch events. 024 * <p> 025 * CloudWatch events are delivered to AWS Lambda functions as JSON documents. There are a small number of top-level 026 * properties in the JSON; these are mostly metadata, and are unmarshalled into the {@link Header}, obtainable via 027 * {@link #header()}. The event proper is contained in the {@code detail} property, with the {@code detail-type} 028 * property providing a way to identify what properties may be expected in {@code detail}. 029 * </p> 030 * <p> 031 * Subclasses should <em>always</em> be annotated with {@link DetailType}, as this is the only mechanism to indicate 032 * to which subclass an event should be unmarshalled. Assume an event such as this is received by an AWS Lambda: 033 * </p> 034 * <pre> 035 * { 036 * ... 037 * "detail-type": "Some Service Event", 038 * "detail": { 039 * "blah": "some value", 040 * "foo": 17 041 * } 042 * ... 043 * }</pre> 044 * <p> 045 * A proper class to which to unmarshal this event might look like this: 046 * </p> 047 * <pre> 048 * @DetailType("Some Service Event") 049 * class MyCustomEvent extends CloudWatchEvent { 050 * private String blah; 051 * private int foo; 052 * 053 * String blah() { 054 * return blah; 055 * } 056 * 057 * int foo() { 058 * return foo; 059 * } 060 * } 061 * </pre> 062 * <p> 063 * Note that {@code MyCustomEvent} would also need to be registered with 064 * {@link CloudWatchEventDispatcher#withEventHandler(Class, BiConsumer) CloudWatchEventDispatcher.withEventHandler()} 065 * in order to be used during event dispatch. 066 * </p> 067 */ 068public class CloudWatchEvent { 069 private Header header; 070 071 /** 072 * Returns the header (metadata) from the event. 073 * 074 * @return the event header 075 */ 076 public Header header() { 077 return header; 078 } 079 080 /** 081 * Sets the header (metadata) on the event. 082 * 083 * @param header the event header to set 084 */ 085 public void setHeader(Header header) { 086 this.header = header; 087 } 088}