Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--unzip-repository-plugin/src/main/java/org/eclipse/tycho/nexus/internal/plugin/storage/MimeTypeHelper.java44
-rw-r--r--unzip-repository-plugin/src/main/java/org/eclipse/tycho/nexus/internal/plugin/storage/ZippedItem.java6
-rw-r--r--unzip-repository-plugin/src/test/java/org/eclipse/tycho/nexus/internal/plugin/storage/MimeTypeHelperTest.java49
3 files changed, 95 insertions, 4 deletions
diff --git a/unzip-repository-plugin/src/main/java/org/eclipse/tycho/nexus/internal/plugin/storage/MimeTypeHelper.java b/unzip-repository-plugin/src/main/java/org/eclipse/tycho/nexus/internal/plugin/storage/MimeTypeHelper.java
new file mode 100644
index 0000000..e831acc
--- /dev/null
+++ b/unzip-repository-plugin/src/main/java/org/eclipse/tycho/nexus/internal/plugin/storage/MimeTypeHelper.java
@@ -0,0 +1,44 @@
+/*******************************************************************************
+ * Copyright (c) 2014 SAP AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * SAP AG - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tycho.nexus.internal.plugin.storage;
+
+import java.net.URLConnection;
+import java.util.HashMap;
+import java.util.Map;
+
+public class MimeTypeHelper {
+
+ private static final Map<String, String> EXTENSION_TO_MIMETYPE_MAP = createExtensionToMimetypeMap();
+
+ private static Map<String, String> createExtensionToMimetypeMap() {
+ Map<String, String> result = new HashMap<String, String>();
+ result.put("js", "application/javascript");
+ result.put("properties", "text/plain");
+ result.put("json", "application/json");
+ result.put("css", "text/css");
+ result.put("less", "text/css");
+ return result;
+ }
+
+ private MimeTypeHelper() {
+ }
+
+ public static String guessMimeType(String path) {
+ String mimeType = URLConnection.guessContentTypeFromName(path);
+ if (mimeType == null) {
+ int lastDot = path.lastIndexOf('.');
+ if (lastDot != -1) {
+ mimeType = EXTENSION_TO_MIMETYPE_MAP.get(path.substring(lastDot + 1));
+ }
+ }
+ return mimeType;
+ }
+}
diff --git a/unzip-repository-plugin/src/main/java/org/eclipse/tycho/nexus/internal/plugin/storage/ZippedItem.java b/unzip-repository-plugin/src/main/java/org/eclipse/tycho/nexus/internal/plugin/storage/ZippedItem.java
index 62e99a8..5e8249b 100644
--- a/unzip-repository-plugin/src/main/java/org/eclipse/tycho/nexus/internal/plugin/storage/ZippedItem.java
+++ b/unzip-repository-plugin/src/main/java/org/eclipse/tycho/nexus/internal/plugin/storage/ZippedItem.java
@@ -13,7 +13,6 @@ package org.eclipse.tycho.nexus.internal.plugin.storage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import java.net.URLConnection;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
@@ -225,11 +224,10 @@ public class ZippedItem {
* directory
*/
public String getMimeType() {
- if (!isDirectory()) {
- return URLConnection.guessContentTypeFromName(pathInZip);
- } else {
+ if (isDirectory()) {
return null;
}
+ return MimeTypeHelper.guessMimeType(pathInZip);
}
private void initZippedStorageItem(final ZipEntry entry) {
diff --git a/unzip-repository-plugin/src/test/java/org/eclipse/tycho/nexus/internal/plugin/storage/MimeTypeHelperTest.java b/unzip-repository-plugin/src/test/java/org/eclipse/tycho/nexus/internal/plugin/storage/MimeTypeHelperTest.java
new file mode 100644
index 0000000..26fb9a9
--- /dev/null
+++ b/unzip-repository-plugin/src/test/java/org/eclipse/tycho/nexus/internal/plugin/storage/MimeTypeHelperTest.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Copyright (c) 2014 SAP AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * SAP AG - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tycho.nexus.internal.plugin.storage;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
+
+public class MimeTypeHelperTest {
+
+ @Test
+ public void testURLConnectionMimeTypes() throws Exception {
+ assertEquals("text/html", MimeTypeHelper.guessMimeType("test/foo.html"));
+ }
+
+ @Test
+ public void testAdditionalMimeTypes() throws Exception {
+ assertEquals("text/css", MimeTypeHelper.guessMimeType("test/foo.css"));
+ assertEquals("text/css", MimeTypeHelper.guessMimeType("test/foo.less"));
+ assertEquals("application/javascript", MimeTypeHelper.guessMimeType("test/foo.js"));
+ assertEquals("text/plain", MimeTypeHelper.guessMimeType("test/foo.properties"));
+ assertEquals("application/json", MimeTypeHelper.guessMimeType("test/test.json"));
+ }
+
+ @Test
+ public void testNoExtension() throws Exception {
+ assertNull(MimeTypeHelper.guessMimeType("test/foo"));
+ }
+
+ @Test
+ public void testMultipleDots() throws Exception {
+ assertEquals("image/jpeg", MimeTypeHelper.guessMimeType("test/foo.bar.jpeg"));
+ }
+
+ @Test
+ public void testUnknownExtension() throws Exception {
+ assertNull(MimeTypeHelper.guessMimeType("test/foo.bar"));
+ }
+
+}

Back to the top