Search in sources :

Example 1 with NoContentException

use of javax.ws.rs.core.NoContentException in project jersey by jersey.

the class AbstractJaxbElementProvider method readFrom.

@Override
public final JAXBElement<?> readFrom(Class<JAXBElement<?>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
    final EntityInputStream entityStream = EntityInputStream.create(inputStream);
    if (entityStream.isEmpty()) {
        throw new NoContentException(LocalizationMessages.ERROR_READING_ENTITY_MISSING());
    }
    final ParameterizedType pt = (ParameterizedType) genericType;
    final Class ta = (Class) pt.getActualTypeArguments()[0];
    try {
        return readFrom(ta, mediaType, getUnmarshaller(ta, mediaType), entityStream);
    } catch (UnmarshalException ex) {
        throw new BadRequestException(ex);
    } catch (JAXBException ex) {
        throw new InternalServerErrorException(ex);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) EntityInputStream(org.glassfish.jersey.message.internal.EntityInputStream) NoContentException(javax.ws.rs.core.NoContentException)

Example 2 with NoContentException

use of javax.ws.rs.core.NoContentException in project jersey by jersey.

the class BasicTypesMessageProvider method readFrom.

@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    final String entityString = readFromAsString(entityStream, mediaType);
    if (entityString.isEmpty()) {
        throw new NoContentException(LocalizationMessages.ERROR_READING_ENTITY_MISSING());
    }
    final PrimitiveTypes primitiveType = PrimitiveTypes.forType(type);
    if (primitiveType != null) {
        return primitiveType.convert(entityString);
    }
    final Constructor constructor = AccessController.doPrivileged(ReflectionHelper.getStringConstructorPA(type));
    if (constructor != null) {
        try {
            return type.cast(constructor.newInstance(entityString));
        } catch (Exception e) {
            throw new MessageBodyProcessingException(LocalizationMessages.ERROR_ENTITY_PROVIDER_BASICTYPES_CONSTRUCTOR(type));
        }
    }
    if (AtomicInteger.class.isAssignableFrom(type)) {
        return new AtomicInteger((Integer) PrimitiveTypes.INTEGER.convert(entityString));
    }
    if (AtomicLong.class.isAssignableFrom(type)) {
        return new AtomicLong((Long) PrimitiveTypes.LONG.convert(entityString));
    }
    throw new MessageBodyProcessingException(LocalizationMessages.ERROR_ENTITY_PROVIDER_BASICTYPES_UNKWNOWN(type));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Constructor(java.lang.reflect.Constructor) NoContentException(javax.ws.rs.core.NoContentException) NoContentException(javax.ws.rs.core.NoContentException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 3 with NoContentException

use of javax.ws.rs.core.NoContentException in project tomee by apache.

the class AbstractJAXBProvider method handleExceptionEnd.

protected static void handleExceptionEnd(Throwable t, String message, boolean read) throws NoContentException {
    if (t instanceof WstxEOFException && t.getMessage().startsWith("Unexpected EOF in prolog")) {
        String noContent = new org.apache.cxf.common.i18n.Message("EMPTY_BODY", BUNDLE).toString();
        LOG.warning(noContent);
        throw new NoContentException(noContent);
    }
    Response.Status status = read ? Response.Status.BAD_REQUEST : Response.Status.INTERNAL_SERVER_ERROR;
    Response r = JAXRSUtils.toResponseBuilder(status).type(MediaType.TEXT_PLAIN).entity(message).build();
    throw read ? ExceptionUtils.toBadRequestException(t, r) : ExceptionUtils.toInternalServerErrorException(t, r);
}
Also used : Response(javax.ws.rs.core.Response) WstxEOFException(com.ctc.wstx.exc.WstxEOFException) NoContentException(javax.ws.rs.core.NoContentException)

Example 4 with NoContentException

use of javax.ws.rs.core.NoContentException in project tomee by apache.

the class ResponseImpl method doReadEntity.

public <T> T doReadEntity(Class<T> cls, Type t, Annotation[] anns) throws ProcessingException, IllegalStateException {
    checkEntityIsClosed();
    // according to javadoc, should close when is not buffered.
    boolean shouldClose = !entityBufferred && !JAXRSUtils.isStreamingOutType(cls);
    if (lastEntity != null && cls.isAssignableFrom(lastEntity.getClass()) && !(lastEntity instanceof InputStream)) {
        return cls.cast(lastEntity);
    }
    MediaType mediaType = getMediaType();
    if (mediaType == null) {
        mediaType = MediaType.WILDCARD_TYPE;
    }
    // the stream is available if entity is IS or
    // message contains XMLStreamReader or Reader
    boolean entityStreamAvailable = entityStreamAvailable();
    InputStream entityStream = null;
    if (!entityStreamAvailable) {
        // try create a stream if the entity is String or Number
        entityStream = convertEntityToStreamIfPossible();
        entityStreamAvailable = entityStream != null;
    } else if (entity instanceof InputStream) {
        entityStream = InputStream.class.cast(entity);
    } else {
        Message inMessage = getResponseMessage();
        Reader reader = inMessage.getContent(Reader.class);
        if (reader != null) {
            entityStream = InputStream.class.cast(new ReaderInputStream(reader));
        }
    }
    // we need to check for readers even if no IS is set - the readers may still do it
    List<ReaderInterceptor> readers = outMessage == null ? null : ProviderFactory.getInstance(outMessage).createMessageBodyReaderInterceptor(cls, t, anns, mediaType, outMessage, entityStreamAvailable, null);
    if (readers != null) {
        try {
            if (entityBufferred) {
                InputStream.class.cast(entity).reset();
            }
            Message responseMessage = getResponseMessage();
            responseMessage.put(Message.PROTOCOL_HEADERS, getHeaders());
            lastEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls, t, anns, entityStream, mediaType, responseMessage);
            // close the entity after readEntity is called.
            T tCastLastEntity = castLastEntity();
            shouldClose = shouldClose && !(tCastLastEntity instanceof AutoCloseable) && !(tCastLastEntity instanceof Source);
            if (shouldClose) {
                close();
            }
            return tCastLastEntity;
        } catch (NoContentException ex) {
            // check if basic type. Basic type throw exception, else return null.
            if (isBasicType(cls)) {
                autoClose(cls, true);
                reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, mediaType, ex);
            } else {
                if (shouldClose) {
                    close();
                }
                return null;
            }
        } catch (Exception ex) {
            autoClose(cls, true);
            reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, mediaType, ex);
        } finally {
            ProviderFactory pf = ProviderFactory.getInstance(outMessage);
            if (pf != null) {
                pf.clearThreadLocalProxies();
            }
        }
    } else if (entity != null && cls.isAssignableFrom(entity.getClass())) {
        lastEntity = entity;
        return castLastEntity();
    } else if (entityStreamAvailable) {
        reportMessageHandlerProblem("NO_MSG_READER", cls, mediaType, null);
    }
    throw new IllegalStateException("The entity is not backed by an input stream, entity class is : " + (entity != null ? entity.getClass().getName() : cls.getName()));
}
Also used : ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Message(org.apache.cxf.message.Message) PushbackInputStream(java.io.PushbackInputStream) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) NoContentException(javax.ws.rs.core.NoContentException) Source(javax.xml.transform.Source) NoContentException(javax.ws.rs.core.NoContentException) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) MediaType(javax.ws.rs.core.MediaType)

Example 5 with NoContentException

use of javax.ws.rs.core.NoContentException in project jersey by jersey.

the class AbstractCollectionJaxbProvider method readFrom.

@Override
@SuppressWarnings("unchecked")
public final Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
    final EntityInputStream entityStream = EntityInputStream.create(inputStream);
    if (entityStream.isEmpty()) {
        throw new NoContentException(LocalizationMessages.ERROR_READING_ENTITY_MISSING());
    }
    try {
        final Class<?> elementType = getElementClass(type, genericType);
        final Unmarshaller u = getUnmarshaller(elementType, mediaType);
        final XMLStreamReader r = getXMLStreamReader(elementType, mediaType, u, entityStream);
        boolean jaxbElement = false;
        Collection<Object> l = null;
        if (type.isArray()) {
            l = new ArrayList<Object>();
        } else {
            try {
                l = (Collection<Object>) type.newInstance();
            } catch (Exception e) {
                for (Class<?> c : DEFAULT_IMPLS) {
                    if (type.isAssignableFrom(c)) {
                        try {
                            l = (Collection<Object>) c.newInstance();
                            break;
                        } catch (InstantiationException ex) {
                            LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
                        } catch (IllegalAccessException ex) {
                            LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
                        } catch (SecurityException ex) {
                            LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
                        }
                    }
                }
            }
        }
        if (l == null) {
            l = new ArrayList<Object>();
        }
        // Move to root element
        int event = r.next();
        while (event != XMLStreamReader.START_ELEMENT) {
            event = r.next();
        }
        // Move to first child (if any)
        event = r.next();
        while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
            event = r.next();
        }
        while (event != XMLStreamReader.END_DOCUMENT) {
            if (elementType.isAnnotationPresent(XmlRootElement.class)) {
                l.add(u.unmarshal(r));
            } else if (elementType.isAnnotationPresent(XmlType.class)) {
                l.add(u.unmarshal(r, elementType).getValue());
            } else {
                l.add(u.unmarshal(r, elementType));
                jaxbElement = true;
            }
            // Move to next peer (if any)
            event = r.getEventType();
            while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
                event = r.next();
            }
        }
        return (type.isArray()) ? createArray(l, jaxbElement ? JAXBElement.class : elementType) : l;
    } catch (UnmarshalException ex) {
        throw new BadRequestException(ex);
    } catch (XMLStreamException ex) {
        throw new BadRequestException(ex);
    } catch (JAXBException ex) {
        throw new InternalServerErrorException(ex);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) NoContentException(javax.ws.rs.core.NoContentException) XMLStreamException(javax.xml.stream.XMLStreamException) BadRequestException(javax.ws.rs.BadRequestException) UnmarshalException(javax.xml.bind.UnmarshalException) NoContentException(javax.ws.rs.core.NoContentException) IOException(java.io.IOException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) JAXBException(javax.xml.bind.JAXBException) WebApplicationException(javax.ws.rs.WebApplicationException) XmlType(javax.xml.bind.annotation.XmlType) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) Collection(java.util.Collection) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) EntityInputStream(org.glassfish.jersey.message.internal.EntityInputStream) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

NoContentException (javax.ws.rs.core.NoContentException)6 IOException (java.io.IOException)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 PushbackInputStream (java.io.PushbackInputStream)2 Reader (java.io.Reader)2 BadRequestException (javax.ws.rs.BadRequestException)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 ProcessingException (javax.ws.rs.ProcessingException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)2 MediaType (javax.ws.rs.core.MediaType)2 ReaderInterceptor (javax.ws.rs.ext.ReaderInterceptor)2 JAXBException (javax.xml.bind.JAXBException)2 UnmarshalException (javax.xml.bind.UnmarshalException)2 ReaderInputStream (org.apache.cxf.io.ReaderInputStream)2 ProviderFactory (org.apache.cxf.jaxrs.provider.ProviderFactory)2 Message (org.apache.cxf.message.Message)2 EntityInputStream (org.glassfish.jersey.message.internal.EntityInputStream)2