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