2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.util;
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;
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;
39 public class CrudJaxbTransformation {
41 * Marshal a dynamic entity into a string.
43 * @param entity the dynamic entity
44 * @param jaxbContext the dynamic jaxb context
45 * @return the marshaled entity
46 * @throws RouterException on error
48 public static String marshal(MediaType mediaType, final DynamicEntity entity,
49 final DynamicJAXBContext jaxbContext) throws JAXBException {
51 final JAXBMarshaller marshaller = jaxbContext.createMarshaller();
52 marshaller.setProperty(JAXBMarshaller.JAXB_FORMATTED_OUTPUT, false);
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);
60 final StringWriter writer = new StringWriter();
61 marshaller.marshal(entity, writer);
62 return writer.toString();
71 * @throws JAXBException
74 public static Object unmarshal(String javaClass, String content, MediaType mediaType,
75 final DynamicJAXBContext jaxbContext) throws JAXBException {
77 Unmarshaller unmarshaller = null;
79 clazz = jaxbContext.newDynamicEntity(javaClass);
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);
88 return unmarshaller.unmarshal(new StreamSource(new StringReader(content)),
89 clazz.getClass()).getValue();