use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class MailMapAlternative method sendMail.
private final void sendMail() throws UserException {
retries++;
try {
String result = "";
result = text;
Properties props = System.getProperties();
props.put("mail.smtp.host", mailServer);
Session session = Session.getInstance(props);
javax.mail.Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(sender));
InternetAddress[] addresses = new InternetAddress[this.recipientArray.length];
for (int i = 0; i < this.recipientArray.length; i++) {
addresses[i] = new InternetAddress(this.recipientArray[i]);
}
msg.setRecipients(javax.mail.Message.RecipientType.TO, addresses);
if (ccArray != null) {
InternetAddress[] extra = new InternetAddress[this.ccArray.length];
for (int i = 0; i < this.ccArray.length; i++) {
extra[i] = new InternetAddress(this.ccArray[i]);
}
msg.setRecipients(javax.mail.Message.RecipientType.CC, extra);
}
if (bccArray != null) {
InternetAddress[] extra = new InternetAddress[this.bccArray.length];
for (int i = 0; i < this.bccArray.length; i++) {
extra[i] = new InternetAddress(this.bccArray[i]);
}
msg.setRecipients(javax.mail.Message.RecipientType.BCC, extra);
}
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 navajoDoc = XMLDocumentUtils.createDocument(bis, false);
bis.close();
result = XMLDocumentUtils.transform(navajoDoc, xsl);
}
if (attachments == null && contentType.equals("text/plain")) {
msg.setText(result);
} else {
Multipart multipart = new MimeMultipart("mixed");
BodyPart textBody = new MimeBodyPart();
textBody.setContent(result, contentType);
Multipart related = new MimeMultipart("related");
related.addBodyPart(textBody);
if (bodyparts != null && !bodyparts.isEmpty()) {
// Put related bodyparts in related.
for (int i = 0; i < bodyparts.size(); i++) {
AttachmentMapInterface am = bodyparts.get(i);
String file = am.getAttachFile();
String userFileName = am.getAttachFileName();
Binary content = am.getAttachFileContent();
String encoding = am.getEncoding();
String attachContentType = am.getAttachContentType();
MimeBodyPart bp = new MimeBodyPart();
logger.debug("Embedding: {}", userFileName);
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);
if (encoding != null) {
bp.setHeader("Content-Transfer-Encoding", encoding);
encoding = null;
}
}
bp.setFileName(userFileName);
if (attachContentType != null) {
bp.setHeader("Content-Type", attachContentType);
}
bp.setHeader("Content-ID", "<attach-nr-" + i + ">");
related.addBodyPart(bp);
}
}
MimeBodyPart bop = new MimeBodyPart();
bop.setContent(related);
multipart.addBodyPart(bop);
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();
String attachContentType = am.getAttachContentType();
String attachContentDisposition = am.getAttachContentDisposition();
MimeBodyPart bp = new MimeBodyPart();
logger.debug("Attaching: {}", userFileName);
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);
if (encoding != null) {
bp.setHeader("Content-Transfer-Encoding", encoding);
}
}
bp.setFileName(userFileName);
if (attachContentType != null) {
bp.setHeader("Content-Type", attachContentType);
}
bp.setDisposition(attachContentDisposition);
multipart.addBodyPart(bp);
}
}
msg.setContent(multipart);
}
logger.info("Sending mail to {} cc: {} bcc: {} with subject: {}", recipients, cc, bcc, subject);
Transport.send(msg);
} catch (Exception e) {
if (ignoreFailures) {
AuditLog.log("MailMap", e.getMessage(), e, Level.WARNING, myAccess.accessID);
failure = e.getMessage();
} else {
AuditLog.log("MailMap", e.getMessage(), e, Level.SEVERE, myAccess.accessID);
throw new UserException(-1, e.getMessage());
}
}
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class NavajoMap method getSelections.
public OptionMap[] getSelections() throws UserException {
if (selectionPointer == null) {
throw new UserException(-1, "Set selectionPointer first.");
}
Property p = getPropertyObject(selectionPointer);
if (!p.getType().equals(Property.SELECTION_PROPERTY)) {
throw new UserException(-1, "selections only supported for selection properties");
}
List<Selection> all = p.getAllSelections();
OptionMap[] om = new OptionMap[all.size()];
for (int i = 0; i < all.size(); i++) {
Selection s = all.get(i);
om[i] = new OptionMap();
om[i].setOptionName(s.getName());
om[i].setOptionValue(s.getValue());
om[i].setOptionSelected(s.isSelected());
}
return om;
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class NavajoMap method getMessage.
public Message getMessage(String fullName) throws UserException {
waitForResult();
Message msg = null;
if (msgPointer != null)
msg = msgPointer.getMessage(fullName);
else
msg = inDoc.getMessage(fullName);
if (msg == null)
throw new UserException(-1, "Message " + fullName + " does not exists in response document");
return msg;
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class NavajoMap method setAppendParms.
/**
* @param b
* @throws UserException
*
* if messageOffset is '/', the messages of the received Doc will be appended to the root param block or to the current param message.
*/
public final void setAppendParms(String messageOffset) throws UserException {
waitForResult();
try {
Message parm = (access.getCompiledScript().getCurrentParamMsg() == null ? access.getInDoc().getMessage("__parms__") : access.getCompiledScript().getCurrentParamMsg());
List<Message> list = null;
// If append message equals '/'.
if (messageOffset.equals(Navajo.MESSAGE_SEPARATOR)) {
list = inDoc.getAllMessages();
} else if (inDoc.getMessage(messageOffset) == null) {
return;
} else if (inDoc.getMessage(messageOffset).getType().equals(Message.MSG_TYPE_ARRAY)) {
list = new ArrayList<>();
list.add(inDoc.getMessage(messageOffset));
} else {
list = inDoc.getMessages(messageOffset);
}
for (int i = 0; i < list.size(); i++) {
Message inMsg = list.get(i);
// Clone message and append it to currentMsg if it exists, else
// directly under currentDoc.
// currentDoc.importMessage(inMsg)
Message clone = inDoc.copyMessage(inMsg, parm.getRootDoc());
parm.addMessage(clone, true);
}
} catch (NavajoException ne) {
throw new UserException(-1, ne.getMessage());
}
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class NavajoMap method setSelections.
public void setSelections(OptionMap[] selections) throws UserException {
if (currentProperty == null) {
throw new UserException(-1, "Set property name first.");
}
currentProperty.setType(Property.SELECTION_PROPERTY);
currentProperty.clearValue();
currentProperty.clearSelections();
int selected = 0;
for (int i = 0; i < selections.length; i++) {
Selection s = NavajoFactory.getInstance().createSelection(currentProperty.getRootDoc(), selections[i].getOptionName(), selections[i].getOptionValue(), selections[i].getOptionSelected());
currentProperty.addSelection(s);
if (selections[i].getOptionSelected()) {
selected++;
}
}
if (selected > 1) {
currentProperty.setCardinality("+");
} else {
currentProperty.setCardinality("1");
}
addProperty(currentProperty);
}
Aggregations