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.lang.StringUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.stereotype.Service;
27 import javax.annotation.PostConstruct;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
33 import java.util.HashMap;
35 import java.util.StringTokenizer;
40 @Service( "mimeTpes" )
41 public class MimeTypes
43 private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
45 private String resource = "org/apache/archiva/webdav/util/mime.types";
47 private Map<String, String> mimeMap = new HashMap<>();
49 private Logger log = LoggerFactory.getLogger( MimeTypes.class );
52 * Get the Mime Type for the provided filename.
54 * @param filename the filename to obtain the mime type for.
55 * @return a mime type String, or null if filename is null, has no extension, or no mime type is associated with it.
57 public String getMimeType( String filename )
60 if ( !StringUtils.isEmpty( filename ) )
62 int index = filename.lastIndexOf( '.' );
66 value = (String) mimeMap.get( filename.substring( index + 1 ).toLowerCase() );
72 value = DEFAULT_MIME_TYPE;
80 public void initialize()
85 public void load( String resourceName )
87 ClassLoader cloader = this.getClass().getClassLoader();
89 /* Load up the mime types table */
90 URL mimeURL = cloader.getResource( resourceName );
92 if ( mimeURL == null )
94 throw new IllegalStateException( "Unable to find resource " + resourceName );
97 try (InputStream mimeStream = mimeURL.openStream())
101 catch ( IOException e )
103 log.error( "Unable to load mime map " + resourceName + " : " + e.getMessage(), e );
107 public void load( InputStream mimeStream )
111 try (InputStreamReader reader = new InputStreamReader( mimeStream ))
113 try (BufferedReader buf = new BufferedReader( reader ))
118 while ( ( line = buf.readLine() ) != null )
122 if ( line.length() == 0 )
124 // empty line. skip it
128 if ( line.startsWith( "#" ) )
134 StringTokenizer tokenizer = new StringTokenizer( line );
135 if ( tokenizer.countTokens() > 1 )
137 String type = tokenizer.nextToken();
138 while ( tokenizer.hasMoreTokens() )
140 String extension = tokenizer.nextToken().toLowerCase();
141 this.mimeMap.put( extension, type );
147 catch ( IOException e )
149 log.error( "Unable to read mime types from input stream : " + e.getMessage(), e );
153 public String getResource()
158 public void setResource( String resource )
160 this.resource = resource;