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:
006: import javax.mail.Message;
007: import javax.mail.MessagingException;
008: import javax.mail.Multipart;
009:
010: import org.apache.commons.lang.StringUtils;
011: import org.apache.log4j.Logger;
012:
013: import com.canoo.webtest.engine.StepFailedException;
014: import com.canoo.webtest.util.ConversionUtil;
015:
016: /**
017: * Stores the value of a header field within a message (or within a message part).
018: *
019: * @author Paul King, ASERT
020: * @webtest.step category="Email"
021: * name="emailStoreHeader"
022: * description="Stores the value of a message (or message part) header field into a property."
023: */
024: public class EmailStoreHeader extends AbstractMessageStoreStep {
025: private static final Logger LOG = Logger
026: .getLogger(EmailStoreHeader.class);
027: private String fHeaderName;
028: private String fPartIndex;
029:
030: public String getHeaderName() {
031: return fHeaderName;
032: }
033:
034: /**
035: * Sets the name of the header of interest.
036: *
037: * @param headerName The header name
038: * @webtest.parameter required="yes"
039: * description="The name of the header of interest."
040: */
041: public void setHeaderName(final String headerName) {
042: fHeaderName = headerName;
043: }
044:
045: public String getPartIndex() {
046: return fPartIndex;
047: }
048:
049: /**
050: * Sets the part index.
051: *
052: * @param partIndex The message part of interest
053: * @webtest.parameter
054: * required="no"
055: * description="The index of the message part of interest. If set for a <em>Simple</em> message, will cause the step to fail. If set for a MIME <em>MultiPart</em> message will retrieve headers related to the message part instead of headers of the message itself."
056: */
057: public void setPartIndex(final String partIndex) {
058: fPartIndex = partIndex;
059: }
060:
061: /**
062: * Calculate the result.
063: *
064: * @param message
065: * @return The result
066: */
067: protected String performOperation(final Message message)
068: throws MessagingException {
069: if (StringUtils.isEmpty(getPartIndex())) {
070: return arrayToString(message.getHeader(getHeaderName()));
071: }
072: final Object content;
073: try {
074: content = message.getContent();
075: } catch (IOException e) {
076: LOG.error("Error processing email message: ", e);
077: throw new MessagingException(
078: "Error processing email message: " + e.getMessage());
079: }
080: if (content instanceof Multipart) {
081: final Multipart mp = (Multipart) content;
082: final int part = ConversionUtil.convertToInt(
083: getPartIndex(), 0);
084: if (part >= mp.getCount()) {
085: throw new StepFailedException("PartIndex too large.",
086: this );
087: }
088: return arrayToString(mp.getBodyPart(part).getHeader(
089: getHeaderName()));
090: }
091: throw new StepFailedException(
092: "PartIndex supplied for a non-MultiPart message.", this );
093: }
094:
095: private static String arrayToString(final String[] values) {
096: final StringBuffer buf = new StringBuffer();
097: for (int i = 0; i < values.length; i++) {
098: buf.append(values[i]);
099: if (i < values.length - 1) {
100: buf.append(", ");
101: }
102: }
103: return buf.toString();
104: }
105:
106: protected void verifyParameters() {
107: super .verifyParameters();
108: nullParamCheck(getHeaderName(), "headerName");
109: optionalIntegerParamCheck(getPartIndex(), "partIndex", true);
110: }
111: }
|