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