use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class JDBCMap method getStreamingResultSet.
@Override
public Iterator<ResultSetMap> getStreamingResultSet() throws UserException {
ResultSet rs = null;
try {
if (resultSet == null) {
rs = getDBResultSet(false);
}
if (debug) {
Access.writeToConsole(myAccess, "SQLMAP, QUERY HAS BEEN EXECUTED, RETRIEVING RESULTSET\n");
}
if (rs != null) {
int columns = 0;
ResultSetMetaData meta = null;
try {
meta = rs.getMetaData();
columns = meta.getColumnCount();
} catch (Exception e) {
throw new UserException(-1, "Error getting metadata / columns", e);
}
// Check if previous version exists, if so, close it.
if (myResultSetIterator != null) {
myResultSetIterator.close();
}
myResultSetIterator = new ResultSetIterator(rs, meta, columns);
return myResultSetIterator;
} else {
return null;
}
} catch (SQLException sqle) {
AuditLog.log("SQLMap", sqle.getMessage(), Level.SEVERE, (myAccess != null ? (myAccess != null ? myAccess.accessID : "unknown access") : "unknown access"));
throw new UserException(-1, sqle.getMessage(), sqle);
}
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class FileMap method setContent.
public void setContent(Binary b) throws UserException {
if (fileName == null) {
throw new UserException(-1, "Set filename before setting content");
}
if (b == null) {
throw new UserException(-1, "No or empty content specified");
}
this.content = b;
try {
FileOutputStream fo = new FileOutputStream(this.fileName);
b.write(fo);
fo.flush();
fo.close();
this.fileName = null;
} catch (Exception e) {
throw new UserException("Error writing file: " + this.fileName, e);
}
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class FileMap method getContent.
// TODO Should be streaming, easy rewrite
public Binary getContent() throws UserException {
try {
Binary b = new Binary(getBytes());
b.setMimeType("application/text");
return b;
} catch (Exception e) {
throw new UserException(-1, e.getMessage());
}
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class MailMap method sendMail.
private final void sendMail() throws UserException {
final ClassLoader current = Thread.currentThread().getContextClassLoader();
retries++;
try {
Thread.currentThread().setContextClassLoader(javax.mail.Session.class.getClassLoader());
String result = "";
result = text;
Session session = createSession();
javax.mail.Message msg = new MimeMessage(session);
if (sender == null || "".equals(sender)) {
throw new UserException(-1, "Error: Required sender address not set!");
}
msg.setFrom(new InternetAddress(sender));
InternetAddress[] recipients = convertToInternetAddresses(this.recipientArray);
msg.setRecipients(javax.mail.Message.RecipientType.TO, recipients);
InternetAddress[] ccrecipients = null;
if (ccArray != null) {
ccrecipients = convertToInternetAddresses(this.ccArray);
msg.setRecipients(javax.mail.Message.RecipientType.CC, ccrecipients);
}
InternetAddress[] bccrecipients = null;
if (bccArray != null) {
bccrecipients = convertToInternetAddresses(this.bccArray);
msg.setRecipients(javax.mail.Message.RecipientType.BCC, bccrecipients);
}
msg.setSubject(subject);
msg.setSentDate(new java.util.Date());
// Use stylesheet if specified.
if (!xslFile.equals("")) {
java.io.File xsl = new java.io.File(xslFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
doc.write(bos);
bos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
Document dc = XMLDocumentUtils.createDocument(bis, false);
bis.close();
result = XMLDocumentUtils.transform(dc, xsl);
}
if (attachments == null && contentType.equals("text/plain")) {
msg.setText(result);
} else if (attachments == null || attachments.isEmpty()) {
msg.setContent(result, contentType);
} else {
Multipart multipart = (relatedMultipart ? new MimeMultipart("related") : new MimeMultipart());
BodyPart textBody = new MimeBodyPart();
textBody.setContent(result, contentType);
multipart.addBodyPart(textBody);
if (attachments != null) {
for (int i = 0; i < attachments.size(); i++) {
AttachmentMapInterface am = attachments.get(i);
String file = am.getAttachFile();
String userFileName = am.getAttachFileName();
Binary content = am.getAttachFileContent();
String encoding = am.getEncoding();
MimeBodyPart bp = new MimeBodyPart();
if (file != null) {
if (userFileName == null) {
userFileName = file;
}
FileDataSource fileDatasource = new FileDataSource(file);
bp.setDataHandler(new DataHandler(fileDatasource));
} else if (content != null) {
BinaryDataSource bds = new BinaryDataSource(content, "");
DataHandler dh = new DataHandler(bds);
bp.setDataHandler(dh);
bp.setFileName(userFileName);
if (encoding != null) {
bp.setHeader("Content-Transfer-Encoding", encoding);
encoding = null;
}
}
if (relatedMultipart) {
bp.setHeader("Content-ID", "<attach-nr-" + i + ">");
}
bp.getFileName();
// iPhone headers
bp.setDisposition(am.getAttachContentDisposition());
multipart.addBodyPart(bp);
}
}
msg.setContent(multipart);
}
logger.info("Sending mail to: {}, cc: {}, bcc: {} with subject: {}", Arrays.toString(recipients), ccrecipients != null ? Arrays.toString(ccrecipients) : "[]", bccrecipients != null ? Arrays.toString(bccrecipients) : "[]", subject);
Transport.send(msg);
} catch (Exception e) {
logger.error("Exception on sending mail!", e);
if (ignoreFailures) {
AuditLog.log("MailMap", e.getMessage(), e, Level.WARNING, myAccess.accessID);
logger.warn("ingoreFailures flag is set. Ignoring the invalid e-mail address.");
failureBuffer.append(e.getMessage());
failureBuffer.append(";");
} else {
AuditLog.log("MailMap", e.getMessage(), e, Level.SEVERE, myAccess.accessID);
throw new UserException(-1, e.getMessage(), e);
}
} finally {
Thread.currentThread().setContextClassLoader(current);
}
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class MailMap method convertToInternetAddresses.
/**
* Converts each e-mail string of an array of strings to their corresponding
* InternetAddreses and returns an array with InternetAddreses
*
* If ignoreFailures is set, then no exceptions will be thrown
*/
private InternetAddress[] convertToInternetAddresses(String[] strAddreses) throws UserException {
ArrayList<InternetAddress> addresses = new ArrayList<>();
for (int i = 0; i < strAddreses.length; i++) {
try {
addresses.add(new InternetAddress(strAddreses[i]));
} catch (Exception e) {
logger.warn("Invalid e-mail address was provided : {}", strAddreses[i]);
if (ignoreFailures) {
AuditLog.log("MailMap", e.getMessage(), e, Level.WARNING, myAccess.accessID);
logger.warn("ingoreFailures flag is set. Ignoring the invalid e-mail address.");
failureBuffer.append(e.getMessage());
failureBuffer.append(";");
} else {
AuditLog.log("MailMap", e.getMessage(), e, Level.SEVERE, myAccess.accessID);
throw new UserException(-1, e.getMessage(), e);
}
}
}
InternetAddress[] addr = new InternetAddress[addresses.size()];
for (int i = 0; i < addresses.size(); i++) {
addr[i] = addresses.get(i);
}
return addr;
}
Aggregations