001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.component.directvm;
018
019import org.apache.camel.AsyncCallback;
020import org.apache.camel.Exchange;
021import org.apache.camel.Message;
022import org.apache.camel.impl.DefaultAsyncProducer;
023import org.apache.camel.spi.HeaderFilterStrategy;
024import org.apache.camel.util.ExchangeHelper;
025
026/**
027 * The Direct-VM producer.
028 */
029public class DirectVmProducer extends DefaultAsyncProducer {
030
031    private DirectVmEndpoint endpoint;
032
033    public DirectVmProducer(DirectVmEndpoint endpoint) {
034        super(endpoint);
035        this.endpoint = endpoint;
036    }
037
038    @Override
039    public boolean process(Exchange exchange, AsyncCallback callback) {
040        // send to consumer
041        DirectVmConsumer consumer = endpoint.getComponent().getConsumer(endpoint);
042        
043        if (consumer == null) {
044            if (endpoint.isFailIfNoConsumers()) {
045                exchange.setException(new DirectVmConsumerNotAvailableException("No consumers available on endpoint: " + endpoint, exchange));
046            } else {
047                log.debug("message ignored, no consumers available on endpoint: " + endpoint);
048            }
049            callback.done(true);
050            return true;
051        }
052        
053        final HeaderFilterStrategy headerFilterStrategy = endpoint.getHeaderFilterStrategy();
054
055        // Only clone the Exchange if we actually need to filter out properties or headers.
056        final Exchange submitted = (!endpoint.isPropagateProperties() || headerFilterStrategy != null) ? exchange.copy(true) : exchange;
057
058        // Clear properties in the copy if we are not propagating them.
059        if (!endpoint.isPropagateProperties()) {
060            submitted.getProperties().clear();
061        }
062        
063        // Filter headers by Header Filter Strategy if there is one set.
064        if (headerFilterStrategy != null) {
065            submitted.getIn().getHeaders().entrySet().removeIf(e -> headerFilterStrategy.applyFilterToCamelHeaders(e.getKey(), e.getValue(), submitted));
066        }
067        
068        return consumer.getAsyncProcessor().process(submitted, done -> {
069            Message msg = submitted.hasOut() ? submitted.getOut() : submitted.getIn();
070
071            if (headerFilterStrategy != null) {
072                msg.getHeaders().entrySet().removeIf(e -> headerFilterStrategy.applyFilterToExternalHeaders(e.getKey(), e.getValue(), submitted));
073            }
074
075            if (exchange != submitted) {
076                // only need to copy back if they are different
077                exchange.setException(submitted.getException());
078                exchange.getOut().copyFrom(msg);
079            }
080
081            if (endpoint.isPropagateProperties()) {
082                exchange.getProperties().putAll(submitted.getProperties());
083            }
084
085            callback.done(done);
086        });
087    }
088    
089}