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.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;
44 * Provider that serializes and de-serializes xacml request/response json.
46 * @author Chenfei Gao (cgao@research.att.com)
49 @Consumes(XacmlJsonMessageBodyHandler.APPLICATION_XACML_JSON)
50 @Produces(XacmlJsonMessageBodyHandler.APPLICATION_XACML_JSON)
51 public class XacmlJsonMessageBodyHandler implements MessageBodyReader<Request>, MessageBodyWriter<Response> {
53 public static final String APPLICATION_XACML_JSON = "application/xacml+json";
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(JsonResponseTranslator.toString(response, true));
67 } catch (Exception exc) {
68 throw new IOException("failed to convert a json 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 {
81 Request jsonRequest = null;
83 jsonRequest = JsonRequestTranslator.load(entityStream);
84 } catch (Exception exc) {
85 throw new IOException("failed to decode incoming request string to a json request");
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
97 private boolean canHandle(MediaType mediaType, Class<?> type) {
98 if (mediaType == null) {
101 return ("xacml+json".equals(mediaType.getSubtype()))
102 && (Request.class.isAssignableFrom(type) || Response.class.isAssignableFrom(type));