use of org.pentaho.di.job.entries.getpop.MailConnection in project pentaho-kettle by pentaho.
the class ParseMailInputTest method beforeTest.
@Before
public void beforeTest() throws MessagingException, IOException, KettleException {
message = Mockito.mock(Message.class);
MailConnection conn = mock(MailConnection.class);
when(conn.getMessageBody(any(Message.class))).thenReturn(MSG_BODY);
when(conn.getFolderName()).thenReturn(FLD_NAME);
when(conn.getAttachedFilesCount(any(Message.class), any(Pattern.class))).thenReturn(ATTCH_COUNT);
when(conn.getMessageBodyContentType(any(Message.class))).thenReturn(CNTNT_TYPE);
data = mock(MailInputData.class);
data.mailConn = conn;
mailInput = new MailInput(stepMockHelper.stepMeta, data, 0, stepMockHelper.transMeta, stepMockHelper.trans);
Address addrFrom1 = mock(Address.class);
when(addrFrom1.toString()).thenReturn(FROM1);
Address addrFrom2 = mock(Address.class);
when(addrFrom2.toString()).thenReturn(FROM2);
Address addrRep1 = mock(Address.class);
when(addrRep1.toString()).thenReturn(REP1);
Address addrRep2 = mock(Address.class);
when(addrRep2.toString()).thenReturn(REP2);
Address allRec1 = mock(Address.class);
when(allRec1.toString()).thenReturn(REC1);
Address allRec2 = mock(Address.class);
when(allRec2.toString()).thenReturn(REC2);
Address[] adrFr = { addrFrom1, addrFrom2 };
Address[] adrRep = { addrRep1, addrRep2 };
Address[] adrRecip = { allRec1, allRec2 };
message = Mockito.mock(Message.class);
when(message.getMessageNumber()).thenReturn(MSG_NUMB);
when(message.getSubject()).thenReturn(SUBJ);
when(message.getFrom()).thenReturn(adrFr);
when(message.getReplyTo()).thenReturn(adrRep);
when(message.getAllRecipients()).thenReturn(adrRecip);
when(message.getDescription()).thenReturn(DESC);
when(message.getReceivedDate()).thenReturn(DATE1);
when(message.getSentDate()).thenReturn(DATE2);
when(message.getContentType()).thenReturn(CNTNT_TYPE_EMAIL);
when(message.getSize()).thenReturn(CNTNT_SIZE);
Header ex1 = new Header(HDR_EX1, HDR_EX1V);
Header ex2 = new Header(HDR_EX2, HDR_EX2V);
// for fixed [PDI-6532]
when(message.getMatchingHeaders(AdditionalMatchers.aryEq(new String[] { HDR_EX1 }))).thenReturn(getEnum(new Header[] { ex1 }));
when(message.getMatchingHeaders(AdditionalMatchers.aryEq(new String[] { HDR_EX2 }))).thenReturn(getEnum(new Header[] { ex2 }));
when(message.getMatchingHeaders(AdditionalMatchers.aryEq(new String[] { HDR_EX1, HDR_EX2 }))).thenReturn(getEnum(new Header[] { ex1, ex2 }));
// for previous implementation
when(message.getHeader(eq(HDR_EX1))).thenReturn(new String[] { ex1.getValue() });
when(message.getHeader(eq(HDR_EX2))).thenReturn(new String[] { ex2.getValue() });
}
use of org.pentaho.di.job.entries.getpop.MailConnection in project pentaho-kettle by pentaho.
the class MailInputDialog method connect.
private boolean connect() {
String errordescription = null;
boolean retval = false;
if (mailConn != null && mailConn.isConnected()) {
retval = mailConn.isConnected();
}
if (!retval) {
String realserver = transMeta.environmentSubstitute(wServerName.getText());
String realuser = transMeta.environmentSubstitute(wUserName.getText());
String realpass = Utils.resolvePassword(transMeta, wPassword.getText());
String realProxyUsername = transMeta.environmentSubstitute(wProxyUsername.getText());
int realport = Const.toInt(transMeta.environmentSubstitute(wPort.getText()), -1);
try {
mailConn = new MailConnection(LogChannel.UI, MailConnectionMeta.getProtocolFromString(wProtocol.getText(), MailConnectionMeta.PROTOCOL_IMAP), realserver, realport, realuser, realpass, wUseSSL.getSelection(), wUseProxy.getSelection(), realProxyUsername);
mailConn.connect();
retval = true;
} catch (Exception e) {
errordescription = e.getMessage();
}
}
if (!retval) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(BaseMessages.getString(PKG, "MailInput.Connected.NOK.ConnectionBad", wServerName.getText()) + Const.CR + Const.NVL(errordescription, ""));
mb.setText(BaseMessages.getString(PKG, "MailInput.Connected.Title.Bad"));
mb.open();
}
return (mailConn.isConnected());
}
use of org.pentaho.di.job.entries.getpop.MailConnection in project pentaho-kettle by pentaho.
the class JobEntryGetPOPDialog method connect.
private boolean connect() {
String errordescription = null;
boolean retval = false;
if (mailConn != null && mailConn.isConnected()) {
retval = mailConn.isConnected();
}
if (!retval) {
String realserver = jobMeta.environmentSubstitute(wServerName.getText());
String realuser = jobMeta.environmentSubstitute(wUserName.getText());
String realpass = jobEntry.getRealPassword(jobMeta.environmentSubstitute(wPassword.getText()));
int realport = Const.toInt(jobMeta.environmentSubstitute(wPort.getText()), -1);
String realproxyuser = jobMeta.environmentSubstitute(wProxyUsername.getText());
try {
mailConn = new MailConnection(LogChannel.UI, MailConnectionMeta.getProtocolFromString(wProtocol.getText(), MailConnectionMeta.PROTOCOL_IMAP), realserver, realport, realuser, realpass, wUseSSL.getSelection(), wUseProxy.getSelection(), realproxyuser);
mailConn.connect();
retval = true;
} catch (Exception e) {
errordescription = e.getMessage();
}
}
if (!retval) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(BaseMessages.getString(PKG, "JobGetPOP.Connected.NOK.ConnectionBad", wServerName.getText()) + Const.CR + Const.NVL(errordescription, ""));
mb.setText(BaseMessages.getString(PKG, "JobGetPOP.Connected.Title.Bad"));
mb.open();
}
return (mailConn.isConnected());
}
use of org.pentaho.di.job.entries.getpop.MailConnection in project pentaho-kettle by pentaho.
the class MailInput method init.
public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
meta = (MailInputMeta) smi;
data = (MailInputData) sdi;
if (!super.init(smi, sdi)) {
return false;
}
if (!meta.isDynamicFolder()) {
try {
// Create the output row meta-data
data.outputRowMeta = new RowMeta();
// get the
meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
// metadata
// populated
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "MailInput.ErrorInit", e.toString()));
logError(Const.getStackTracker(e));
return false;
}
}
data.usePOP = meta.getProtocol().equals(MailConnectionMeta.PROTOCOL_STRING_POP3);
String realserver = environmentSubstitute(meta.getServerName());
if (meta.getProtocol().equals(MailConnectionMeta.PROTOCOL_STRING_MBOX) && StringUtils.startsWith(realserver, "file://")) {
realserver = StringUtils.remove(realserver, "file://");
}
String realusername = environmentSubstitute(meta.getUserName());
String realpassword = Utils.resolvePassword(variables, meta.getPassword());
int realport = Const.toInt(environmentSubstitute(meta.getPort()), -1);
String realProxyUsername = environmentSubstitute(meta.getProxyUsername());
if (!meta.isDynamicFolder()) {
// Limit field has absolute priority
String reallimitrow = environmentSubstitute(meta.getRowLimit());
int limit = Const.toInt(reallimitrow, 0);
// Limit field has absolute priority
if (limit == 0) {
limit = getReadFirst(meta.getProtocol());
}
data.rowlimit = limit;
}
Date beginDate = null;
Date endDate = null;
SimpleDateFormat df = new SimpleDateFormat(MailInputMeta.DATE_PATTERN);
// Received Date
try {
switch(meta.getConditionOnReceivedDate()) {
case MailConnectionMeta.CONDITION_DATE_EQUAL:
case MailConnectionMeta.CONDITION_DATE_GREATER:
case MailConnectionMeta.CONDITION_DATE_SMALLER:
String realBeginDate = environmentSubstitute(meta.getReceivedDate1());
if (Utils.isEmpty(realBeginDate)) {
throw new KettleException(BaseMessages.getString(PKG, "MailInput.Error.ReceivedDateSearchTermEmpty"));
}
beginDate = df.parse(realBeginDate);
break;
case MailConnectionMeta.CONDITION_DATE_BETWEEN:
realBeginDate = environmentSubstitute(meta.getReceivedDate1());
if (Utils.isEmpty(realBeginDate)) {
throw new KettleException(BaseMessages.getString(PKG, "MailInput.Error.ReceivedDatesSearchTermEmpty"));
}
beginDate = df.parse(realBeginDate);
String realEndDate = environmentSubstitute(meta.getReceivedDate2());
if (Utils.isEmpty(realEndDate)) {
throw new KettleException(BaseMessages.getString(PKG, "MailInput.Error.ReceivedDatesSearchTermEmpty"));
}
endDate = df.parse(realEndDate);
break;
default:
break;
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "MailInput.Error.SettingSearchTerms", e.getMessage()));
setErrors(1);
stopAll();
}
try {
// create a mail connection object
data.mailConn = new MailConnection(log, MailConnectionMeta.getProtocolFromString(meta.getProtocol(), MailConnectionMeta.PROTOCOL_IMAP), realserver, realport, realusername, realpassword, meta.isUseSSL(), meta.isUseProxy(), realProxyUsername);
// connect
data.mailConn.connect();
// Need to apply search filters?
applySearch(beginDate, endDate);
if (!meta.isDynamicFolder()) {
// pass static folder name
String realIMAPFolder = environmentSubstitute(meta.getIMAPFolder());
// return folders list
// including sub folders if necessary
data.folders = getFolders(realIMAPFolder);
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "MailInput.Error.OpeningConnection", e.getMessage()));
setErrors(1);
stopAll();
}
data.nrFields = meta.getInputFields() != null ? meta.getInputFields().length : 0;
return true;
}
Aggregations