001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: * $Id: EmailProviderProcessor.java 12189 2008-03-03 16:05:03Z mpreston $
023: *
024: */
025: package com.bostechcorp.cbesb.runtime.component.email.processors;
026:
027: import java.io.ByteArrayInputStream;
028: import java.io.ByteArrayOutputStream;
029: import java.io.InputStreamReader;
030: import java.io.UnsupportedEncodingException;
031: import java.util.ArrayList;
032: import java.util.List;
033: import java.util.Properties;
034: import java.util.StringTokenizer;
035:
036: import javax.jbi.messaging.MessageExchange;
037: import javax.jbi.messaging.NormalizedMessage;
038: import javax.xml.namespace.QName;
039: import javax.xml.transform.Source;
040:
041: import com.bostechcorp.cbesb.common.constant.MetadataConstants;
042: import com.bostechcorp.cbesb.common.runtime.CbesbException;
043: import com.bostechcorp.cbesb.common.runtime.DataContentException;
044: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.CbProviderProcessor;
045: import com.bostechcorp.cbesb.runtime.ccsl.lib.NormalizedMessageUtil;
046: import com.bostechcorp.cbesb.runtime.ccsl.lib.OutputWriter;
047: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
048: import com.bostechcorp.cbesb.runtime.component.email.EmailEndpoint;
049: import com.bostechcorp.cbesb.runtime.email.SMTPService;
050:
051: /**
052: *
053: */
054: public class EmailProviderProcessor extends CbProviderProcessor {
055:
056: private EmailEndpoint endpoint;
057:
058: protected static final String EMAIL_RESPONSE = "email_response";
059:
060: protected static final String EMAIL_SCRIPT_NAMESPACE = "http://cbesb.bostechcorp.com/wsdl/email/1.0";
061:
062: protected static final String STATUS = "status";
063:
064: protected static final String RESULT = "result";
065:
066: protected static final String ERROR = "error";
067:
068: public EmailProviderProcessor(EmailEndpoint endpoint) {
069: super (endpoint);
070: this .endpoint = endpoint;
071: }
072:
073: @Override
074: protected void processInMessage(QName service, QName operation,
075: NormalizedMessage in, MessageExchange exchange)
076: throws CbesbException {
077:
078: String host = endpoint.getHost();
079: String port = endpoint.getPort();
080: String user = endpoint.getUser();
081: String password = endpoint.getPassword();
082: boolean useSSL = endpoint.getUseSSL();
083: boolean useAuthentication = true;
084: boolean isHTML = false;
085: String charset = endpoint.getCharset();
086:
087: String from = getSetting(exchange,
088: MetadataConstants.EMAIL_FROM, endpoint.getFrom());
089: String to = getSetting(exchange, MetadataConstants.EMAIL_TO,
090: endpoint.getTo());
091: String cc = getSetting(exchange, MetadataConstants.EMAIL_CC,
092: endpoint.getCc());
093: String bcc = getSetting(exchange, MetadataConstants.EMAIL_BCC,
094: endpoint.getBcc());
095: String subject = getSetting(exchange,
096: MetadataConstants.EMAIL_SUBJECT, endpoint.getSubject());
097: Properties properties = System.getProperties();
098: logger.debug("SMTP Host is: [" + host + "] Port is: [" + port
099: + "]");
100: NormalizedMessageHandler nmh = new NormalizedMessageHandler(in);
101: for (int i = 0; i < nmh.getRecordCount(); i++) {
102: Source src = nmh.getRecordAtIndex(i);
103: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
104: if (charset == null || charset.trim().length() == 0) {
105: charset = (new InputStreamReader(
106: new ByteArrayInputStream(new byte[0])))
107: .getEncoding();
108: }
109: OutputWriter.processOutputStream(src, outputStream, "raw",
110: charset);
111: String body;
112: try {
113: body = outputStream.toString(charset);
114: } catch (UnsupportedEncodingException e) {
115: throw new DataContentException(
116: "The Character encoding '" + charset
117: + "' is not supported. - "
118: + e.getMessage(), e);
119: }
120: if (logger.isDebugEnabled())
121: logger.debug("Body is: " + body);
122:
123: SMTPService smtp = new SMTPService(host, port, user,
124: password, charset, useSSL, useAuthentication,
125: properties);
126:
127: if (logger.isDebugEnabled())
128: logger.debug("from is: " + from + "\n" + "to is: " + to
129: + "\n" + "cc is: " + cc + "\n" + "bcc is: "
130: + bcc + "\n" + "subject is: " + subject);
131: smtp.setDebug(false, logger, System.out);
132: smtp.setMessageAttributes(from, to, cc, bcc, subject);
133: smtp.setMessageBody(body, isHTML);
134: if (exchange
135: .getProperty(MetadataConstants.EMAIL_ATTACHMENT) != null
136: && !"".equals(exchange.getProperty(
137: MetadataConstants.EMAIL_ATTACHMENT)
138: .toString())) {
139: String attachment = exchange.getProperty(
140: MetadataConstants.EMAIL_ATTACHMENT).toString();
141: List<String> attachements = new ArrayList<String>();
142: String delimter = ",";
143: StringTokenizer st = new StringTokenizer(attachment,
144: delimter);
145: while (st.hasMoreTokens()) {
146: attachements.add(st.nextToken());
147: }
148:
149: smtp.setMessageAttachements(attachements);
150: }
151:
152: smtp.sendEmailMessage();
153:
154: // catch (MailConnectionException ex) {
155: //
156: // if (exchange instanceof RobustInOnly )
157: // endpoint.createRecoverableFault(ex, exchange);
158: // }
159:
160: }
161: }
162:
163: @Override
164: protected boolean processInOutMessage(QName service,
165: QName operation, NormalizedMessage in,
166: NormalizedMessage out, boolean optionalOut,
167: MessageExchange exchange) throws CbesbException {
168:
169: StringBuffer buffer = new StringBuffer("");
170: appendEmailResponseOpeningTag(buffer);
171: // try {
172: processInMessage(service, operation, in, exchange);
173: // } catch (Exception e) {
174: // appendElement(buffer, false, "", e.getMessage());
175: // appendEmailResponseClosingTag(buffer);
176: // setOutMessageContent(out, buffer);
177: // }
178:
179: appendElement(buffer, true, "", "");
180: appendEmailResponseClosingTag(buffer);
181: NormalizedMessageUtil.addXmlRecord(out, buffer.toString());
182: return true;
183:
184: }
185:
186: private String getSetting(MessageExchange exchange,
187: String metadataKey, String defaultValue) {
188: String metadata = (String) exchange.getProperty(metadataKey);
189: if (metadata == null) {
190: return defaultValue;
191: } else {
192: return metadata;
193: }
194: }
195:
196: private void appendEmailResponseOpeningTag(StringBuffer buffer) {
197: buffer.append("<" + EMAIL_RESPONSE + " xmlns=\""
198: + EMAIL_SCRIPT_NAMESPACE + "\">");
199: }
200:
201: private void appendEmailResponseClosingTag(StringBuffer buffer) {
202: buffer.append("</" + EMAIL_RESPONSE + ">");
203: }
204:
205: private void appendElement(StringBuffer buffer,
206: boolean elementStatus, String elementResult,
207: String elementError) {
208: buffer.append("<" + STATUS + ">");
209: if (elementStatus) {
210: buffer.append("true");
211: buffer.append("</" + STATUS + ">");
212: buffer.append("<" + RESULT + ">");
213: buffer.append(elementResult);
214: buffer.append("</" + RESULT + ">");
215: } else {
216: buffer.append("false");
217: buffer.append("</" + STATUS + ">");
218: buffer.append("<" + ERROR + ">");
219: buffer.append(elementError);
220: buffer.append("</" + ERROR + ">");
221: }
222: buffer.append("\r\n");
223: }
224: }
|