01: package org.obe.event;
02:
03: import javax.mail.Multipart;
04: import javax.mail.MessagingException;
05: import javax.mail.BodyPart;
06: import javax.mail.Header;
07: import org.obe.spi.service.ServiceManager;
08: import org.obe.spi.event.ApplicationEvent;
09: import org.obe.client.api.repository.RepositoryException;
10: import java.util.Properties;
11: import java.util.Map;
12: import java.util.Enumeration;
13: import java.util.HashMap;
14: import java.io.IOException;
15:
16: /**
17: * Handles MIME <tag>multipart/*</tag> content.
18: *
19: * @author Adrian Price
20: */
21: public class MIMEMultipartHandler extends AbstractContentHandler {
22: public MIMEMultipartHandler() {
23: }
24:
25: public void init(ServiceManager svcMgr, Properties props) {
26: init(svcMgr);
27: }
28:
29: public String getContentType(Object data) {
30: // TODO: figure out how to handle multipart content in textual form.
31: return data instanceof Multipart ? ((Multipart) data)
32: .getContentType() : "multipart/mixed";
33: }
34:
35: protected String getSchema(Object data, String contentType) {
36: return "";
37: }
38:
39: public ApplicationEvent[] process(Object data, Map attrs,
40: String contentType) throws RepositoryException {
41:
42: try {
43: // Convert data to the required class, if required.
44: Multipart multipart = (Multipart) convert(data, contentType);
45:
46: int count = multipart.getCount();
47: ApplicationEvent[] events = new ApplicationEvent[count];
48: for (int i = 0; i < count; i++) {
49: BodyPart part = multipart.getBodyPart(i);
50: Map partAttrs = new HashMap();
51: partAttrs.putAll(attrs);
52: Enumeration e = part.getAllHeaders();
53: while (e.hasMoreElements()) {
54: Header header = (Header) e.nextElement();
55: partAttrs.put(header.getName(), header.getValue());
56: }
57: contentType = part.getContentType();
58: data = convert(part.getContent(), contentType);
59: events[i] = new ApplicationEvent(data, contentType,
60: getSchema(data, contentType), partAttrs);
61: }
62: return events;
63: } catch (MessagingException e) {
64: throw new RepositoryException(e);
65: } catch (IOException e) {
66: throw new RepositoryException(e);
67: }
68: }
69: }
|