use of org.eclipse.persistence.internal.queries.CollectionContainerPolicy in project eclipselink by eclipse-ee4j.
the class MOXyJsonProvider method readFrom.
/*
* @see jakarta.ws.rs.ext.MessageBodyReader#readFrom(java.lang.Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], jakarta.ws.rs.core.MediaType, jakarta.ws.rs.core.MultivaluedMap, java.io.InputStream)
*/
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
try {
if (null == genericType) {
genericType = type;
}
Set<Class<?>> domainClasses = getDomainClasses(genericType);
JAXBContext jaxbContext = getJAXBContext(domainClasses, annotations, mediaType, httpHeaders);
SessionLog logger = AbstractSessionLog.getLog();
if (logger.shouldLog(SessionLog.FINE, SessionLog.MOXY)) {
logger.log(SessionLog.FINE, SessionLog.MOXY, "moxy_read_from_moxy_json_provider", new Object[0]);
}
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
unmarshaller.setProperty(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX, attributePrefix);
unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, includeRoot);
unmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_PREFIX_MAPPER, namespacePrefixMapper);
unmarshaller.setProperty(UnmarshallerProperties.JSON_NAMESPACE_SEPARATOR, namespaceSeperator);
if (null != valueWrapper) {
unmarshaller.setProperty(UnmarshallerProperties.JSON_VALUE_WRAPPER, valueWrapper);
}
unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, wrapperAsArrayName);
preReadFrom(type, genericType, annotations, mediaType, httpHeaders, unmarshaller);
StreamSource jsonSource;
Map<String, String> mediaTypeParameters = null;
if (null != mediaType) {
mediaTypeParameters = mediaType.getParameters();
}
if (null != mediaTypeParameters && mediaTypeParameters.containsKey(CHARSET)) {
String charSet = mediaTypeParameters.get(CHARSET);
Reader entityReader = new InputStreamReader(entityStream, charSet);
jsonSource = new StreamSource(entityReader);
} else {
jsonSource = new StreamSource(entityStream);
}
Class<?> domainClass = getDomainClass(domainClasses);
JAXBElement<?> jaxbElement = unmarshaller.unmarshal(jsonSource, domainClass);
if (type.isAssignableFrom(JAXBElement.class)) {
return jaxbElement;
} else {
Object value = jaxbElement.getValue();
if (value instanceof ArrayList) {
if (type.isArray()) {
ArrayList<Object> arrayList = (ArrayList<Object>) value;
int arrayListSize = arrayList.size();
boolean wrapItemInJAXBElement = wrapItemInJAXBElement(genericType);
Object array;
if (wrapItemInJAXBElement) {
array = Array.newInstance(JAXBElement.class, arrayListSize);
} else {
array = Array.newInstance(domainClass, arrayListSize);
}
for (int x = 0; x < arrayListSize; x++) {
Object element = handleJAXBElement(arrayList.get(x), domainClass, wrapItemInJAXBElement);
Array.set(array, x, element);
}
return array;
} else {
ContainerPolicy containerPolicy;
if (type.isAssignableFrom(List.class) || type.isAssignableFrom(ArrayList.class) || type.isAssignableFrom(Collection.class)) {
containerPolicy = new CollectionContainerPolicy(ArrayList.class);
} else if (type.isAssignableFrom(Set.class)) {
containerPolicy = new CollectionContainerPolicy(HashSet.class);
} else if (type.isAssignableFrom(Deque.class) || type.isAssignableFrom(Queue.class)) {
containerPolicy = new CollectionContainerPolicy(LinkedList.class);
} else if (type.isAssignableFrom(NavigableSet.class) || type.isAssignableFrom(SortedSet.class)) {
containerPolicy = new CollectionContainerPolicy(TreeSet.class);
} else {
containerPolicy = new CollectionContainerPolicy(type);
}
Object container = containerPolicy.containerInstance();
boolean wrapItemInJAXBElement = wrapItemInJAXBElement(genericType);
for (Object element : (Collection<Object>) value) {
element = handleJAXBElement(element, domainClass, wrapItemInJAXBElement);
containerPolicy.addInto(element, container, null);
}
return container;
}
} else {
return value;
}
}
} catch (UnmarshalException unmarshalException) {
ResponseBuilder builder = Response.status(Status.BAD_REQUEST);
throw new WebApplicationException(unmarshalException, builder.build());
} catch (JAXBException jaxbException) {
throw new WebApplicationException(jaxbException);
} catch (NullPointerException nullPointerException) {
throw new WebApplicationException(JSONException.errorInvalidDocument(nullPointerException));
}
}
Aggregations