Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.Blocker;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -94,6 +98,30 @@ public void testMultiBufferFileMappedMaxBufferSizeRounding(int maxBufferSize) th
assertThat(writeToString(content, 0, -1), is("0123456789abcdefghijABCDEFGHIJ"));
}

@ParameterizedTest
@ValueSource(ints = {8, 10})
public void testUnsupportedFileSystemStillServesContent(int maxBufferSize) throws IOException
{
Path jarFile = MavenTestingUtils.getTestResourcePathFile("example.jar");

try (ResourceFactory.Closeable resourceFactory = ResourceFactory.closeable())
{
Resource jarResource = resourceFactory.newJarFileResource(jarFile.toUri());

// First make sure that this kind of FS cannot mmap files.
Resource resolved = jarResource.resolve("WEB-INF/web.xml");
try (FileChannel channel = FileChannel.open(resolved.getPath(), StandardOpenOption.READ))
{
assertThrows(UnsupportedOperationException.class, () -> channel.map(FileChannel.MapMode.READ_ONLY, 0, resolved.length()));
}

// Then make sure FileMappingHttpContentFactory manages to fall back to some other way to serve the content.
FileMappingHttpContentFactory fileMappingHttpContentFactory = new FileMappingHttpContentFactory(new ResourceHttpContentFactory(jarResource, MimeTypes.DEFAULTS, ByteBufferPool.SIZED_NON_POOLING), 0, maxBufferSize);
HttpContent content = fileMappingHttpContentFactory.getContent("WEB-INF/web.xml");
assertThat(content.getContentLengthValue(), is(35L));
}
}

private static String writeToString(HttpContent content, long offset, long length) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.Buffer;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -1196,7 +1195,7 @@ public static ByteBuffer toMappedBuffer(Path filePath, long pos, long len) throw
{
return channel.map(MapMode.READ_ONLY, pos, len);
}
catch (UnsupportedEncodingException e)
catch (UnsupportedOperationException e)
{
if (LOG.isTraceEnabled())
LOG.trace("Ignored exception", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -392,4 +393,16 @@ public void testToMappedBufferResource() throws Exception
assertThat(BufferUtil.toMappedBuffer(jarResource), nullValue());
}
}

@Test
public void testToMappedBufferOnUnsupportedFileSystem() throws Exception
{
Path jarFile = MavenTestingUtils.getTestResourcePathFile("example.jar");

try (ResourceFactory.Closeable resourceFactory = ResourceFactory.closeable())
{
Path jarPath = resourceFactory.newJarFileResource(jarFile.toUri()).resolve("WEB-INF/web.xml").getPath();
assertThat(BufferUtil.toMappedBuffer(jarPath), nullValue());
}
}
}