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 sender.
031         */
032        public static final String GMAIL_SENDER = "CamelGmailSender";
033        
034        /**
035         * Camel header for setting the mail message subject.
036         */
037        public static final String GMAIL_SUBJECT = "CamelGmailSubject";
038    
039        /**
040         * Camel header for setting the mail message to-recipient (single recipient
041         * or comma-separated list).
042         */
043        public static final String GMAIL_TO = "CamelGmailTo";
044        
045        /**
046         * Camel header for setting the mail message cc-recipient (single recipient
047         * or comma-separated list).
048         */
049        public static final String GMAIL_CC = "CamelGmailCc";
050        
051        /**
052         * Camel header for setting the mail message bcc-recipient (single recipient
053         * or comma-separated list).
054         */
055        public static final String GMAIL_BCC = "CamelGmailBcc";
056        
057        /**
058         * Reads data from <code>exchange</code> and writes it to a newly created
059         * {@link Message} instance. The <code>request</code> parameter is
060         * ignored.
061         * 
062         * @param endpoint
063         * @param exchange
064         * @param request
065         *            ignored.
066         * @return a newly created {@link Message} instance containing data from
067         *         <code>exchange</code>.
068         */
069        public Message writeRequest(GMailEndpoint endpoint, Exchange exchange, Message request) {
070            Message message = new Message();
071            writeFrom(endpoint, exchange, message);
072            writeTo(endpoint, exchange, message);
073            writeCc(endpoint, exchange, message);
074            writeBcc(endpoint, exchange, message);
075            writeSubject(endpoint, exchange, message);
076            writeBody(endpoint, exchange, message);
077            writeAttachments(endpoint, exchange, message);
078            return message;
079        }
080    
081        public Exchange readResponse(GMailEndpoint endpoint, Exchange exchange, Void response) {
082            throw new UnsupportedOperationException("gmail responses not supported");
083        }
084    
085        protected void writeFrom(GMailEndpoint endpoint, Exchange exchange, Message request) {
086            String sender = (String)exchange.getIn().getHeader(GMAIL_SENDER);
087            if (sender == null) {
088                sender = endpoint.getSender();
089            }
090            request.setSender(sender);
091        }
092        
093        protected void writeTo(GMailEndpoint endpoint, Exchange exchange, Message request) {
094            String to = (String)exchange.getIn().getHeader(GMAIL_TO);
095            if (to == null) {
096                to = endpoint.getTo();
097            }
098            request.setTo(to.split(","));
099        }
100        
101        protected void writeCc(GMailEndpoint endpoint, Exchange exchange, Message request) {
102            String cc = (String)exchange.getIn().getHeader(GMAIL_CC);
103            if (cc == null) {
104                cc = endpoint.getCc();
105            }
106            if (cc != null) {
107                request.setCc(cc.split(","));
108            }
109        }
110        
111        protected void writeBcc(GMailEndpoint endpoint, Exchange exchange, Message request) {
112            String bcc = (String)exchange.getIn().getHeader(GMAIL_BCC);
113            if (bcc == null) {
114                bcc = endpoint.getBcc();
115            }
116            if (bcc != null) {
117                request.setBcc(bcc.split(","));
118            }
119        }
120        
121        protected void writeSubject(GMailEndpoint endpoint, Exchange exchange, Message request) {
122            String subject = (String)exchange.getIn().getHeader(GMAIL_SUBJECT);
123            if (subject == null) {
124                subject = endpoint.getSubject();
125            }
126            request.setSubject(subject);
127        }
128        
129        protected void writeBody(GMailEndpoint endpoint, Exchange exchange, Message request) {
130            // TODO: allow message header or endpoint uri to configure character encoding
131            request.setTextBody(exchange.getIn().getBody(String.class));
132        }
133        
134        protected void writeAttachments(GMailEndpoint endpoint, Exchange exchange, Message request) {
135            // TODO: support attachments
136        }
137        
138    }