This project has retired. For details please refer to its
Attic page.
MimeTypes xref
1 package org.apache.archiva.webdav.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.StringUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.stereotype.Service;
26
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;
32 import java.net.URL;
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.StringTokenizer;
36
37
38
39
40 @Service( "mimeTpes" )
41 public class MimeTypes
42 {
43 private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
44
45 private String resource = "org/apache/archiva/webdav/util/mime.types";
46
47 private Map<String, String> mimeMap = new HashMap<>();
48
49 private Logger log = LoggerFactory.getLogger( MimeTypes.class );
50
51
52
53
54
55
56
57 public String getMimeType( String filename )
58 {
59 String value = null;
60 if ( !StringUtils.isEmpty( filename ) )
61 {
62 int index = filename.lastIndexOf( '.' );
63
64 if ( index >= 0 )
65 {
66 value = (String) mimeMap.get( filename.substring( index + 1 ).toLowerCase() );
67 }
68 }
69
70 if ( value == null )
71 {
72 value = DEFAULT_MIME_TYPE;
73 }
74
75 return value;
76
77 }
78
79 @PostConstruct
80 public void initialize()
81 {
82 load( resource );
83 }
84
85 public void load( String resourceName )
86 {
87 ClassLoader cloader = this.getClass().getClassLoader();
88
89
90 URL mimeURL = cloader.getResource( resourceName );
91
92 if ( mimeURL == null )
93 {
94 throw new IllegalStateException( "Unable to find resource " + resourceName );
95 }
96
97 try (InputStream mimeStream = mimeURL.openStream())
98 {
99 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
125 continue;
126 }
127
128 if ( line.startsWith( "#" ) )
129 {
130
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 }