alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (ConfigurableMimeFileTypeMap.java)

This example Spring Framework source code file (ConfigurableMimeFileTypeMap.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

classpathresource, configurablemimefiletypemap, filetypemap, filetypemap, illegalstateexception, initializingbean, io, ioexception, ioexception, mime, mimetypesfiletypemap, mimetypesfiletypemap, resource, string, string

The Spring Framework ConfigurableMimeFileTypeMap.java source code

/*
 * Copyright 2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.mail.javamail;

import java.io.File;
import java.io.IOException;

import javax.activation.FileTypeMap;
import javax.activation.MimetypesFileTypeMap;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * Spring-configurable <code>FileTypeMap implementation that will read
 * MIME type to file extension mappings from a standard JavaMail MIME type
 * mapping file, using a standard <code>MimetypesFileTypeMap underneath.
 *
 * <p>The mapping file should be in the following format, as specified by the
 * Java Activation Framework:
 *
 * <pre>
 * # map text/html to .htm and .html files
 * text/html  html htm HTML HTM</pre>
 *
 * Lines starting with <code># are treated as comments and are ignored. All
 * other lines are treated as mappings. Each mapping line should contain the MIME
 * type as the first entry and then each file extension to map to that MIME type
 * as subsequent entries. Each entry is separated by spaces or tabs.
 *
 * <p>By default, the mappings in the mime.types file located in the
 * same package as this class are used, which cover many common file extensions
 * (in contrast to the out-of-the-box mappings in <code>activation.jar).
 * This can be overridden using the <code>mappingLocation property.
 *
 * <p>Additional mappings can be added via the mappings bean property,
 * as lines that follow the <code>mime.types file format.
 *
 * @author Rob Harrop
 * @author Juergen Hoeller
 * @since 1.2
 * @see #setMappingLocation
 * @see #setMappings
 * @see javax.activation.MimetypesFileTypeMap
 */
public class ConfigurableMimeFileTypeMap extends FileTypeMap implements InitializingBean {

	/**
	 * The <code>Resource to load the mapping file from.
	 */
	private Resource mappingLocation = new ClassPathResource("mime.types", getClass());

	/**
	 * Used to configure additional mappings.
	 */
	private String[] mappings;

	/**
	 * The delegate FileTypeMap, compiled from the mappings in the mapping file
	 * and the entries in the <code>mappings property.
	 */
	private FileTypeMap fileTypeMap;


	/**
	 * Specify the <code>Resource from which mappings are loaded.
	 * Needs to follow the <code>mime.types file format, as specified
	 * by the Java Activation Framework, containing lines such as:<br>
	 * <code>text/html  html htm HTML HTM
	 */
	public void setMappingLocation(Resource mappingLocation) {
		this.mappingLocation = mappingLocation;
	}

	/**
	 * Specify additional MIME type mappings as lines that follow the
	 * <code>mime.types file format, as specified by the
	 * Java Activation Framework, for example:<br>
	 * <code>text/html  html htm HTML HTM
	 */
	public void setMappings(String[] mappings) {
		this.mappings = mappings;
	}


	/**
	 * Creates the final merged mapping set.
	 */
	public void afterPropertiesSet() {
		getFileTypeMap();
	}

	/**
	 * Return the delegate FileTypeMap, compiled from the mappings in the mapping file
	 * and the entries in the <code>mappings property.
	 * @see #setMappingLocation
	 * @see #setMappings
	 * @see #createFileTypeMap
	 */
	protected final FileTypeMap getFileTypeMap() {
		if (this.fileTypeMap == null) {
			try {
				this.fileTypeMap = createFileTypeMap(this.mappingLocation, this.mappings);
			}
			catch (IOException ex) {
				throw new IllegalStateException(
						"Could not load specified MIME type mapping file: " + this.mappingLocation);
			}
		}
		return fileTypeMap;
	}

	/**
	 * Compile a FileTypeMap from the mappings in the given mapping file and the
	 * given mapping entries.
	 * <p>Default implementation creates an Activation Framework MimetypesFileTypeMap,
	 * passing in an InputStream from the mapping resource (if any) and registering
	 * the mapping lines programmatically.
	 * @param mappingLocation a <code>mime.types mapping resource (can be null)
	 * @param mappings MIME type mapping lines (can be <code>null)
	 * @return the compiled FileTypeMap
	 * @throws IOException if resource access failed
	 * @see javax.activation.MimetypesFileTypeMap#MimetypesFileTypeMap(java.io.InputStream)
	 * @see javax.activation.MimetypesFileTypeMap#addMimeTypes(String)
	 */
	protected FileTypeMap createFileTypeMap(Resource mappingLocation, String[] mappings) throws IOException {
		MimetypesFileTypeMap fileTypeMap = (mappingLocation != null) ?
				new MimetypesFileTypeMap(mappingLocation.getInputStream()) : new MimetypesFileTypeMap();
		if (mappings != null) {
			for (int i = 0; i < mappings.length; i++) {
				fileTypeMap.addMimeTypes(mappings[i]);
			}
		}
		return fileTypeMap;
	}


	/**
	 * Delegates to the underlying FileTypeMap.
	 * @see #getFileTypeMap()
	 */
	public String getContentType(File file) {
		return getFileTypeMap().getContentType(file);
	}

	/**
	 * Delegates to the underlying FileTypeMap.
	 * @see #getFileTypeMap()
	 */
	public String getContentType(String fileName) {
		return getFileTypeMap().getContentType(fileName);
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework ConfigurableMimeFileTypeMap.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.