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