001package org.apache.archiva.webdav.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.commons.lang3.StringUtils; 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025import org.springframework.stereotype.Service; 026 027import javax.annotation.PostConstruct; 028import java.io.BufferedReader; 029import java.io.IOException; 030import java.io.InputStream; 031import java.io.InputStreamReader; 032import java.net.URL; 033import java.util.HashMap; 034import java.util.Map; 035import java.util.StringTokenizer; 036 037/** 038 * MimeTypes 039 */ 040@Service( "mimeTpes" ) 041public class MimeTypes 042{ 043 private static final String DEFAULT_MIME_TYPE = "application/octet-stream"; 044 045 private String resource = "org/apache/archiva/webdav/util/mime.types"; 046 047 private Map<String, String> mimeMap = new HashMap<>(); 048 049 private Logger log = LoggerFactory.getLogger( MimeTypes.class ); 050 051 /** 052 * Get the Mime Type for the provided filename. 053 * 054 * @param filename the filename to obtain the mime type for. 055 * @return a mime type String, or null if filename is null, has no extension, or no mime type is associated with it. 056 */ 057 public String getMimeType( String filename ) 058 { 059 String value = null; 060 if ( !StringUtils.isEmpty( filename ) ) 061 { 062 int index = filename.lastIndexOf( '.' ); 063 064 if ( index >= 0 ) 065 { 066 value = (String) mimeMap.get( filename.substring( index + 1 ).toLowerCase() ); 067 } 068 } 069 070 if ( value == null ) 071 { 072 value = DEFAULT_MIME_TYPE; 073 } 074 075 return value; 076 077 } 078 079 @PostConstruct 080 public void initialize() 081 { 082 load( resource ); 083 } 084 085 public void load( String resourceName ) 086 { 087 ClassLoader cloader = this.getClass().getClassLoader(); 088 089 /* Load up the mime types table */ 090 URL mimeURL = cloader.getResource( resourceName ); 091 092 if ( mimeURL == null ) 093 { 094 throw new IllegalStateException( "Unable to find resource " + resourceName ); 095 } 096 097 try (InputStream mimeStream = mimeURL.openStream()) 098 { 099 load( mimeStream ); 100 } 101 catch ( IOException e ) 102 { 103 log.error( "Unable to load mime map {} : {}", resourceName, e.getMessage(), e ); 104 } 105 } 106 107 public void load( InputStream mimeStream ) 108 { 109 mimeMap.clear(); 110 111 try (InputStreamReader reader = new InputStreamReader( mimeStream )) 112 { 113 try (BufferedReader buf = new BufferedReader( reader )) 114 { 115 116 String line = null; 117 118 while ( ( line = buf.readLine() ) != null ) 119 { 120 line = line.trim(); 121 122 if ( line.length() == 0 ) 123 { 124 // empty line. skip it 125 continue; 126 } 127 128 if ( line.startsWith( "#" ) ) 129 { 130 // Comment. skip it 131 continue; 132 } 133 134 StringTokenizer tokenizer = new StringTokenizer( line ); 135 if ( tokenizer.countTokens() > 1 ) 136 { 137 String type = tokenizer.nextToken(); 138 while ( tokenizer.hasMoreTokens() ) 139 { 140 String extension = tokenizer.nextToken().toLowerCase(); 141 this.mimeMap.put( extension, type ); 142 } 143 } 144 } 145 } 146 } 147 catch ( IOException e ) 148 { 149 log.error( "Unable to read mime types from input stream : {}", e.getMessage(), e ); 150 } 151 } 152 153 public String getResource() 154 { 155 return resource; 156 } 157 158 public void setResource( String resource ) 159 { 160 this.resource = resource; 161 } 162}