001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.impl;
018
019import java.net.URLConnection;
020
021import org.apache.commons.vfs2.FileContent;
022import org.apache.commons.vfs2.FileContentInfo;
023import org.apache.commons.vfs2.FileContentInfoFactory;
024
025/**
026 * The FileContentInfoFilenameFactory.
027 * <p>
028 * Uses the file name extension to determine the content-type. The content-encoding is not resolved.
029 * </p>
030 */
031public class FileContentInfoFilenameFactory implements FileContentInfoFactory {
032
033    private static final FileContentInfo NULL_INSTANCE = new DefaultFileContentInfo(null, null);
034
035    /**
036     * Constructs a new instance.
037     */
038    public FileContentInfoFilenameFactory() {
039        // empty
040    }
041
042    @Override
043    public FileContentInfo create(final FileContent fileContent) {
044        String contentType = null;
045
046        final String name = fileContent.getFile().getName().getBaseName();
047        if (name != null) {
048            contentType = URLConnection.getFileNameMap().getContentTypeFor(name);
049        }
050
051        // optimize object creation for common case
052        if (contentType == null) {
053            return NULL_INSTANCE;
054        }
055        return new DefaultFileContentInfo(contentType, null);
056    }
057}