5e72534abaeac2ccb681090d73f3bbd30b4ece60
[aai/gizmo.git] / src / main / java / org / onap / crud / util / CrudJaxbTransformation.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.crud.util;
22
23 import org.eclipse.persistence.dynamic.DynamicEntity;
24 import org.eclipse.persistence.jaxb.JAXBMarshaller;
25 import org.eclipse.persistence.jaxb.MarshallerProperties;
26 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
27 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
28
29 import java.io.StringReader;
30 import java.io.StringWriter;
31 import javax.ws.rs.core.MediaType;
32 import javax.xml.bind.JAXBException;
33 import javax.xml.bind.Unmarshaller;
34 import javax.xml.transform.stream.StreamSource;
35
36 public class CrudJaxbTransformation {
37   /**
38    * Marshal a dynamic entity into a string.
39    *
40    * @param entity      the dynamic entity
41    * @param jaxbContext the dynamic jaxb context
42    * @return the marshaled entity
43    * @throws RouterException on error
44    */
45   public static String marshal(MediaType mediaType, final DynamicEntity entity,
46                                final DynamicJAXBContext jaxbContext) throws JAXBException {
47
48     final JAXBMarshaller marshaller = jaxbContext.createMarshaller();
49     marshaller.setProperty(JAXBMarshaller.JAXB_FORMATTED_OUTPUT, false);
50
51     if (MediaType.APPLICATION_JSON_TYPE.isCompatible(mediaType)) {
52       marshaller.setProperty("eclipselink.media-type", "application/json");
53       marshaller.setProperty("eclipselink.json.include-root", false);
54       marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, Boolean.FALSE);
55     }
56
57     final StringWriter writer = new StringWriter();
58     marshaller.marshal(entity, writer);
59     return writer.toString();
60
61   }
62
63   /**
64    * @param type
65    * @param json
66    * @param mediaType
67    * @return
68    * @throws JAXBException
69    * @throws Exception
70    */
71   public static Object unmarshal(String javaClass, String content, MediaType mediaType,
72                                  final DynamicJAXBContext jaxbContext) throws JAXBException {
73     Object clazz = null;
74     Unmarshaller unmarshaller = null;
75
76     clazz = jaxbContext.newDynamicEntity(javaClass);
77
78     unmarshaller = jaxbContext.createUnmarshaller();
79     if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) {
80       unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
81       unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
82       unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
83     }
84
85     return unmarshaller.unmarshal(new StreamSource(new StringReader(content)),
86         clazz.getClass()).getValue();
87   }
88
89 }