Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#1061] Improvement(server): Bad request may not response JSON content (#3879)

Liang Chun8aeb41a207ab
diff --git a/server/src/main/java/com/datastrato/gravitino/server/GravitinoServer.java b/server/src/main/java/com/datastrato/gravitino/server/GravitinoServer.java
index ca7b9c36ba..e21d71680a 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/GravitinoServer.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/GravitinoServer.java
@@ -37,6 +37,9 @@ import com.datastrato.gravitino.server.web.JettyServerConfig;
import com.datastrato.gravitino.server.web.ObjectMapperProvider;
import com.datastrato.gravitino.server.web.VersioningFilter;
import com.datastrato.gravitino.server.web.filter.AccessControlNotAllowedFilter;
+import com.datastrato.gravitino.server.web.mapper.JsonMappingExceptionMapper;
+import com.datastrato.gravitino.server.web.mapper.JsonParseExceptionMapper;
+import com.datastrato.gravitino.server.web.mapper.JsonProcessingExceptionMapper;
import com.datastrato.gravitino.server.web.ui.WebUIFilter;
import java.io.File;
import java.util.Properties;
@@ -102,6 +105,9 @@ public class GravitinoServer extends ResourceConfig {
bind(gravitinoEnv.topicDispatcher()).to(TopicDispatcher.class).ranked(1);
}
});
+ register(JsonProcessingExceptionMapper.class);
+ register(JsonParseExceptionMapper.class);
+ register(JsonMappingExceptionMapper.class);
register(ObjectMapperProvider.class).register(JacksonFeature.class);
if (!enableAuthorization) {
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/mapper/JsonMappingExceptionMapper.java b/server/src/main/java/com/datastrato/gravitino/server/web/mapper/JsonMappingExceptionMapper.java
new file mode 100644
index 0000000000..52e958e288
--- /dev/null
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/mapper/JsonMappingExceptionMapper.java
@@ -0,0 +1,39 @@
+/*
+ * 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 com.datastrato.gravitino.server.web.mapper;
+
+import com.datastrato.gravitino.server.web.Utils;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import javax.annotation.Priority;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+
+/**
+ * JsonMappingExceptionMapper is used for returning consistent format of response when an illegal
+ * Json request is sent. This class overrides the built-in JsonMappingExceptionMapper defined in
+ * Jersey.
+ */
+@Priority(1)
+public class JsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException> {
+ @Override
+ public Response toResponse(JsonMappingException e) {
+ String errorMsg = "Malformed json request";
+ return Utils.illegalArguments(errorMsg, e);
+ }
+}
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/mapper/JsonParseExceptionMapper.java b/server/src/main/java/com/datastrato/gravitino/server/web/mapper/JsonParseExceptionMapper.java
new file mode 100644
index 0000000000..4b1f43b841
--- /dev/null
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/mapper/JsonParseExceptionMapper.java
@@ -0,0 +1,38 @@
+/*
+ * 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 com.datastrato.gravitino.server.web.mapper;
+
+import com.datastrato.gravitino.server.web.Utils;
+import com.fasterxml.jackson.core.JsonParseException;
+import javax.annotation.Priority;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+
+/**
+ * JsonParseExceptionMapper is used for returning consistent format of response when an illegal Json
+ * request is sent. This class overrides the built-in JsonParseExceptionMapper defined in Jersey.
+ */
+@Priority(1)
+public class JsonParseExceptionMapper implements ExceptionMapper<JsonParseException> {
+ @Override
+ public Response toResponse(JsonParseException e) {
+ String errorMsg = "Malformed json request";
+ return Utils.illegalArguments(errorMsg, e);
+ }
+}
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/mapper/JsonProcessingExceptionMapper.java b/server/src/main/java/com/datastrato/gravitino/server/web/mapper/JsonProcessingExceptionMapper.java
new file mode 100644
index 0000000000..85b61e1f43
--- /dev/null
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/mapper/JsonProcessingExceptionMapper.java
@@ -0,0 +1,38 @@
+/*
+ * 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 com.datastrato.gravitino.server.web.mapper;
+
+import com.datastrato.gravitino.server.web.Utils;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import javax.annotation.Priority;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+
+/**
+ * JsonProcessingExceptionMapper is used for returning consistent format of response when a general
+ * Json issue occurs.
+ */
+@Priority(1)
+public class JsonProcessingExceptionMapper implements ExceptionMapper<JsonProcessingException> {
+ @Override
+ public Response toResponse(JsonProcessingException e) {
+ String errorMsg = "Unexpected error occurs when json processing.";
+ return Utils.internalError(errorMsg, e);
+ }
+}
diff --git a/server/src/test/java/com/datastrato/gravitino/server/web/mapper/TestJsonMappingExceptionMapper.java b/server/src/test/java/com/datastrato/gravitino/server/web/mapper/TestJsonMappingExceptionMapper.java
new file mode 100644
index 0000000000..4540b8c67c
--- /dev/null
+++ b/server/src/test/java/com/datastrato/gravitino/server/web/mapper/TestJsonMappingExceptionMapper.java
@@ -0,0 +1,45 @@
+/*
+ * 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 com.datastrato.gravitino.server.web.mapper;
+
+import com.datastrato.gravitino.dto.responses.ErrorResponse;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import javax.ws.rs.core.Response;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+public class TestJsonMappingExceptionMapper {
+ private final JsonMappingExceptionMapper jsonMappingExceptionMapper =
+ new JsonMappingExceptionMapper();
+
+ @Test
+ public void testJsonMappingExceptionMapper() {
+ JsonParser mockParser = Mockito.mock(JsonParser.class);
+ Response response =
+ jsonMappingExceptionMapper.toResponse(JsonMappingException.from(mockParser, ""));
+ ErrorResponse errorResponse = ErrorResponse.illegalArguments("Malformed json request");
+ ErrorResponse entity = (ErrorResponse) response.getEntity();
+ Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
+ Assertions.assertEquals(errorResponse.getCode(), entity.getCode());
+ Assertions.assertEquals(errorResponse.getMessage(), entity.getMessage());
+ Assertions.assertEquals(errorResponse.getType(), entity.getType());
+ }
+}
diff --git a/server/src/test/java/com/datastrato/gravitino/server/web/mapper/TestJsonParseExceptionMapper.java b/server/src/test/java/com/datastrato/gravitino/server/web/mapper/TestJsonParseExceptionMapper.java
new file mode 100644
index 0000000000..ed4ea3a088
--- /dev/null
+++ b/server/src/test/java/com/datastrato/gravitino/server/web/mapper/TestJsonParseExceptionMapper.java
@@ -0,0 +1,40 @@
+/*
+ * 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 com.datastrato.gravitino.server.web.mapper;
+
+import com.datastrato.gravitino.dto.responses.ErrorResponse;
+import com.fasterxml.jackson.core.JsonParseException;
+import javax.ws.rs.core.Response;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestJsonParseExceptionMapper {
+ private final JsonParseExceptionMapper jsonParseExceptionMapper = new JsonParseExceptionMapper();
+
+ @Test
+ public void testJsonParseExceptionMapper() {
+ Response response = jsonParseExceptionMapper.toResponse(new JsonParseException(""));
+ ErrorResponse errorResponse = ErrorResponse.illegalArguments("Malformed json request");
+ ErrorResponse entity = (ErrorResponse) response.getEntity();
+ Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
+ Assertions.assertEquals(errorResponse.getCode(), entity.getCode());
+ Assertions.assertEquals(errorResponse.getMessage(), entity.getMessage());
+ Assertions.assertEquals(errorResponse.getType(), entity.getType());
+ }
+}
diff --git a/server/src/test/java/com/datastrato/gravitino/server/web/mapper/TestJsonProcessingExceptionMapper.java b/server/src/test/java/com/datastrato/gravitino/server/web/mapper/TestJsonProcessingExceptionMapper.java
new file mode 100644
index 0000000000..09d991f3bb
--- /dev/null
+++ b/server/src/test/java/com/datastrato/gravitino/server/web/mapper/TestJsonProcessingExceptionMapper.java
@@ -0,0 +1,44 @@
+/*
+ * 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 com.datastrato.gravitino.server.web.mapper;
+
+import com.datastrato.gravitino.dto.responses.ErrorResponse;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import javax.ws.rs.core.Response;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestJsonProcessingExceptionMapper {
+ private final JsonProcessingExceptionMapper jsonProcessingExceptionMapper =
+ new JsonProcessingExceptionMapper();
+
+ @Test
+ public void testJsonProcessingExceptionMapper() {
+ Response response =
+ jsonProcessingExceptionMapper.toResponse(new JsonProcessingException("") {});
+ ErrorResponse errorResponse =
+ ErrorResponse.internalError("Unexpected error occurs when json processing.");
+ ErrorResponse entity = (ErrorResponse) response.getEntity();
+ Assertions.assertEquals(
+ Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
+ Assertions.assertEquals(errorResponse.getCode(), entity.getCode());
+ Assertions.assertEquals(errorResponse.getMessage(), entity.getMessage());
+ Assertions.assertEquals(errorResponse.getType(), entity.getType());
+ }
+}
diff --git a/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestMetalakeOperations.java b/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestMetalakeOperations.java
index 29db5b2efc..f2cf473fe4 100644
--- a/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestMetalakeOperations.java
+++ b/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestMetalakeOperations.java
@@ -46,12 +46,21 @@ import com.datastrato.gravitino.meta.SchemaVersion;
import com.datastrato.gravitino.metalake.MetalakeDispatcher;
import com.datastrato.gravitino.metalake.MetalakeManager;
import com.datastrato.gravitino.rest.RESTUtils;
+import com.datastrato.gravitino.server.web.mapper.JsonMappingExceptionMapper;
+import com.datastrato.gravitino.server.web.mapper.JsonParseExceptionMapper;
+import com.datastrato.gravitino.server.web.mapper.JsonProcessingExceptionMapper;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
@@ -107,10 +116,35 @@ public class TestMetalakeOperations extends JerseyTest {
bindFactory(MockServletRequestFactory.class).to(HttpServletRequest.class);
}
});
-
+ resourceConfig.register(JsonProcessingExceptionMapper.class);
+ resourceConfig.register(JsonParseExceptionMapper.class);
+ resourceConfig.register(JsonMappingExceptionMapper.class);
+ resourceConfig.register(TestException.class);
return resourceConfig;
}
+ @Path("/test")
+ public static class TestException {
+ @GET
+ @Path("/jsonProcessingException")
+ public Response getJsonProcessingException() throws JsonProcessingException {
+ throw new JsonProcessingException("Error processing JSON") {};
+ }
+
+ @GET
+ @Path("/jsonMappingException")
+ public Response getJsonMappingException() throws JsonMappingException {
+ JsonParser mockParser = Mockito.mock(JsonParser.class);
+ throw JsonMappingException.from(mockParser, "Error mapping JSON");
+ }
+
+ @GET
+ @Path("/jsonParseException")
+ public Response getJsonParseException() throws JsonParseException {
+ throw new JsonParseException("Error parsing JSON");
+ }
+ }
+
@Test
public void testListMetalakes() {
String metalakeName = "test";
@@ -391,4 +425,30 @@ public class TestMetalakeOperations extends JerseyTest {
Assertions.assertTrue(
errorResponse.getMessage().contains("Failed to operate object [test] operation [DROP]"));
}
+
+ @Test
+ public void testExceptionMapper() {
+ Response resp = target("/test/jsonProcessingException").request().get();
+ Assertions.assertEquals(
+ Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp.getStatus());
+ ErrorResponse errorResp = resp.readEntity(ErrorResponse.class);
+ Assertions.assertEquals(ErrorConstants.INTERNAL_ERROR_CODE, errorResp.getCode());
+ Assertions.assertEquals(RuntimeException.class.getSimpleName(), errorResp.getType());
+ Assertions.assertTrue(
+ errorResp.getMessage().contains("Unexpected error occurs when json processing."));
+
+ Response resp1 = target("/test/jsonMappingException").request().get();
+ Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp1.getStatus());
+ ErrorResponse errorResp1 = resp1.readEntity(ErrorResponse.class);
+ Assertions.assertEquals(ErrorConstants.ILLEGAL_ARGUMENTS_CODE, errorResp1.getCode());
+ Assertions.assertEquals(IllegalArgumentException.class.getSimpleName(), errorResp1.getType());
+ Assertions.assertTrue(errorResp1.getMessage().contains("Malformed json request"));
+
+ Response resp2 = target("/test/jsonParseException").request().get();
+ Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp2.getStatus());
+ ErrorResponse errorResp2 = resp2.readEntity(ErrorResponse.class);
+ Assertions.assertEquals(ErrorConstants.ILLEGAL_ARGUMENTS_CODE, errorResp2.getCode());
+ Assertions.assertEquals(IllegalArgumentException.class.getSimpleName(), errorResp2.getType());
+ Assertions.assertTrue(errorResp2.getMessage().contains("Malformed json request"));
+ }
}