001: /*
002: * Created on 29.07.2005
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package de.schlund.pfixcore.webservice.fault;
008:
009: import java.util.ArrayList;
010: import java.util.StringTokenizer;
011:
012: import org.apache.log4j.Logger;
013:
014: import de.schlund.pfixcore.util.email.EmailSender;
015: import de.schlund.pfixcore.util.email.EmailSenderException;
016: import de.schlund.pfixcore.webservice.HttpServiceRequest;
017:
018: public class EmailNotifyingHandler extends FaultHandler {
019:
020: private Logger LOG = Logger.getLogger(getClass().getName());
021:
022: private final static String PARAM_SMTPHOST = "smtphost";
023: private final static String PARAM_SENDER = "sender";
024: private final static String PARAM_RECIPIENTS = "recipients";
025:
026: private String smtpHost;
027: private String sender;
028: private String[] recipients;
029:
030: public void init() {
031: smtpHost = getParam(PARAM_SMTPHOST);
032: if (smtpHost == null)
033: throw new IllegalArgumentException("Parameter '"
034: + PARAM_SMTPHOST + "' is missing.");
035: sender = getParam(PARAM_SENDER);
036: if (sender == null)
037: throw new IllegalArgumentException("Parameter '"
038: + PARAM_SENDER + "' is missing.");
039: String str = getParam(PARAM_RECIPIENTS);
040: if (str == null)
041: throw new IllegalArgumentException("Parameter '"
042: + PARAM_RECIPIENTS + "' is missing.");
043: ArrayList<String> al = new ArrayList<String>();
044: StringTokenizer st = new StringTokenizer(str, ",");
045: while (st.hasMoreTokens()) {
046: String s = st.nextToken();
047: al.add(s.trim());
048: }
049: if (al.size() < 1)
050: throw new IllegalArgumentException("Parameter '"
051: + PARAM_RECIPIENTS + "' has illegal value.");
052: recipients = new String[al.size()];
053: al.toArray(recipients);
054: }
055:
056: public void handleFault(Fault fault) {
057: if (isNotificationError(fault))
058: sendMail(fault);
059: if (isInternalServerError(fault))
060: fault.setThrowable(new InternalServerError());
061: }
062:
063: public void sendDirectMail(Fault fault) {
064: try {
065: String subject = createSubject(fault);
066: String text = createText(fault);
067: EmailSender.sendMail(subject, text, recipients, sender,
068: smtpHost);
069: } catch (EmailSenderException x) {
070: LOG.error("Error while sending exception mail.", x);
071: }
072: }
073:
074: public void sendMail(Fault fault) {
075: EmailNotifier notifier = EmailNotifier.getInstance();
076: String subject = createSubject(fault);
077: String text = createText(fault);
078: notifier.sendMail(subject, text, recipients, sender, smtpHost);
079: }
080:
081: public String createSubject(Fault fault) {
082: StringBuffer sb = new StringBuffer();
083: HttpServiceRequest srvReq = (HttpServiceRequest) fault
084: .getRequest();
085: sb.append(srvReq.getServerName());
086: sb.append("|webservice|");
087: sb.append(fault.getServiceName());
088: sb.append("|");
089: sb.append(fault.getFaultString());
090: return sb.toString();
091: }
092:
093: public String createText(Fault fault) {
094: StringBuffer sb = new StringBuffer();
095: HttpServiceRequest srvReq = (HttpServiceRequest) fault
096: .getRequest();
097: sb.append("Request: \t");
098: sb.append(srvReq.getRequestURI());
099: sb.append("\n");
100: sb.append("Service: \t");
101: sb.append(fault.getServiceName());
102: sb.append("\n");
103: sb.append("Exception: \t");
104: sb.append(fault.getFaultString());
105: sb.append("\n\n");
106: sb.append("Context:\n");
107: sb.append(fault.getContext() == null ? "-" : fault.getContext()
108: .toString());
109: sb.append("\n\n");
110: sb.append("Request message:\n\n");
111: sb.append(fault.getRequestMessage());
112: sb.append("\n\n");
113: sb.append("Stacktrace:\n\n");
114: sb.append(fault.getStackTrace());
115: sb.append("\n");
116: return sb.toString();
117: }
118:
119: public boolean isInternalServerError(Fault fault) {
120: Throwable t = fault.getThrowable();
121: if (t instanceof Error)
122: return true;
123: return false;
124: }
125:
126: public boolean isNotificationError(Fault fault) {
127: return true;
128: }
129:
130: }
|