From 4b920622d5c54da508c7d6473f29e6854a6f7669 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Wed, 3 Aug 2016 18:33:53 +1000 Subject: [PATCH] Issue #797 --- .../org/eclipse/jetty/http/MimeTypes.java | 97 +++++++------------ jetty-osgi/test-jetty-osgi-webapp/pom.xml | 1 + .../main/java/com/acme/osgi/Activator.java | 49 ++++++---- .../TestJettyOSGiBootWebAppAsService.java | 5 + 4 files changed, 73 insertions(+), 79 deletions(-) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java b/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java index d7f275dc599..c465b6a0e78 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java @@ -30,7 +30,9 @@ import java.util.HashSet; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; +import java.util.MissingResourceException; import java.util.Properties; +import java.util.ResourceBundle; import java.util.Set; import org.eclipse.jetty.util.ArrayTrie; @@ -201,79 +203,50 @@ public class MimeTypes } } - try - { - String resourceName = "org/eclipse/jetty/http/mime.properties"; - URL mimeTypesUrl = Loader.getResource(MimeTypes.class, resourceName); - if (mimeTypesUrl == null) - { - LOG.warn("Missing mime-type resource: {}", resourceName); - } - else - { - try (InputStream in = mimeTypesUrl.openStream(); - InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) - { - Properties mime = new Properties(); - mime.load(reader); - mime.stringPropertyNames().stream() - .filter(x->x!=null) - .forEach(x-> - __dftMimeMap.put(StringUtil.asciiToLowerCase(x), normalizeMimeType(mime.getProperty(x)))); - - if (__dftMimeMap.size()x!=null) + .forEach(x-> + __dftMimeMap.put(StringUtil.asciiToLowerCase(x), normalizeMimeType(mimeBundle.getString(x)))); + + if (__dftMimeMap.size()==0) { - LOG.warn("Missing mime-type resource: {}", resourceName); + LOG.warn("Empty mime types at {}", resourceName); } - else + else if (__dftMimeMap.size()t!=null) - .forEach(t->__encodings.put(t, encoding.getProperty(t))); + LOG.warn("Duplicate or null mime-type extension in resource: {}", resourceName); + } + } + catch (MissingResourceException e) + { + LOG.warn("Missing mime-type resource: {}", resourceName); + } - if (__encodings.size()t!=null) + .forEach(t->__encodings.put(t, encodingBundle.getString(t))); - if (__encodings.size()==0) - { - LOG.warn("Empty mime types declaration at {}", mimeTypesUrl); - } + if (__encodings.size()==0) + { + LOG.warn("Empty encodings at {}", resourceName); + } + else if (__encodings.size() + com.acme.osgi org.eclipse.jetty.*;version="[$(version;===;${parsedVersion.osgiVersion}),$(version;==+;${parsedVersion.osgiVersion}))" diff --git a/jetty-osgi/test-jetty-osgi-webapp/src/main/java/com/acme/osgi/Activator.java b/jetty-osgi/test-jetty-osgi-webapp/src/main/java/com/acme/osgi/Activator.java index 5ca58be8c3f..cbcd4efc449 100644 --- a/jetty-osgi/test-jetty-osgi-webapp/src/main/java/com/acme/osgi/Activator.java +++ b/jetty-osgi/test-jetty-osgi-webapp/src/main/java/com/acme/osgi/Activator.java @@ -18,10 +18,18 @@ package com.acme.osgi; +import java.io.IOException; +import java.io.PrintWriter; import java.util.Dictionary; import java.util.Hashtable; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.webapp.WebAppContext; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -34,6 +42,28 @@ import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { + + public static class TestServlet extends HttpServlet + { + + /** + * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + */ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + //report the mimetype of a file + String mimetype = req.getServletContext().getMimeType("file.gz"); + resp.setContentType("text/html"); + PrintWriter writer = resp.getWriter(); + writer.write("

MIMETYPE="+mimetype+"

"); + writer.flush(); + } + + } + + + /** * * @param context @@ -41,25 +71,10 @@ public class Activator implements BundleActivator public void start(BundleContext context) throws Exception { String serverName = "defaultJettyServer"; - - /* Uncomment to create a different server instance to deploy to. Also change - * TestJettyOSGiBootWebAppAsService to use the port 9999 - - Server server = new Server(); - //do any setup on Server in here - serverName = "fooServer"; - Dictionary serverProps = new Hashtable(); - //define the unique name of the server instance - serverProps.put("managedServerName", serverName); - serverProps.put("jetty.http.port", "9999"); - //let Jetty apply some configuration files to the Server instance - serverProps.put("jetty.etc.config.urls", "file:/opt/jetty/etc/jetty.xml,file:/opt/jetty/etc/jetty-selector.xml,file:/opt/jetty/etc/jetty-deployer.xml"); - //register as an OSGi Service for Jetty to find - context.registerService(Server.class.getName(), server, serverProps); - */ - + //Create a webapp context as a Service and target it at the Server created above WebAppContext webapp = new WebAppContext(); + webapp.addServlet(new ServletHolder(new TestServlet()), "/mime"); Dictionary props = new Hashtable(); props.put("war","."); props.put("contextPath","/acme"); diff --git a/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootWebAppAsService.java b/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootWebAppAsService.java index ee9904c5e18..f9d9c58bd74 100644 --- a/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootWebAppAsService.java +++ b/jetty-osgi/test-jetty-osgi/src/test/java/org/eclipse/jetty/osgi/test/TestJettyOSGiBootWebAppAsService.java @@ -142,6 +142,11 @@ public class TestJettyOSGiBootWebAppAsService String content = new String(response.getContent()); assertTrue(content.indexOf("

Test OSGi WebApp

") != -1); + + response = client.GET("http://127.0.0.1:" + TestJettyOSGiBootCore.DEFAULT_HTTP_PORT + "/acme/mime"); + assertEquals(HttpStatus.OK_200, response.getStatus()); + content = new String(response.getContent()); + assertTrue(content.indexOf("MIMETYPE=application/gzip") != -1); } finally {