Coverage Report - org.mule.transport.email.DefaultDataContentHandlerFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultDataContentHandlerFactory
0%
0/25
0%
0/4
1.286
 
 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  0
         DataHandler.setDataContentHandlerFactory(getInstance());
 32  0
     }
 33  
 
 34  0
     private Map types = new HashMap();
 35  0
     private Map classToHandlers = new HashMap();
 36  0
     private Map classToType = new HashMap();
 37  
     private static DefaultDataContentHandlerFactory factory;
 38  
 
 39  
     public static DefaultDataContentHandlerFactory getInstance()
 40  
     {
 41  0
         if(factory==null)
 42  
         {
 43  0
             factory = new DefaultDataContentHandlerFactory();
 44  
         }
 45  0
         return factory;
 46  
     }
 47  
 
 48  
     private DefaultDataContentHandlerFactory()
 49  0
     {
 50  0
         register(new image_jpeg());
 51  0
         register(new image_gif());
 52  0
         register(new text_plain());
 53  0
         register(new text_xml());
 54  0
         register(new text_html());
 55  0
     }
 56  
 
 57  
     public DataContentHandler createDataContentHandler(String contentType)
 58  
     {
 59  0
         return (DataContentHandler) types.get(contentType);
 60  
     }
 61  
 
 62  
     public DataContentHandler getDataContentHandler(Class clazz)
 63  
     {
 64  0
         return (DataContentHandler) classToHandlers.get(clazz);
 65  
     }
 66  
 
 67  
     public String getContentType(Class clazz)
 68  
     {
 69  0
         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  0
         types.put(contentType, handler);
 80  0
         classToHandlers.put(clazz, handler);
 81  0
         classToType.put(clazz, contentType);
 82  0
     }
 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  0
         for (int i = 0; i < handler.getTransferDataFlavors().length; i++)
 95  
         {
 96  0
               register(handler.getTransferDataFlavors()[i].getMimeType(),
 97  
                 handler.getTransferDataFlavors()[i].getDefaultRepresentationClass(),
 98  
                 handler);
 99  
         }
 100  
 
 101  0
     }
 102  
 }
 103