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.component.binding; 018 019 import java.util.Map; 020 021 import org.apache.camel.CamelContext; 022 import org.apache.camel.Endpoint; 023 import org.apache.camel.impl.DefaultComponent; 024 import org.apache.camel.spi.Binding; 025 import org.apache.camel.util.ObjectHelper; 026 027 import static org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint; 028 029 /** 030 * A composite {@link org.apache.camel.Component} which creates a {@link BindingEndpoint} from a 031 * configured {@link Binding} instance and using the optional {@link #setUriPrefix(String)} 032 * and {@link #setUriPostfix(String)} to create the underlying endpoint from the remaining URI 033 */ 034 public class BindingComponent extends DefaultComponent { 035 private Binding binding; 036 private String uriPrefix; 037 private String uriPostfix; 038 039 public BindingComponent() { 040 } 041 042 public BindingComponent(Binding binding) { 043 this.binding = binding; 044 } 045 046 public BindingComponent(Binding binding, String uriPrefix) { 047 this(binding); 048 this.uriPrefix = uriPrefix; 049 } 050 051 public BindingComponent(Binding binding, String uriPrefix, String uriPostfix) { 052 this(binding, uriPrefix); 053 this.uriPostfix = uriPostfix; 054 } 055 056 public Binding getBinding() { 057 return binding; 058 } 059 060 public void setBinding(Binding binding) { 061 this.binding = binding; 062 } 063 064 public String getUriPostfix() { 065 return uriPostfix; 066 } 067 068 public void setUriPostfix(String uriPostfix) { 069 this.uriPostfix = uriPostfix; 070 } 071 072 public String getUriPrefix() { 073 return uriPrefix; 074 } 075 076 public void setUriPrefix(String uriPrefix) { 077 this.uriPrefix = uriPrefix; 078 } 079 080 @Override 081 protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { 082 Binding bindingValue = getBinding(); 083 ObjectHelper.notNull(bindingValue, "binding"); 084 085 CamelContext camelContext = getCamelContext(); 086 String delegateURI = createDelegateURI(remaining, parameters); 087 Endpoint delegate = getMandatoryEndpoint(camelContext, delegateURI); 088 return new BindingEndpoint(uri, this, bindingValue, delegate); 089 } 090 091 protected String createDelegateURI(String remaining, Map<String, Object> parameters) { 092 return getOrEmpty(uriPrefix) + remaining + getOrEmpty(uriPostfix); 093 } 094 095 protected static String getOrEmpty(String text) { 096 return text != null ? text : ""; 097 } 098 }