001: // Copyright © 2006-2007 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.plugins.emailtest;
003:
004: import java.io.IOException;
005: import java.util.regex.Matcher;
006: import java.util.regex.Pattern;
007:
008: import javax.mail.BodyPart;
009: import javax.mail.Message;
010: import javax.mail.MessagingException;
011: import javax.mail.Multipart;
012: import javax.mail.Part;
013:
014: import org.apache.log4j.Logger;
015:
016: import com.canoo.webtest.engine.StepFailedException;
017: import com.canoo.webtest.util.ConversionUtil;
018:
019: /**
020: * Stores the filename of a message part.
021: *
022: * @author Paul King, ASERT
023: * @webtest.step category="Email"
024: * name="emailStorePartFilename"
025: * description="Stores the filename of a message part. Fails if the message contains no parts or if the partIndex is invalid or if the part is an inline part with no filename."
026: */
027: public class EmailStorePartFilename extends AbstractMessageStoreStep {
028: private static final Logger LOG = Logger
029: .getLogger(EmailStorePartFilename.class);
030: private String fPartIndex;
031:
032: public String getPartIndex() {
033: return fPartIndex;
034: }
035:
036: /**
037: * Sets the part index.
038: *
039: * @param partIndex The message part of interest
040: * @webtest.parameter
041: * required="no"
042: * default="0, the first part"
043: * description="The index of the message part of interest. If set for a <em>Simple</em> message, will search for a UU-encoded attachment within the message."
044: */
045: public void setPartIndex(final String partIndex) {
046: fPartIndex = partIndex;
047: }
048:
049: /**
050: * Calculate the result.
051: *
052: * @param message
053: * @return The result
054: */
055: protected String performOperation(final Message message)
056: throws MessagingException {
057: final Object content;
058: try {
059: content = message.getContent();
060: } catch (IOException e) {
061: LOG.error("Error processing email message: ", e);
062: throw new MessagingException(
063: "Error processing email message: " + e.getMessage());
064: }
065:
066: final int part = ConversionUtil.convertToInt(getPartIndex(), 0);
067: if (content instanceof Multipart) {
068: final Multipart mp = (Multipart) content;
069: if (part >= mp.getCount()) {
070: throw new StepFailedException("PartIndex too large.",
071: this );
072: }
073: final BodyPart bodyPart = mp.getBodyPart(part);
074: final String disp = bodyPart.getDisposition();
075: if (Part.ATTACHMENT.equals(disp)) {
076: return bodyPart.getFileName();
077: }
078: throw new StepFailedException(
079: "No filename for inline Message Part.", this );
080: }
081: return getSimpleMessageFilename((String) content, part);
082: }
083:
084: private String getSimpleMessageFilename(final String content,
085: final int part) {
086: // iterate over string looking for ^begin ddd$
087: final String lineStr = "(^.*$)";
088: final String startUuencodeStr = "begin \\d\\d\\d .*";
089: final Pattern linePattern = Pattern.compile(lineStr,
090: Pattern.MULTILINE);
091: final Matcher matcher = linePattern.matcher(content);
092: int count = 0;
093: while (matcher.find()) {
094: final String line = matcher.group(0).trim();
095: if (line.matches(startUuencodeStr)) {
096: if (count == part) {
097: final int lastSpace = line.lastIndexOf(" ");
098: return line.substring(lastSpace + 1);
099: }
100: count++;
101: }
102: }
103: throw new StepFailedException("No matching part found.", this );
104: }
105:
106: protected void verifyParameters() {
107: super .verifyParameters();
108: optionalIntegerParamCheck(getPartIndex(), "partIndex", true);
109: }
110: }
|