blob: 98df4ef368b43042f31f307c1c0541ac8da3cabe [file] [log] [blame]
/**
* 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.salesforce.internal.streaming;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.cometd.bayeux.Channel;
import org.cometd.bayeux.Message;
import org.cometd.bayeux.client.ClientSession;
import org.cometd.bayeux.client.ClientSession.Extension.Adapter;
/**
* CometDReplayExtension, typical usages are the following:
* {@code client.addExtension(new CometDReplayExtension<>(replayMap));}
*
* @author yzhao
* @since 198 (Winter '16)
*/
public class CometDReplayExtension<V> extends Adapter {
private static final String EXTENSION_NAME = "replay";
private final ConcurrentMap<String, V> dataMap = new ConcurrentHashMap<>();
private final AtomicBoolean supported = new AtomicBoolean();
public CometDReplayExtension(Map<String, V> dataMap) {
this.dataMap.putAll(dataMap);
}
@Override
@SuppressWarnings("unchecked")
public boolean rcv(ClientSession session, Message.Mutable message) {
Object data = message.get(EXTENSION_NAME);
if (this.supported.get() && data != null) {
try {
dataMap.put(message.getChannel(), (V) data);
} catch (ClassCastException e) {
return false;
}
}
return true;
}
@Override
public boolean rcvMeta(ClientSession session, Message.Mutable message) {
switch (message.getChannel()) {
case Channel.META_HANDSHAKE:
Map<String, Object> ext = message.getExt(false);
this.supported.set(ext != null && Boolean.TRUE.equals(ext.get(EXTENSION_NAME)));
break;
default:
break;
}
return true;
}
@Override
public boolean sendMeta(ClientSession session, Message.Mutable message) {
switch (message.getChannel()) {
case Channel.META_HANDSHAKE:
message.getExt(true).put(EXTENSION_NAME, Boolean.TRUE);
break;
case Channel.META_SUBSCRIBE:
if (supported.get()) {
message.getExt(true).put(EXTENSION_NAME, dataMap);
}
break;
default:
break;
}
return true;
}
}