5ec176346d1793ba2c9e68bf28ff2155293d5914
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / serialization / XacmlXmlMessageBodyHandler.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.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;
42
43 /**
44  * Provider that serializes and de-serializes xacml request/response xml.
45  *
46  * @author Chenfei Gao (cgao@research.att.com)
47  */
48 @Provider
49 @Consumes(XacmlXmlMessageBodyHandler.APPLICATION_XACML_XML)
50 @Produces(XacmlXmlMessageBodyHandler.APPLICATION_XACML_XML)
51 public class XacmlXmlMessageBodyHandler implements MessageBodyReader<Request>, MessageBodyWriter<Response> {
52
53     public static final String APPLICATION_XACML_XML = "application/xacml+xml";
54
55     @Override
56     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
57         return canHandle(mediaType, type);
58     }
59
60     @Override
61     public void writeTo(Response response, Class<?> type, Type genericType, Annotation[] annotations,
62             MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
63                     throws IOException {
64
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");
69         }
70     }
71
72     @Override
73     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
74         return canHandle(mediaType, type);
75     }
76
77     @Override
78     public Request readFrom(Class<Request> type, Type genericType, Annotation[] annotations, MediaType mediaType,
79             MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
80
81         try {
82             return DOMRequest.load(entityStream);
83         } catch (DOMStructureException e) {
84             throw new IOException("failed to decode incoming request string to a dom request");
85         }
86     }
87
88     /**
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
94      */
95     private boolean canHandle(MediaType mediaType, Class<?> type) {
96         if (mediaType == null) {
97             return false;
98         }
99
100         return ("xacml+xml".equals(mediaType.getSubtype()))
101                 && (Request.class.isAssignableFrom(type) || Response.class.isAssignableFrom(type));
102     }
103 }