flink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ches...@apache.org
Subject flink git commit: [FLINK-7226] [webUI] Properly include UTF-8 in content-type header
Date Wed, 26 Jul 2017 12:56:03 GMT
Repository: flink
Updated Branches:
  refs/heads/release-1.3 96ccffd66 -> 64d249eb0


[FLINK-7226] [webUI] Properly include UTF-8 in content-type header


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/64d249eb
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/64d249eb
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/64d249eb

Branch: refs/heads/release-1.3
Commit: 64d249eb00b16115fd38d1b01b9ac0caa54f0637
Parents: 96ccffd
Author: zentol <chesnay@apache.org>
Authored: Wed Jul 19 13:51:35 2017 +0200
Committer: zentol <chesnay@apache.org>
Committed: Wed Jul 26 12:22:38 2017 +0200

----------------------------------------------------------------------
 .../webmonitor/RuntimeMonitorHandler.java       |  6 +--
 .../handlers/AbstractJsonRequestHandler.java    |  2 +-
 .../JobCancellationWithSavepointHandlers.java   |  8 ++--
 .../webmonitor/WebRuntimeMonitorITCase.java     |  4 +-
 ...obCancellationWithSavepointHandlersTest.java | 14 +++----
 .../apache/flink/test/util/TestBaseUtils.java   |  2 +-
 .../flink/test/web/WebFrontendITCase.java       | 44 ++++++++++++++++++--
 7 files changed, 57 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/64d249eb/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/RuntimeMonitorHandler.java
----------------------------------------------------------------------
diff --git a/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/RuntimeMonitorHandler.java
b/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/RuntimeMonitorHandler.java
index 90fe2e0..d1925e4 100644
--- a/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/RuntimeMonitorHandler.java
+++ b/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/RuntimeMonitorHandler.java
@@ -113,7 +113,7 @@ public class RuntimeMonitorHandler extends RuntimeMonitorHandlerBase {
 			ByteBuf message = e.getMessage() == null ? Unpooled.buffer(0)
 					: Unpooled.wrappedBuffer(e.getMessage().getBytes(ENCODING));
 			response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND,
message);
-			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
+			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name());
 			response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
 			LOG.debug("Error while handling request", e);
 		}
@@ -121,15 +121,13 @@ public class RuntimeMonitorHandler extends RuntimeMonitorHandlerBase
{
 			byte[] bytes = ExceptionUtils.stringifyException(e).getBytes(ENCODING);
 			response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
 					HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
-			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
+			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name());
 			response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
 
 			LOG.debug("Error while handling request", e);
 		}
 
 		response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigin);
-		// Content-Encoding:utf-8
-		response.headers().set(HttpHeaders.Names.CONTENT_ENCODING, ENCODING.name());
 
 		KeepAliveWrite.flush(ctx, routed.request(), response);
 	}

http://git-wip-us.apache.org/repos/asf/flink/blob/64d249eb/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/handlers/AbstractJsonRequestHandler.java
----------------------------------------------------------------------
diff --git a/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/handlers/AbstractJsonRequestHandler.java
b/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/handlers/AbstractJsonRequestHandler.java
index ae163cb..c65b014 100644
--- a/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/handlers/AbstractJsonRequestHandler.java
+++ b/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/handlers/AbstractJsonRequestHandler.java
@@ -44,7 +44,7 @@ public abstract class AbstractJsonRequestHandler implements RequestHandler
{
 		DefaultFullHttpResponse response = new DefaultFullHttpResponse(
 				HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(bytes));
 
-		response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
+		response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json; charset=" + ENCODING.name());
 		response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
 
 		return response;

http://git-wip-us.apache.org/repos/asf/flink/blob/64d249eb/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/handlers/JobCancellationWithSavepointHandlers.java
----------------------------------------------------------------------
diff --git a/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/handlers/JobCancellationWithSavepointHandlers.java
b/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/handlers/JobCancellationWithSavepointHandlers.java
index f5d6853..f8ab884 100644
--- a/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/handlers/JobCancellationWithSavepointHandlers.java
+++ b/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/handlers/JobCancellationWithSavepointHandlers.java
@@ -262,7 +262,7 @@ public class JobCancellationWithSavepointHandlers {
 
 			response.headers().set(HttpHeaders.Names.LOCATION, location);
 
-			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
+			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json; charset=" +
ENCODING.name());
 			response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
 
 			FullHttpResponse accepted = response;
@@ -373,7 +373,7 @@ public class JobCancellationWithSavepointHandlers {
 					HttpResponseStatus.CREATED,
 					Unpooled.wrappedBuffer(bytes));
 
-			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
+			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json; charset=" +
ENCODING.name());
 			response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
 
 			return response;
@@ -398,7 +398,7 @@ public class JobCancellationWithSavepointHandlers {
 					HttpResponseStatus.ACCEPTED,
 					Unpooled.wrappedBuffer(bytes));
 
-			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
+			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json; charset=" +
ENCODING.name());
 			response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
 
 			return response;
@@ -424,7 +424,7 @@ public class JobCancellationWithSavepointHandlers {
 					code,
 					Unpooled.wrappedBuffer(bytes));
 
-			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");
+			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json; charset=" +
ENCODING.name());
 			response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
 
 			return response;

http://git-wip-us.apache.org/repos/asf/flink/blob/64d249eb/flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/WebRuntimeMonitorITCase.java
----------------------------------------------------------------------
diff --git a/flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/WebRuntimeMonitorITCase.java
b/flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/WebRuntimeMonitorITCase.java
index b418141..6515e87 100644
--- a/flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/WebRuntimeMonitorITCase.java
+++ b/flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/WebRuntimeMonitorITCase.java
@@ -103,7 +103,7 @@ public class WebRuntimeMonitorITCase extends TestLogger {
 
 				response = client.getNextResponse(deadline.timeLeft());
 				assertEquals(HttpResponseStatus.OK, response.getStatus());
-				assertEquals(response.getType(), MimeTypes.getMimeTypeForExtension("json"));
+				assertEquals("application/json; charset=UTF-8", response.getType());
 				assertTrue(response.getContent().contains("\"taskmanagers\":1"));
 			}
 		}
@@ -250,7 +250,7 @@ public class WebRuntimeMonitorITCase extends TestLogger {
 
 				response = followingClient.getNextResponse(deadline.timeLeft());
 				assertEquals(HttpResponseStatus.OK, response.getStatus());
-				assertEquals(response.getType(), MimeTypes.getMimeTypeForExtension("json"));
+				assertEquals("application/json; charset=UTF-8", response.getType());
 				assertTrue(response.getContent().contains("\"taskmanagers\":1") ||
 						response.getContent().contains("\"taskmanagers\":0"));
 			} finally {

http://git-wip-us.apache.org/repos/asf/flink/blob/64d249eb/flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/handlers/JobCancellationWithSavepointHandlersTest.java
----------------------------------------------------------------------
diff --git a/flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/handlers/JobCancellationWithSavepointHandlersTest.java
b/flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/handlers/JobCancellationWithSavepointHandlersTest.java
index 8c2d3fc..057ba7a 100644
--- a/flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/handlers/JobCancellationWithSavepointHandlersTest.java
+++ b/flink-runtime-web/src/test/java/org/apache/flink/runtime/webmonitor/handlers/JobCancellationWithSavepointHandlersTest.java
@@ -194,7 +194,7 @@ public class JobCancellationWithSavepointHandlersTest {
 		String location = String.format("/jobs/%s/cancel-with-savepoint/in-progress/1", jobId);
 
 		assertEquals(HttpResponseStatus.ACCEPTED, response.getStatus());
-		assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
+		assertEquals("application/json; charset=UTF-8", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
 		assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
 		assertEquals(location, response.headers().get(HttpHeaders.Names.LOCATION));
 
@@ -208,7 +208,7 @@ public class JobCancellationWithSavepointHandlersTest {
 		// Trigger again
 		response = trigger.handleRequest(params, Collections.<String, String>emptyMap(),
jobManager);
 		assertEquals(HttpResponseStatus.ACCEPTED, response.getStatus());
-		assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
+		assertEquals("application/json; charset=UTF-8", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
 		assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
 		assertEquals(location, response.headers().get(HttpHeaders.Names.LOCATION));
 
@@ -227,7 +227,7 @@ public class JobCancellationWithSavepointHandlersTest {
 
 		response = progress.handleRequest(params, Collections.<String, String>emptyMap(),
jobManager);
 		assertEquals(HttpResponseStatus.ACCEPTED, response.getStatus());
-		assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
+		assertEquals("application/json; charset=UTF-8", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
 		assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
 
 		json = response.content().toString(Charset.forName("UTF-8"));
@@ -242,7 +242,7 @@ public class JobCancellationWithSavepointHandlersTest {
 		response = progress.handleRequest(params, Collections.<String, String>emptyMap(),
jobManager);
 
 		assertEquals(HttpResponseStatus.CREATED, response.getStatus());
-		assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
+		assertEquals("application/json; charset=UTF-8", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
 		assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
 
 		json = response.content().toString(Charset.forName("UTF-8"));
@@ -258,7 +258,7 @@ public class JobCancellationWithSavepointHandlersTest {
 		response = progress.handleRequest(params, Collections.<String, String>emptyMap(),
jobManager);
 
 		assertEquals(HttpResponseStatus.CREATED, response.getStatus());
-		assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
+		assertEquals("application/json; charset=UTF-8", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
 		assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
 
 		json = response.content().toString(Charset.forName("UTF-8"));
@@ -274,7 +274,7 @@ public class JobCancellationWithSavepointHandlersTest {
 
 		response = progress.handleRequest(params, Collections.<String, String>emptyMap(),
jobManager);
 		assertEquals(HttpResponseStatus.BAD_REQUEST, response.getStatus());
-		assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
+		assertEquals("application/json; charset=UTF-8", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
 		assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
 
 		json = response.content().toString(Charset.forName("UTF-8"));
@@ -321,7 +321,7 @@ public class JobCancellationWithSavepointHandlersTest {
 
 		FullHttpResponse response = progress.handleRequest(params, Collections.<String, String>emptyMap(),
jobManager);
 		assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, response.getStatus());
-		assertEquals("application/json", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
+		assertEquals("application/json; charset=UTF-8", response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
 		assertEquals(Integer.toString(response.content().readableBytes()), response.headers().get(HttpHeaders.Names.CONTENT_LENGTH));
 
 		String json = response.content().toString(Charset.forName("UTF-8"));

http://git-wip-us.apache.org/repos/asf/flink/blob/64d249eb/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/TestBaseUtils.java
----------------------------------------------------------------------
diff --git a/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/TestBaseUtils.java
b/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/TestBaseUtils.java
index f96ab3d..ee1931b 100644
--- a/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/TestBaseUtils.java
+++ b/flink-test-utils-parent/flink-test-utils/src/main/java/org/apache/flink/test/util/TestBaseUtils.java
@@ -616,7 +616,7 @@ public class TestBaseUtils extends TestLogger {
 			is = connection.getInputStream();
 		}
 
-		return IOUtils.toString(is, connection.getContentEncoding() != null ? connection.getContentEncoding()
: "UTF-8");
+		return IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET);
 	}
 
 	public static class TupleComparator<T extends Tuple> implements Comparator<T>
{

http://git-wip-us.apache.org/repos/asf/flink/blob/64d249eb/flink-tests/src/test/java/org/apache/flink/test/web/WebFrontendITCase.java
----------------------------------------------------------------------
diff --git a/flink-tests/src/test/java/org/apache/flink/test/web/WebFrontendITCase.java b/flink-tests/src/test/java/org/apache/flink/test/web/WebFrontendITCase.java
index 003eb0c..c99dbee 100644
--- a/flink-tests/src/test/java/org/apache/flink/test/web/WebFrontendITCase.java
+++ b/flink-tests/src/test/java/org/apache/flink/test/web/WebFrontendITCase.java
@@ -33,11 +33,12 @@ import org.apache.flink.runtime.jobgraph.JobVertex;
 import org.apache.flink.runtime.minicluster.LocalFlinkMiniCluster;
 import org.apache.flink.runtime.testutils.StoppableInvokable;
 import org.apache.flink.runtime.webmonitor.WebMonitorUtils;
-import org.apache.flink.runtime.webmonitor.files.MimeTypes;
 import org.apache.flink.runtime.webmonitor.testutils.HttpTestClient;
 import org.apache.flink.test.util.TestBaseUtils;
 
 import org.apache.flink.util.TestLogger;
+import org.apache.commons.io.IOUtils;
+import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -47,6 +48,9 @@ import scala.concurrent.duration.FiniteDuration;
 import io.netty.handler.codec.http.HttpResponseStatus;
 
 import java.io.File;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.nio.file.Files;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
@@ -107,6 +111,38 @@ public class WebFrontendITCase extends TestLogger {
 	}
 
 	@Test
+	public void testResponseHeaders() throws Exception {
+		// check headers for successful json response
+		URL taskManagersUrl = new URL("http://localhost:" + port + "/taskmanagers");
+		HttpURLConnection taskManagerConnection = (HttpURLConnection) taskManagersUrl.openConnection();
+		taskManagerConnection.setConnectTimeout(100000);
+		taskManagerConnection.connect();
+		if (taskManagerConnection.getResponseCode() >= 400) {
+			// error!
+			InputStream is = taskManagerConnection.getErrorStream();
+			String errorMessage = IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET);
+			throw new RuntimeException(errorMessage);
+		}
+
+		// we don't set the content-encoding header
+		Assert.assertNull(taskManagerConnection.getContentEncoding());
+		Assert.assertEquals("application/json; charset=UTF-8", taskManagerConnection.getContentType());
+
+		// check headers in case of an error
+		URL notFoundJobUrl = new URL("http://localhost:" + port + "/jobs/dontexist");
+		HttpURLConnection notFoundJobConnection = (HttpURLConnection) notFoundJobUrl.openConnection();
+		notFoundJobConnection.setConnectTimeout(100000);
+		notFoundJobConnection.connect();
+		if (notFoundJobConnection.getResponseCode() >= 400) {
+			// we don't set the content-encoding header
+			Assert.assertNull(notFoundJobConnection.getContentEncoding());
+			Assert.assertEquals("text/plain; charset=UTF-8", notFoundJobConnection.getContentType());
+		} else {
+			throw new RuntimeException("Request for non-existing job did not return an error.");
+		}
+	}
+
+	@Test
 	public void getNumberOfTaskManagers() {
 		try {
 			String json = TestBaseUtils.getFromHTTP("http://localhost:" + port + "/taskmanagers/");
@@ -234,7 +270,7 @@ public class WebFrontendITCase extends TestLogger {
 				HttpTestClient.SimpleHttpResponse response = client.getNextResponse(deadline.timeLeft());
 	
 				assertEquals(HttpResponseStatus.OK, response.getStatus());
-				assertEquals(response.getType(), MimeTypes.getMimeTypeForExtension("json"));
+				assertEquals("application/json; charset=UTF-8", response.getType());
 				assertEquals("{}", response.getContent());
 			}
 
@@ -248,7 +284,7 @@ public class WebFrontendITCase extends TestLogger {
 			HttpTestClient.SimpleHttpResponse response = client.getNextResponse(timeout);
 
 			assertEquals(HttpResponseStatus.OK, response.getStatus());
-			assertEquals(response.getType(), MimeTypes.getMimeTypeForExtension("json"));
+			assertEquals("application/json; charset=UTF-8", response.getType());
 			assertEquals("{\"jid\":\""+jid+"\",\"name\":\"Stoppable streaming test job\"," +
 					"\"execution-config\":{\"execution-mode\":\"PIPELINED\",\"restart-strategy\":\"default\","
+
 					"\"job-parallelism\":-1,\"object-reuse-mode\":false,\"user-config\":{}}}", response.getContent());
@@ -287,7 +323,7 @@ public class WebFrontendITCase extends TestLogger {
 						.getNextResponse(deadline.timeLeft());
 	
 				assertEquals(HttpResponseStatus.OK, response.getStatus());
-				assertEquals(response.getType(), MimeTypes.getMimeTypeForExtension("json"));
+				assertEquals("application/json; charset=UTF-8", response.getType());
 				assertEquals("{}", response.getContent());
 			}
 			


Mime
View raw message