395794 NullPointerException in when requesting a file without extension

This commit is contained in:
Jan Bartel 2012-12-17 13:58:39 +11:00
parent 930f567c32
commit 2966192aa4
5 changed files with 30 additions and 1 deletions

View File

@ -20,6 +20,7 @@ package org.eclipse.jetty.http;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test; import org.junit.Test;
@ -58,6 +59,14 @@ public class MimeTypesTest
assertMimeTypeByExtension("text/plain","TEST.TXT"); assertMimeTypeByExtension("text/plain","TEST.TXT");
} }
@Test
public void testGetMimeByExtension_NoExtension()
{
MimeTypes mimetypes = new MimeTypes();
String contentType = mimetypes.getMimeByExtension("README");
assertNull(contentType);
}
private void assertMimeTypeByExtension(String expectedMimeType, String filename) private void assertMimeTypeByExtension(String expectedMimeType, String filename)
{ {
MimeTypes mimetypes = new MimeTypes(); MimeTypes mimetypes = new MimeTypes();

View File

@ -382,7 +382,8 @@ public class ResourceCache
_key=pathInContext; _key=pathInContext;
_resource=resource; _resource=resource;
_contentType=BufferUtil.toBuffer(_mimeTypes.getMimeByExtension(_resource.toString())); String mimeType = _mimeTypes.getMimeByExtension(_resource.toString());
_contentType=(mimeType==null?null:BufferUtil.toBuffer(mimeType));
boolean exists=resource.exists(); boolean exists=resource.exists();
_lastModified=exists?resource.lastModified():-1; _lastModified=exists?resource.lastModified():-1;
_lastModifiedBytes=_lastModified<0?null:BufferUtil.toBuffer(HttpFields.formatDate(_lastModified)); _lastModifiedBytes=_lastModified<0?null:BufferUtil.toBuffer(HttpFields.formatDate(_lastModified));

View File

@ -229,6 +229,23 @@ public class ResourceCacheTest
cache.flushCache(); cache.flushCache();
} }
@Test
public void testNoextension() throws Exception
{
ResourceCollection rc = new ResourceCollection(new String[]{
"../jetty-util/src/test/resources/org/eclipse/jetty/util/resource/four/"
});
Resource[] resources = rc.getResources();
MimeTypes mime = new MimeTypes();
ResourceCache cache = new ResourceCache(null,resources[0],mime,false,false);
assertEquals("4 - four", getContent(cache, "four.txt"));
assertEquals("4 - four (no extension)", getContent(cache, "four"));
}
static String getContent(Resource r, String path) throws Exception static String getContent(Resource r, String path) throws Exception
{ {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();

View File

@ -0,0 +1 @@
4 - four (no extension)