diff options
author | Jim Hahn <jrh3@att.com> | 2019-08-23 18:35:40 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2019-08-23 18:46:14 -0400 |
commit | e7fbfd23ef9a03951935509856d3b4d33e5da546 (patch) | |
tree | 46acb34764d6eed44ce455b1e25b496331f03ff2 /policy-endpoints/src/main/java | |
parent | cf0cd76d63f9eba680a6307dd4a708e7169cb403 (diff) |
Rest servers should return 400 for bad syntax JSON
Modified common RestServer to inject an exception handler
into the list of providers so that it returns 400 instead
of 500 for JSON parsing errors.
Change-Id: I7c77625e3531e26413d72cc386296a709946cda2
Issue-ID: POLICY-1725
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'policy-endpoints/src/main/java')
2 files changed, 57 insertions, 1 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/JsonExceptionMapper.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/JsonExceptionMapper.java new file mode 100644 index 00000000..9ee72f03 --- /dev/null +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/JsonExceptionMapper.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.endpoints.http.server; + +import com.google.gson.JsonSyntaxException; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; +import lombok.Getter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Catches JSON exceptions when decoding a REST request and converts them from an HTTP 500 + * error code to an HTTP 400 error code. + */ +@Provider +@Produces(MediaType.APPLICATION_JSON) +public class JsonExceptionMapper implements ExceptionMapper<JsonSyntaxException> { + private static Logger logger = LoggerFactory.getLogger(JsonExceptionMapper.class); + + @Override + public Response toResponse(JsonSyntaxException exception) { + logger.warn("invalid JSON request", exception); + return Response.status(Response.Status.BAD_REQUEST).entity(new SimpleResponse("Invalid JSON request")).build(); + } + + @Getter + private static class SimpleResponse { + private String errorDetails; + + public SimpleResponse(String errorDetails) { + this.errorDetails = errorDetails; + } + } +} diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/RestServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/RestServer.java index 597375bf..2368c0b8 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/RestServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/RestServer.java @@ -99,7 +99,7 @@ public class RestServer extends ServiceManagerContainer { props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, String.valueOf(restServerParameters.isAaf())); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, - GsonMessageBodyHandler.class.getName()); + String.join(",", GsonMessageBodyHandler.class.getName(), JsonExceptionMapper.class.getName())); return props; } |