Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
svn merge -c 1353928 from branch-1 for HDFS-3516.
Browse files Browse the repository at this point in the history
  • Loading branch information
szetszwo committed Jun 26, 2012
1 parent 1bc363b commit 9b0c026
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Expand Up @@ -107,6 +107,8 @@ Release 1.1.0 - unreleased
connections and RPC calls, and add MultipleLinearRandomRetry, a new retry
policy. (szetszwo)

HDFS-3516. Check content-type in WebHdfsFileSystem. (szetszwo)

BUG FIXES

MAPREDUCE-4087. [Gridmix] GenerateDistCacheData job of Gridmix can
Expand Down
24 changes: 20 additions & 4 deletions src/hdfs/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java
Expand Up @@ -32,6 +32,8 @@
import java.util.Map;
import java.util.StringTokenizer;

import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
Expand Down Expand Up @@ -234,9 +236,23 @@ private Path makeAbsolute(Path 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) {
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));
}
Expand All @@ -247,7 +263,7 @@ private Path makeAbsolute(Path f) {
if (code != op.getExpectedHttpResponseCode()) {
final Map<?, ?> m;
try {
m = jsonParse(conn.getErrorStream());
m = jsonParse(conn, true);
} catch(IOException e) {
throw new IOException("Unexpected HTTP response: code=" + code + " != "
+ op.getExpectedHttpResponseCode() + ", " + op.toQueryString()
Expand Down Expand Up @@ -403,7 +419,7 @@ static HttpURLConnection twoStepWrite(HttpURLConnection conn,
final HttpURLConnection conn = httpConnect(op, fspath, parameters);
try {
final Map<?, ?> m = validateResponse(op, conn);
return m != null? m: jsonParse(conn.getInputStream());
return m != null? m: jsonParse(conn, false);
} finally {
conn.disconnect();
}
Expand Down
Expand Up @@ -293,6 +293,10 @@ public void testResponseCode() throws IOException {
final Path root = new Path("/");
final Path dir = new Path("/test/testUrl");
assertTrue(webhdfs.mkdirs(dir));
final Path file = new Path("/test/file");
final FSDataOutputStream out = webhdfs.create(file);
out.write(1);
out.close();

{//test GETHOMEDIRECTORY
final URL url = webhdfs.toUrl(GetOpParam.Op.GETHOMEDIRECTORY, root);
Expand Down Expand Up @@ -354,5 +358,21 @@ public void testResponseCode() throws IOException {
assertEquals((short)0755, webhdfs.getFileStatus(dir).getPermission().toShort());
conn.disconnect();
}

{//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();
}
}
}
4 changes: 2 additions & 2 deletions src/test/org/apache/hadoop/hdfs/web/WebHdfsTestUtil.java
Expand Up @@ -48,7 +48,7 @@ public static WebHdfsFileSystem getWebHdfsFileSystem(final Configuration conf

public static WebHdfsFileSystem getWebHdfsFileSystemAs(
final UserGroupInformation ugi, final Configuration conf
) throws IOException, URISyntaxException, InterruptedException {
) throws IOException, InterruptedException {
return ugi.doAs(new PrivilegedExceptionAction<WebHdfsFileSystem>() {
@Override
public WebHdfsFileSystem run() throws Exception {
Expand All @@ -69,7 +69,7 @@ public static URL toUrl(final WebHdfsFileSystem webhdfs,
final int expectedResponseCode) throws IOException {
conn.connect();
Assert.assertEquals(expectedResponseCode, conn.getResponseCode());
return WebHdfsFileSystem.jsonParse(conn.getInputStream());
return WebHdfsFileSystem.jsonParse(conn, false);
}

public static HttpURLConnection twoStepWrite(HttpURLConnection conn,
Expand Down

0 comments on commit 9b0c026

Please sign in to comment.