Coverage Report - org.apache.maven.archiva.webdav.util.MimeTypes
 
Classes in this File Line Coverage Branch Coverage Complexity
MimeTypes
0%
0/62
0%
0/24
0
 
 1  
 package org.apache.maven.archiva.webdav.util;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.BufferedReader;
 23  
 import java.io.File;
 24  
 import java.io.FileInputStream;
 25  
 import java.io.FileNotFoundException;
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.io.InputStreamReader;
 29  
 import java.net.URL;
 30  
 import java.util.HashMap;
 31  
 import java.util.Map;
 32  
 import java.util.StringTokenizer;
 33  
 
 34  
 import org.apache.commons.io.IOUtils;
 35  
 import org.apache.commons.lang.StringUtils;
 36  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 37  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
 38  
 import org.slf4j.Logger;
 39  
 import org.slf4j.LoggerFactory;
 40  
 
 41  
 /**
 42  
  * MimeTypes 
 43  
  *
 44  
  * @version $Id: MimeTypes.java 7010 2007-10-25 23:35:02Z joakime $
 45  
  * 
 46  
  * @plexus.component role="org.apache.maven.archiva.webdav.util.MimeTypes"
 47  
  */
 48  0
 public class MimeTypes
 49  
     implements Initializable
 50  
 {
 51  
     private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
 52  
 
 53  0
     private String resource = "org/apache/maven/archiva/webdav/util/mime.types";
 54  
     
 55  0
     private Map<String, String> mimeMap = new HashMap<String, String>();
 56  
 
 57  0
     private Logger log = LoggerFactory.getLogger( MimeTypes.class );
 58  
     
 59  
     /**
 60  
      * Get the Mime Type for the provided filename.
 61  
      * 
 62  
      * @param filename the filename to obtain the mime type for.
 63  
      * @return a mime type String, or null if filename is null, has no extension, or no mime type is associated with it.
 64  
      */
 65  
     public String getMimeType( String filename )
 66  
     {
 67  0
         String value = null;
 68  0
         if ( !StringUtils.isEmpty( filename ) )
 69  
         {
 70  0
             int index = filename.lastIndexOf( '.' );
 71  
 
 72  0
             if ( index >= 0 )
 73  
             {
 74  0
                 value = (String) mimeMap.get( filename.substring( index + 1 ).toLowerCase() );
 75  
             }
 76  
         }
 77  
 
 78  
 
 79  0
         if (value == null)
 80  
         {
 81  0
             value = DEFAULT_MIME_TYPE;
 82  
         }
 83  
 
 84  0
         return value;
 85  
 
 86  
     }
 87  
 
 88  
     public void initialize()
 89  
         throws InitializationException
 90  
     {
 91  0
         load( resource );
 92  0
     }
 93  
 
 94  
     public void load( File file )
 95  
     {
 96  0
         if ( !file.exists() || !file.isFile() || !file.canRead() )
 97  
         {
 98  0
             log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : not a readable file." );
 99  0
             return;
 100  
         }
 101  
 
 102  0
         FileInputStream fis = null;
 103  
 
 104  
         try
 105  
         {
 106  0
             fis = new FileInputStream( file );
 107  
         }
 108  0
         catch ( FileNotFoundException e )
 109  
         {
 110  0
             log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : " + e.getMessage(), e );
 111  
         }
 112  
         finally
 113  
         {
 114  0
             IOUtils.closeQuietly( fis );
 115  0
         }
 116  0
     }
 117  
 
 118  
     public void load( String resourceName )
 119  
     {
 120  0
         ClassLoader cloader = this.getClass().getClassLoader();
 121  
 
 122  
         /* Load up the mime types table */
 123  0
         URL mimeURL = cloader.getResource( resourceName );
 124  
 
 125  0
         if ( mimeURL == null )
 126  
         {
 127  0
             throw new IllegalStateException( "Unable to find resource " + resourceName );
 128  
         }
 129  
 
 130  0
         InputStream mimeStream = null;
 131  
 
 132  
         try
 133  
         {
 134  0
             mimeStream = mimeURL.openStream();
 135  0
             load( mimeStream );
 136  
         }
 137  0
         catch ( IOException e )
 138  
         {
 139  0
             log.error( "Unable to load mime map " + resourceName + " : " + e.getMessage(), e );
 140  
         }
 141  
         finally
 142  
         {
 143  0
             IOUtils.closeQuietly( mimeStream );
 144  0
         }
 145  0
     }
 146  
 
 147  
     public void load( InputStream mimeStream )
 148  
     {
 149  0
         mimeMap.clear();
 150  
 
 151  0
         InputStreamReader reader = null;
 152  0
         BufferedReader buf = null;
 153  
 
 154  
         try
 155  
         {
 156  0
             reader = new InputStreamReader( mimeStream );
 157  0
             buf = new BufferedReader( reader );
 158  0
             String line = null;
 159  
 
 160  0
             while ( ( line = buf.readLine() ) != null )
 161  
             {
 162  0
                 line = line.trim();
 163  
 
 164  0
                 if ( line.length() == 0 )
 165  
                 {
 166  
                     // empty line. skip it
 167  0
                     continue;
 168  
                 }
 169  
 
 170  0
                 if ( line.startsWith( "#" ) )
 171  
                 {
 172  
                     // Comment. skip it
 173  0
                     continue;
 174  
                 }
 175  
 
 176  0
                 StringTokenizer tokenizer = new StringTokenizer( line );
 177  0
                 if ( tokenizer.countTokens() > 1 )
 178  
                 {
 179  0
                     String type = tokenizer.nextToken();
 180  0
                     while ( tokenizer.hasMoreTokens() )
 181  
                     {
 182  0
                         String extension = tokenizer.nextToken().toLowerCase();
 183  0
                         this.mimeMap.put( extension, type );
 184  0
                     }
 185  
                 }
 186  0
             }
 187  
         }
 188  0
         catch ( IOException e )
 189  
         {
 190  0
             log.error( "Unable to read mime types from input stream : " + e.getMessage(), e );
 191  
         }
 192  
         finally
 193  
         {
 194  0
             IOUtils.closeQuietly( buf );
 195  0
             IOUtils.closeQuietly( reader );
 196  0
         }
 197  0
     }
 198  
 }