2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.crud.util;
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;
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;
36 public class CrudJaxbTransformation {
38 * Marshal a dynamic entity into a string.
40 * @param entity the dynamic entity
41 * @param jaxbContext the dynamic jaxb context
42 * @return the marshaled entity
43 * @throws RouterException on error
45 public static String marshal(MediaType mediaType, final DynamicEntity entity,
46 final DynamicJAXBContext jaxbContext) throws JAXBException {
48 final JAXBMarshaller marshaller = jaxbContext.createMarshaller();
49 marshaller.setProperty(JAXBMarshaller.JAXB_FORMATTED_OUTPUT, false);
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);
57 final StringWriter writer = new StringWriter();
58 marshaller.marshal(entity, writer);
59 return writer.toString();
68 * @throws JAXBException
71 public static Object unmarshal(String javaClass, String content, MediaType mediaType,
72 final DynamicJAXBContext jaxbContext) throws JAXBException {
74 Unmarshaller unmarshaller = null;
76 clazz = jaxbContext.newDynamicEntity(javaClass);
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);
85 return unmarshaller.unmarshal(new StreamSource(new StringReader(content)),
86 clazz.getClass()).getValue();