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 */ 017 package org.apache.camel.model; 018 019 import javax.xml.bind.annotation.XmlAccessType; 020 import javax.xml.bind.annotation.XmlAccessorType; 021 import javax.xml.bind.annotation.XmlAttribute; 022 import javax.xml.bind.annotation.XmlRootElement; 023 import javax.xml.bind.annotation.XmlTransient; 024 025 import org.apache.camel.Endpoint; 026 import org.apache.camel.Processor; 027 import org.apache.camel.processor.Enricher; 028 import org.apache.camel.processor.aggregate.AggregationStrategy; 029 import org.apache.camel.spi.RouteContext; 030 import org.apache.camel.util.ObjectHelper; 031 032 /** 033 * Represents an XML <enrich/> element 034 * 035 * @see Enricher 036 */ 037 @XmlRootElement(name = "enrich") 038 @XmlAccessorType(XmlAccessType.FIELD) 039 public class EnrichDefinition extends NoOutputDefinition<EnrichDefinition> { 040 @XmlAttribute(name = "uri") 041 private String resourceUri; 042 // TODO: For Camel 3.0 we should remove this ref attribute as you can do that in the uri, by prefixing with ref: 043 @XmlAttribute(name = "ref") 044 private String resourceRef; 045 @XmlAttribute(name = "strategyRef") 046 private String aggregationStrategyRef; 047 @XmlTransient 048 private AggregationStrategy aggregationStrategy; 049 050 public EnrichDefinition() { 051 this(null, null); 052 } 053 054 public EnrichDefinition(String resourceUri) { 055 this(null, resourceUri); 056 } 057 058 public EnrichDefinition(AggregationStrategy aggregationStrategy, String resourceUri) { 059 this.aggregationStrategy = aggregationStrategy; 060 this.resourceUri = resourceUri; 061 } 062 063 @Override 064 public String toString() { 065 return "Enrich[" + description() + " " + aggregationStrategy + "]"; 066 } 067 068 protected String description() { 069 return FromDefinition.description(resourceUri, resourceRef, (Endpoint) null); 070 } 071 072 @Override 073 public String getLabel() { 074 return "enrich[" + description() + "]"; 075 } 076 077 @Override 078 public String getShortName() { 079 return "enrich"; 080 } 081 082 @Override 083 public Processor createProcessor(RouteContext routeContext) throws Exception { 084 if (ObjectHelper.isEmpty(resourceUri) && ObjectHelper.isEmpty(resourceRef)) { 085 throw new IllegalArgumentException("Either uri or ref must be provided for resource endpoint"); 086 } 087 088 // lookup endpoint 089 Endpoint endpoint; 090 if (resourceUri != null) { 091 endpoint = routeContext.resolveEndpoint(resourceUri); 092 } else { 093 endpoint = routeContext.resolveEndpoint(null, resourceRef); 094 } 095 096 Enricher enricher = new Enricher(null, endpoint.createProducer()); 097 if (aggregationStrategyRef != null) { 098 aggregationStrategy = routeContext.mandatoryLookup(aggregationStrategyRef, AggregationStrategy.class); 099 } 100 if (aggregationStrategy == null) { 101 enricher.setDefaultAggregationStrategy(); 102 } else { 103 enricher.setAggregationStrategy(aggregationStrategy); 104 } 105 return enricher; 106 } 107 108 public String getResourceUri() { 109 return resourceUri; 110 } 111 112 public void setResourceUri(String resourceUri) { 113 this.resourceUri = resourceUri; 114 } 115 116 public String getResourceRef() { 117 return resourceRef; 118 } 119 120 public void setResourceRef(String resourceRef) { 121 this.resourceRef = resourceRef; 122 } 123 124 public String getAggregationStrategyRef() { 125 return aggregationStrategyRef; 126 } 127 128 public void setAggregationStrategyRef(String aggregationStrategyRef) { 129 this.aggregationStrategyRef = aggregationStrategyRef; 130 } 131 132 public AggregationStrategy getAggregationStrategy() { 133 return aggregationStrategy; 134 } 135 136 public void setAggregationStrategy(AggregationStrategy aggregationStrategy) { 137 this.aggregationStrategy = aggregationStrategy; 138 } 139 }