Skip to content

Package: KapuaSerializableBodyWriter

KapuaSerializableBodyWriter

nameinstructionbranchcomplexitylinemethod
KapuaSerializableBodyWriter()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSize(KapuaSerializable, Class, Type, Annotation[], MediaType)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isWriteable(Class, Type, Annotation[], MediaType)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
writeTo(KapuaSerializable, Class, Type, Annotation[], MediaType, MultivaluedMap, OutputStream)
M: 39 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.app.api.core;
14:
15: import java.io.IOException;
16: import java.io.OutputStream;
17: import java.lang.annotation.Annotation;
18: import java.lang.reflect.Type;
19:
20: import javax.ws.rs.Produces;
21: import javax.ws.rs.WebApplicationException;
22: import javax.ws.rs.core.Context;
23: import javax.ws.rs.core.MediaType;
24: import javax.ws.rs.core.MultivaluedMap;
25: import javax.ws.rs.ext.ContextResolver;
26: import javax.ws.rs.ext.MessageBodyWriter;
27: import javax.ws.rs.ext.Provider;
28: import javax.ws.rs.ext.Providers;
29: import javax.xml.bind.JAXBContext;
30: import javax.xml.bind.JAXBException;
31:
32: import org.eclipse.kapua.KapuaSerializable;
33:
34: @Provider
35: @Produces(MediaType.APPLICATION_XML)
36: public class KapuaSerializableBodyWriter implements MessageBodyWriter<KapuaSerializable> {
37:
38: @Context
39: Providers providers;
40:
41: @Override
42: public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
43: // TODO any ode here to do more significant check on the types ?
44: return true;
45: }
46:
47: @Override
48: public long getSize(KapuaSerializable t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
49: return 0;
50: }
51:
52: @Override
53: public void writeTo(KapuaSerializable t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
54: MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
55: throws IOException, WebApplicationException {
56: try {
57:• if (providers == null) {
58: throw new WebApplicationException("Unable to find any provider.");
59: }
60:
61: ContextResolver<JAXBContext> cr = providers.getContextResolver(JAXBContext.class,
62: MediaType.APPLICATION_XML_TYPE);
63: JAXBContext jaxbContext = cr.getContext(JAXBContext.class);
64:• if (jaxbContext == null) {
65: throw new WebApplicationException("Unable to get a JAXBContext.");
66: }
67:
68: // serialize the entity myBean to the entity output stream
69: jaxbContext.createMarshaller().marshal(t, entityStream);
70: } catch (JAXBException e) {
71: throw new WebApplicationException(e);
72: }
73: }
74:
75: }