1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.email; |
8 | |
|
9 | |
import com.sun.mail.handlers.image_gif; |
10 | |
import com.sun.mail.handlers.image_jpeg; |
11 | |
import com.sun.mail.handlers.text_html; |
12 | |
import com.sun.mail.handlers.text_plain; |
13 | |
import com.sun.mail.handlers.text_xml; |
14 | |
|
15 | |
import java.util.HashMap; |
16 | |
import java.util.Map; |
17 | |
|
18 | |
import javax.activation.DataContentHandler; |
19 | |
import javax.activation.DataContentHandlerFactory; |
20 | |
import javax.activation.DataHandler; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
public class DefaultDataContentHandlerFactory implements DataContentHandlerFactory |
26 | |
{ |
27 | |
|
28 | |
static |
29 | |
{ |
30 | |
|
31 | 0 | DataHandler.setDataContentHandlerFactory(getInstance()); |
32 | 0 | } |
33 | |
|
34 | 0 | private Map types = new HashMap(); |
35 | 0 | private Map classToHandlers = new HashMap(); |
36 | 0 | private Map classToType = new HashMap(); |
37 | |
private static DefaultDataContentHandlerFactory factory; |
38 | |
|
39 | |
public static DefaultDataContentHandlerFactory getInstance() |
40 | |
{ |
41 | 0 | if(factory==null) |
42 | |
{ |
43 | 0 | factory = new DefaultDataContentHandlerFactory(); |
44 | |
} |
45 | 0 | return factory; |
46 | |
} |
47 | |
|
48 | |
private DefaultDataContentHandlerFactory() |
49 | 0 | { |
50 | 0 | register(new image_jpeg()); |
51 | 0 | register(new image_gif()); |
52 | 0 | register(new text_plain()); |
53 | 0 | register(new text_xml()); |
54 | 0 | register(new text_html()); |
55 | 0 | } |
56 | |
|
57 | |
public DataContentHandler createDataContentHandler(String contentType) |
58 | |
{ |
59 | 0 | return (DataContentHandler) types.get(contentType); |
60 | |
} |
61 | |
|
62 | |
public DataContentHandler getDataContentHandler(Class clazz) |
63 | |
{ |
64 | 0 | return (DataContentHandler) classToHandlers.get(clazz); |
65 | |
} |
66 | |
|
67 | |
public String getContentType(Class clazz) |
68 | |
{ |
69 | 0 | return (String) classToHandlers.get(clazz); |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public void register(String contentType, Class clazz, DataContentHandler handler) |
78 | |
{ |
79 | 0 | types.put(contentType, handler); |
80 | 0 | classToHandlers.put(clazz, handler); |
81 | 0 | classToType.put(clazz, contentType); |
82 | 0 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public void register(DataContentHandler handler) |
93 | |
{ |
94 | 0 | for (int i = 0; i < handler.getTransferDataFlavors().length; i++) |
95 | |
{ |
96 | 0 | register(handler.getTransferDataFlavors()[i].getMimeType(), |
97 | |
handler.getTransferDataFlavors()[i].getDefaultRepresentationClass(), |
98 | |
handler); |
99 | |
} |
100 | |
|
101 | 0 | } |
102 | |
} |
103 | |
|