2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.rest.serialization;
23 import com.att.research.xacml.api.Request;
24 import com.att.research.xacml.api.Response;
25 import com.att.research.xacml.std.dom.DOMRequest;
26 import com.att.research.xacml.std.dom.DOMResponse;
27 import com.att.research.xacml.std.dom.DOMStructureException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.io.OutputStreamWriter;
32 import java.lang.annotation.Annotation;
33 import java.lang.reflect.Type;
34 import java.nio.charset.StandardCharsets;
35 import javax.ws.rs.Consumes;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.ext.MessageBodyReader;
40 import javax.ws.rs.ext.MessageBodyWriter;
41 import javax.ws.rs.ext.Provider;
44 * Provider that serializes and de-serializes xacml request/response xml.
46 * @author Chenfei Gao (cgao@research.att.com)
49 @Consumes(XacmlXmlMessageBodyHandler.APPLICATION_XACML_XML)
50 @Produces(XacmlXmlMessageBodyHandler.APPLICATION_XACML_XML)
51 public class XacmlXmlMessageBodyHandler implements MessageBodyReader<Request>, MessageBodyWriter<Response> {
53 public static final String APPLICATION_XACML_XML = "application/xacml+xml";
56 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
57 return canHandle(mediaType, type);
61 public void writeTo(Response response, Class<?> type, Type genericType, Annotation[] annotations,
62 MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
65 try (var writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
66 writer.write(DOMResponse.toString(response, true));
67 } catch (Exception exc) {
68 throw new IOException("failed to convert a dom response to a string");
73 public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
74 return canHandle(mediaType, type);
78 public Request readFrom(Class<Request> type, Type genericType, Annotation[] annotations, MediaType mediaType,
79 MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
82 return DOMRequest.load(entityStream);
83 } catch (DOMStructureException e) {
84 throw new IOException("failed to decode incoming request string to a dom request");
89 * Determines if this provider can handle the given media type.
90 * @param mediaType the media type of interest
91 * @param type the class type of the object to read/write
92 * @return {@code true} if this provider handles the given media type and class type
93 * {@code false} otherwise
95 private boolean canHandle(MediaType mediaType, Class<?> type) {
96 if (mediaType == null) {
100 return ("xacml+xml".equals(mediaType.getSubtype()))
101 && (type == Request.class || type == Response.class);