Coverage Report - org.mule.providers.email.DefaultDataContentHandlerFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultDataContentHandlerFactory
0%
0/25
0%
0/4
1.286
 
 1  
 /*
 2  
  * $Id: DefaultDataContentHandlerFactory.java 7963 2007-08-21 08:53:15Z 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  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  
      * 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  0
         types.put(contentType, handler);
 83  0
         classToHandlers.put(clazz, handler);
 84  0
         classToType.put(clazz, contentType);
 85  0
     }
 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  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