From 18c6d657492b94e0126f24c005517d98a6dc7b1d Mon Sep 17 00:00:00 2001 From: Tsz-wo Sze Date: Tue, 16 Feb 2010 22:17:54 +0000 Subject: [PATCH] HADOOP-6560. Handle invalid har:// uri in HarFileSystem. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@910726 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 2 + .../org/apache/hadoop/fs/HarFileSystem.java | 18 +++++-- .../apache/hadoop/fs/TestHarFileSystem.java | 48 +++++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 src/test/core/org/apache/hadoop/fs/TestHarFileSystem.java diff --git a/CHANGES.txt b/CHANGES.txt index 0467f1eeef7..f8a6f7dfa90 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -202,6 +202,8 @@ Trunk (unreleased changes) HADOOP-6548. Replace mortbay imports with commons logging. (cdouglas) + HADOOP-6560. Handle invalid har:// uri in HarFileSystem. (szetszwo) + Release 0.21.0 - Unreleased INCOMPATIBLE CHANGES diff --git a/src/java/org/apache/hadoop/fs/HarFileSystem.java b/src/java/org/apache/hadoop/fs/HarFileSystem.java index d9175eedbbd..8350d46a9e5 100644 --- a/src/java/org/apache/hadoop/fs/HarFileSystem.java +++ b/src/java/org/apache/hadoop/fs/HarFileSystem.java @@ -182,12 +182,20 @@ public class HarFileSystem extends FilterFileSystem { return FileSystem.getDefaultUri(conf); } String host = rawURI.getHost(); - String[] str = host.split("-", 2); - if (str[0] == null) { - throw new IOException("URI: " + rawURI + " is an invalid Har URI."); + if (host == null) { + throw new IOException("URI: " + rawURI + + " is an invalid Har URI since host==null." + + " Expecting har://-/."); } - String underLyingScheme = str[0]; - String underLyingHost = (str.length > 1)? str[1]:null; + int i = host.indexOf('-'); + if (i < 0) { + throw new IOException("URI: " + rawURI + + " is an invalid Har URI since '-' not found." + + " Expecting har://-/."); + } + final String underLyingScheme = host.substring(0, i); + i++; + final String underLyingHost = i == host.length()? null: host.substring(i); int underLyingPort = rawURI.getPort(); String auth = (underLyingHost == null && underLyingPort == -1)? null:(underLyingHost+":"+underLyingPort); diff --git a/src/test/core/org/apache/hadoop/fs/TestHarFileSystem.java b/src/test/core/org/apache/hadoop/fs/TestHarFileSystem.java new file mode 100644 index 00000000000..cacc862448e --- /dev/null +++ b/src/test/core/org/apache/hadoop/fs/TestHarFileSystem.java @@ -0,0 +1,48 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.fs; + +import java.io.IOException; + +import org.apache.hadoop.conf.Configuration; +import org.junit.Assert; +import org.junit.Test; + +public class TestHarFileSystem { + @Test + public void testHarUri() { + final Configuration conf = new Configuration(); + checkInvalidPath("har://hdfs-/foo.har", conf); + checkInvalidPath("har://hdfs/foo.har", conf); + checkInvalidPath("har://-hdfs/foo.har", conf); + checkInvalidPath("har://-/foo.har", conf); + } + + static void checkInvalidPath(String s, Configuration conf) { + System.out.println("\ncheckInvalidPath: " + s); + final Path p = new Path(s); + try { + p.getFileSystem(conf); + Assert.fail(p + " is an invalid path."); + } catch (IOException e) { + System.out.println("GOOD: Got an exception."); + e.printStackTrace(System.out); + } + } +}