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 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 if (config.getPort() <= 0) { 074 throw new IllegalArgumentException("port mut be specified"); 075 } 076 077 return endpoint; 078 } 079 080 private void configureAdditionalJavaMailProperties(MailConfiguration config, Map parameters) { 081 // we cannot remove while iterating, as we will get a modification exception 082 Set toRemove = new HashSet(); 083 084 for (Object key : parameters.keySet()) { 085 if (key.toString().startsWith("mail.")) { 086 config.getAdditionalJavaMailProperties().put(key, parameters.get(key)); 087 toRemove.add(key); 088 } 089 } 090 091 for (Object key : toRemove) { 092 parameters.remove(key); 093 } 094 } 095 096 public MailConfiguration getConfiguration() { 097 return configuration; 098 } 099 100 /** 101 * Sets the Mail configuration 102 * 103 * @param configuration the configuration to use by default for endpoints 104 */ 105 public void setConfiguration(MailConfiguration configuration) { 106 this.configuration = configuration; 107 } 108 109 /** 110 * A strategy method allowing the URI destination to be translated into the actual Mail destination name 111 * (say by looking up in JNDI or something) 112 */ 113 protected String convertPathToActualDestination(String path) { 114 return path; 115 } 116 117 public ContentTypeResolver getContentTypeResolver() { 118 return contentTypeResolver; 119 } 120 121 public void setContentTypeResolver(ContentTypeResolver contentTypeResolver) { 122 this.contentTypeResolver = contentTypeResolver; 123 } 124 }