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.impl; 018 019import java.net.URI; 020import java.net.URISyntaxException; 021import java.util.SortedMap; 022import java.util.TreeMap; 023 024import org.apache.camel.CamelContext; 025import org.apache.camel.Endpoint; 026import org.apache.camel.EndpointConfiguration; 027import org.apache.camel.RuntimeCamelException; 028import org.apache.camel.spi.UriParam; 029import org.apache.camel.spi.UriParams; 030import org.apache.camel.util.IntrospectionSupport; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034/** 035 * Implements {@link EndpointConfiguration} for Endpoint implementations 036 * which are annotated with {@link org.apache.camel.spi.UriEndpoint} to use the {@link UriParam} and {@link UriParams} annotations 037 * to denote its parameters which can be specified via URI query parameters. 038 */ 039@Deprecated 040public class UriEndpointConfiguration implements EndpointConfiguration { 041 private static final Logger LOG = LoggerFactory.getLogger(UriEndpointConfiguration.class); 042 043 private final CamelContext camelContext; 044 private final Endpoint endpoint; 045 private String uriText; 046 private URI uri; 047 private SortedMap<String, ParameterConfiguration> propertyMap; 048 049 public UriEndpointConfiguration(CamelContext camelContext, Endpoint endpoint, String uriText) { 050 this.camelContext = camelContext; 051 this.endpoint = endpoint; 052 this.uriText = uriText; 053 } 054 055 @Override 056 public URI getURI() { 057 if (uri == null) { 058 // lazily create the URI which may fail as not all camel uriText are valid URI text 059 try { 060 uri = new URI(uriText); 061 } catch (URISyntaxException e) { 062 throw new RuntimeCamelException(e); 063 } 064 } 065 return uri; 066 } 067 068 public void setURI(URI uri) { 069 this.uriText = null; 070 this.uri = uri; 071 } 072 073 @Override 074 public <T> T getParameter(String name) throws RuntimeCamelException { 075 ParameterConfiguration config = getPropertyConfiguration(name); 076 077 // lets try get the property regardless of if this maps to a valid property name 078 // then if the introspection fails we will get a valid error otherwise 079 // lets raise a warning afterwards that we should update the metadata on the endpoint class 080 try { 081 @SuppressWarnings("unchecked") 082 T answer = (T)IntrospectionSupport.getProperty(endpoint, name); 083 if (config == null) { 084 warnMissingUriParamOnProperty(name); 085 } 086 return answer; 087 } catch (Exception e) { 088 throw new RuntimeCamelException( 089 "Failed to get property '" + name + "' on " + endpoint + " due " + e.getMessage(), e); 090 } 091 } 092 093 protected void warnMissingUriParamOnProperty(String name) { 094 LOG.warn("Using property " + name + " on endpoint " + getEndpointClass().getName() 095 + " which does not have a @UriParam annotation! " 096 + "Please add the @UriParam annotation to the " + name + " field"); 097 } 098 099 @Override 100 public <T> void setParameter(String name, T value) throws RuntimeCamelException { 101 ParameterConfiguration config = getPropertyConfiguration(name); 102 103 // lets try set the property regardless of if this maps to a valid property name 104 // then if the injection fails we will get a valid error otherwise 105 // lets raise a warning afterwards that we should update the metadata on the endpoint class 106 try { 107 IntrospectionSupport.setProperty(endpoint, name, value); 108 } catch (Exception e) { 109 throw new RuntimeCamelException( 110 "Failed to set property '" + name + "' on " + endpoint + " to value " + value + " due " 111 + e.getMessage(), e); 112 } 113 if (config == null) { 114 warnMissingUriParamOnProperty(name); 115 } 116 } 117 118 @Override 119 public String toUriString(UriFormat format) { 120 // TODO 121 return null; 122 } 123 124 public CamelContext getCamelContext() { 125 return camelContext; 126 } 127 128 public Class<? extends Endpoint> getEndpointClass() { 129 return endpoint.getClass(); 130 } 131 132 /** 133 * Returns the property configuration for the given property name or null if it does not exist 134 */ 135 public ParameterConfiguration getPropertyConfiguration(String name) { 136 return getPropertyConfigurationMap().get(name); 137 } 138 139 /** 140 * Returns the sorted map of all the property names to their {@link ParameterConfiguration} objects 141 */ 142 public SortedMap<String, ParameterConfiguration> getPropertyConfigurationMap() { 143 if (propertyMap == null) { 144 propertyMap = UriEndpointComponent.createParameterConfigurationMap(getEndpointClass()); 145 } 146 return new TreeMap<>(propertyMap); 147 } 148 149}