org.apache.camel.component.aws.cloudtrail.CloudtrailConsumer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camel-aws-cloudtrail Show documentation
Show all versions of camel-aws-cloudtrail Show documentation
Consume Events coming from AWS Cloudtrail Service
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.camel.component.aws.cloudtrail;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import org.apache.camel.AsyncCallback;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.support.ScheduledBatchPollingConsumer;
import org.apache.camel.util.CastUtils;
import org.apache.camel.util.ObjectHelper;
import software.amazon.awssdk.services.cloudtrail.CloudTrailClient;
import software.amazon.awssdk.services.cloudtrail.model.*;
public class CloudtrailConsumer extends ScheduledBatchPollingConsumer {
private static Instant lastTime;
public CloudtrailConsumer(CloudtrailEndpoint endpoint, Processor processor) {
super(endpoint, processor);
}
@Override
protected int poll() throws Exception {
LookupEventsRequest.Builder eventsRequestBuilder
= LookupEventsRequest.builder().maxResults(getEndpoint().getConfiguration().getMaxResults());
List attributes = new ArrayList<>();
if (ObjectHelper.isNotEmpty(getEndpoint().getConfiguration().getEventSource())) {
LookupAttribute eventSource = LookupAttribute.builder().attributeKey(LookupAttributeKey.EVENT_SOURCE)
.attributeValue(getEndpoint().getConfiguration().getEventSource()).build();
attributes.add(eventSource);
}
if (!attributes.isEmpty()) {
eventsRequestBuilder.lookupAttributes(attributes);
}
if (lastTime != null) {
eventsRequestBuilder.startTime(lastTime.plusMillis(1000));
}
LookupEventsResponse response = getClient().lookupEvents(eventsRequestBuilder.build());
// okay we have some response from aws so lets mark the consumer as ready
forceConsumerAsReady();
if (!response.events().isEmpty()) {
lastTime = response.events().get(0).eventTime();
}
Queue exchanges = createExchanges(response.events());
return processBatch(CastUtils.cast(exchanges));
}
@Override
public int processBatch(Queue