Fix b/15940781 FsAdaptor.getDocContent throws InvalidPathException This change catches InvalidPathException thrown by Paths.get() and handles it the same as the subsequent check for docid validity. Code Review: http://codereview.appspot.com/106580044
diff --git a/src/com/google/enterprise/adaptor/fs/FsAdaptor.java b/src/com/google/enterprise/adaptor/fs/FsAdaptor.java index 609588b..6f549aa 100644 --- a/src/com/google/enterprise/adaptor/fs/FsAdaptor.java +++ b/src/com/google/enterprise/adaptor/fs/FsAdaptor.java
@@ -41,6 +41,7 @@ import java.io.Writer; import java.nio.charset.Charset; import java.nio.file.AccessDeniedException; +import java.nio.file.InvalidPathException; import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.attribute.AclFileAttributeView; @@ -454,11 +455,20 @@ log.entering("FsAdaptor", "getDocContent", new Object[] {req, resp}); DocId id = req.getDocId(); - Path doc = delegate.getPath(id.getUniqueId()); + Path doc; + + try { + doc = delegate.getPath(id.getUniqueId()); + } catch (InvalidPathException e) { + log.log(Level.WARNING, + "The docid {0} is not a valid id generated by the adaptor.", id); + resp.respondNotFound(); + return; + } if (!id.equals(delegate.newDocId(doc))) { log.log(Level.WARNING, - "The {0} is not a valid id generated by the adaptor.", id); + "The docid {0} is not a valid id generated by the adaptor.", id); resp.respondNotFound(); return; }
diff --git a/test/com/google/enterprise/adaptor/fs/FsAdaptorTest.java b/test/com/google/enterprise/adaptor/fs/FsAdaptorTest.java index 6a5b25f..85e31c7 100644 --- a/test/com/google/enterprise/adaptor/fs/FsAdaptorTest.java +++ b/test/com/google/enterprise/adaptor/fs/FsAdaptorTest.java
@@ -313,6 +313,14 @@ } @Test + public void testGetDocContentInvalidPath() throws Exception { + adaptor.init(context); + MockResponse response = new MockResponse(); + adaptor.getDocContent(new MockRequest(new DocId("")), response); + assertTrue(response.notFound); + } + + @Test public void testGetDocContentUnsupportedPath() throws Exception { root.addChildren(new MockFile("unsupported").setIsRegularFile(false)); adaptor.init(context);
diff --git a/test/com/google/enterprise/adaptor/fs/MockFileDelegate.java b/test/com/google/enterprise/adaptor/fs/MockFileDelegate.java index c8fd6a1..f03f444 100644 --- a/test/com/google/enterprise/adaptor/fs/MockFileDelegate.java +++ b/test/com/google/enterprise/adaptor/fs/MockFileDelegate.java
@@ -15,6 +15,7 @@ package com.google.enterprise.adaptor.fs; import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import com.google.enterprise.adaptor.AsyncDocIdPusher; import com.google.enterprise.adaptor.DocId; @@ -25,6 +26,7 @@ import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; import java.nio.file.DirectoryStream; +import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.concurrent.BlockingQueue; @@ -68,7 +70,10 @@ @Override public Path getPath(String pathname) throws IOException { - Preconditions.checkNotNull(pathname, "pathname cannot be null"); + if (Strings.isNullOrEmpty(pathname)) { + throw new InvalidPathException(pathname, + "pathname cannot be null or empty"); + } return Paths.get(pathname); }