diff options
author | Jim Hahn <jrh3@att.com> | 2020-03-19 16:23:08 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-03-19 16:23:08 +0000 |
commit | 3008ca45fb70a4694ba6b9ff69f278ab9484f3e3 (patch) | |
tree | 40d73755c08b24d828fa37dda3911f90e0f3e644 /main/src | |
parent | df4599f94a4ef319810d3dae91e9bfcbcbc86ad2 (diff) | |
parent | 3b755ec2e3d9776980236db6ba754ae6b7cc2402 (diff) |
Merge "Re-factor matchable to reduce complexity"
Diffstat (limited to 'main/src')
3 files changed, 89 insertions, 56 deletions
diff --git a/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlExceptionMapper.java b/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlExceptionMapper.java new file mode 100644 index 00000000..4112e64e --- /dev/null +++ b/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlExceptionMapper.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 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.pdpx.main.rest.serialization; + +import java.io.IOException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import lombok.Getter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Catches IOException when decoding/encoding a REST xacml request/response and converts them from an HTTP 500 + * error code to an HTTP 400 error code. + * + */ +public abstract class XacmlExceptionMapper implements ExceptionMapper<IOException> { + private static final Logger LOGGER = LoggerFactory.getLogger(XacmlExceptionMapper.class); + + @Getter + String invalidRequest; + @Getter + String invalidResponse; + + public abstract boolean isInvalidRequest(String message); + + public abstract boolean isInvalidResponse(String message); + + @Override + public Response toResponse(IOException exc) { + if (isInvalidRequest(exc.getMessage())) { + LOGGER.warn(invalidRequest, exc); + return Response.status(Response.Status.BAD_REQUEST).entity(new SimpleResponse(invalidRequest)).build(); + } else if (isInvalidResponse(exc.getMessage())) { + LOGGER.warn(invalidResponse, exc); + return Response.status(Response.Status.BAD_REQUEST).entity(new SimpleResponse(invalidResponse)).build(); + } else { + // Unexpected 500 + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + } + + @Getter + private static class SimpleResponse { + private String errorDetails; + + public SimpleResponse(String errorDetails) { + this.errorDetails = errorDetails; + } + } +} diff --git a/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlJsonExceptionMapper.java b/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlJsonExceptionMapper.java index 81fc2d26..03f3ddce 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlJsonExceptionMapper.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlJsonExceptionMapper.java @@ -20,14 +20,8 @@ package org.onap.policy.pdpx.main.rest.serialization; -import java.io.IOException; import javax.ws.rs.Produces; -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 IOException when decoding/encoding a REST xacml request/response and converts them from an HTTP 500 @@ -37,32 +31,20 @@ import org.slf4j.LoggerFactory; */ @Provider @Produces(XacmlJsonMessageBodyHandler.APPLICATION_XACML_JSON) -public class XacmlJsonExceptionMapper implements ExceptionMapper<IOException> { +public class XacmlJsonExceptionMapper extends XacmlExceptionMapper { - private static final Logger LOGGER = LoggerFactory.getLogger(XacmlJsonExceptionMapper.class); - private static final String INVALID_REQUEST = "invalid JSON xacml request"; - private static final String INVALID_RESPONSE = "invalid JSON xacml response"; + public XacmlJsonExceptionMapper() { + this.invalidRequest = "invalid JSON xacml request"; + this.invalidResponse = "invalid JSON xacml response"; + } @Override - public Response toResponse(IOException exc) { - if (exc.getMessage().contains("json request")) { - LOGGER.warn(INVALID_REQUEST, exc); - return Response.status(Response.Status.BAD_REQUEST).entity(new SimpleResponse(INVALID_REQUEST)).build(); - } else if (exc.getMessage().contains("json response")) { - LOGGER.warn(INVALID_RESPONSE, exc); - return Response.status(Response.Status.BAD_REQUEST).entity(new SimpleResponse(INVALID_RESPONSE)).build(); - } else { - // Unexpected 500 - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } + public boolean isInvalidRequest(String message) { + return message.contains("json request"); } - @Getter - private static class SimpleResponse { - private String errorDetails; - - public SimpleResponse(String errorDetails) { - this.errorDetails = errorDetails; - } + @Override + public boolean isInvalidResponse(String message) { + return message.contains("json response"); } }
\ No newline at end of file diff --git a/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlXmlExceptionMapper.java b/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlXmlExceptionMapper.java index 8e62abec..562c0e91 100644 --- a/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlXmlExceptionMapper.java +++ b/main/src/main/java/org/onap/policy/pdpx/main/rest/serialization/XacmlXmlExceptionMapper.java @@ -20,14 +20,8 @@ package org.onap.policy.pdpx.main.rest.serialization; -import java.io.IOException; import javax.ws.rs.Produces; -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 IOException when decoding/encoding a REST xacml request/response and converts them from an HTTP 500 @@ -37,32 +31,20 @@ import org.slf4j.LoggerFactory; */ @Provider @Produces(XacmlXmlMessageBodyHandler.APPLICATION_XACML_XML) -public class XacmlXmlExceptionMapper implements ExceptionMapper<IOException> { +public class XacmlXmlExceptionMapper extends XacmlExceptionMapper { - private static final Logger LOGGER = LoggerFactory.getLogger(XacmlXmlExceptionMapper.class); - private static final String INVALID_REQUEST = "invalid XML xacml request"; - private static final String INVALID_RESPONSE = "invalid XML xacml response"; + public XacmlXmlExceptionMapper() { + this.invalidRequest = "invalid XML xacml request"; + this.invalidResponse = "invalid XML xacml response"; + } @Override - public Response toResponse(IOException exc) { - if (exc.getMessage().contains("dom request")) { - LOGGER.warn(INVALID_REQUEST, exc); - return Response.status(Response.Status.BAD_REQUEST).entity(new SimpleResponse(INVALID_REQUEST)).build(); - } else if (exc.getMessage().contains("dom response")) { - LOGGER.warn(INVALID_RESPONSE, exc); - return Response.status(Response.Status.BAD_REQUEST).entity(new SimpleResponse(INVALID_RESPONSE)).build(); - } else { - // Unexpected 500 - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } + public boolean isInvalidRequest(String message) { + return message.contains("dom request"); } - @Getter - private static class SimpleResponse { - private String errorDetails; - - public SimpleResponse(String errorDetails) { - this.errorDetails = errorDetails; - } + @Override + public boolean isInvalidResponse(String message) { + return message.contains("dom response"); } }
\ No newline at end of file |