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 org.apache.commons.io.IOUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.codehaus.plexus.logging.AbstractLogEnabled;
25 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
26 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
28 import java.io.BufferedReader;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
36 import java.util.HashMap;
38 import java.util.StringTokenizer;
43 * @version $Id: MimeTypes.java 7010 2007-10-25 23:35:02Z joakime $
45 * @plexus.component role="org.apache.maven.archiva.webdav.util.MimeTypes"
47 public class MimeTypes
48 extends AbstractLogEnabled
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();
58 * Get the Mime Type for the provided filename.
60 * @param filename the filename to obtain the mime type for.
61 * @return a mime type String, or null if filename is null, has no extension, or no mime type is associated with it.
63 public String getMimeType( String filename )
66 if ( !StringUtils.isEmpty( filename ) )
68 int index = filename.lastIndexOf( '.' );
72 value = (String) mimeMap.get( filename.substring( index + 1 ).toLowerCase() );
79 value = DEFAULT_MIME_TYPE;
86 public void initialize()
87 throws InitializationException
92 public void load( File file )
94 if ( !file.exists() || !file.isFile() || !file.canRead() )
96 getLogger().error( "Unable to load mime types from file " + file.getAbsolutePath() + " : not a readable file." );
100 FileInputStream fis = null;
104 fis = new FileInputStream( file );
106 catch ( FileNotFoundException e )
108 getLogger().error( "Unable to load mime types from file " + file.getAbsolutePath() + " : " + e.getMessage(), e );
112 IOUtils.closeQuietly( fis );
116 public void load( String resourceName )
118 ClassLoader cloader = this.getClass().getClassLoader();
120 /* Load up the mime types table */
121 URL mimeURL = cloader.getResource( resourceName );
123 if ( mimeURL == null )
125 throw new IllegalStateException( "Unable to find resource " + resourceName );
128 InputStream mimeStream = null;
132 mimeStream = mimeURL.openStream();
135 catch ( IOException e )
137 getLogger().error( "Unable to load mime map " + resourceName + " : " + e.getMessage(), e );
141 IOUtils.closeQuietly( mimeStream );
145 public void load( InputStream mimeStream )
149 InputStreamReader reader = null;
150 BufferedReader buf = null;
154 reader = new InputStreamReader( mimeStream );
155 buf = new BufferedReader( reader );
158 while ( ( line = buf.readLine() ) != null )
162 if ( line.length() == 0 )
164 // empty line. skip it
168 if ( line.startsWith( "#" ) )
174 StringTokenizer tokenizer = new StringTokenizer( line );
175 if ( tokenizer.countTokens() > 1 )
177 String type = tokenizer.nextToken();
178 while ( tokenizer.hasMoreTokens() )
180 String extension = tokenizer.nextToken().toLowerCase();
181 this.mimeMap.put( extension, type );
186 catch ( IOException e )
188 getLogger().error( "Unable to read mime types from input stream : " + e.getMessage(), e );
192 IOUtils.closeQuietly( buf );
193 IOUtils.closeQuietly( reader );