HDFS-3516. Check content-type in WebHdfsFileSystem.
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1353800 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d9bbd5997d
commit
361ea9a62c
|
@ -238,6 +238,8 @@ Branch-2 ( Unreleased changes )
|
||||||
HDFS-3372. offlineEditsViewer should be able to read a binary
|
HDFS-3372. offlineEditsViewer should be able to read a binary
|
||||||
edits file with recovery mode. (Colin Patrick McCabe via eli)
|
edits file with recovery mode. (Colin Patrick McCabe via eli)
|
||||||
|
|
||||||
|
HDFS-3516. Check content-type in WebHdfsFileSystem. (szetszwo)
|
||||||
|
|
||||||
OPTIMIZATIONS
|
OPTIMIZATIONS
|
||||||
|
|
||||||
HDFS-2982. Startup performance suffers when there are many edit log
|
HDFS-2982. Startup performance suffers when there are many edit log
|
||||||
|
|
|
@ -34,6 +34,8 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.apache.hadoop.conf.Configuration;
|
import org.apache.hadoop.conf.Configuration;
|
||||||
|
@ -252,9 +254,23 @@ public class WebHdfsFileSystem extends FileSystem
|
||||||
return f.isAbsolute()? f: new Path(workingDir, f);
|
return f.isAbsolute()? f: new Path(workingDir, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Map<?, ?> jsonParse(final InputStream in) throws IOException {
|
static Map<?, ?> jsonParse(final HttpURLConnection c, final boolean useErrorStream
|
||||||
|
) throws IOException {
|
||||||
|
if (c.getContentLength() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
final InputStream in = useErrorStream? c.getErrorStream(): c.getInputStream();
|
||||||
if (in == null) {
|
if (in == null) {
|
||||||
throw new IOException("The input stream is null.");
|
throw new IOException("The " + (useErrorStream? "error": "input") + " stream is null.");
|
||||||
|
}
|
||||||
|
final String contentType = c.getContentType();
|
||||||
|
if (contentType != null) {
|
||||||
|
final MediaType parsed = MediaType.valueOf(contentType);
|
||||||
|
if (!MediaType.APPLICATION_JSON_TYPE.isCompatible(parsed)) {
|
||||||
|
throw new IOException("Content-Type \"" + contentType
|
||||||
|
+ "\" is incompatible with \"" + MediaType.APPLICATION_JSON
|
||||||
|
+ "\" (parsed=\"" + parsed + "\")");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return (Map<?, ?>)JSON.parse(new InputStreamReader(in));
|
return (Map<?, ?>)JSON.parse(new InputStreamReader(in));
|
||||||
}
|
}
|
||||||
|
@ -265,7 +281,7 @@ public class WebHdfsFileSystem extends FileSystem
|
||||||
if (code != op.getExpectedHttpResponseCode()) {
|
if (code != op.getExpectedHttpResponseCode()) {
|
||||||
final Map<?, ?> m;
|
final Map<?, ?> m;
|
||||||
try {
|
try {
|
||||||
m = jsonParse(conn.getErrorStream());
|
m = jsonParse(conn, true);
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
throw new IOException("Unexpected HTTP response: code=" + code + " != "
|
throw new IOException("Unexpected HTTP response: code=" + code + " != "
|
||||||
+ op.getExpectedHttpResponseCode() + ", " + op.toQueryString()
|
+ op.getExpectedHttpResponseCode() + ", " + op.toQueryString()
|
||||||
|
@ -425,7 +441,7 @@ public class WebHdfsFileSystem extends FileSystem
|
||||||
final HttpURLConnection conn = httpConnect(op, fspath, parameters);
|
final HttpURLConnection conn = httpConnect(op, fspath, parameters);
|
||||||
try {
|
try {
|
||||||
final Map<?, ?> m = validateResponse(op, conn);
|
final Map<?, ?> m = validateResponse(op, conn);
|
||||||
return m != null? m: jsonParse(conn.getInputStream());
|
return m != null? m: jsonParse(conn, false);
|
||||||
} finally {
|
} finally {
|
||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
}
|
}
|
||||||
|
|
|
@ -287,6 +287,10 @@ public class TestWebHdfsFileSystemContract extends FileSystemContractBaseTest {
|
||||||
final Path root = new Path("/");
|
final Path root = new Path("/");
|
||||||
final Path dir = new Path("/test/testUrl");
|
final Path dir = new Path("/test/testUrl");
|
||||||
assertTrue(webhdfs.mkdirs(dir));
|
assertTrue(webhdfs.mkdirs(dir));
|
||||||
|
final Path file = new Path("/test/file");
|
||||||
|
final FSDataOutputStream out = webhdfs.create(file);
|
||||||
|
out.write(1);
|
||||||
|
out.close();
|
||||||
|
|
||||||
{//test GETHOMEDIRECTORY
|
{//test GETHOMEDIRECTORY
|
||||||
final URL url = webhdfs.toUrl(GetOpParam.Op.GETHOMEDIRECTORY, root);
|
final URL url = webhdfs.toUrl(GetOpParam.Op.GETHOMEDIRECTORY, root);
|
||||||
|
@ -378,5 +382,21 @@ public class TestWebHdfsFileSystemContract extends FileSystemContractBaseTest {
|
||||||
conn.connect();
|
conn.connect();
|
||||||
assertEquals(HttpServletResponse.SC_BAD_REQUEST, conn.getResponseCode());
|
assertEquals(HttpServletResponse.SC_BAD_REQUEST, conn.getResponseCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{//test jsonParse with non-json type.
|
||||||
|
final HttpOpParam.Op op = GetOpParam.Op.OPEN;
|
||||||
|
final URL url = webhdfs.toUrl(op, file);
|
||||||
|
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setRequestMethod(op.getType().toString());
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
|
try {
|
||||||
|
WebHdfsFileSystem.jsonParse(conn, false);
|
||||||
|
fail();
|
||||||
|
} catch(IOException ioe) {
|
||||||
|
WebHdfsFileSystem.LOG.info("GOOD", ioe);
|
||||||
|
}
|
||||||
|
conn.disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ public class WebHdfsTestUtil {
|
||||||
|
|
||||||
public static WebHdfsFileSystem getWebHdfsFileSystemAs(
|
public static WebHdfsFileSystem getWebHdfsFileSystemAs(
|
||||||
final UserGroupInformation ugi, final Configuration conf
|
final UserGroupInformation ugi, final Configuration conf
|
||||||
) throws IOException, URISyntaxException, InterruptedException {
|
) throws IOException, InterruptedException {
|
||||||
return ugi.doAs(new PrivilegedExceptionAction<WebHdfsFileSystem>() {
|
return ugi.doAs(new PrivilegedExceptionAction<WebHdfsFileSystem>() {
|
||||||
@Override
|
@Override
|
||||||
public WebHdfsFileSystem run() throws Exception {
|
public WebHdfsFileSystem run() throws Exception {
|
||||||
|
@ -70,7 +70,7 @@ public class WebHdfsTestUtil {
|
||||||
final int expectedResponseCode) throws IOException {
|
final int expectedResponseCode) throws IOException {
|
||||||
conn.connect();
|
conn.connect();
|
||||||
Assert.assertEquals(expectedResponseCode, conn.getResponseCode());
|
Assert.assertEquals(expectedResponseCode, conn.getResponseCode());
|
||||||
return WebHdfsFileSystem.jsonParse(conn.getInputStream());
|
return WebHdfsFileSystem.jsonParse(conn, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HttpURLConnection twoStepWrite(HttpURLConnection conn,
|
public static HttpURLConnection twoStepWrite(HttpURLConnection conn,
|
||||||
|
|
Loading…
Reference in New Issue