aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-11-09 11:50:40 +0100
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-11-17 12:00:22 +0100
commite754b602349f2c69373d24036d6f9a70aa8353ea (patch)
tree8415ce94d433801bc2e07cf2f2f5d9f24865bcf2 /sonar-ws
parent7588a73be20420017455023aab36f65ce0bcfc8d (diff)
downloadsonarqube-e754b602349f2c69373d24036d6f9a70aa8353ea.tar.gz
sonarqube-e754b602349f2c69373d24036d6f9a70aa8353ea.zip
SONAR-6947 Move and rename MimeTypes to sonar-ws package
Diffstat (limited to 'sonar-ws')
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/MediaTypes.java82
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/MediaTypesTest.java44
2 files changed, 126 insertions, 0 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/MediaTypes.java b/sonar-ws/src/main/java/org/sonarqube/ws/MediaTypes.java
new file mode 100644
index 00000000000..ab3beddeca9
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/MediaTypes.java
@@ -0,0 +1,82 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonarqube.ws;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
+import org.apache.commons.io.FilenameUtils;
+
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * @since 3.1
+ */
+public final class MediaTypes {
+
+ public static final String JSON = "application/json";
+ public static final String XML = "application/xml";
+ public static final String TXT = "text/plain";
+ public static final String PROTOBUF = "application/x-protobuf";
+ public static final String DEFAULT = "application/octet-stream";
+
+ private static final Map<String, String> MAP = new ImmutableMap.Builder<String, String>()
+ .put("json", JSON)
+ .put("zip", "application/zip")
+ .put("tgz", "application/tgz")
+ .put("ps", "application/postscript")
+ .put("jnlp", "application/jnlp")
+ .put("jar", "application/java-archive")
+ .put("xls", "application/vnd.ms-excel")
+ .put("ppt", "application/vnd.ms-powerpoint")
+ .put("tar", "application/x-tar")
+ .put("xml", XML)
+ .put("dtd", "application/xml-dtd")
+ .put("xslt", "application/xslt+xml")
+ .put("bmp", "image/bmp")
+ .put("gif", "image/gif")
+ .put("jpg", "image/jpeg")
+ .put("jpeg", "image/jpeg")
+ .put("tiff", "image/tiff")
+ .put("png", "image/png")
+ .put("svg", "image/svg+xml")
+ .put("ico", "image/x-icon")
+ .put("txt", TXT)
+ .put("csv", "text/csv")
+ .put("properties", "text/plain")
+ .put("rtf", "text/rtf")
+ .put("html", "text/html")
+ .put("css", "text/css")
+ .put("tsv", "text/tab-separated-values")
+ .build();
+
+ private MediaTypes() {
+ // only static methods
+ }
+
+ public static String getByFilename(String filename) {
+ String extension = FilenameUtils.getExtension(filename);
+ String mime = null;
+ if (!Strings.isNullOrEmpty(extension)) {
+ mime = MAP.get(extension.toLowerCase(Locale.ENGLISH));
+ }
+ return mime != null ? mime : DEFAULT;
+ }
+}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/MediaTypesTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/MediaTypesTest.java
new file mode 100644
index 00000000000..c1b7b9d257a
--- /dev/null
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/MediaTypesTest.java
@@ -0,0 +1,44 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonarqube.ws;
+
+import org.junit.Test;
+import org.sonar.test.TestUtils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class MediaTypesTest {
+ @Test
+ public void getByFilename_default_mime_type() {
+ assertThat(MediaTypes.getByFilename("")).isEqualTo(MediaTypes.DEFAULT);
+ assertThat(MediaTypes.getByFilename("unknown.extension")).isEqualTo(MediaTypes.DEFAULT);
+ }
+
+ @Test
+ public void getByFilename() {
+ assertThat(MediaTypes.getByFilename("static/sqale/sqale.css")).isEqualTo("text/css");
+ assertThat(MediaTypes.getByFilename("sqale.css")).isEqualTo("text/css");
+ }
+
+ @Test
+ public void only_statics() {
+ assertThat(TestUtils.hasOnlyPrivateConstructors(MediaTypes.class)).isTrue();
+ }
+}