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.mail;
018
019 import java.net.URI;
020 import java.util.HashSet;
021 import java.util.Map;
022 import java.util.Set;
023
024 import org.apache.camel.CamelContext;
025 import org.apache.camel.Endpoint;
026 import org.apache.camel.impl.DefaultComponent;
027 import org.apache.camel.util.ObjectHelper;
028
029 /**
030 * Component for JavaMail.
031 *
032 * @version $Revision:520964 $
033 */
034 public class MailComponent extends DefaultComponent {
035 private MailConfiguration configuration;
036 private ContentTypeResolver contentTypeResolver;
037
038 public MailComponent() {
039 this.configuration = new MailConfiguration();
040 }
041
042 public MailComponent(MailConfiguration configuration) {
043 this.configuration = configuration;
044 }
045
046 public MailComponent(CamelContext context) {
047 super(context);
048 this.configuration = new MailConfiguration();
049 }
050
051 @Override
052 protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
053 URI url = new URI(uri);
054 if ("nntp".equalsIgnoreCase(url.getScheme())) {
055 throw new UnsupportedOperationException("nntp protocol is not supported");
056 }
057
058 // must use copy as each endpoint can have different options
059 ObjectHelper.notNull(configuration, "configuration");
060 MailConfiguration config = configuration.copy();
061
062 // only configure if we have a url with a known protocol
063 config.configure(url);
064 configureAdditionalJavaMailProperties(config, parameters);
065
066 MailEndpoint endpoint = new MailEndpoint(uri, this, config);
067 endpoint.setContentTypeResolver(contentTypeResolver);
068 setProperties(endpoint.getConfiguration(), parameters);
069
070 // sanity check that we know the mail server
071 ObjectHelper.notEmpty(config.getHost(), "host");
072 ObjectHelper.notEmpty(config.getProtocol(), "protocol");
073
074 return endpoint;
075 }
076
077 @SuppressWarnings("unchecked")
078 private void configureAdditionalJavaMailProperties(MailConfiguration config, Map parameters) {
079 // we cannot remove while iterating, as we will get a modification exception
080 Set toRemove = new HashSet();
081
082 for (Object key : parameters.keySet()) {
083 if (key.toString().startsWith("mail.")) {
084 config.getAdditionalJavaMailProperties().put(key, parameters.get(key));
085 toRemove.add(key);
086 }
087 }
088
089 for (Object key : toRemove) {
090 parameters.remove(key);
091 }
092 }
093
094 public MailConfiguration getConfiguration() {
095 return configuration;
096 }
097
098 /**
099 * Sets the Mail configuration
100 *
101 * @param configuration the configuration to use by default for endpoints
102 */
103 public void setConfiguration(MailConfiguration configuration) {
104 this.configuration = configuration;
105 }
106
107 public ContentTypeResolver getContentTypeResolver() {
108 return contentTypeResolver;
109 }
110
111 public void setContentTypeResolver(ContentTypeResolver contentTypeResolver) {
112 this.contentTypeResolver = contentTypeResolver;
113 }
114 }