2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2023 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main.rest.serialization;
24 import jakarta.ws.rs.core.Response;
25 import jakarta.ws.rs.ext.ExceptionMapper;
26 import java.io.IOException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Catches IOException when decoding/encoding a REST xacml request/response and converts them from an HTTP 500
33 * error code to an HTTP 400 error code.
36 public abstract class XacmlExceptionMapper implements ExceptionMapper<IOException> {
37 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlExceptionMapper.class);
40 protected String invalidRequest;
42 protected String invalidResponse;
44 public abstract boolean isInvalidRequest(String message);
46 public abstract boolean isInvalidResponse(String message);
49 public Response toResponse(IOException exc) {
50 if (isInvalidRequest(exc.getMessage())) {
51 LOGGER.warn(invalidRequest, exc);
52 return Response.status(Response.Status.BAD_REQUEST).entity(new SimpleResponse(invalidRequest)).build();
53 } else if (isInvalidResponse(exc.getMessage())) {
54 LOGGER.warn(invalidResponse, exc);
55 return Response.status(Response.Status.BAD_REQUEST).entity(new SimpleResponse(invalidResponse)).build();
58 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
63 private static class SimpleResponse {
64 private String errorDetails;
66 public SimpleResponse(String errorDetails) {
67 this.errorDetails = errorDetails;