001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.server.plugins;
021
022import com.google.common.base.Strings;
023import com.google.common.collect.ImmutableMap;
024import org.apache.commons.io.FilenameUtils;
025
026import java.util.Locale;
027import java.util.Map;
028
029/**
030 * @since 3.1
031 */
032public final class MimeTypes {
033  private MimeTypes() {
034  }
035
036  private static final Map<String, String> MAP = new ImmutableMap.Builder<String, String>()
037    .put("json", "application/json")
038    .put("zip", "application/zip")
039    .put("tgz", "application/tgz")
040    .put("ps", "application/postscript")
041    .put("jnlp", "application/jnlp")
042    .put("jar", "application/java-archive")
043    .put("xls", "application/vnd.ms-excel")
044    .put("ppt", "application/vnd.ms-powerpoint")
045    .put("tar", "application/x-tar")
046    .put("xml", "application/xml")
047    .put("dtd", "application/xml-dtd")
048    .put("xslt", "application/xslt+xml")
049    .put("bmp", "image/bmp")
050    .put("gif", "image/gif")
051    .put("jpg", "image/jpeg")
052    .put("jpeg", "image/jpeg")
053    .put("tiff", "image/tiff")
054    .put("png", "image/png")
055    .put("svg", "image/svg+xml")
056    .put("ico", "image/x-icon")
057    .put("txt", "text/plain")
058    .put("csv", "text/csv")
059    .put("properties", "text/plain")
060    .put("rtf", "text/rtf")
061    .put("html", "text/html")
062    .put("css", "text/css")
063    .put("tsv", "text/tab-separated-values")
064    .build();
065
066  public static final String DEFAULT = "application/octet-stream";
067
068  public static String getByFilename(String filename) {
069    String extension = FilenameUtils.getExtension(filename);
070    String mime = null;
071    if (!Strings.isNullOrEmpty(extension)) {
072      mime = MAP.get(extension.toLowerCase(Locale.ENGLISH));
073    }
074    return mime != null ? mime : DEFAULT;
075  }
076}