Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ef2606c69347207fc1fcebd465994cc32bb98a64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2013-2016 LAAS-CNRS (www.laas.fr)
 * 7 Colonel Roche 31077 Toulouse - France
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Initial Contributors:
 *     Thierry Monteil : Project manager, technical co-manager
 *     Mahdi Ben Alaya : Technical co-manager
 *     Samir Medjiah : Technical co-manager
 *     Khalil Drira : Strategy expert
 *     Guillaume Garzone : Developer
 *     François Aïssaoui : Developer
 *
 * New contributors :
 *******************************************************************************/
package org.eclipse.om2m.datamapping.jaxb;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Unmarshaller.Listener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.om2m.commons.constants.MimeMediaType;
import org.eclipse.om2m.commons.resource.AbstractFlexContainer;
import org.eclipse.om2m.commons.resource.URIList;
import org.eclipse.om2m.datamapping.service.DataMapperService;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;

/**
 * Datamapper (JAXB) implementing DataMapper service
 */
public class Mapper implements DataMapperService {

	/** XML Mapper logger */
	private static Log LOGGER = LogFactory.getLog(Mapper.class);
	/** JAXB Context, entry point to the JAXB API */
	private JAXBContext context;
	/** Resource package name used for JAXBContext instantiation */
	// org.eclipse.om2m.commons.resource:
	private String resourcePackage = "org.eclipse.om2m.commons.resource:org.eclipse.om2m.commons.resource.flexcontainerspec";
	private String mediaType;

	/**
	 * Private constructor that will create the JAXB context.
	 */
	public Mapper(String mediaType) {
		this.mediaType=mediaType;
		try {
			if(context==null){
				if(mediaType.equals(MimeMediaType.JSON)){
					// JSON
					ClassLoader classLoader = Mapper.class.getClassLoader(); 
					InputStream iStreamJsonBinding = classLoader.getResourceAsStream("json-binding.json");
					InputStream iStreamJsonBindingFlexcontainer = classLoader.getResourceAsStream("json-binding-flexcontainer.json");
					List<Object> iStreamList = new ArrayList<>();
					iStreamList.add(iStreamJsonBinding);
					iStreamList.add(iStreamJsonBindingFlexcontainer);
					Map<String, Object> properties = new HashMap<String, Object>(); 
					properties.put("eclipselink-oxm-xml", iStreamList); 
					properties.put("eclipselink.media-type", "application/json");;
					context = JAXBContext.newInstance(resourcePackage, classLoader , properties);
				} else if (mediaType.equals(MimeMediaType.XML)) {
					// XML
					ClassLoader classLoader = Mapper.class.getClassLoader(); 
					InputStream iStream = classLoader.getResourceAsStream("xml-binding.xml"); 
					Map<String, Object> properties = new HashMap<String, Object>(); 
					properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
					context = JAXBContext.newInstance(resourcePackage, classLoader , properties);
				} else {
					// other
					context = JAXBContext.newInstance(resourcePackage, Mapper.class.getClassLoader());
				}
			}
		} catch (Throwable e) { 
			LOGGER.error("Create JAXBContext error", e);
		}
	}

	/**
	 * Converts a resource Java object into resource XML representation.
	 * 
	 * @param object
	 *            - resource Java object
	 * @return resource XML representation
	 */
	@Override
	public String objToString(Object obj) {
		try {
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			marshaller.setProperty(MarshallerProperties.MEDIA_TYPE,mediaType);
			if (obj instanceof URIList) {
				marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
				marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, true);
				marshaller.setProperty(MarshallerProperties.JSON_REDUCE_ANY_ARRAYS, false);
			} else {
				marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
				marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, false);
				marshaller.setProperty(MarshallerProperties.JSON_REDUCE_ANY_ARRAYS, true);
			}
			marshaller.setProperty(MarshallerProperties.JSON_VALUE_WRAPPER, "val");
			
			
			Map<String, String> namespaces = new HashMap<String, String>(); 
			namespaces.put("http://www.onem2m.org/xml/protocols/homedomain", "hd"); 
			namespaces.put("http://www.onem2m.org/xml/protocols", "m2m"); 
			marshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces);
			marshaller.setProperty(MarshallerProperties.JSON_NAMESPACE_SEPARATOR, ':');
			marshaller.marshal(obj, outputStream);
			
			return outputStream.toString("UTF-8");
		} catch (JAXBException e) {
			LOGGER.error("JAXB marshalling error!", e);
		} catch (UnsupportedEncodingException e) {
			LOGGER.error("JAXB marshalling error!", e);
		}
		return null;
	}

	/**
	 * Converts a resource XML representation data into resource Java object.
	 * 
	 * @param representation
	 *            - resource XML representation
	 * @return resource Java object
	 */
	@Override
	public Object stringToObj(String representation) {
		if(representation.isEmpty()){
			return null;
		}
		StringReader stringReader = new StringReader(representation);
		try {
			Unmarshaller unmarshaller = context.createUnmarshaller();
			unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, mediaType);
			if (representation.contains("m2m:uril")) {
				unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
				unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME , true);
			} else {
				unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, true);
				unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME , false);
			}
			unmarshaller.setProperty(UnmarshallerProperties.JSON_VALUE_WRAPPER , "val");
			Map<String, String> namespaces = new HashMap<String, String>(); 
			namespaces.put("http://www.onem2m.org/xml/protocols/homedomain", "hd"); 
			namespaces.put("http://www.onem2m.org/xml/protocols", "m2m"); 
			unmarshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, namespaces);
			unmarshaller.setProperty(MarshallerProperties.JSON_NAMESPACE_SEPARATOR, ':');
			
			unmarshaller.setListener(new Listener() {
				
				@Override
				public void afterUnmarshal(Object target, Object parent) {
					LOGGER.debug("afterUnmarshal (target=" + target + ", parent=" + parent + ")");
					super.afterUnmarshal(target, parent);
					
					if (target instanceof AbstractFlexContainer) {
						((AbstractFlexContainer) target).finalizeDeserialization();
					}
				}
			});
			
			Object unmarshaledObject = unmarshaller.unmarshal(stringReader);
			Object toBeReturned = null;
			toBeReturned = unmarshaledObject;

			return toBeReturned;
		} catch (JAXBException e) {
			LOGGER.error("JAXB unmarshalling error!", e);
		}
		return null;
	}

	@Override
	public String getServiceDataType() {
		return mediaType;
	}

}

Back to the top