diff --git a/java/google/registry/rdap/RdapActionBase.java b/java/google/registry/rdap/RdapActionBase.java index 22360071c..c56ea7b4f 100644 --- a/java/google/registry/rdap/RdapActionBase.java +++ b/java/google/registry/rdap/RdapActionBase.java @@ -14,6 +14,7 @@ package google.registry.rdap; +import static com.google.common.base.Charsets.UTF_8; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import static com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN; @@ -76,7 +77,8 @@ public abstract class RdapActionBase implements Runnable { */ static final Pattern LDH_PATTERN = Pattern.compile("[-.a-zA-Z0-9*]+"); - private static final MediaType RESPONSE_MEDIA_TYPE = MediaType.create("application", "rdap+json"); + private static final MediaType RESPONSE_MEDIA_TYPE = + MediaType.create("application", "rdap+json").withCharset(UTF_8); @Inject HttpServletRequest request; @Inject Response response; @@ -131,10 +133,10 @@ public abstract class RdapActionBase implements Runnable { requestMethod == Action.Method.HEAD, rdapLinkBase); response.setStatus(SC_OK); + response.setContentType(RESPONSE_MEDIA_TYPE); if (requestMethod != Action.Method.HEAD) { response.setPayload(JSONValue.toJSONString(rdapJson)); } - response.setContentType(RESPONSE_MEDIA_TYPE); } catch (HttpException e) { setError(e.getResponseCode(), e.getResponseCodeString(), e.getMessage()); } catch (URISyntaxException | IllegalArgumentException e) { @@ -147,12 +149,12 @@ public abstract class RdapActionBase implements Runnable { void setError(int status, String title, String description) { response.setStatus(status); + response.setContentType(RESPONSE_MEDIA_TYPE); try { if (requestMethod != Action.Method.HEAD) { response.setPayload( JSONValue.toJSONString(rdapJsonFormatter.makeError(status, title, description))); } - response.setContentType(RESPONSE_MEDIA_TYPE); } catch (Exception ex) { if (requestMethod != Action.Method.HEAD) { response.setPayload(""); diff --git a/javatests/google/registry/rdap/RdapActionBaseTest.java b/javatests/google/registry/rdap/RdapActionBaseTest.java index ebf66970f..5667db789 100644 --- a/javatests/google/registry/rdap/RdapActionBaseTest.java +++ b/javatests/google/registry/rdap/RdapActionBaseTest.java @@ -16,7 +16,6 @@ package google.registry.rdap; import static com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth8.assertThat; import static google.registry.request.Action.Method.GET; import static google.registry.request.Action.Method.HEAD; import static google.registry.testing.DatastoreHelper.createTld; @@ -143,6 +142,13 @@ public class RdapActionBaseTest { assertThat(response.getStatus()).isEqualTo(200); } + @Test + public void testContentType_rdapjson_utf8() throws Exception { + generateActualJson("no.thing"); + assertThat(response.getContentType().toString()) + .isEqualTo("application/rdap+json; charset=utf-8"); + } + @Test public void testHeadRequest_returnsNoContent() throws Exception { assertThat(generateHeadPayload("no.thing")).isEmpty();