View Javadoc

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