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