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