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        /**
082         * @throws UnsupportedOperationException.
083         */
084        public Exchange readResponse(GMailEndpoint endpoint, Exchange exchange, Void response) {
085            throw new UnsupportedOperationException("gmail responses not supported");
086        }
087    
088        protected void writeFrom(GMailEndpoint endpoint, Exchange exchange, Message request) {
089            String sender = (String)exchange.getIn().getHeader(GMAIL_SENDER);
090            if (sender == null) {
091                sender = endpoint.getSender();
092            }
093            request.setSender(sender);
094        }
095        
096        protected void writeTo(GMailEndpoint endpoint, Exchange exchange, Message request) {
097            String to = (String)exchange.getIn().getHeader(GMAIL_TO);
098            if (to == null) {
099                to = endpoint.getTo();
100            }
101            request.setTo(to.split(","));
102        }
103        
104        protected void writeCc(GMailEndpoint endpoint, Exchange exchange, Message request) {
105            String cc = (String)exchange.getIn().getHeader(GMAIL_CC);
106            if (cc == null) {
107                cc = endpoint.getCc();
108            }
109            if (cc != null) {
110                request.setCc(cc.split(","));
111            }
112        }
113        
114        protected void writeBcc(GMailEndpoint endpoint, Exchange exchange, Message request) {
115            String bcc = (String)exchange.getIn().getHeader(GMAIL_BCC);
116            if (bcc == null) {
117                bcc = endpoint.getBcc();
118            }
119            if (bcc != null) {
120                request.setBcc(bcc.split(","));
121            }
122        }
123        
124        protected void writeSubject(GMailEndpoint endpoint, Exchange exchange, Message request) {
125            String subject = (String)exchange.getIn().getHeader(GMAIL_SUBJECT);
126            if (subject == null) {
127                subject = endpoint.getSubject();
128            }
129            request.setSubject(subject);
130        }
131        
132        protected void writeBody(GMailEndpoint endpoint, Exchange exchange, Message request) {
133            // TODO: allow message header or endpoint uri to configure character encoding
134            request.setTextBody(exchange.getIn().getBody(String.class));
135        }
136        
137        protected void writeAttachments(GMailEndpoint endpoint, Exchange exchange, Message request) {
138            // TODO: support attachments
139        }
140        
141    }