Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / serialization / XacmlJsonMessageBodyHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020-2022 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 com.att.research.xacml.api.Request;
25 import com.att.research.xacml.api.Response;
26 import com.att.research.xacml.std.json.JsonRequestTranslator;
27 import com.att.research.xacml.std.json.JsonResponseTranslator;
28 import jakarta.ws.rs.Consumes;
29 import jakarta.ws.rs.Produces;
30 import jakarta.ws.rs.core.MediaType;
31 import jakarta.ws.rs.core.MultivaluedMap;
32 import jakarta.ws.rs.ext.MessageBodyReader;
33 import jakarta.ws.rs.ext.MessageBodyWriter;
34 import jakarta.ws.rs.ext.Provider;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import java.io.OutputStreamWriter;
39 import java.lang.annotation.Annotation;
40 import java.lang.reflect.Type;
41 import java.nio.charset.StandardCharsets;
42
43 /**
44  * Provider that serializes and de-serializes xacml request/response json.
45  *
46  * @author Chenfei Gao (cgao@research.att.com)
47  */
48 @Provider
49 @Consumes(XacmlJsonMessageBodyHandler.APPLICATION_XACML_JSON)
50 @Produces(XacmlJsonMessageBodyHandler.APPLICATION_XACML_JSON)
51 public class XacmlJsonMessageBodyHandler implements MessageBodyReader<Request>, MessageBodyWriter<Response> {
52
53     public static final String APPLICATION_XACML_JSON = "application/xacml+json";
54
55     @Override
56     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
57         return canHandle(mediaType, type);
58     }
59
60     @Override
61     public void writeTo(Response response, Class<?> type, Type genericType, Annotation[] annotations,
62             MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
63                     throws IOException {
64
65         try (var writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
66             writer.write(JsonResponseTranslator.toString(response, true));
67         } catch (Exception exc) {
68             throw new IOException("failed to convert a json response to a string");
69         }
70     }
71
72     @Override
73     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
74         return canHandle(mediaType, type);
75     }
76
77     @Override
78     public Request readFrom(Class<Request> type, Type genericType, Annotation[] annotations, MediaType mediaType,
79             MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
80
81         Request jsonRequest = null;
82         try {
83             jsonRequest = JsonRequestTranslator.load(entityStream);
84         } catch (Exception exc) {
85             throw new IOException("failed to decode incoming request string to a json request");
86         }
87         return jsonRequest;
88     }
89
90     /**
91      * Determines if this provider can handle the given media type.
92      * @param mediaType the media type of interest
93      * @param type the class type of the object to read/write
94      * @return {@code true} if this provider handles the given media type and class type
95      *         {@code false} otherwise
96      */
97     private boolean canHandle(MediaType mediaType, Class<?> type) {
98         if (mediaType == null) {
99             return false;
100         }
101         return ("xacml+json".equals(mediaType.getSubtype()))
102                 && (Request.class.isAssignableFrom(type) || Response.class.isAssignableFrom(type));
103     }
104 }