2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2022 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.json.JsonRequestTranslator;
26 import com.att.research.xacml.std.json.JsonResponseTranslator;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.io.OutputStreamWriter;
31 import java.lang.annotation.Annotation;
32 import java.lang.reflect.Type;
33 import java.nio.charset.StandardCharsets;
34 import javax.ws.rs.Consumes;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.MultivaluedMap;
38 import javax.ws.rs.ext.MessageBodyReader;
39 import javax.ws.rs.ext.MessageBodyWriter;
40 import javax.ws.rs.ext.Provider;
43 * Provider that serializes and de-serializes xacml request/response json.
45 * @author Chenfei Gao (cgao@research.att.com)
48 @Consumes(XacmlJsonMessageBodyHandler.APPLICATION_XACML_JSON)
49 @Produces(XacmlJsonMessageBodyHandler.APPLICATION_XACML_JSON)
50 public class XacmlJsonMessageBodyHandler implements MessageBodyReader<Request>, MessageBodyWriter<Response> {
52 public static final String APPLICATION_XACML_JSON = "application/xacml+json";
55 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
56 return canHandle(mediaType, type);
60 public void writeTo(Response response, Class<?> type, Type genericType, Annotation[] annotations,
61 MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
64 try (var writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) {
65 writer.write(JsonResponseTranslator.toString(response, true));
66 } catch (Exception exc) {
67 throw new IOException("failed to convert a json response to a string");
72 public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
73 return canHandle(mediaType, type);
77 public Request readFrom(Class<Request> type, Type genericType, Annotation[] annotations, MediaType mediaType,
78 MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
80 Request jsonRequest = null;
82 jsonRequest = JsonRequestTranslator.load(entityStream);
83 } catch (Exception exc) {
84 throw new IOException("failed to decode incoming request string to a json 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) {
100 return ("xacml+json".equals(mediaType.getSubtype()))
101 && (Request.class.isAssignableFrom(type) || Response.class.isAssignableFrom(type));