Search in sources :

Example 6 with CollectionContainerPolicy

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));
    }
}
Also used : CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) SortedSet(java.util.SortedSet) Set(java.util.Set) NavigableSet(java.util.NavigableSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) WebApplicationException(jakarta.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) MessageBodyReader(jakarta.ws.rs.ext.MessageBodyReader) JAXBContext(jakarta.xml.bind.JAXBContext) UnmarshalException(jakarta.xml.bind.UnmarshalException) Unmarshaller(jakarta.xml.bind.Unmarshaller) ResponseBuilder(jakarta.ws.rs.core.Response.ResponseBuilder) Queue(java.util.Queue) InputStreamReader(java.io.InputStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) JAXBElement(jakarta.xml.bind.JAXBElement) Deque(java.util.Deque) LinkedList(java.util.LinkedList) SessionLog(org.eclipse.persistence.logging.SessionLog) AbstractSessionLog(org.eclipse.persistence.logging.AbstractSessionLog) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) Collection(java.util.Collection)

Aggregations

CollectionContainerPolicy (org.eclipse.persistence.internal.queries.CollectionContainerPolicy)6 Collection (java.util.Collection)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Set (java.util.Set)2 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)2 ForeignReferenceMapping (org.eclipse.persistence.mappings.ForeignReferenceMapping)2 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)2 WebApplicationException (jakarta.ws.rs.WebApplicationException)1 ResponseBuilder (jakarta.ws.rs.core.Response.ResponseBuilder)1 MessageBodyReader (jakarta.ws.rs.ext.MessageBodyReader)1 JAXBContext (jakarta.xml.bind.JAXBContext)1 JAXBElement (jakarta.xml.bind.JAXBElement)1 JAXBException (jakarta.xml.bind.JAXBException)1 UnmarshalException (jakarta.xml.bind.UnmarshalException)1 Unmarshaller (jakarta.xml.bind.Unmarshaller)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Deque (java.util.Deque)1 HashSet (java.util.HashSet)1