1 package org.apache.maven.archiva.webdav.util;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.io.BufferedReader;
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;
30 import java.util.HashMap;
32 import java.util.StringTokenizer;
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;
44 * @version $Id: MimeTypes.java 7010 2007-10-25 23:35:02Z joakime $
46 * @plexus.component role="org.apache.maven.archiva.webdav.util.MimeTypes"
48 public class MimeTypes
49 implements Initializable
51 private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
53 private String resource = "org/apache/maven/archiva/webdav/util/mime.types";
55 private Map mimeMap = new HashMap();
57 private Logger log = LoggerFactory.getLogger( MimeTypes.class );
60 * Get the Mime Type for the provided filename.
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.
65 public String getMimeType( String filename )
68 if ( !StringUtils.isEmpty( filename ) )
70 int index = filename.lastIndexOf( '.' );
74 value = (String) mimeMap.get( filename.substring( index + 1 ).toLowerCase() );
81 value = DEFAULT_MIME_TYPE;
88 public void initialize()
89 throws InitializationException
94 public void load( File file )
96 if ( !file.exists() || !file.isFile() || !file.canRead() )
98 log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : not a readable file." );
102 FileInputStream fis = null;
106 fis = new FileInputStream( file );
108 catch ( FileNotFoundException e )
110 log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : " + e.getMessage(), e );
114 IOUtils.closeQuietly( fis );
118 public void load( String resourceName )
120 ClassLoader cloader = this.getClass().getClassLoader();
122 /* Load up the mime types table */
123 URL mimeURL = cloader.getResource( resourceName );
125 if ( mimeURL == null )
127 throw new IllegalStateException( "Unable to find resource " + resourceName );
130 InputStream mimeStream = null;
134 mimeStream = mimeURL.openStream();
137 catch ( IOException e )
139 log.error( "Unable to load mime map " + resourceName + " : " + e.getMessage(), e );
143 IOUtils.closeQuietly( mimeStream );
147 public void load( InputStream mimeStream )
151 InputStreamReader reader = null;
152 BufferedReader buf = null;
156 reader = new InputStreamReader( mimeStream );
157 buf = new BufferedReader( reader );
160 while ( ( line = buf.readLine() ) != null )
164 if ( line.length() == 0 )
166 // empty line. skip it
170 if ( line.startsWith( "#" ) )
176 StringTokenizer tokenizer = new StringTokenizer( line );
177 if ( tokenizer.countTokens() > 1 )
179 String type = tokenizer.nextToken();
180 while ( tokenizer.hasMoreTokens() )
182 String extension = tokenizer.nextToken().toLowerCase();
183 this.mimeMap.put( extension, type );
188 catch ( IOException e )
190 log.error( "Unable to read mime types from input stream : " + e.getMessage(), e );
194 IOUtils.closeQuietly( buf );
195 IOUtils.closeQuietly( reader );