01: /*
02: * $Id:DefaultDataContentHandlerFactory.java 7555 2007-07-18 03:17:16Z aperepel $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10: package org.mule.transport.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: * This is a default registry for mapping MimeTypes to DataHandlers
27: */
28: public class DefaultDataContentHandlerFactory implements
29: DataContentHandlerFactory {
30:
31: static {
32: //If this class gets loaded then register this Factory with Activation
33: DataHandler.setDataContentHandlerFactory(getInstance());
34: }
35:
36: private Map types = new HashMap();
37: private Map classToHandlers = new HashMap();
38: private Map classToType = new HashMap();
39: private static DefaultDataContentHandlerFactory factory;
40:
41: public static DefaultDataContentHandlerFactory getInstance() {
42: if (factory == null) {
43: factory = new DefaultDataContentHandlerFactory();
44: }
45: return factory;
46: }
47:
48: private DefaultDataContentHandlerFactory() {
49: register(new image_jpeg());
50: register(new image_gif());
51: register(new text_plain());
52: register(new text_xml());
53: register(new text_html());
54: }
55:
56: public DataContentHandler createDataContentHandler(
57: String contentType) {
58: return (DataContentHandler) types.get(contentType);
59: }
60:
61: public DataContentHandler getDataContentHandler(Class clazz) {
62: return (DataContentHandler) classToHandlers.get(clazz);
63: }
64:
65: public String getContentType(Class clazz) {
66: return (String) classToHandlers.get(clazz);
67: }
68:
69: /**
70: * Register a DataContentHandler for a particular MIME type.
71: * @param contentType The Content Type.
72: * @param handler The DataContentHandler.
73: */
74: public void register(String contentType, Class clazz,
75: DataContentHandler handler) {
76: types.put(contentType, handler);
77: classToHandlers.put(clazz, handler);
78: classToType.put(clazz, contentType);
79: }
80:
81: /**
82: * Registers a {@link DataContenetHandler} for use with certain mime types. To use this registration
83: * method the DataHandler has to be implmented correctly. This method uses the DataFalvour of the
84: * DataHandler to obtain the mimeType and DefaultRepresentation class. If there is more than one DataFlavour
85: * on the DataHandler, then each flavour will be registered seperately.
86: * @param handler
87: */
88: public void register(DataContentHandler handler) {
89: for (int i = 0; i < handler.getTransferDataFlavors().length; i++) {
90: register(handler.getTransferDataFlavors()[i].getMimeType(),
91: handler.getTransferDataFlavors()[i]
92: .getDefaultRepresentationClass(), handler);
93: }
94:
95: }
96: }
|