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.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.stereotype.Service;
28 import javax.annotation.PostConstruct;
29 import java.io.BufferedReader;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
37 import java.util.HashMap;
39 import java.util.StringTokenizer;
44 * @version $Id: MimeTypes.java 7010 2007-10-25 23:35:02Z joakime $
46 * plexus.component role="org.apache.maven.archiva.webdav.util.MimeTypes"
49 public class MimeTypes
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<String, String> mimeMap = new HashMap<String, String>();
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;
89 public void initialize()
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 );
199 public String getResource()
204 public void setResource( String resource )
206 this.resource = resource;