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
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 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;
45 * Provider that serializes and de-serializes xacml request/response xml.
47 * @author Chenfei Gao (cgao@research.att.com)
50 @Consumes(XacmlXmlMessageBodyHandler.APPLICATION_XACML_XML)
51 @Produces(XacmlXmlMessageBodyHandler.APPLICATION_XACML_XML)
52 public class XacmlXmlMessageBodyHandler implements MessageBodyReader<Request>, MessageBodyWriter<Response> {
54 public static final String APPLICATION_XACML_XML = "application/xacml+xml";
57 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
58 return canHandle(mediaType, type);
62 public void writeTo(Response response, Class<?> type, Type genericType, Annotation[] annotations,
63 MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
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");
74 public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
75 return canHandle(mediaType, type);
79 public Request readFrom(Class<Request> type, Type genericType, Annotation[] annotations, MediaType mediaType,
80 MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
83 return DOMRequest.load(entityStream);
84 } catch (DOMStructureException e) {
85 throw new IOException("failed to decode incoming request string to a dom request");
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
96 private boolean canHandle(MediaType mediaType, Class<?> type) {
97 if (mediaType == null) {
101 return ("xacml+xml".equals(mediaType.getSubtype()))
102 && (Request.class.isAssignableFrom(type) || Response.class.isAssignableFrom(type));