1 | |
package org.apache.maven.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 java.io.BufferedReader; |
23 | |
import java.io.File; |
24 | |
import java.io.FileInputStream; |
25 | |
import java.io.FileNotFoundException; |
26 | |
import java.io.IOException; |
27 | |
import java.io.InputStream; |
28 | |
import java.io.InputStreamReader; |
29 | |
import java.net.URL; |
30 | |
import java.util.HashMap; |
31 | |
import java.util.Map; |
32 | |
import java.util.StringTokenizer; |
33 | |
|
34 | |
import org.apache.commons.io.IOUtils; |
35 | |
import org.apache.commons.lang.StringUtils; |
36 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; |
37 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; |
38 | |
import org.slf4j.Logger; |
39 | |
import org.slf4j.LoggerFactory; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class MimeTypes |
49 | |
implements Initializable |
50 | |
{ |
51 | |
private static final String DEFAULT_MIME_TYPE = "application/octet-stream"; |
52 | |
|
53 | 0 | private String resource = "org/apache/maven/archiva/webdav/util/mime.types"; |
54 | |
|
55 | 0 | private Map<String, String> mimeMap = new HashMap<String, String>(); |
56 | |
|
57 | 0 | private Logger log = LoggerFactory.getLogger( MimeTypes.class ); |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public String getMimeType( String filename ) |
66 | |
{ |
67 | 0 | String value = null; |
68 | 0 | if ( !StringUtils.isEmpty( filename ) ) |
69 | |
{ |
70 | 0 | int index = filename.lastIndexOf( '.' ); |
71 | |
|
72 | 0 | if ( index >= 0 ) |
73 | |
{ |
74 | 0 | value = (String) mimeMap.get( filename.substring( index + 1 ).toLowerCase() ); |
75 | |
} |
76 | |
} |
77 | |
|
78 | |
|
79 | 0 | if (value == null) |
80 | |
{ |
81 | 0 | value = DEFAULT_MIME_TYPE; |
82 | |
} |
83 | |
|
84 | 0 | return value; |
85 | |
|
86 | |
} |
87 | |
|
88 | |
public void initialize() |
89 | |
throws InitializationException |
90 | |
{ |
91 | 0 | load( resource ); |
92 | 0 | } |
93 | |
|
94 | |
public void load( File file ) |
95 | |
{ |
96 | 0 | if ( !file.exists() || !file.isFile() || !file.canRead() ) |
97 | |
{ |
98 | 0 | log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : not a readable file." ); |
99 | 0 | return; |
100 | |
} |
101 | |
|
102 | 0 | FileInputStream fis = null; |
103 | |
|
104 | |
try |
105 | |
{ |
106 | 0 | fis = new FileInputStream( file ); |
107 | |
} |
108 | 0 | catch ( FileNotFoundException e ) |
109 | |
{ |
110 | 0 | log.error( "Unable to load mime types from file " + file.getAbsolutePath() + " : " + e.getMessage(), e ); |
111 | |
} |
112 | |
finally |
113 | |
{ |
114 | 0 | IOUtils.closeQuietly( fis ); |
115 | 0 | } |
116 | 0 | } |
117 | |
|
118 | |
public void load( String resourceName ) |
119 | |
{ |
120 | 0 | ClassLoader cloader = this.getClass().getClassLoader(); |
121 | |
|
122 | |
|
123 | 0 | URL mimeURL = cloader.getResource( resourceName ); |
124 | |
|
125 | 0 | if ( mimeURL == null ) |
126 | |
{ |
127 | 0 | throw new IllegalStateException( "Unable to find resource " + resourceName ); |
128 | |
} |
129 | |
|
130 | 0 | InputStream mimeStream = null; |
131 | |
|
132 | |
try |
133 | |
{ |
134 | 0 | mimeStream = mimeURL.openStream(); |
135 | 0 | load( mimeStream ); |
136 | |
} |
137 | 0 | catch ( IOException e ) |
138 | |
{ |
139 | 0 | log.error( "Unable to load mime map " + resourceName + " : " + e.getMessage(), e ); |
140 | |
} |
141 | |
finally |
142 | |
{ |
143 | 0 | IOUtils.closeQuietly( mimeStream ); |
144 | 0 | } |
145 | 0 | } |
146 | |
|
147 | |
public void load( InputStream mimeStream ) |
148 | |
{ |
149 | 0 | mimeMap.clear(); |
150 | |
|
151 | 0 | InputStreamReader reader = null; |
152 | 0 | BufferedReader buf = null; |
153 | |
|
154 | |
try |
155 | |
{ |
156 | 0 | reader = new InputStreamReader( mimeStream ); |
157 | 0 | buf = new BufferedReader( reader ); |
158 | 0 | String line = null; |
159 | |
|
160 | 0 | while ( ( line = buf.readLine() ) != null ) |
161 | |
{ |
162 | 0 | line = line.trim(); |
163 | |
|
164 | 0 | if ( line.length() == 0 ) |
165 | |
{ |
166 | |
|
167 | 0 | continue; |
168 | |
} |
169 | |
|
170 | 0 | if ( line.startsWith( "#" ) ) |
171 | |
{ |
172 | |
|
173 | 0 | continue; |
174 | |
} |
175 | |
|
176 | 0 | StringTokenizer tokenizer = new StringTokenizer( line ); |
177 | 0 | if ( tokenizer.countTokens() > 1 ) |
178 | |
{ |
179 | 0 | String type = tokenizer.nextToken(); |
180 | 0 | while ( tokenizer.hasMoreTokens() ) |
181 | |
{ |
182 | 0 | String extension = tokenizer.nextToken().toLowerCase(); |
183 | 0 | this.mimeMap.put( extension, type ); |
184 | 0 | } |
185 | |
} |
186 | 0 | } |
187 | |
} |
188 | 0 | catch ( IOException e ) |
189 | |
{ |
190 | 0 | log.error( "Unable to read mime types from input stream : " + e.getMessage(), e ); |
191 | |
} |
192 | |
finally |
193 | |
{ |
194 | 0 | IOUtils.closeQuietly( buf ); |
195 | 0 | IOUtils.closeQuietly( reader ); |
196 | 0 | } |
197 | 0 | } |
198 | |
} |