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.gae.mail;
018
019 import com.google.appengine.api.mail.MailService.Message;
020
021 import org.apache.camel.Exchange;
022 import org.apache.camel.component.gae.bind.OutboundBinding;
023
024 /**
025 * Binds the {@link Message} of the mail service to a Camel {@link Exchange}.
026 */
027 public class GMailBinding implements OutboundBinding<GMailEndpoint, Message, Void> {
028
029 /**
030 * Camel header for setting the mail message subject.
031 */
032 public static final String GMAIL_SUBJECT = "org.apache.camel.component.gae.mail.Subject";
033
034 /**
035 * Camel header for setting the mail message recipient (list).
036 */
037 public static final String GMAIL_TO = "org.apache.camel.component.gae.mail.To";
038
039 /**
040 * Reads data from <code>exchange</code> and writes it to a newly created
041 * {@link Message} instance. The <code>request</code> parameter is
042 * ignored.
043 *
044 * @param endpoint
045 * @param exchange
046 * @param request
047 * ignored.
048 * @return a newly created {@link Message} instance containing data from
049 * <code>exchange</code>.
050 */
051 public Message writeRequest(GMailEndpoint endpoint, Exchange exchange, Message request) {
052 Message message = new Message();
053 writeFrom(endpoint, exchange, message);
054 writeTo(endpoint, exchange, message);
055 writeSubject(endpoint, exchange, message);
056 writeBody(endpoint, exchange, message);
057 writeAttachments(endpoint, exchange, message);
058 return message;
059 }
060
061 /**
062 * @throws UnsupportedOperationException.
063 */
064 public Exchange readResponse(GMailEndpoint endpoint, Exchange exchange, Void response) {
065 throw new UnsupportedOperationException("gmail responses not supported");
066 }
067
068 protected void writeFrom(GMailEndpoint endpoint, Exchange exchange, Message request) {
069 request.setSender(endpoint.getSender());
070 }
071
072 protected void writeTo(GMailEndpoint endpoint, Exchange exchange, Message request) {
073 // TODO: support comma-separated list of receivers
074 String to = (String)exchange.getIn().getHeader(GMAIL_TO);
075 if (to == null) {
076 to = endpoint.getTo();
077 }
078 request.setTo(to);
079 }
080
081 protected void writeSubject(GMailEndpoint endpoint, Exchange exchange, Message request) {
082 String subject = (String)exchange.getIn().getHeader(GMAIL_SUBJECT);
083 if (subject == null) {
084 subject = endpoint.getSubject();
085 }
086 request.setSubject(subject);
087 }
088
089 protected void writeBody(GMailEndpoint endpoint, Exchange exchange, Message request) {
090 // TODO: allow message header or endpoint uri to configure character encoding
091 request.setTextBody(exchange.getIn().getBody(String.class));
092 }
093
094 protected void writeAttachments(GMailEndpoint endpoint, Exchange exchange, Message request) {
095 // TODO: support attachments
096 }
097
098 }