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.builder; 018 019import java.util.Map; 020import java.util.function.BiConsumer; 021import java.util.function.Consumer; 022 023import org.apache.camel.Exchange; 024import org.apache.camel.Message; 025import org.apache.camel.Processor; 026 027public class ProcessClause<T> implements Processor { 028 private final T parent; 029 private Processor processor; 030 031 public ProcessClause(T parent) { 032 this.parent = parent; 033 } 034 035 @Override 036 public void process(Exchange exchange) throws Exception { 037 if (processor != null) { 038 processor.process(exchange); 039 } 040 } 041 042 // ******************************* 043 // Exchange 044 // ******************************* 045 046 /** 047 * TODO: document 048 * 049 * Note: this is experimental and subject to changes in future releases. 050 */ 051 public T exchange(final Consumer<Exchange> consumer) { 052 processor = consumer::accept; 053 return parent; 054 } 055 056 057 // ******************************* 058 // Message 059 // ******************************* 060 061 /** 062 * TODO: document 063 * 064 * Note: this is experimental and subject to changes in future releases. 065 */ 066 public T message(final Consumer<Message> consumer) { 067 processor = e -> consumer.accept(e.getIn()); 068 return parent; 069 } 070 071 // ******************************* 072 // Body 073 // ******************************* 074 075 /** 076 * TODO: document 077 * 078 * Note: this is experimental and subject to changes in future releases. 079 */ 080 public T body(final Consumer<Object> consumer) { 081 processor = e -> consumer.accept(e.getIn().getBody()); 082 return parent; 083 } 084 085 /** 086 * TODO: document 087 * 088 * Note: this is experimental and subject to changes in future releases. 089 */ 090 public <B> T body(Class<B> type, final Consumer<B> consumer) { 091 processor = e -> consumer.accept(e.getIn().getBody(type)); 092 return parent; 093 } 094 095 /** 096 * TODO: document 097 * 098 * Note: this is experimental and subject to changes in future releases. 099 */ 100 public T body(final BiConsumer<Object, Map<String, Object>> consumer) { 101 processor = e -> consumer.accept( 102 e.getIn().getBody(), 103 e.getIn().getHeaders() 104 ); 105 return parent; 106 } 107 108 /** 109 * TODO: document 110 * 111 * Note: this is experimental and subject to changes in future releases. 112 */ 113 public <B> T body(Class<B> type, final BiConsumer<B, Map<String, Object>> consumer) { 114 processor = e -> consumer.accept( 115 e.getIn().getBody(type), 116 e.getIn().getHeaders() 117 ); 118 return parent; 119 } 120}