Search in sources :

Example 1 with JSONSequenceTooLargeException

use of org.codehaus.jettison.JSONSequenceTooLargeException in project cxf by apache.

the class JSONProvider method readFrom.

public T readFrom(Class<T> type, Type genericType, Annotation[] anns, MediaType mt, MultivaluedMap<String, String> headers, InputStream is) throws IOException {
    if (isPayloadEmpty(headers)) {
        if (AnnotationUtils.getAnnotation(anns, Nullable.class) != null) {
            return null;
        }
        reportEmptyContentLength();
    }
    XMLStreamReader reader = null;
    String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
    Unmarshaller unmarshaller = null;
    try {
        InputStream realStream = getInputStream(type, genericType, is);
        if (Document.class.isAssignableFrom(type)) {
            W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
            reader = createReader(type, realStream, false, enc);
            copyReaderToWriter(reader, writer);
            return type.cast(writer.getDocument());
        }
        boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(type);
        Class<?> theGenericType = isCollection ? InjectionUtils.getActualType(genericType) : type;
        Class<?> theType = getActualType(theGenericType, genericType, anns);
        unmarshaller = createUnmarshaller(theType, genericType, isCollection);
        XMLStreamReader xsr = createReader(type, realStream, isCollection, enc);
        Object response;
        if (JAXBElement.class.isAssignableFrom(type) || !isCollection && (unmarshalAsJaxbElement || jaxbElementClassMap != null && jaxbElementClassMap.containsKey(theType.getName()))) {
            response = unmarshaller.unmarshal(xsr, theType);
        } else {
            response = unmarshaller.unmarshal(xsr);
        }
        if (response instanceof JAXBElement && !JAXBElement.class.isAssignableFrom(type)) {
            response = ((JAXBElement<?>) response).getValue();
        }
        if (isCollection) {
            response = ((CollectionWrapper) response).getCollectionOrArray(unmarshaller, theType, type, genericType, org.apache.cxf.jaxrs.utils.JAXBUtils.getAdapter(theGenericType, anns));
        } else {
            response = checkAdapter(response, type, anns, false);
        }
        return type.cast(response);
    } catch (JAXBException e) {
        handleJAXBException(e, true);
    } catch (XMLStreamException e) {
        if (e.getCause() instanceof JSONSequenceTooLargeException) {
            throw new WebApplicationException(413);
        }
        handleXMLStreamException(e, true);
    } catch (WebApplicationException e) {
        throw e;
    } catch (Exception e) {
        throw ExceptionUtils.toBadRequestException(e, null);
    } finally {
        try {
            StaxUtils.close(reader);
        } catch (XMLStreamException e) {
            throw ExceptionUtils.toBadRequestException(e, null);
        }
        JAXBUtils.closeUnmarshaller(unmarshaller);
    }
    // unreachable
    return null;
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) XMLStreamReader(javax.xml.stream.XMLStreamReader) WebApplicationException(javax.ws.rs.WebApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement) XMLStreamException(javax.xml.stream.XMLStreamException) JSONSequenceTooLargeException(org.codehaus.jettison.JSONSequenceTooLargeException) JAXBException(javax.xml.bind.JAXBException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) JSONSequenceTooLargeException(org.codehaus.jettison.JSONSequenceTooLargeException) XMLStreamException(javax.xml.stream.XMLStreamException) Unmarshaller(javax.xml.bind.Unmarshaller) Nullable(org.apache.cxf.jaxrs.ext.Nullable)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JAXBElement (javax.xml.bind.JAXBElement)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Nullable (org.apache.cxf.jaxrs.ext.Nullable)1 W3CDOMStreamWriter (org.apache.cxf.staxutils.W3CDOMStreamWriter)1 JSONSequenceTooLargeException (org.codehaus.jettison.JSONSequenceTooLargeException)1