use of com.axway.ats.action.objects.model.PackageException in project ats-framework by Axway.
the class MimePackage method getRegularPartData.
/**
* Get the decoded data of a specified part
*
* @param partIndex
* @return
* @throws PackageException
*/
@PublicAtsApi
public InputStream getRegularPartData(int partIndex) throws PackageException {
boolean storeReconnected = false;
try {
// store should be opened for actions including getting InputStream.
// Hence store open is not in getPart
storeReconnected = reconnectStoreIfClosed();
MimePart part = getPart(partIndex, false);
return part.getInputStream();
} catch (MessagingException e) {
throw new PackageException(e);
} catch (IOException ioe) {
throw new PackageException(ioe);
} finally {
try {
closeStoreConnection(storeReconnected);
} catch (MessagingException e) {
log.error(e);
}
}
}
use of com.axway.ats.action.objects.model.PackageException in project ats-framework by Axway.
the class MimePackage method addPart.
/**
* Add a part with the specified text to the specified position
*
* @param content the part's content
* @param contentTypeSubtype the part's subtype of content type like plain (sub type of text/plain) or html (sub type of text/html)
* @param charset the part's charset
* @throws PackageException
*/
@PublicAtsApi
public void addPart(String content, String contentTypeSubtype, String charset) throws PackageException {
// create a new inline part
MimeBodyPart part = new MimeBodyPart();
try {
part.setText(content, charset, contentTypeSubtype);
part.setDisposition(MimeBodyPart.INLINE);
} catch (MessagingException me) {
throw new PackageException(me);
}
addPart(part, PART_POSITION_LAST);
}
use of com.axway.ats.action.objects.model.PackageException in project ats-framework by Axway.
the class PackageLoader method loadPackageFromDb.
private static InputStream loadPackageFromDb(int packageId, String messagesHost, String messagesDB, String messagestable, String messagesUser, String messagesPassword) throws PackageException {
DbConnMySQL dbConnection = new DbConnMySQL(messagesHost, messagesDB, messagesUser, messagesPassword);
MysqlDbProvider dbProvider = new MysqlDbProvider(dbConnection);
try {
InputStream packageContent = dbProvider.selectValue(messagestable, "message_id", Integer.toString(packageId), "data");
log.info("Successfully extracted package with id '" + packageId + "' from '" + messagestable + "' DB");
return packageContent;
} catch (DbRecordsException dbre) {
throw new PackageException("Package with id '" + packageId + "' does not exist in 'messages' DB");
} catch (DbException dbe) {
throw new PackageException("Could not get package with id '" + packageId + "' from the 'messages' DB");
}
}
use of com.axway.ats.action.objects.model.PackageException in project ats-framework by Axway.
the class MimePackage method setBody.
/**
* Replaces body parts with the specified content. <br>
* If this is a plain text only message, the provided HTML content will not
* affect the message in any way.
* <em>Note:</em> This method has effect only if there are already such (plain and/or HTML) parts.
*
* @param plainTextContent
* the text content of the message
* @param htmlContent
* the HTML content of the message
* @throws PackageException
*/
@PublicAtsApi
public void setBody(String plainTextContent, String htmlContent) throws PackageException {
try {
String messageContentType = message.getContentType();
if (messageContentType == null) {
// not expected as default should be "text/plain"
log.info("No content type is set yet. Body of message is not changed");
return;
} else {
// type is not not case-sensitive as mentioned in
// http://www.w3.org/Protocols/rfc1341/4_Content-Type.html and RFC 2045
messageContentType = messageContentType.toLowerCase();
}
if (messageContentType.startsWith(CONTENT_TYPE_TEXT_PLAIN)) {
// this is a text/plain message
message.setContent(plainTextContent, message.getContentType());
} else {
if (messageContentType.startsWith(CONTENT_TYPE_MULTIPART_PREFIX)) {
// this is a MULTIPART message
try {
BodyPart tmpBodyPart;
MimeMultipart tmpMultipartContent = (MimeMultipart) message.getContent();
for (int index = 0; index < tmpMultipartContent.getCount(); index++) {
tmpBodyPart = tmpMultipartContent.getBodyPart(index);
if (tmpBodyPart.getContentType().startsWith(CONTENT_PART_TYPE_TEXT_HTML)) {
// just replace content, do not create additional part
tmpBodyPart.setContent(htmlContent, tmpBodyPart.getContentType());
// for some reason after setting content there is match for text/plain too for the same body part
// so use if-else or continue
} else if (tmpBodyPart.getContentType().startsWith(CONTENT_PART_TYPE_TEXT_PLAIN)) {
// replace text part
tmpBodyPart.setContent(plainTextContent, tmpBodyPart.getContentType());
continue;
}
// do not check content type and go to process next part
}
} catch (IOException ioe) {
throw new PackageException("Could not add MIME body parts", ioe);
}
}
}
// make sure all changes to the message are saved before decomposing
try {
message.saveChanges();
} catch (MessagingException me) {
throw new PackageException("Could not save message changes", me);
}
// we need to decompose again, as a new part has been added
decompose();
} catch (MessagingException me) {
throw new PackageException(me);
}
}
use of com.axway.ats.action.objects.model.PackageException in project ats-framework by Axway.
the class MimePackage method setSender.
/**
* Set the sender (From header) for the package
*
* @param sender
* the sender email address
* @throws PackageException
*/
@PublicAtsApi
public void setSender(String sender) throws PackageException {
String newSenderAddress;
String newSenderPersonal;
try {
InternetAddress address = new InternetAddress();
sender = sender.replaceAll("[<>]", "").trim();
boolean hasPersonal = sender.contains(" ");
if (hasPersonal) {
newSenderAddress = sender.substring(sender.lastIndexOf(' '));
newSenderPersonal = sender.substring(0, sender.lastIndexOf(' '));
address.setPersonal(newSenderPersonal.trim());
} else {
newSenderAddress = sender;
}
// set the sender address
address.setAddress(newSenderAddress.trim());
message.setFrom(address);
} catch (ArrayIndexOutOfBoundsException aioobe) {
throw new PackageException("Sender not present");
} catch (MessagingException me) {
throw new PackageException(me);
} catch (UnsupportedEncodingException uee) {
throw new PackageException("Error setting address personal", uee);
}
}
Aggregations