Coverage Report - org.trails.component.blob.image.MimedImage
 
Classes in this File Line Coverage Branch Coverage Complexity
MimedImage
0% 
0% 
0
 
 1  
 package org.trails.component.blob.image;
 2  
 
 3  
 import java.util.HashMap;
 4  
 import java.util.Map;
 5  
 
 6  
 import org.apache.tapestry.IAsset;
 7  
 import org.apache.tapestry.IMarkupWriter;
 8  
 import org.apache.tapestry.IRequestCycle;
 9  
 import org.apache.tapestry.Tapestry;
 10  
 import org.apache.tapestry.annotations.ComponentClass;
 11  
 import org.apache.tapestry.annotations.InjectObject;
 12  
 import org.apache.tapestry.annotations.Parameter;
 13  
 import org.apache.tapestry.asset.AssetFactory;
 14  
 import org.apache.tapestry.html.Image;
 15  
 import org.trails.component.blob.ITrailsBlob;
 16  
 import org.trails.descriptor.BlobDescriptorExtension;
 17  
 import org.trails.descriptor.IPropertyDescriptor;
 18  
 import org.trails.persistence.PersistenceService;
 19  
 
 20  
 /**
 21  
  * This widget overloads the src attribute of @Image
 22  
  * <p/>
 23  
  * It substitutes icons when tapestry fails to negotiate a
 24  
  * proper src for uploadable media.
 25  
  * <p/>
 26  
  * This is exclusively intended for org.trails.component.blob.ITrailsBlob
 27  
  *
 28  
  * @author kenneth.colassi
 29  
  */
 30  
 
 31  
 @ComponentClass(allowBody = false, allowInformalParameters = true)
 32  
 public abstract class MimedImage extends Image
 33  
 {
 34  0
         private Map<String, String> map = new HashMap<String, String>();
 35  
 
 36  
         @InjectObject("service:tapestry.asset.ClasspathAssetFactory")
 37  
         public abstract AssetFactory getClasspathAssetFactory();
 38  
 
 39  
         @InjectObject("spring:persistenceService")
 40  
         public abstract PersistenceService getPersistenceService();
 41  
 
 42  
         @Parameter(required = true)
 43  
         public abstract IPropertyDescriptor getPropertyDescriptor();
 44  
 
 45  
         public abstract void setPropertyDescriptor(
 46  
                 IPropertyDescriptor propertyDescriptor);
 47  
 
 48  
         @Parameter(required = true)
 49  
         public abstract Object getBytes();
 50  
 
 51  
         public abstract void setBytes(Object bytes);
 52  
 
 53  
         @Parameter(required = true)
 54  
         public abstract Object getModel();
 55  
 
 56  
         public abstract void setModel(Object bytes);
 57  
 
 58  
         @Parameter(required = true)
 59  
         public abstract IAsset getImage();
 60  
 
 61  
         public abstract void setImage(IAsset image);
 62  
 
 63  
         public BlobDescriptorExtension getBlobDescriptorExtension()
 64  
         {
 65  0
                 return getPropertyDescriptor().getExtension(
 66  0
                         BlobDescriptorExtension.class);
 67  
         }
 68  
 
 69  0
         public MimedImage()
 70  
         {
 71  
                 /**
 72  
                  * Map keyes MUST adhere to standard mime
 73  
                  */
 74  0
                 map.put("application/x-zip-compressed",
 75  0
                         "/org/trails/component/blob/image/asset/winzip.gif");
 76  0
                 map.put("application/pdf",
 77  0
                         "/org/trails/component/blob/image/asset/icadobe.gif");
 78  0
                 map.put("application/msword",
 79  0
                         "/org/trails/component/blob/image/asset/icdoc.gif");
 80  0
                 map.put("application/vnd.visio",
 81  0
                         "/org/trails/component/blob/image/asset/icdoc.gif");
 82  0
                 map.put("application/vnd.ms-powerpoint",
 83  0
                         "/org/trails/component/blob/image/asset/icppt.gif");
 84  0
                 map.put("application/vnd.ms-excel",
 85  0
                         "/org/trails/component/blob/image/asset/icxls.gif");
 86  0
                 map.put("application/octet-stream",
 87  0
                         "/org/trails/component/blob/image/asset/icgen.gif");
 88  
 
 89  0
                 map.put("text/html", "/org/trails/component/blob/image/asset/ichtm.gif");
 90  0
                 map.put("text/plain", "/org/trails/component/blob/image/asset/ictxt.gif");
 91  0
                 map.put("text/css", "/org/trails/component/blob/image/asset/ictxt.gif");
 92  0
                 map.put("text/xml", "/org/trails/component/blob/image/asset/icxml.gif");
 93  
 
 94  0
                 map.put("image/tiff", "/org/trails/component/blob/image/asset/icgen.gif");
 95  
 
 96  0
                 map.put("video/avi", "/org/trails/component/blob/image/asset/icwmp.gif");
 97  0
                 map.put("video/mpeg", "/org/trails/component/blob/image/asset/icwmp.gif");
 98  0
                 map.put("video/mp4", "/org/trails/component/blob/image/asset/icwmp.gif");
 99  0
                 map.put("video/quicktime",
 100  0
                         "/org/trails/component/blob/image/asset/icwmp.gif");
 101  0
                 map
 102  0
                         .put("video/x-ms-wmv",
 103  0
                                 "/org/trails/component/blob/image/asset/icwmp.gif");
 104  
 
 105  0
         }
 106  
 
 107  
         @Override
 108  
         protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 109  
         {
 110  0
                 if (cycle.isRewinding())
 111  0
                         return;
 112  
 
 113  0
                 IAsset imageAsset = getImage();
 114  
 
 115  0
                 if (imageAsset == null)
 116  0
                         throw Tapestry.createRequiredParameterException(this, "image");
 117  
 
 118  0
                 writer.beginEmpty("img");
 119  
 
 120  0
                 ITrailsBlob trailsBlob = null;
 121  0
                 String contentType = null;
 122  
 
 123  0
                 if (getBlobDescriptorExtension().isBytes())
 124  
                 {
 125  0
                         trailsBlob = (ITrailsBlob) getModel();
 126  0
                 } else if (getBlobDescriptorExtension().isITrailsBlob())
 127  
                 {
 128  0
                         trailsBlob = (ITrailsBlob) getBytes();
 129  
                 }
 130  0
                 contentType = trailsBlob.getContentType();
 131  
 
 132  0
                 if (contentType == null)
 133  
                 {
 134  0
                         writer.attribute("src", imageAsset.buildURL());
 135  0
                 } else if (contentType.equalsIgnoreCase("image/jpeg")
 136  0
                                 || contentType.equalsIgnoreCase("image/pjpeg")
 137  0
                                 || contentType.equalsIgnoreCase("image/tiff")
 138  0
                                 || contentType.equalsIgnoreCase("image/bmp")
 139  0
                                 || contentType.equalsIgnoreCase("video/mpeg")
 140  0
                                 || contentType.equalsIgnoreCase("video/quicktime")
 141  0
                                 || contentType.equalsIgnoreCase("video/x-msvideo")
 142  0
                                 || contentType.equalsIgnoreCase("image/gif"))
 143  
                 {
 144  0
                         writer.attribute("src", imageAsset.buildURL());
 145  
                 } else
 146  
                 {
 147  0
                         AssetFactory fact = getClasspathAssetFactory();
 148  
 
 149  
                         IAsset asset;
 150  
 
 151  0
                         if (map.containsKey(contentType))
 152  
                         {
 153  0
                                 asset = fact.createAbsoluteAsset(map.get(contentType)
 154  0
                                         .toString(), null, null);
 155  0
                                 writer.attribute("src", asset.buildURL());
 156  
                         } else
 157  
                         {
 158  0
                                 asset = fact.createAbsoluteAsset(map.get(
 159  0
                                         "application/octet-stream").toString(), null, null);
 160  0
                                 writer.attribute("src", asset.buildURL());
 161  
                         }
 162  
                 }
 163  
 
 164  0
                 renderInformalParameters(writer, cycle);
 165  
 
 166  0
                 writer.closeTag();
 167  0
         }
 168  
 }