Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / serialization / XacmlExceptionMapper.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main.rest.serialization;
23
24 import jakarta.ws.rs.core.Response;
25 import jakarta.ws.rs.ext.ExceptionMapper;
26 import java.io.IOException;
27 import lombok.Getter;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
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.
34  *
35  */
36 public abstract class XacmlExceptionMapper implements ExceptionMapper<IOException> {
37     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlExceptionMapper.class);
38
39     @Getter
40     protected String invalidRequest;
41     @Getter
42     protected String invalidResponse;
43
44     public abstract boolean isInvalidRequest(String message);
45
46     public abstract boolean isInvalidResponse(String message);
47
48     @Override
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();
56         } else {
57             // Unexpected 500
58             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
59         }
60     }
61
62     @Getter
63     private static class SimpleResponse {
64         private String errorDetails;
65
66         public SimpleResponse(String errorDetails) {
67             this.errorDetails = errorDetails;
68         }
69     }
70 }