1 package org.apache.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 @Service( "mimeTpes" )
47 public class MimeTypes
49 private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
51 private String resource = "org/apache/archiva/webdav/util/mime.types";
53 private Map<String, String> mimeMap = new HashMap<String, String>();
55 private Logger log = LoggerFactory.getLogger( MimeTypes.class );
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() );
78 value = DEFAULT_MIME_TYPE;
86 public void initialize()
91 public void load( File file )
93 if ( !file.exists() || !file.isFile() || !file.canRead() )
95 log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : not a readable file." );
99 FileInputStream fis = null;
103 fis = new FileInputStream( file );
105 catch ( FileNotFoundException e )
107 log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : " + e.getMessage(), e );
111 IOUtils.closeQuietly( fis );
115 public void load( String resourceName )
117 ClassLoader cloader = this.getClass().getClassLoader();
119 /* Load up the mime types table */
120 URL mimeURL = cloader.getResource( resourceName );
122 if ( mimeURL == null )
124 throw new IllegalStateException( "Unable to find resource " + resourceName );
127 InputStream mimeStream = null;
131 mimeStream = mimeURL.openStream();
134 catch ( IOException e )
136 log.error( "Unable to load mime map " + resourceName + " : " + e.getMessage(), e );
140 IOUtils.closeQuietly( mimeStream );
144 public void load( InputStream mimeStream )
148 InputStreamReader reader = null;
149 BufferedReader buf = null;
153 reader = new InputStreamReader( mimeStream );
154 buf = new BufferedReader( reader );
157 while ( ( line = buf.readLine() ) != null )
161 if ( line.length() == 0 )
163 // empty line. skip it
167 if ( line.startsWith( "#" ) )
173 StringTokenizer tokenizer = new StringTokenizer( line );
174 if ( tokenizer.countTokens() > 1 )
176 String type = tokenizer.nextToken();
177 while ( tokenizer.hasMoreTokens() )
179 String extension = tokenizer.nextToken().toLowerCase();
180 this.mimeMap.put( extension, type );
185 catch ( IOException e )
187 log.error( "Unable to read mime types from input stream : " + e.getMessage(), e );
191 IOUtils.closeQuietly( buf );
192 IOUtils.closeQuietly( reader );
196 public String getResource()
201 public void setResource( String resource )
203 this.resource = resource;