58ed5f76be3ebd60051473a38f51d468ef04d16b
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / serialization / XacmlJsonMessageBodyHandler.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.rest.serialization;
22
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;
41
42 /**
43  * Provider that serializes and de-serializes xacml request/response json.
44  *
45  * @author Chenfei Gao (cgao@research.att.com)
46  */
47 @Provider
48 @Consumes(XacmlJsonMessageBodyHandler.APPLICATION_XACML_JSON)
49 @Produces(XacmlJsonMessageBodyHandler.APPLICATION_XACML_JSON)
50 public class XacmlJsonMessageBodyHandler implements MessageBodyReader<Request>, MessageBodyWriter<Response> {
51
52     public static final String APPLICATION_XACML_JSON = "application/xacml+json";
53
54     @Override
55     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
56         return canHandle(mediaType, type);
57     }
58
59     @Override
60     public void writeTo(Response response, Class<?> type, Type genericType, Annotation[] annotations,
61             MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
62                     throws IOException {
63
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");
68         }
69     }
70
71     @Override
72     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
73         return canHandle(mediaType, type);
74     }
75
76     @Override
77     public Request readFrom(Class<Request> type, Type genericType, Annotation[] annotations, MediaType mediaType,
78             MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
79
80         Request jsonRequest = null;
81         try {
82             jsonRequest = JsonRequestTranslator.load(entityStream);
83         } catch (Exception exc) {
84             throw new IOException("failed to decode incoming request string to a json request");
85         }
86         return jsonRequest;
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         return ("xacml+json".equals(mediaType.getSubtype()))
101                 && (Request.class.isAssignableFrom(type) || Response.class.isAssignableFrom(type));
102     }
103 }