This project has retired. For details please refer to its Attic page.
MimeTypes xref
View Javadoc
1   package org.apache.archiva.webdav.util;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.commons.lang3.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   * MimeTypes
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       * Get the Mime Type for the provided filename.
53       *
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.
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          /* Load up the mime types table */
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                         // 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 }